123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- declare(strict_types=1);
- namespace OCA\DAV\CalDAV\Reminder;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\IDBConnection;
- class Backend {
-
- protected $db;
-
- private $timeFactory;
-
- public function __construct(IDBConnection $db,
- ITimeFactory $timeFactory) {
- $this->db = $db;
- $this->timeFactory = $timeFactory;
- }
-
- public function getRemindersToProcess():array {
- $query = $this->db->getQueryBuilder();
- $query->select(['cr.*', 'co.calendardata', 'c.displayname', 'c.principaluri'])
- ->from('calendar_reminders', 'cr')
- ->where($query->expr()->lte('cr.notification_date', $query->createNamedParameter($this->timeFactory->getTime())))
- ->join('cr', 'calendarobjects', 'co', $query->expr()->eq('cr.object_id', 'co.id'))
- ->join('cr', 'calendars', 'c', $query->expr()->eq('cr.calendar_id', 'c.id'));
- $stmt = $query->execute();
- return array_map(
- [$this, 'fixRowTyping'],
- $stmt->fetchAll()
- );
- }
-
- public function getAllScheduledRemindersForEvent(int $objectId):array {
- $query = $this->db->getQueryBuilder();
- $query->select('*')
- ->from('calendar_reminders')
- ->where($query->expr()->eq('object_id', $query->createNamedParameter($objectId)));
- $stmt = $query->execute();
- return array_map(
- [$this, 'fixRowTyping'],
- $stmt->fetchAll()
- );
- }
-
- public function insertReminder(int $calendarId,
- int $objectId,
- string $uid,
- bool $isRecurring,
- int $recurrenceId,
- bool $isRecurrenceException,
- string $eventHash,
- string $alarmHash,
- string $type,
- bool $isRelative,
- int $notificationDate,
- bool $isRepeatBased):int {
- $query = $this->db->getQueryBuilder();
- $query->insert('calendar_reminders')
- ->values([
- 'calendar_id' => $query->createNamedParameter($calendarId),
- 'object_id' => $query->createNamedParameter($objectId),
- 'uid' => $query->createNamedParameter($uid),
- 'is_recurring' => $query->createNamedParameter($isRecurring ? 1 : 0),
- 'recurrence_id' => $query->createNamedParameter($recurrenceId),
- 'is_recurrence_exception' => $query->createNamedParameter($isRecurrenceException ? 1 : 0),
- 'event_hash' => $query->createNamedParameter($eventHash),
- 'alarm_hash' => $query->createNamedParameter($alarmHash),
- 'type' => $query->createNamedParameter($type),
- 'is_relative' => $query->createNamedParameter($isRelative ? 1 : 0),
- 'notification_date' => $query->createNamedParameter($notificationDate),
- 'is_repeat_based' => $query->createNamedParameter($isRepeatBased ? 1 : 0),
- ])
- ->execute();
- return $query->getLastInsertId();
- }
-
- public function updateReminder(int $reminderId,
- int $newNotificationDate):void {
- $query = $this->db->getQueryBuilder();
- $query->update('calendar_reminders')
- ->set('notification_date', $query->createNamedParameter($newNotificationDate))
- ->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
- ->execute();
- }
-
- public function removeReminder(int $reminderId):void {
- $query = $this->db->getQueryBuilder();
- $query->delete('calendar_reminders')
- ->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
- ->execute();
- }
-
- public function cleanRemindersForEvent(int $objectId):void {
- $query = $this->db->getQueryBuilder();
- $query->delete('calendar_reminders')
- ->where($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
- ->execute();
- }
-
- public function cleanRemindersForCalendar(int $calendarId):void {
- $query = $this->db->getQueryBuilder();
- $query->delete('calendar_reminders')
- ->where($query->expr()->eq('calendar_id', $query->createNamedParameter($calendarId)))
- ->execute();
- }
-
- private function fixRowTyping(array $row): array {
- $row['id'] = (int) $row['id'];
- $row['calendar_id'] = (int) $row['calendar_id'];
- $row['object_id'] = (int) $row['object_id'];
- $row['is_recurring'] = (bool) $row['is_recurring'];
- $row['recurrence_id'] = (int) $row['recurrence_id'];
- $row['is_recurrence_exception'] = (bool) $row['is_recurrence_exception'];
- $row['is_relative'] = (bool) $row['is_relative'];
- $row['notification_date'] = (int) $row['notification_date'];
- $row['is_repeat_based'] = (bool) $row['is_repeat_based'];
- return $row;
- }
- }
|