2013-10-21 22:46:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Sabre\CalDAV\Backend;
|
2016-05-11 00:26:44 +00:00
|
|
|
|
2013-10-21 22:46:31 +00:00
|
|
|
use Sabre\DAV;
|
|
|
|
use Sabre\CalDAV;
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
class Mock extends AbstractBackend {
|
|
|
|
|
|
|
|
protected $calendarData;
|
|
|
|
protected $calendars;
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
function __construct(array $calendars = [], array $calendarData = []) {
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
foreach ($calendars as &$calendar) {
|
|
|
|
if (!isset($calendar['id'])) {
|
|
|
|
$calendar['id'] = DAV\UUIDUtil::getUUID();
|
|
|
|
}
|
|
|
|
}
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
$this->calendars = $calendars;
|
|
|
|
$this->calendarData = $calendarData;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of calendars for a principal.
|
|
|
|
*
|
|
|
|
* Every project is an array with the following keys:
|
|
|
|
* * id, a unique id that will be used by other functions to modify the
|
|
|
|
* calendar. This can be the same as the uri or a database key.
|
|
|
|
* * uri, which the basename of the uri with which the calendar is
|
|
|
|
* accessed.
|
|
|
|
* * principalUri. The owner of the calendar. Almost always the same as
|
|
|
|
* principalUri passed to this method.
|
|
|
|
*
|
|
|
|
* Furthermore it can contain webdav properties in clark notation. A very
|
|
|
|
* common one is '{DAV:}displayname'.
|
|
|
|
*
|
|
|
|
* @param string $principalUri
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getCalendarsForUser($principalUri) {
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$r = [];
|
|
|
|
foreach ($this->calendars as $row) {
|
2013-10-21 22:46:31 +00:00
|
|
|
if ($row['principaluri'] == $principalUri) {
|
|
|
|
$r[] = $row;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $r;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new calendar for a principal.
|
|
|
|
*
|
|
|
|
* If the creation was a success, an id must be returned that can be used to reference
|
|
|
|
* this calendar in other methods, such as updateCalendar.
|
|
|
|
*
|
|
|
|
* This function must return a server-wide unique id that can be used
|
|
|
|
* later to reference the calendar.
|
|
|
|
*
|
|
|
|
* @param string $principalUri
|
|
|
|
* @param string $calendarUri
|
|
|
|
* @param array $properties
|
|
|
|
* @return string|int
|
|
|
|
*/
|
2016-05-11 00:26:44 +00:00
|
|
|
function createCalendar($principalUri, $calendarUri, array $properties) {
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
$id = DAV\UUIDUtil::getUUID();
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->calendars[] = array_merge([
|
|
|
|
'id' => $id,
|
|
|
|
'principaluri' => $principalUri,
|
|
|
|
'uri' => $calendarUri,
|
|
|
|
'{' . CalDAV\Plugin::NS_CALDAV . '}supported-calendar-component-set' => new CalDAV\Xml\Property\SupportedCalendarComponentSet(['VEVENT', 'VTODO']),
|
|
|
|
], $properties);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
return $id;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
/**
|
|
|
|
* Updates properties for a calendar.
|
|
|
|
*
|
|
|
|
* The list of mutations is stored in a Sabre\DAV\PropPatch object.
|
|
|
|
* To do the actual updates, you must tell this object which properties
|
|
|
|
* you're going to process with the handle() method.
|
|
|
|
*
|
|
|
|
* Calling the handle method is like telling the PropPatch object "I
|
|
|
|
* promise I can handle updating this property".
|
|
|
|
*
|
|
|
|
* Read the PropPatch documentation for more info and examples.
|
|
|
|
*
|
|
|
|
* @param mixed $calendarId
|
|
|
|
* @param \Sabre\DAV\PropPatch $propPatch
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function updateCalendar($calendarId, \Sabre\DAV\PropPatch $propPatch) {
|
|
|
|
|
|
|
|
$propPatch->handleRemaining(function($props) use ($calendarId) {
|
|
|
|
|
|
|
|
foreach ($this->calendars as $k => $calendar) {
|
|
|
|
|
|
|
|
if ($calendar['id'] === $calendarId) {
|
|
|
|
foreach ($props as $propName => $propValue) {
|
|
|
|
if (is_null($propValue)) {
|
|
|
|
unset($this->calendars[$k][$propName]);
|
|
|
|
} else {
|
|
|
|
$this->calendars[$k][$propName] = $propValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-21 22:46:31 +00:00
|
|
|
/**
|
|
|
|
* Delete a calendar and all it's objects
|
|
|
|
*
|
|
|
|
* @param string $calendarId
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-05-11 00:26:44 +00:00
|
|
|
function deleteCalendar($calendarId) {
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
foreach ($this->calendars as $k => $calendar) {
|
2013-10-21 22:46:31 +00:00
|
|
|
if ($calendar['id'] === $calendarId) {
|
|
|
|
unset($this->calendars[$k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all calendar objects within a calendar object.
|
|
|
|
*
|
|
|
|
* Every item contains an array with the following keys:
|
|
|
|
* * id - unique identifier which will be used for subsequent updates
|
|
|
|
* * calendardata - The iCalendar-compatible calendar data
|
|
|
|
* * uri - a unique key which will be used to construct the uri. This can be any arbitrary string.
|
|
|
|
* * lastmodified - a timestamp of the last modification time
|
|
|
|
* * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
|
|
|
|
* ' "abcdef"')
|
|
|
|
* * calendarid - The calendarid as it was passed to this function.
|
|
|
|
*
|
|
|
|
* Note that the etag is optional, but it's highly encouraged to return for
|
|
|
|
* speed reasons.
|
|
|
|
*
|
|
|
|
* The calendardata is also optional. If it's not returned
|
|
|
|
* 'getCalendarObject' will be called later, which *is* expected to return
|
|
|
|
* calendardata.
|
|
|
|
*
|
|
|
|
* @param string $calendarId
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-05-11 00:26:44 +00:00
|
|
|
function getCalendarObjects($calendarId) {
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
if (!isset($this->calendarData[$calendarId]))
|
2016-05-11 00:26:44 +00:00
|
|
|
return [];
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
$objects = $this->calendarData[$calendarId];
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
foreach ($objects as $uri => &$object) {
|
2013-10-21 22:46:31 +00:00
|
|
|
$object['calendarid'] = $calendarId;
|
|
|
|
$object['uri'] = $uri;
|
2016-05-11 00:26:44 +00:00
|
|
|
$object['lastmodified'] = null;
|
2013-10-21 22:46:31 +00:00
|
|
|
}
|
|
|
|
return $objects;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns information from a single calendar object, based on it's object
|
|
|
|
* uri.
|
|
|
|
*
|
2016-05-28 15:46:24 +00:00
|
|
|
* The object uri is only the basename, or filename and not a full path.
|
|
|
|
*
|
2013-10-21 22:46:31 +00:00
|
|
|
* The returned array must have the same keys as getCalendarObjects. The
|
|
|
|
* 'calendardata' object is required here though, while it's not required
|
|
|
|
* for getCalendarObjects.
|
|
|
|
*
|
2016-05-28 15:46:24 +00:00
|
|
|
* This method must return null if the object did not exist.
|
|
|
|
*
|
|
|
|
* @param mixed $calendarId
|
2013-10-21 22:46:31 +00:00
|
|
|
* @param string $objectUri
|
2016-05-28 15:46:24 +00:00
|
|
|
* @return array|null
|
2013-10-21 22:46:31 +00:00
|
|
|
*/
|
2016-05-11 00:26:44 +00:00
|
|
|
function getCalendarObject($calendarId, $objectUri) {
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
if (!isset($this->calendarData[$calendarId][$objectUri])) {
|
2016-05-28 15:46:24 +00:00
|
|
|
return null;
|
2013-10-21 22:46:31 +00:00
|
|
|
}
|
|
|
|
$object = $this->calendarData[$calendarId][$objectUri];
|
|
|
|
$object['calendarid'] = $calendarId;
|
|
|
|
$object['uri'] = $objectUri;
|
2016-05-11 00:26:44 +00:00
|
|
|
$object['lastmodified'] = null;
|
2013-10-21 22:46:31 +00:00
|
|
|
return $object;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new calendar object.
|
|
|
|
*
|
|
|
|
* @param string $calendarId
|
|
|
|
* @param string $objectUri
|
|
|
|
* @param string $calendarData
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-05-11 00:26:44 +00:00
|
|
|
function createCalendarObject($calendarId, $objectUri, $calendarData) {
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->calendarData[$calendarId][$objectUri] = [
|
2013-10-21 22:46:31 +00:00
|
|
|
'calendardata' => $calendarData,
|
2016-05-11 00:26:44 +00:00
|
|
|
'calendarid' => $calendarId,
|
|
|
|
'uri' => $objectUri,
|
|
|
|
];
|
|
|
|
return '"' . md5($calendarData) . '"';
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an existing calendarobject, based on it's uri.
|
|
|
|
*
|
|
|
|
* @param string $calendarId
|
|
|
|
* @param string $objectUri
|
|
|
|
* @param string $calendarData
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-05-11 00:26:44 +00:00
|
|
|
function updateCalendarObject($calendarId, $objectUri, $calendarData) {
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->calendarData[$calendarId][$objectUri] = [
|
2013-10-21 22:46:31 +00:00
|
|
|
'calendardata' => $calendarData,
|
2016-05-11 00:26:44 +00:00
|
|
|
'calendarid' => $calendarId,
|
|
|
|
'uri' => $objectUri,
|
|
|
|
];
|
|
|
|
return '"' . md5($calendarData) . '"';
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an existing calendar object.
|
|
|
|
*
|
|
|
|
* @param string $calendarId
|
|
|
|
* @param string $objectUri
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-05-11 00:26:44 +00:00
|
|
|
function deleteCalendarObject($calendarId, $objectUri) {
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-28 15:46:24 +00:00
|
|
|
unset($this->calendarData[$calendarId][$objectUri]);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|