Backend.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV\Reminder;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\IDBConnection;
  10. /**
  11. * Class Backend
  12. *
  13. * @package OCA\DAV\CalDAV\Reminder
  14. */
  15. class Backend {
  16. /** @var IDBConnection */
  17. protected $db;
  18. /** @var ITimeFactory */
  19. private $timeFactory;
  20. /**
  21. * Backend constructor.
  22. *
  23. * @param IDBConnection $db
  24. * @param ITimeFactory $timeFactory
  25. */
  26. public function __construct(IDBConnection $db,
  27. ITimeFactory $timeFactory) {
  28. $this->db = $db;
  29. $this->timeFactory = $timeFactory;
  30. }
  31. /**
  32. * Get all reminders with a notification date before now
  33. *
  34. * @return array
  35. * @throws \Exception
  36. */
  37. public function getRemindersToProcess():array {
  38. $query = $this->db->getQueryBuilder();
  39. $query->select(['cr.id', 'cr.calendar_id','cr.object_id','cr.is_recurring','cr.uid','cr.recurrence_id','cr.is_recurrence_exception','cr.event_hash','cr.alarm_hash','cr.type','cr.is_relative','cr.notification_date','cr.is_repeat_based','co.calendardata', 'c.displayname', 'c.principaluri'])
  40. ->from('calendar_reminders', 'cr')
  41. ->where($query->expr()->lte('cr.notification_date', $query->createNamedParameter($this->timeFactory->getTime())))
  42. ->join('cr', 'calendarobjects', 'co', $query->expr()->eq('cr.object_id', 'co.id'))
  43. ->join('cr', 'calendars', 'c', $query->expr()->eq('cr.calendar_id', 'c.id'))
  44. ->groupBy('cr.event_hash', 'cr.notification_date', 'cr.type', 'cr.id', 'cr.calendar_id', 'cr.object_id', 'cr.is_recurring', 'cr.uid', 'cr.recurrence_id', 'cr.is_recurrence_exception', 'cr.alarm_hash', 'cr.is_relative', 'cr.is_repeat_based', 'co.calendardata', 'c.displayname', 'c.principaluri');
  45. $stmt = $query->execute();
  46. return array_map(
  47. [$this, 'fixRowTyping'],
  48. $stmt->fetchAll()
  49. );
  50. }
  51. /**
  52. * Get all scheduled reminders for an event
  53. *
  54. * @param int $objectId
  55. * @return array
  56. */
  57. public function getAllScheduledRemindersForEvent(int $objectId):array {
  58. $query = $this->db->getQueryBuilder();
  59. $query->select('*')
  60. ->from('calendar_reminders')
  61. ->where($query->expr()->eq('object_id', $query->createNamedParameter($objectId)));
  62. $stmt = $query->execute();
  63. return array_map(
  64. [$this, 'fixRowTyping'],
  65. $stmt->fetchAll()
  66. );
  67. }
  68. /**
  69. * Insert a new reminder into the database
  70. *
  71. * @param int $calendarId
  72. * @param int $objectId
  73. * @param string $uid
  74. * @param bool $isRecurring
  75. * @param int $recurrenceId
  76. * @param bool $isRecurrenceException
  77. * @param string $eventHash
  78. * @param string $alarmHash
  79. * @param string $type
  80. * @param bool $isRelative
  81. * @param int $notificationDate
  82. * @param bool $isRepeatBased
  83. * @return int The insert id
  84. */
  85. public function insertReminder(int $calendarId,
  86. int $objectId,
  87. string $uid,
  88. bool $isRecurring,
  89. int $recurrenceId,
  90. bool $isRecurrenceException,
  91. string $eventHash,
  92. string $alarmHash,
  93. string $type,
  94. bool $isRelative,
  95. int $notificationDate,
  96. bool $isRepeatBased):int {
  97. $query = $this->db->getQueryBuilder();
  98. $query->insert('calendar_reminders')
  99. ->values([
  100. 'calendar_id' => $query->createNamedParameter($calendarId),
  101. 'object_id' => $query->createNamedParameter($objectId),
  102. 'uid' => $query->createNamedParameter($uid),
  103. 'is_recurring' => $query->createNamedParameter($isRecurring ? 1 : 0),
  104. 'recurrence_id' => $query->createNamedParameter($recurrenceId),
  105. 'is_recurrence_exception' => $query->createNamedParameter($isRecurrenceException ? 1 : 0),
  106. 'event_hash' => $query->createNamedParameter($eventHash),
  107. 'alarm_hash' => $query->createNamedParameter($alarmHash),
  108. 'type' => $query->createNamedParameter($type),
  109. 'is_relative' => $query->createNamedParameter($isRelative ? 1 : 0),
  110. 'notification_date' => $query->createNamedParameter($notificationDate),
  111. 'is_repeat_based' => $query->createNamedParameter($isRepeatBased ? 1 : 0),
  112. ])
  113. ->execute();
  114. return $query->getLastInsertId();
  115. }
  116. /**
  117. * Sets a new notificationDate on an existing reminder
  118. *
  119. * @param int $reminderId
  120. * @param int $newNotificationDate
  121. */
  122. public function updateReminder(int $reminderId,
  123. int $newNotificationDate):void {
  124. $query = $this->db->getQueryBuilder();
  125. $query->update('calendar_reminders')
  126. ->set('notification_date', $query->createNamedParameter($newNotificationDate))
  127. ->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
  128. ->execute();
  129. }
  130. /**
  131. * Remove a reminder by it's id
  132. *
  133. * @param integer $reminderId
  134. * @return void
  135. */
  136. public function removeReminder(int $reminderId):void {
  137. $query = $this->db->getQueryBuilder();
  138. $query->delete('calendar_reminders')
  139. ->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
  140. ->execute();
  141. }
  142. /**
  143. * Cleans reminders in database
  144. *
  145. * @param int $objectId
  146. */
  147. public function cleanRemindersForEvent(int $objectId):void {
  148. $query = $this->db->getQueryBuilder();
  149. $query->delete('calendar_reminders')
  150. ->where($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
  151. ->execute();
  152. }
  153. /**
  154. * Remove all reminders for a calendar
  155. *
  156. * @param int $calendarId
  157. * @return void
  158. */
  159. public function cleanRemindersForCalendar(int $calendarId):void {
  160. $query = $this->db->getQueryBuilder();
  161. $query->delete('calendar_reminders')
  162. ->where($query->expr()->eq('calendar_id', $query->createNamedParameter($calendarId)))
  163. ->execute();
  164. }
  165. /**
  166. * @param array $row
  167. * @return array
  168. */
  169. private function fixRowTyping(array $row): array {
  170. $row['id'] = (int) $row['id'];
  171. $row['calendar_id'] = (int) $row['calendar_id'];
  172. $row['object_id'] = (int) $row['object_id'];
  173. $row['is_recurring'] = (bool) $row['is_recurring'];
  174. $row['recurrence_id'] = (int) $row['recurrence_id'];
  175. $row['is_recurrence_exception'] = (bool) $row['is_recurrence_exception'];
  176. $row['is_relative'] = (bool) $row['is_relative'];
  177. $row['notification_date'] = (int) $row['notification_date'];
  178. $row['is_repeat_based'] = (bool) $row['is_repeat_based'];
  179. return $row;
  180. }
  181. }