3 abstract class Sabre_CalDAV_Backend_Common extends Sabre_CalDAV_Backend_Abstract {
5 * List of CalDAV properties, and how they map to database fieldnames
7 * Add your own properties by simply adding on to this array
11 public $propertyMap = array(
12 '{DAV:}displayname' => 'displayname',
13 '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description',
14 '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone',
15 '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder',
16 '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor',
20 abstract public function getNamespace();
21 abstract public function getCalUrlPrefix();
24 * @param int $namespace
25 * @param int $namespace_id
27 protected function increaseCalendarCtag($namespace, $namespace_id) {
28 $namespace = IntVal($namespace);
29 $namespace_id = IntVal($namespace_id);
31 q("UPDATE %s%scalendars SET `ctag` = `ctag` + 1 WHERE `namespace` = %d AND `namespace_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $namespace, $namespace_id);
37 * Returns a list of calendars for a principal.
39 * Every project is an array with the following keys:
40 * * id, a unique id that will be used by other functions to modify the
41 * calendar. This can be the same as the uri or a database key.
42 * * uri, which the basename of the uri with which the calendar is
44 * * principaluri. The owner of the calendar. Almost always the same as
45 * principalUri passed to this method.
47 * Furthermore it can contain webdav properties in clark notation. A very
48 * common one is '{DAV:}displayname'.
50 * @param string $principalUri
53 public function getCalendarsForUser($principalUri)
55 list(,$name) = Sabre_DAV_URLUtil::splitPath($principalUri);
56 $user_id = dav_compat_username2id($name);
58 $cals = q("SELECT * FROM %s%scalendars WHERE `uid`=%d AND `namespace` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $user_id, $this->getNamespace());
60 foreach ($cals as $cal) {
62 "id" => $cal["namespace"] . "-" . $cal["namespace_id"],
63 "uri" => $this->getCalUrlPrefix() . "-" . $cal["namespace_id"],
64 "principaluri" => $principalUri,
65 '{' . Sabre_CalDAV_Plugin::NS_CALENDARSERVER . '}getctag' => $cal['ctag']?$cal['ctag']:'0',
66 "calendar_class" => "Sabre_CalDAV_Calendar",
68 foreach ($this->propertyMap as $key=>$field) $dat[$key] = $cal[$field];
77 * Updates properties for a calendar.
79 * The mutations array uses the propertyName in clark-notation as key,
80 * and the array value for the property value. In the case a property
81 * should be deleted, the property value will be null.
83 * This method must be atomic. If one property cannot be changed, the
84 * entire operation must fail.
86 * If the operation was successful, true can be returned.
87 * If the operation failed, false can be returned.
89 * Deletion of a non-existent property is always successful.
91 * Lastly, it is optional to return detailed information about any
92 * failures. In this case an array should be returned with the following
97 * '{DAV:}displayname' => null,
100 * '{DAV:}owner' => null,
104 * In this example it was forbidden to update {DAV:}displayname.
105 * (403 Forbidden), which in turn also caused {DAV:}owner to fail
106 * (424 Failed Dependency) because the request needs to be atomic.
108 * @param mixed $calendarId
109 * @param array $mutations
112 public function updateCalendar($calendarId, array $mutations) {
114 $newValues = array();
116 200 => array(), // Ok
117 403 => array(), // Forbidden
118 424 => array(), // Failed Dependency
123 foreach($mutations as $propertyName=>$propertyValue) {
125 // We don't know about this property.
126 if (!isset($this->propertyMap[$propertyName])) {
128 $result[403][$propertyName] = null;
129 unset($mutations[$propertyName]);
133 $fieldName = $this->propertyMap[$propertyName];
134 $newValues[$fieldName] = $propertyValue;
138 // If there were any errors we need to fail the request
140 // Properties has the remaining properties
141 foreach($mutations as $propertyName=>$propertyValue) {
142 $result[424][$propertyName] = null;
145 // Removing unused statuscodes for cleanliness
146 foreach($result as $status=>$properties) {
147 if (is_array($properties) && count($properties)===0) unset($result[$status]);
154 $x = explode("-", $calendarId);
156 $this->increaseCalendarCtag($x[0], $x[1]);
158 $valuesSql = array();
159 foreach($newValues as $fieldName=>$value) $valuesSql[] = "`" . $fieldName . "` = '" . dbesc($value) . "'";
160 if (count($valuesSql) > 0) {
161 q("UPDATE %s%scalendars SET " . implode(", ", $valuesSql) . " WHERE `namespace` = %d AND `namespace_id` = %d",
162 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1])