1
0

Backend.php 5.5 KB

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