]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/calendar_rendering.fnk.php
Move StatusNet addon dependencies in own library subfolder
[friendica-addons.git] / dav / common / calendar_rendering.fnk.php
1 <?php
2
3
4
5 /**
6  * @param Sabre\VObject\Component\VAlarm $alarm
7  * @param Sabre\VObject\Component\VEvent|Sabre\VObject\Component\VTodo $parent
8  * @return DateTime|null
9  * @throws Sabre_DAV_Exception
10  */
11 function renderCalDavEntry_calcalarm(&$alarm, &$parent)
12 {
13         $trigger = $alarm->__get("TRIGGER");
14         if (!isset($trigger['VALUE']) || strtoupper($trigger['VALUE']) === 'DURATION') {
15                 $triggerDuration = Sabre\VObject\DateTimeParser::parseDuration($trigger->value);
16
17                 $related = (isset($trigger['RELATED']) && strtoupper($trigger['RELATED']) == 'END') ? 'END' : 'START';
18
19                 if ($related === 'START') {
20                         /** @var Sabre\VObject\Property\DateTime $dtstart  */
21                         $dtstart          = $parent->__get("DTSTART");
22                         $effectiveTrigger = $dtstart->getDateTime();
23                         $effectiveTrigger->add($triggerDuration);
24                 } else {
25                         if ($parent->name === 'VTODO') {
26                                 $endProp = 'DUE';
27                         } else {
28                                 $endProp = 'DTEND';
29                         }
30
31                         /** @var Sabre\VObject\Property\DateTime $dtstart  */
32                         $dtstart = $parent->__get("DTSTART");
33                         if (isset($parent->$endProp)) {
34                                 $effectiveTrigger = clone $parent->$endProp->getDateTime();
35                                 $effectiveTrigger->add($triggerDuration);
36                         } elseif ($parent->__get("DURATION") != "") {
37                                 $effectiveTrigger = clone $dtstart->getDateTime();
38                                 $duration         = Sabre\VObject\DateTimeParser::parseDuration($parent->__get("DURATION"));
39                                 $effectiveTrigger->add($duration);
40                                 $effectiveTrigger->add($triggerDuration);
41                         } else {
42                                 $effectiveTrigger = clone $dtstart->getDateTime();
43                                 $effectiveTrigger->add($triggerDuration);
44                         }
45                 }
46         } else {
47                 // ??? @TODO
48                 $effectiveTrigger = $trigger->getDateTime();
49         }
50         return $effectiveTrigger;
51 }
52
53 /**
54  * @param array $calendar
55  * @param array $calendarobject
56  * @throws Sabre_DAV_Exception_BadRequest
57  * @return void
58  */
59 function renderCalDavEntry_data(&$calendar, &$calendarobject)
60 {
61         /** @var Sabre\VObject\Component\VCalendar $vObject  */
62         $vObject       = Sabre\VObject\Reader::read($calendarobject["calendardata"]);
63         $componentType = null;
64         /** @var Sabre\VObject\Component\VEvent $component  */
65         $component = null;
66         foreach ($vObject->getComponents() as $component) {
67                 if ($component->name !== 'VTIMEZONE') {
68                         $componentType = $component->name;
69                         break;
70                 }
71         }
72         if (!$componentType) {
73                 throw new Sabre_DAV_Exception_BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component');
74         }
75
76         $timezoneOffset = date("P"); // @TODO Get the actual timezone from the event
77
78
79         if ($componentType !== 'VEVENT') return;
80
81         $event = array(
82                 "description" => ($component->__get("DESCRIPTION") ? $component->__get("DESCRIPTION")->value : null),
83                 "summary"     => ($component->__get("SUMMARY") ? $component->__get("SUMMARY")->value : null),
84                 "location"    => ($component->__get("LOCATION") ? $component->__get("LOCATION")->value : null),
85                 "color"       => ($component->__get("X-ANIMEXX-COLOR") ? $component->__get("X-ANIMEXX-COLOR")->value : null),
86         );
87
88         $recurring = ($component->__get("RRULE") ? 1 : 0);
89         /** @var Sabre\VObject\Property\DateTime $dtstart  */
90         $dtstart = $component->__get("DTSTART");
91         $allday  = ($dtstart->getDateType() == Sabre\VObject\Property\DateTime::DATE ? 1 : 0);
92
93         /** @var array|Sabre\VObject\Component\VAlarm[] $alarms  */
94         $alarms = array();
95         foreach ($component->getComponents() as $a_component) if ($a_component->name == "VALARM") {
96                 /** var Sabre\VObject\Component\VAlarm $component */
97                 $alarms[] = $a_component;
98         }
99
100         $it       = new Sabre\VObject\RecurrenceIterator($vObject, (string)$component->__get("UID"));
101         $last_end = 0;
102         $max_ts   = mktime(0, 0, 0, 1, 1, CALDAV_MAX_YEAR * 1);
103         $first    = true;
104
105         while ($it->valid() && $last_end < $max_ts && ($recurring || $first)) {
106                 $first    = false;
107                 $last_end = $it->getDtEnd()->getTimestamp();
108                 $start    = $it->getDtStart()->getTimestamp();
109
110                 q("INSERT INTO %s%sjqcalendar (`calendar_id`, `calendarobject_id`, `Summary`, `StartTime`, `EndTime`, `IsEditable`, `IsAllDayEvent`, `IsRecurring`, `Color`) VALUES
111                         (%d, %d, '%s', CONVERT_TZ('%s', '$timezoneOffset', @@session.time_zone), CONVERT_TZ('%s', '$timezoneOffset', @@session.time_zone), %d, %d, %d, '%s')",
112                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendar["id"]), IntVal($calendarobject["id"]), dbesc($event["summary"]), date("Y-m-d H:i:s", $start),
113                         date("Y-m-d H:i:s", $last_end), 1, $allday, $recurring, dbesc(substr($event["color"], 1))
114                 );
115
116                 foreach ($alarms as $alarm) {
117                         $alarm    = renderCalDavEntry_calcalarm($alarm, $component);
118                         $notified = ($alarm->getTimestamp() < time() ? 1 : 0);
119                         q("INSERT INTO %s%snotifications (`calendar_id`, `calendarobject_id`, `alert_date`, `notified`) VALUES (%d, %d, CONVERT_TZ('%s', '$timezoneOffset', @@session.time_zone), %d)",
120                                 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendar["id"]), IntVal($calendarobject["id"]), $alarm->format("Y-m-d H:i:s"), $notified
121                         );
122                 }
123
124                 $it->next();
125         }
126
127         return;
128
129 }
130
131
132 /**
133  *
134  */
135 function renderAllCalDavEntries()
136 {
137         q("DELETE FROM %s%sjqcalendar", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
138         q("DELETE FROM %s%snotifications", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
139         $calendars = q("SELECT * FROM %s%scalendars", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
140         $anz       = count($calendars);
141         $i         = 0;
142         foreach ($calendars as $calendar) {
143                 $i++;
144                 if (($i % 100) == 0) echo "$i / $anz\n";
145                 $calobjs = q("SELECT * FROM %s%scalendarobjects WHERE `calendar_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendar["id"]));
146                 foreach ($calobjs as $calobj) renderCalDavEntry_data($calendar, $calobj);
147         }
148 }
149
150
151 /**
152  * @param string $uri
153  * @return bool
154  */
155 function renderCalDavEntry_uri($uri)
156 {
157         $calobj = q("SELECT * FROM %s%scalendarobjects WHERE `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($uri));
158         if (count($calobj) == 0) return false;
159
160         q("DELETE FROM %s%sjqcalendar WHERE `calendar_id` = %d AND `calendarobject_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calobj[0]["calendar_id"]), IntVal($calobj[0]["id"]));
161         q("DELETE FROM %s%snotifications WHERE `calendar_id` = %d AND `calendarobject_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calobj[0]["calendar_id"]), IntVal($calobj[0]["id"]));
162
163         $calendars = q("SELECT * FROM %s%scalendars WHERE `id`=%d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calobj[0]["calendar_id"]));
164
165         renderCalDavEntry_data($calendars[0], $calobj[0]);
166         return true;
167 }
168
169
170 /**
171  * @param int $calobj_id
172  * @return bool
173  */
174 function renderCalDavEntry_calobj_id($calobj_id)
175 {
176         $calobj_id = IntVal($calobj_id);
177         q("DELETE FROM %s%sjqcalendar WHERE `calendarobject_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calobj_id);
178         q("DELETE FROM %s%snotifications WHERE `calendarobject_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calobj_id);
179
180         $calobj = q("SELECT * FROM %s%scalendarobjects WHERE `id` = '%d'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calobj_id);
181         if (count($calobj) == 0) return false;
182
183         $calendars = q("SELECT * FROM %s%scalendars WHERE `id`=%d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calobj[0]["calendar_id"]));
184
185         renderCalDavEntry_data($calendars[0], $calobj[0]);
186         return true;
187 }