]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/calendar_rendering.fnk.php
Set notifications of events; the actual notification routine is not yet implemented
[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
77         if ($componentType !== 'VEVENT') return;
78
79         $event = array(
80                 "description" => ($component->__get("DESCRIPTION") ? $component->__get("DESCRIPTION")->value : null),
81                 "summary"     => ($component->__get("SUMMARY") ? $component->__get("SUMMARY")->value : null),
82                 "location"    => ($component->__get("LOCATION") ? $component->__get("LOCATION")->value : null),
83                 "color"       => ($component->__get("X-ANIMEXX-COLOR") ? $component->__get("X-ANIMEXX-COLOR")->value : null),
84         );
85
86         $recurring = ($component->__get("RRULE") ? 1 : 0);
87         /** @var Sabre_VObject_Property_DateTime $dtstart  */
88         $dtstart = $component->__get("DTSTART");
89         $allday  = ($dtstart->getDateType() == Sabre_VObject_Property_DateTime::DATE ? 1 : 0);
90
91         /** @var array|Sabre_VObject_Component_VAlarm[] $alarms  */
92         $alarms = array();
93         foreach ($component->getComponents() as $a_component) if ($a_component->name == "VALARM") {
94                 /** var Sabre_VObject_Component_VAlarm $component */
95                 $alarms[] = $a_component;
96         }
97
98         $it       = new Sabre_VObject_RecurrenceIterator($vObject, (string)$component->__get("UID"));
99         $last_end = 0;
100         $max_ts   = mktime(0, 0, 0, 1, 1, CALDAV_MAX_YEAR * 1);
101         $first    = true;
102
103         while ($it->valid() && $last_end < $max_ts && ($recurring || $first)) {
104                 $first    = false;
105                 $last_end = $it->getDtEnd()->getTimestamp();
106                 $start    = $it->getDtStart()->getTimestamp();
107
108                 q("INSERT INTO %s%sjqcalendar (`calendar_id`, `calendarobject_id`, `Summary`, `StartTime`, `EndTime`, `IsEditable`, `IsAllDayEvent`, `IsRecurring`, `Color`) VALUES
109                         (%d, %d, '%s', '%s', '%s', %d, %d, %d, '%s')", CALDAV_SQL_DB, CALDAV_SQL_PREFIX,
110                         IntVal($calendar["id"]), IntVal($calendarobject["id"]), dbesc($event["summary"]), date("Y-m-d H:i:s", $start), date("Y-m-d H:i:s", $last_end),
111                         1, $allday, $recurring, dbesc(substr($event["color"], 1))
112                 );
113
114                 foreach ($alarms as $alarm) {
115                         $alarm    = renderCalDavEntry_calcalarm($alarm, $component);
116                         $notified = ($alarm->getTimestamp() < time() ? 1 : 0);
117                         q("INSERT INTO %s%snotifications (`calendar_id`, `calendarobject_id`, `alert_date`, `notified`) VALUES (%d, %d, '%s', %d)",
118                                 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendar["id"]), IntVal($calendarobject["id"]), $alarm->format("Y-m-d H:i:s"), $notified
119                         );
120                 }
121
122                 $it->next();
123         }
124
125         return;
126
127 }
128
129
130 /**
131  *
132  */
133 function renderAllCalDavEntries()
134 {
135         q("DELETE FROM %s%sjqcalendar", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
136         q("DELETE FROM %s%snotifications", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
137         $calendars = q("SELECT * FROM %s%scalendars", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
138         $anz       = count($calendars);
139         $i         = 0;
140         foreach ($calendars as $calendar) {
141                 $i++;
142                 if (($i % 100) == 0) echo "$i / $anz\n";
143                 $calobjs = q("SELECT * FROM %s%scalendarobjects WHERE `calendar_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendar["id"]));
144                 foreach ($calobjs as $calobj) renderCalDavEntry_data($calendar, $calobj);
145         }
146 }
147
148
149 /**
150  * @param string $uri
151  * @return bool
152  */
153 function renderCalDavEntry_uri($uri)
154 {
155         $calobj = q("SELECT * FROM %s%scalendarobjects WHERE `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($uri));
156         if (count($calobj) == 0) return false;
157
158         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"]));
159         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"]));
160
161         $calendars = q("SELECT * FROM %s%scalendars WHERE `id`=%d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calobj[0]["calendar_id"]));
162
163         renderCalDavEntry_data($calendars[0], $calobj[0]);
164         return true;
165 }
166
167
168 /**
169  * @param int $calobj_id
170  * @return bool
171  */
172 function renderCalDavEntry_calobj_id($calobj_id)
173 {
174         $calobj_id = IntVal($calobj_id);
175         q("DELETE FROM %s%sjqcalendar WHERE `calendarobject_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calobj_id);
176         q("DELETE FROM %s%snotifications WHERE `calendarobject_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calobj_id);
177
178         $calobj = q("SELECT * FROM %s%scalendarobjects WHERE `id` = '%d'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calobj_id);
179         if (count($calobj) == 0) return false;
180
181         $calendars = q("SELECT * FROM %s%scalendars WHERE `id`=%d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calobj[0]["calendar_id"]));
182
183         renderCalDavEntry_data($calendars[0], $calobj[0]);
184         return true;
185 }