You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Backend.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Thomas Citharel
  5. * @copyright Copyright (c) 2019, Georg Ehrke
  6. *
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\CalDAV\Reminder;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\IDBConnection;
  30. /**
  31. * Class Backend
  32. *
  33. * @package OCA\DAV\CalDAV\Reminder
  34. */
  35. class Backend {
  36. /** @var IDBConnection */
  37. protected $db;
  38. /** @var ITimeFactory */
  39. private $timeFactory;
  40. /**
  41. * Backend constructor.
  42. *
  43. * @param IDBConnection $db
  44. * @param ITimeFactory $timeFactory
  45. */
  46. public function __construct(IDBConnection $db,
  47. ITimeFactory $timeFactory) {
  48. $this->db = $db;
  49. $this->timeFactory = $timeFactory;
  50. }
  51. /**
  52. * Get all reminders with a notification date before now
  53. *
  54. * @return array
  55. * @throws \Exception
  56. */
  57. public function getRemindersToProcess():array {
  58. $query = $this->db->getQueryBuilder();
  59. $query->select(['cr.*', 'co.calendardata', 'c.displayname', 'c.principaluri'])
  60. ->from('calendar_reminders', 'cr')
  61. ->where($query->expr()->lte('cr.notification_date', $query->createNamedParameter($this->timeFactory->getTime())))
  62. ->leftJoin('cr', 'calendarobjects', 'co', $query->expr()->eq('cr.object_id', 'co.id'))
  63. ->leftJoin('cr', 'calendars', 'c', $query->expr()->eq('cr.calendar_id', 'c.id'));
  64. $stmt = $query->execute();
  65. return array_map(
  66. [$this, 'fixRowTyping'],
  67. $stmt->fetchAll()
  68. );
  69. }
  70. /**
  71. * Get all scheduled reminders for an event
  72. *
  73. * @param int $objectId
  74. * @return array
  75. */
  76. public function getAllScheduledRemindersForEvent(int $objectId):array {
  77. $query = $this->db->getQueryBuilder();
  78. $query->select('*')
  79. ->from('calendar_reminders')
  80. ->where($query->expr()->eq('object_id', $query->createNamedParameter($objectId)));
  81. $stmt = $query->execute();
  82. return array_map(
  83. [$this, 'fixRowTyping'],
  84. $stmt->fetchAll()
  85. );
  86. }
  87. /**
  88. * Insert a new reminder into the database
  89. *
  90. * @param int $calendarId
  91. * @param int $objectId
  92. * @param string $uid
  93. * @param bool $isRecurring
  94. * @param int $recurrenceId
  95. * @param bool $isRecurrenceException
  96. * @param string $eventHash
  97. * @param string $alarmHash
  98. * @param string $type
  99. * @param bool $isRelative
  100. * @param int $notificationDate
  101. * @param bool $isRepeatBased
  102. * @return int The insert id
  103. */
  104. public function insertReminder(int $calendarId,
  105. int $objectId,
  106. string $uid,
  107. bool $isRecurring,
  108. int $recurrenceId,
  109. bool $isRecurrenceException,
  110. string $eventHash,
  111. string $alarmHash,
  112. string $type,
  113. bool $isRelative,
  114. int $notificationDate,
  115. bool $isRepeatBased):int {
  116. $query = $this->db->getQueryBuilder();
  117. $query->insert('calendar_reminders')
  118. ->values([
  119. 'calendar_id' => $query->createNamedParameter($calendarId),
  120. 'object_id' => $query->createNamedParameter($objectId),
  121. 'uid' => $query->createNamedParameter($uid),
  122. 'is_recurring' => $query->createNamedParameter($isRecurring ? 1 : 0),
  123. 'recurrence_id' => $query->createNamedParameter($recurrenceId),
  124. 'is_recurrence_exception' => $query->createNamedParameter($isRecurrenceException ? 1 : 0),
  125. 'event_hash' => $query->createNamedParameter($eventHash),
  126. 'alarm_hash' => $query->createNamedParameter($alarmHash),
  127. 'type' => $query->createNamedParameter($type),
  128. 'is_relative' => $query->createNamedParameter($isRelative ? 1 : 0),
  129. 'notification_date' => $query->createNamedParameter($notificationDate),
  130. 'is_repeat_based' => $query->createNamedParameter($isRepeatBased ? 1 : 0),
  131. ])
  132. ->execute();
  133. return $query->getLastInsertId();
  134. }
  135. /**
  136. * Sets a new notificationDate on an existing reminder
  137. *
  138. * @param int $reminderId
  139. * @param int $newNotificationDate
  140. */
  141. public function updateReminder(int $reminderId,
  142. int $newNotificationDate):void {
  143. $query = $this->db->getQueryBuilder();
  144. $query->update('calendar_reminders')
  145. ->set('notification_date', $query->createNamedParameter($newNotificationDate))
  146. ->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
  147. ->execute();
  148. }
  149. /**
  150. * Remove a reminder by it's id
  151. *
  152. * @param integer $reminderId
  153. * @return void
  154. */
  155. public function removeReminder(int $reminderId):void {
  156. $query = $this->db->getQueryBuilder();
  157. $query->delete('calendar_reminders')
  158. ->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
  159. ->execute();
  160. }
  161. /**
  162. * Cleans reminders in database
  163. *
  164. * @param int $objectId
  165. */
  166. public function cleanRemindersForEvent(int $objectId):void {
  167. $query = $this->db->getQueryBuilder();
  168. $query->delete('calendar_reminders')
  169. ->where($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
  170. ->execute();
  171. }
  172. /**
  173. * Remove all reminders for a calendar
  174. *
  175. * @param int $calendarId
  176. * @return void
  177. */
  178. public function cleanRemindersForCalendar(int $calendarId):void {
  179. $query = $this->db->getQueryBuilder();
  180. $query->delete('calendar_reminders')
  181. ->where($query->expr()->eq('calendar_id', $query->createNamedParameter($calendarId)))
  182. ->execute();
  183. }
  184. /**
  185. * @param array $row
  186. * @return array
  187. */
  188. private function fixRowTyping(array $row): array {
  189. $row['id'] = (int) $row['id'];
  190. $row['calendar_id'] = (int) $row['calendar_id'];
  191. $row['object_id'] = (int) $row['object_id'];
  192. $row['is_recurring'] = (bool) $row['is_recurring'];
  193. $row['recurrence_id'] = (int) $row['recurrence_id'];
  194. $row['is_recurrence_exception'] = (bool) $row['is_recurrence_exception'];
  195. $row['is_relative'] = (bool) $row['is_relative'];
  196. $row['notification_date'] = (int) $row['notification_date'];
  197. $row['is_repeat_based'] = (bool) $row['is_repeat_based'];
  198. return $row;
  199. }
  200. }