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);
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');
77 if ($componentType !== 'VEVENT') return;
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),
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);
91 /** @var array|Sabre_VObject_Component_VAlarm[] $alarms */
93 foreach ($component->getComponents() as $a_component) if ($a_component->name == "VALARM") {
94 /** var Sabre_VObject_Component_VAlarm $component */
95 $alarms[] = $a_component;
98 $it = new Sabre_VObject_RecurrenceIterator($vObject, (string)$component->__get("UID"));
100 $max_ts = mktime(0, 0, 0, 1, 1, CALDAV_MAX_YEAR * 1);
103 while ($it->valid() && $last_end < $max_ts && ($recurring || $first)) {
105 $last_end = $it->getDtEnd()->getTimestamp();
106 $start = $it->getDtStart()->getTimestamp();
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))
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
133 function renderAllCalDavEntries()
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);
140 foreach ($calendars as $calendar) {
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);
153 function renderCalDavEntry_uri($uri)
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;
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"]));
161 $calendars = q("SELECT * FROM %s%scalendars WHERE `id`=%d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calobj[0]["calendar_id"]));
163 renderCalDavEntry_data($calendars[0], $calobj[0]);
169 * @param int $calobj_id
172 function renderCalDavEntry_calobj_id($calobj_id)
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);
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;
181 $calendars = q("SELECT * FROM %s%scalendars WHERE `id`=%d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calobj[0]["calendar_id"]));
183 renderCalDavEntry_data($calendars[0], $calobj[0]);