/**
 * @return string
 */
function wdcal_getEditPage_exception_selector()
{
    header("Content-type: application/json");
    $a = get_app();
    $localization = wdcal_local::getInstanceByUser($a->user["uid"]);
    $vObject = dav_create_empty_vevent();
    foreach ($vObject->getComponents() as $component) {
        if ($component->name !== 'VTIMEZONE') {
            break;
        }
    }
    /** @var Sabre\VObject\Component\VEvent $component */
    wdcal_set_component_date($component, $localization);
    wdcal_set_component_recurrence($component, $localization);
    $it = new Sabre\VObject\RecurrenceIterator($vObject, (string) $component->__get("UID"));
    $max_ts = mktime(0, 0, 0, 1, 1, CALDAV_MAX_YEAR + 1);
    $last_start = 0;
    $o = "<ul>";
    $i = 0;
    while ($it->valid() && $last_start < $max_ts && $i++ < 1000) {
        $last_start = $it->getDtStart()->getTimestamp();
        $o .= "<li><a href='#' class='exception_selector_link' data-timestamp='{$last_start}'>" . $localization->date_timestamp2localDate($last_start) . "</a></li>\n";
        $it->next();
    }
    $o .= "</ul>\n";
    return $o;
}
/**
 * @param array $calendar
 * @param array $calendarobject
 * @throws Sabre_DAV_Exception_BadRequest
 * @return void
 */
function renderCalDavEntry_data(&$calendar, &$calendarobject)
{
    /** @var Sabre\VObject\Component\VCalendar $vObject  */
    $vObject = Sabre\VObject\Reader::read($calendarobject["calendardata"]);
    $componentType = null;
    /** @var Sabre\VObject\Component\VEvent $component  */
    $component = null;
    foreach ($vObject->getComponents() as $component) {
        if ($component->name !== 'VTIMEZONE') {
            $componentType = $component->name;
            break;
        }
    }
    if (!$componentType) {
        throw new Sabre_DAV_Exception_BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component');
    }
    $timezoneOffset = date("P");
    // @TODO Get the actual timezone from the event
    if ($componentType !== 'VEVENT') {
        return;
    }
    $event = array("description" => $component->__get("DESCRIPTION") ? $component->__get("DESCRIPTION")->value : null, "summary" => $component->__get("SUMMARY") ? $component->__get("SUMMARY")->value : null, "location" => $component->__get("LOCATION") ? $component->__get("LOCATION")->value : null, "color" => $component->__get("X-ANIMEXX-COLOR") ? $component->__get("X-ANIMEXX-COLOR")->value : null);
    $recurring = $component->__get("RRULE") ? 1 : 0;
    /** @var Sabre\VObject\Property\DateTime $dtstart  */
    $dtstart = $component->__get("DTSTART");
    $allday = $dtstart->getDateType() == Sabre\VObject\Property\DateTime::DATE ? 1 : 0;
    /** @var array|Sabre\VObject\Component\VAlarm[] $alarms  */
    $alarms = array();
    foreach ($component->getComponents() as $a_component) {
        if ($a_component->name == "VALARM") {
            /** var Sabre\VObject\Component\VAlarm $component */
            $alarms[] = $a_component;
        }
    }
    $it = new Sabre\VObject\RecurrenceIterator($vObject, (string) $component->__get("UID"));
    $last_end = 0;
    $max_ts = mktime(0, 0, 0, 1, 1, CALDAV_MAX_YEAR * 1);
    $first = true;
    while ($it->valid() && $last_end < $max_ts && ($recurring || $first)) {
        $first = false;
        $last_end = $it->getDtEnd()->getTimestamp();
        $start = $it->getDtStart()->getTimestamp();
        q("INSERT INTO %s%sjqcalendar (`calendar_id`, `calendarobject_id`, `Summary`, `StartTime`, `EndTime`, `IsEditable`, `IsAllDayEvent`, `IsRecurring`, `Color`) VALUES\n\t\t\t(%d, %d, '%s', CONVERT_TZ('%s', '{$timezoneOffset}', @@session.time_zone), CONVERT_TZ('%s', '{$timezoneOffset}', @@session.time_zone), %d, %d, %d, '%s')", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, 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), 1, $allday, $recurring, dbesc(substr($event["color"], 1)));
        foreach ($alarms as $alarm) {
            $alarm = renderCalDavEntry_calcalarm($alarm, $component);
            $notified = $alarm->getTimestamp() < time() ? 1 : 0;
            q("INSERT INTO %s%snotifications (`calendar_id`, `calendarobject_id`, `alert_date`, `notified`) VALUES (%d, %d, CONVERT_TZ('%s', '{$timezoneOffset}', @@session.time_zone), %d)", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendar["id"]), IntVal($calendarobject["id"]), $alarm->format("Y-m-d H:i:s"), $notified);
        }
        $it->next();
    }
    return;
}