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