]> git.mxchange.org Git - friendica-addons.git/blob - dav/virtual_cal_source_friendica.inc.php
Initial Release of the calendar plugin
[friendica-addons.git] / dav / virtual_cal_source_friendica.inc.php
1 <?php
2
3 class FriendicaVirtualCalSourceBackend extends VirtualCalSourceBackend
4 {
5
6         /**
7          * @static
8          * @return int
9          */
10         static public function getNamespace()
11         {
12                 return CALDAV_NAMESPACE_FRIENDICA_NATIVE;
13         }
14
15         /**
16          * @static
17          * @param int $uid
18          * @param int $namespace_id
19          * @throws Sabre_DAV_Exception_NotFound
20          * @return void
21          */
22         static function createCache($uid = 0, $namespace_id = 0)
23         {
24         }
25
26
27         static private function row2array($row, $timezone, $hostname, $uid, $namespace_id) {
28                 $v = new vcalendar();
29                 $v->setConfig('unique_id', $hostname);
30
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);
35
36                 if ($row["adjust"]) {
37                         $start = datetime_convert('UTC', date_default_timezone_get(), $row["start"]);
38                         $finish = datetime_convert('UTC', date_default_timezone_get(), $row["finish"]);
39                 } else {
40                         $start = $row["start"];
41                         $finish = $row["finish"];
42                 }
43                 $allday      = (strpos($start, "00:00:00") !== false && strpos($finish, "00:00:00") !== false);
44
45                 /*
46
47                 if ($allday) {
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"));
51                         var_dump($finish);
52                 }
53                 */
54
55                 $subject     = substr(preg_replace("/\[[^\]]*\]/", "", $row["desc"]), 0, 100);
56                 $description = preg_replace("/\[[^\]]*\]/", "", $row["desc"]);
57
58                 $vevent = dav_create_vevent(wdcal_mySql2icalTime($row["start"]), wdcal_mySql2icalTime($row["finish"]), false);
59                 $vevent->setLocation(icalendar_sanitize_string($row["location"]));
60                 $vevent->setSummary(icalendar_sanitize_string($subject));
61                 $vevent->setDescription(icalendar_sanitize_string($description));
62
63                 $v->setComponent($vevent);
64                 $ical  = $v->createCalendar();
65                 return array(
66                         "uid"              => $uid,
67                         "namespace"        => CALDAV_NAMESPACE_FRIENDICA_NATIVE,
68                         "namespace_id"     => $namespace_id,
69                         "date"             => $row["edited"],
70                         "data_uri"         => "friendica-" . $namespace_id . "-" . $row["id"] . "@" . $hostname,
71                         "data_subject"     => $subject,
72                         "data_location"    => $row["location"],
73                         "data_description" => $description,
74                         "data_start"       => $start,
75                         "data_end"         => $finish,
76                         "data_allday"      => $allday,
77                         "data_type"        => $row["type"],
78                         "ical"             => $ical,
79                         "ical_size"        => strlen($ical),
80                         "ical_etag"        => md5($ical),
81                 );
82
83         }
84
85         /**
86          * @static
87          * @param int $uid
88          * @param int $namespace_id
89          * @param string|int $date_from
90          * @param string|int $date_to
91          * @throws Sabre_DAV_Exception_NotFound
92          * @return array
93          */
94         static public function getItemsByTime($uid = 0, $namespace_id = 0, $date_from = "", $date_to = "")
95         {
96                 $uid          = IntVal($uid);
97                 $namespace_id = IntVal($namespace_id);
98
99                 switch ($namespace_id) {
100                         case CALDAV_FRIENDICA_MINE:
101                                 $sql_where = " AND cid = 0";
102                                 break;
103                         case CALDAV_FRIENDICA_CONTACTS:
104                                 $sql_where = " AND cid > 0";
105                                 break;
106                         default:
107                                 throw new Sabre_DAV_Exception_NotFound();
108                 }
109
110                 if ($date_from != "") {
111                         if (is_numeric($date_from)) $sql_where .= " AND `finish` >= '" . date("Y-m-d H:i:s", $date_from) . "'";
112                         else $sql_where .= " AND `finish` >= '" . dbesc($date_from) . "'";
113                 }
114                 if ($date_to != "") {
115                         if (is_numeric($date_to)) $sql_where .= " AND `start` <= '" . date("Y-m-d H:i:s", $date_to) . "'";
116                         else $sql_where .= " AND `start` <= '" . dbesc($date_to) . "'";
117                 }
118
119                 $ret  = array();
120                 $a    = get_app();
121                 $host = $a->get_hostname();
122
123
124                 $r    = q("SELECT * FROM `event` WHERE `uid` = %d " . $sql_where . " ORDER BY `start`", $uid);
125                 foreach ($r as $row) $ret[] =self::row2array($row, $a->timezone, $host, $uid, $namespace_id);
126
127                 return $ret;
128         }
129
130
131         /**
132          * @static
133          * @param int $uid
134          * @param string $uri
135          * @throws Sabre_DAV_Exception_NotFound
136          * @return array
137          */
138         static public function getItemsByUri($uid = 0, $uri)
139         {
140                 $x = explode("-", $uri);
141                 if ($x[0] != "friendica") throw new Sabre_DAV_Exception_NotFound();
142
143                 $namespace_id = IntVal($x[1]);
144                 switch ($namespace_id) {
145                         case CALDAV_FRIENDICA_MINE:
146                                 $sql_where = " AND cid = 0";
147                                 break;
148                         case CALDAV_FRIENDICA_CONTACTS:
149                                 $sql_where = " AND cid > 0";
150                                 break;
151                         default:
152                                 throw new Sabre_DAV_Exception_NotFound();
153                 }
154
155                 $a    = get_app();
156                 $host = $a->get_hostname();
157
158                 $r    = q("SELECT * FROM `event` WHERE `uid` = %d AND id = %d " . $sql_where, $uid, IntVal($x[2]));
159                 if (count($r) != 1) throw new Sabre_DAV_Exception_NotFound();
160                 $ret =self::row2array($r[0], $a->timezone, $host, $uid, $namespace_id);
161
162                 return $ret;
163         }
164
165
166 }