2019-03-16 18:19:25 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2019-08-09 21:25:21 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2019-03-16 18:19:25 +03:00
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* @copyright Copyright (c) 2019, Thomas Citharel
|
|
|
|
* @copyright Copyright (c) 2019, Georg Ehrke
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
2019-08-09 21:25:21 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
* @author Thomas Citharel <tcit@tcit.fr>
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2019-03-16 18:19:25 +03:00
|
|
|
namespace OCA\DAV\CalDAV\Reminder;
|
|
|
|
|
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IDBConnection;
|
2019-03-16 18:19:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Backend
|
|
|
|
*
|
|
|
|
* @package OCA\DAV\CalDAV\Reminder
|
|
|
|
*/
|
|
|
|
class Backend {
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
protected $db;
|
|
|
|
|
|
|
|
/** @var ITimeFactory */
|
|
|
|
private $timeFactory;
|
|
|
|
|
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* Backend constructor.
|
|
|
|
*
|
2019-03-16 18:19:25 +03:00
|
|
|
* @param IDBConnection $db
|
|
|
|
* @param ITimeFactory $timeFactory
|
|
|
|
*/
|
2019-08-09 21:25:21 +03:00
|
|
|
public function __construct(IDBConnection $db,
|
|
|
|
ITimeFactory $timeFactory) {
|
2019-03-16 18:19:25 +03:00
|
|
|
$this->db = $db;
|
|
|
|
$this->timeFactory = $timeFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* Get all reminders with a notification date before now
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function getRemindersToProcess():array {
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->select(['cr.*', 'co.calendardata', 'c.displayname', 'c.principaluri'])
|
|
|
|
->from('calendar_reminders', 'cr')
|
2019-08-12 14:20:03 +03:00
|
|
|
->where($query->expr()->lte('cr.notification_date', $query->createNamedParameter($this->timeFactory->getTime())))
|
2019-08-09 21:25:21 +03:00
|
|
|
->leftJoin('cr', 'calendarobjects', 'co', $query->expr()->eq('cr.object_id', 'co.id'))
|
|
|
|
->leftJoin('cr', 'calendars', 'c', $query->expr()->eq('cr.calendar_id', 'c.id'));
|
|
|
|
$stmt = $query->execute();
|
|
|
|
|
|
|
|
return array_map(
|
|
|
|
[$this, 'fixRowTyping'],
|
|
|
|
$stmt->fetchAll()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all scheduled reminders for an event
|
|
|
|
*
|
|
|
|
* @param int $objectId
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
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()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new reminder into the database
|
|
|
|
*
|
|
|
|
* @param int $calendarId
|
|
|
|
* @param int $objectId
|
2019-03-16 18:19:25 +03:00
|
|
|
* @param string $uid
|
2019-08-09 21:25:21 +03:00
|
|
|
* @param bool $isRecurring
|
|
|
|
* @param int $recurrenceId
|
|
|
|
* @param bool $isRecurrenceException
|
|
|
|
* @param string $eventHash
|
|
|
|
* @param string $alarmHash
|
2019-03-16 18:19:25 +03:00
|
|
|
* @param string $type
|
2019-08-09 21:25:21 +03:00
|
|
|
* @param bool $isRelative
|
2019-03-16 18:19:25 +03:00
|
|
|
* @param int $notificationDate
|
2019-08-09 21:25:21 +03:00
|
|
|
* @param bool $isRepeatBased
|
|
|
|
* @return int The insert id
|
2019-03-16 18:19:25 +03:00
|
|
|
*/
|
2019-08-09 21:25:21 +03:00
|
|
|
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 {
|
2019-03-16 18:19:25 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->insert('calendar_reminders')
|
|
|
|
->values([
|
2019-08-09 21:25:21 +03:00
|
|
|
'calendar_id' => $query->createNamedParameter($calendarId),
|
|
|
|
'object_id' => $query->createNamedParameter($objectId),
|
2019-03-16 18:19:25 +03:00
|
|
|
'uid' => $query->createNamedParameter($uid),
|
2019-08-09 21:25:21 +03:00
|
|
|
'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),
|
2019-03-16 18:19:25 +03:00
|
|
|
'type' => $query->createNamedParameter($type),
|
2019-08-09 21:25:21 +03:00
|
|
|
'is_relative' => $query->createNamedParameter($isRelative ? 1 : 0),
|
|
|
|
'notification_date' => $query->createNamedParameter($notificationDate),
|
|
|
|
'is_repeat_based' => $query->createNamedParameter($isRepeatBased ? 1 : 0),
|
|
|
|
])
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
return $query->getLastInsertId();
|
2019-03-16 18:19:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* Sets a new notificationDate on an existing reminder
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
2019-08-09 21:25:21 +03:00
|
|
|
* @param int $reminderId
|
|
|
|
* @param int $newNotificationDate
|
2019-03-16 18:19:25 +03:00
|
|
|
*/
|
2019-08-09 21:25:21 +03:00
|
|
|
public function updateReminder(int $reminderId,
|
|
|
|
int $newNotificationDate):void {
|
2019-03-16 18:19:25 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
2019-08-09 21:25:21 +03:00
|
|
|
$query->update('calendar_reminders')
|
|
|
|
->set('notification_date', $query->createNamedParameter($newNotificationDate))
|
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
|
2019-03-16 18:19:25 +03:00
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* Remove a reminder by it's id
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
2019-08-09 21:25:21 +03:00
|
|
|
* @param integer $reminderId
|
2019-03-16 18:19:25 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
2019-08-09 21:25:21 +03:00
|
|
|
public function removeReminder(int $reminderId):void {
|
2019-03-16 18:19:25 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
|
|
|
|
$query->delete('calendar_reminders')
|
2019-08-09 21:25:21 +03:00
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
|
2019-03-16 18:19:25 +03:00
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* Cleans reminders in database
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
2019-08-09 21:25:21 +03:00
|
|
|
* @param int $objectId
|
2019-03-16 18:19:25 +03:00
|
|
|
*/
|
2019-08-09 21:25:21 +03:00
|
|
|
public function cleanRemindersForEvent(int $objectId):void {
|
2019-03-16 18:19:25 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
|
|
|
|
$query->delete('calendar_reminders')
|
2019-08-09 21:25:21 +03:00
|
|
|
->where($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
|
2019-03-16 18:19:25 +03:00
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* Remove all reminders for a calendar
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
2019-08-09 21:25:21 +03:00
|
|
|
* @param int $calendarId
|
|
|
|
* @return void
|
2019-03-16 18:19:25 +03:00
|
|
|
*/
|
2019-08-09 21:25:21 +03:00
|
|
|
public function cleanRemindersForCalendar(int $calendarId):void {
|
2019-03-16 18:19:25 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
2019-08-09 21:25:21 +03:00
|
|
|
|
|
|
|
$query->delete('calendar_reminders')
|
|
|
|
->where($query->expr()->eq('calendar_id', $query->createNamedParameter($calendarId)))
|
2019-03-16 18:19:25 +03:00
|
|
|
->execute();
|
2019-08-09 21:25:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $row
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
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'];
|
2019-03-16 18:19:25 +03:00
|
|
|
|
2019-08-09 21:25:21 +03:00
|
|
|
return $row;
|
2019-03-16 18:19:25 +03:00
|
|
|
}
|
|
|
|
}
|