1
0

Backend.php 5.9 KB

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