/**
 * @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;
}
 /**
  * Parses some information from calendar objects, used for optimized
  * calendar-queries.
  *
  * Returns an array with the following keys:
  *   * etag
  *   * size
  *   * componentType
  *   * firstOccurence
  *   * lastOccurence
  *
  * @param string $calendarData
  * @throws Sabre_DAV_Exception_BadRequest
  * @return array
  */
 protected function getDenormalizedData($calendarData)
 {
     /** @var Sabre\VObject\Component\VEvent $vObject */
     $vObject = Sabre\VObject\Reader::read($calendarData);
     $componentType = null;
     $component = null;
     $firstOccurence = null;
     $lastOccurence = 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');
     }
     if ($componentType === 'VEVENT') {
         /** @var Sabre\VObject\Component\VEvent $component */
         /** @var Sabre\VObject\Property\DateTime $dtstart  */
         $dtstart = $component->__get("DTSTART");
         $firstOccurence = $dtstart->getDateTime()->getTimeStamp();
         // Finding the last occurence is a bit harder
         if (!$component->__get("RRULE")) {
             $lastOccurence = self::getDtEndTimeStamp($component);
         } else {
             $it = new Sabre\VObject\RecurrenceIterator($vObject, (string) $component->__get("UID"));
             $maxDate = new DateTime(CALDAV_MAX_YEAR . "-01-01");
             if ($it->isInfinite()) {
                 $lastOccurence = $maxDate->getTimeStamp();
             } else {
                 $end = $it->getDtEnd();
                 while ($it->valid() && $end < $maxDate) {
                     $end = $it->getDtEnd();
                     $it->next();
                 }
                 $lastOccurence = $end->getTimeStamp();
             }
         }
     }
     return array('etag' => md5($calendarData), 'size' => strlen($calendarData), 'componentType' => $componentType, 'firstOccurence' => $firstOccurence, 'lastOccurence' => $lastOccurence);
 }
Exemple #4
0
/**
 * Parses some information from calendar objects, used for optimized
 * calendar-queries.
 *
 * Blantently copied from Sabre\CalDAV\Backend\PDO
 *
 * Returns an array with the following keys:
 *   * etag
 *   * size
 *   * componentType
 *   * firstOccurence
 *   * lastOccurence
 *
 * @param string $calendarData
 * @return array
 */
function getDenormalizedData($calendarData)
{
    $vObject = \Sabre\VObject\Reader::read($calendarData);
    $componentType = null;
    $component = null;
    $firstOccurence = null;
    $lastOccurence = 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');
    }
    if ($componentType === 'VEVENT') {
        $firstOccurence = $component->DTSTART->getDateTime()->getTimeStamp();
        // Finding the last occurence is a bit harder
        if (!isset($component->RRULE)) {
            if (isset($component->DTEND)) {
                $lastOccurence = $component->DTEND->getDateTime()->getTimeStamp();
            } elseif (isset($component->DURATION)) {
                $endDate = clone $component->DTSTART->getDateTime();
                $endDate->add(\Sabre\VObject\DateTimeParser::parse($component->DURATION->value));
                $lastOccurence = $endDate->getTimeStamp();
            } elseif (!$component->DTSTART->hasTime()) {
                $endDate = clone $component->DTSTART->getDateTime();
                $endDate->modify('+1 day');
                $lastOccurence = $endDate->getTimeStamp();
            } else {
                $lastOccurence = $firstOccurence;
            }
        } else {
            $it = new \Sabre\VObject\RecurrenceIterator($vObject, (string) $component->UID);
            $maxDate = new DateTime(\Sabre\CalDAV\Backend\PDO::MAX_DATE);
            if ($it->isInfinite()) {
                $lastOccurence = $maxDate->getTimeStamp();
            } else {
                $end = $it->getDtEnd();
                while ($it->valid() && $end < $maxDate) {
                    $end = $it->getDtEnd();
                    $it->next();
                }
                $lastOccurence = $end->getTimeStamp();
            }
        }
    }
    return array('etag' => md5($calendarData), 'size' => strlen($calendarData), 'componentType' => $componentType, 'firstOccurence' => $firstOccurence, 'lastOccurence' => $lastOccurence);
}