]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/dav_carddav_backend_common.inc.php
Merge branch 'master' of github.com:CatoTH/friendica-addons
[friendica-addons.git] / dav / common / dav_carddav_backend_common.inc.php
1 <?php
2
3 abstract class Sabre_CardDAV_Backend_Common extends Sabre_CardDAV_Backend_Abstract
4 {
5         /**
6          * @abstract
7          * @return int
8          */
9         abstract public function getNamespace();
10
11         /**
12          * @static
13          * @abstract
14          * @return string
15          */
16         abstract public static function getBackendTypeName();
17
18         /**
19          * @var array
20          */
21         static private $addressbookCache = array();
22
23         /**
24          * @var array
25          */
26         static private $addressbookObjectCache = array();
27
28         /**
29          * @static
30          * @param int $addressbookId
31          * @return array
32          */
33         static public function loadCalendarById($addressbookId)
34         {
35                 if (!isset(self::$addressbookCache[$addressbookId])) {
36                         $c                                = q("SELECT * FROM %s%saddressbooks WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressbookId));
37                         self::$addressbookCache[$addressbookId] = $c[0];
38                 }
39                 return self::$addressbookCache[$addressbookId];
40         }
41
42         /**
43          * @static
44          * @param int $obj_id
45          * @return array
46          */
47         static public function loadAddressbookobjectById($obj_id)
48         {
49                 if (!isset(self::$addressbookObjectCache[$obj_id])) {
50                         $o                                  = q("SELECT * FROM %s%saddressbookobjects WHERE `id` = %d",
51                                 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($obj_id)
52                         );
53                         self::$addressbookObjectCache[$obj_id] = $o[0];
54                 }
55                 return self::$addressbookObjectCache[$obj_id];
56         }
57
58
59         /**
60          * Updates an addressbook's properties
61          *
62          * See Sabre_DAV_IProperties for a description of the mutations array, as
63          * well as the return value.
64          *
65          * @param mixed $addressBookId
66          * @param array $mutations
67          * @throws Sabre_DAV_Exception_Forbidden
68          * @see Sabre_DAV_IProperties::updateProperties
69          * @return bool|array
70          */
71         public function updateAddressBook($addressBookId, array $mutations)
72         {
73                 $updates = array();
74
75                 foreach ($mutations as $property=> $newValue) {
76
77                         switch ($property) {
78                                 case '{DAV:}displayname' :
79                                         $updates['displayname'] = $newValue;
80                                         break;
81                                 case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
82                                         $updates['description'] = $newValue;
83                                         break;
84                                 default :
85                                         // If any unsupported values were being updated, we must
86                                         // let the entire request fail.
87                                         return false;
88                         }
89
90                 }
91
92                 // No values are being updated?
93                 if (!$updates) {
94                         return false;
95                 }
96
97                 $query = 'UPDATE ' . CALDAV_SQL_DB . CALDAV_SQL_PREFIX . 'addressbooks SET ctag = ctag + 1 ';
98                 foreach ($updates as $key=> $value) {
99                         $query .= ', `' . dbesc($key) . '` = ' . dbesc($key) . ' ';
100                 }
101                 $query .= ' WHERE id = ' . IntVal($addressBookId);
102                 q($query);
103
104                 return true;
105
106         }
107
108         /**
109          * @param int $addressbookId
110          */
111         protected function increaseAddressbookCtag($addressbookId)
112         {
113                 q("UPDATE %s%saddressbooks SET `ctag` = `ctag` + 1 WHERE `id` = '%d'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressbookId));
114                 self::$addressbookCache = array();
115         }
116 }