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
11 function renderCalDavEntry_calcalarm(&$alarm, &$parent)
13 $trigger = $alarm->__get("TRIGGER");
14 if (!isset($trigger['VALUE']) || strtoupper($trigger['VALUE']) === 'DURATION') {
15 $triggerDuration = Sabre\VObject\DateTimeParser::parseDuration($trigger->value);
17 $related = (isset($trigger['RELATED']) && strtoupper($trigger['RELATED']) == 'END') ? 'END' : 'START';
19 if ($related === 'START') {
20 /** @var Sabre\VObject\Property\DateTime $dtstart */
21 $dtstart = $parent->__get("DTSTART");
22 $effectiveTrigger = $dtstart->getDateTime();
23 $effectiveTrigger->add($triggerDuration);
25 if ($parent->name === 'VTODO') {
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);
42 $effectiveTrigger = clone $dtstart->getDateTime();
43 $effectiveTrigger->add($triggerDuration);
48 $effectiveTrigger = $trigger->getDateTime();
50 return $effectiveTrigger;
54 * @param array $calendar
55 * @param array $calendarobject
56 * @throws Sabre_DAV_Exception_BadRequest
59 function renderCalDavEntry_data(&$calendar, &$calendarobject)
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 */
66 foreach ($vObject->getComponents() as $component) {
67 if ($component->name !== 'VTIMEZONE') {
68 $componentType = $component->name;
72 if (!$componentType) {
73 throw new Sabre_DAV_Exception_BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component');
76 $timezoneOffset = date("P"); // @TODO Get the actual timezone from the event
79 if ($componentType !== 'VEVENT') return;
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),
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);
93 /** @var array|Sabre\VObject\Component\VAlarm[] $alarms */
95 foreach ($component->getComponents() as $a_component) if ($a_component->name == "VALARM") {
96 /** var Sabre\VObject\Component\VAlarm $component */
97 $alarms[] = $a_component;
100 $it = new Sabre\VObject\RecurrenceIterator($vObject, (string)$component->__get("UID"));
102 $max_ts = mktime(0, 0, 0, 1, 1, CALDAV_MAX_YEAR * 1);
105 while ($it->valid() && $last_end < $max_ts && ($recurring || $first)) {
107 $last_end = $it->getDtEnd()->getTimestamp();
108 $start = $it->getDtStart()->getTimestamp();
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))
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
135 function renderAllCalDavEntries()
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);
142 foreach ($calendars as $calendar) {
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);
155 function renderCalDavEntry_uri($uri)
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;
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"]));
163 $calendars = q("SELECT * FROM %s%scalendars WHERE `id`=%d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calobj[0]["calendar_id"]));
165 renderCalDavEntry_data($calendars[0], $calobj[0]);
171 * @param int $calobj_id
174 function renderCalDavEntry_calobj_id($calobj_id)
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);
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;
183 $calendars = q("SELECT * FROM %s%scalendars WHERE `id`=%d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calobj[0]["calendar_id"]));
185 renderCalDavEntry_data($calendars[0], $calobj[0]);