Backend.php 6.4 KB

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