3 class FriendicaVirtualCalSourceBackend extends VirtualCalSourceBackend
10 static public function getNamespace()
12 return CALDAV_NAMESPACE_FRIENDICA_NATIVE;
18 * @param int $namespace_id
19 * @throws Sabre_DAV_Exception_NotFound
22 static function createCache($uid = 0, $namespace_id = 0)
27 static private function row2array($row, $timezone, $hostname, $uid, $namespace_id) {
29 $v->setConfig('unique_id', $hostname);
31 $v->setProperty('method', 'PUBLISH');
32 $v->setProperty("x-wr-calname", "AnimexxCal");
33 $v->setProperty("X-WR-CALDESC", "Animexx Calendar");
34 $v->setProperty("X-WR-TIMEZONE", $timezone);
37 $start = datetime_convert('UTC', date_default_timezone_get(), $row["start"]);
38 $finish = datetime_convert('UTC', date_default_timezone_get(), $row["finish"]);
40 $start = $row["start"];
41 $finish = $row["finish"];
43 $allday = (strpos($start, "00:00:00") !== false && strpos($finish, "00:00:00") !== false);
48 $dat = Datetime::createFromFormat("Y-m-d H:i:s", $finish_tmp);
49 $dat->sub(new DateInterval("P1D"));
50 $finish = datetime_convert("UTC", date_default_timezone_get(), $dat->format("Y-m-d H:i:s"));
55 // 2012-06-29 - change to Friendica new event behaviour where summary is present and required,
56 // but use desc for older events where summary wasn't present or required (but desc was)
58 $subject = (($row["summary"]) ? $row["summary"] : substr(preg_replace("/\[[^\]]*\]/", "", $row["desc"]), 0, 100));
59 $description = (($row["desc"]) ? preg_replace("/\[[^\]]*\]/", "", $row["desc"]) : $row["summary"]);
61 $vevent = dav_create_vevent(wdcal_mySql2icalTime($row["start"]), wdcal_mySql2icalTime($row["finish"]), false);
62 $vevent->setLocation(icalendar_sanitize_string($row["location"]));
63 $vevent->setSummary(icalendar_sanitize_string($subject));
64 $vevent->setDescription(icalendar_sanitize_string($description));
66 $v->setComponent($vevent);
67 $ical = $v->createCalendar();
70 "namespace" => CALDAV_NAMESPACE_FRIENDICA_NATIVE,
71 "namespace_id" => $namespace_id,
72 "date" => $row["edited"],
73 "data_uri" => "friendica-" . $namespace_id . "-" . $row["id"] . "@" . $hostname,
74 "data_subject" => $subject,
75 "data_location" => $row["location"],
76 "data_description" => $description,
77 "data_start" => $start,
78 "data_end" => $finish,
79 "data_allday" => $allday,
80 "data_type" => $row["type"],
82 "ical_size" => strlen($ical),
83 "ical_etag" => md5($ical),
91 * @param int $namespace_id
92 * @param string|int $date_from
93 * @param string|int $date_to
94 * @throws Sabre_DAV_Exception_NotFound
97 static public function getItemsByTime($uid = 0, $namespace_id = 0, $date_from = "", $date_to = "")
100 $namespace_id = IntVal($namespace_id);
102 switch ($namespace_id) {
103 case CALDAV_FRIENDICA_MINE:
104 $sql_where = " AND cid = 0";
106 case CALDAV_FRIENDICA_CONTACTS:
107 $sql_where = " AND cid > 0";
110 throw new Sabre_DAV_Exception_NotFound();
113 if ($date_from != "") {
114 if (is_numeric($date_from)) $sql_where .= " AND `finish` >= '" . date("Y-m-d H:i:s", $date_from) . "'";
115 else $sql_where .= " AND `finish` >= '" . dbesc($date_from) . "'";
117 if ($date_to != "") {
118 if (is_numeric($date_to)) $sql_where .= " AND `start` <= '" . date("Y-m-d H:i:s", $date_to) . "'";
119 else $sql_where .= " AND `start` <= '" . dbesc($date_to) . "'";
124 $host = $a->get_hostname();
127 $r = q("SELECT * FROM `event` WHERE `uid` = %d " . $sql_where . " ORDER BY `start`", $uid);
128 foreach ($r as $row) $ret[] =self::row2array($row, $a->timezone, $host, $uid, $namespace_id);
138 * @throws Sabre_DAV_Exception_NotFound
141 static public function getItemsByUri($uid = 0, $uri)
143 $x = explode("-", $uri);
144 if ($x[0] != "friendica") throw new Sabre_DAV_Exception_NotFound();
146 $namespace_id = IntVal($x[1]);
147 switch ($namespace_id) {
148 case CALDAV_FRIENDICA_MINE:
149 $sql_where = " AND cid = 0";
151 case CALDAV_FRIENDICA_CONTACTS:
152 $sql_where = " AND cid > 0";
155 throw new Sabre_DAV_Exception_NotFound();
159 $host = $a->get_hostname();
161 $r = q("SELECT * FROM `event` WHERE `uid` = %d AND id = %d " . $sql_where, $uid, IntVal($x[2]));
162 if (count($r) != 1) throw new Sabre_DAV_Exception_NotFound();
163 $ret =self::row2array($r[0], $a->timezone, $host, $uid, $namespace_id);