0) {
$calendar = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_WRITE);
if (!$calendar) {
$calendar = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_READ);
$calendars = array();
} else {
$calendars = dav_get_current_user_calendars($server, DAV_ACL_WRITE);
}
if ($calendar == null) return "Calendar not found";
$obj_uri = Sabre_CalDAV_Backend_Common::loadCalendarobjectById($uri);
$vObject = dav_get_current_user_calendarobject($server, $calendar, $obj_uri["uri"], DAV_ACL_WRITE);
$component = dav_get_eventComponent($vObject);
if ($component == null) return t('Could not open component for editing');
/** @var Sabre_VObject_Property_DateTime $dtstart */
$dtstart = $component->__get("DTSTART");
$event = array(
"id" => IntVal($uri),
"Summary" => ($component->__get("SUMMARY") ? $component->__get("SUMMARY")->value : null),
"StartTime" => $dtstart->getDateTime()->getTimeStamp(),
"EndTime" => Sabre_CalDAV_Backend_Common::getDtEndTimeStamp($component),
"IsAllDayEvent" => (strlen($dtstart->value) == 8),
"Description" => ($component->__get("DESCRIPTION") ? $component->__get("DESCRIPTION")->value : null),
"Location" => ($component->__get("LOCATION") ? $component->__get("LOCATION")->value : null),
"Color" => ($component->__get("X-ANIMEXX-COLOR") ? $component->__get("X-ANIMEXX-COLOR")->value : null),
);
$exdates = $component->select("EXDATE");
$recurrentce_exdates = array();
/** @var Sabre_VObject_Property_MultiDateTime $x */
foreach ($exdates as $x) {
/** @var DateTime $y */
$z = $x->getDateTimes();
foreach ($z as $y) $recurrentce_exdates[] = $y->getTimeStamp();
}
if ($component->select("RRULE")) $recurrence = new Sabre_VObject_RecurrenceIterator($vObject, (string)$component->__get("UID"));
else $recurrence = null;
} elseif (isset($_REQUEST["start"]) && $_REQUEST["start"] > 0) {
$calendars = dav_get_current_user_calendars($server, DAV_ACL_WRITE);
$calendar = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_WRITE);
$event = array(
"id" => 0,
"Summary" => $_REQUEST["title"],
"StartTime" => InTVal($_REQUEST["start"]),
"EndTime" => IntVal($_REQUEST["end"]),
"IsAllDayEvent" => $_REQUEST["isallday"],
"Description" => "",
"Location" => "",
"Color" => "#5858ff",
);
if ($_REQUEST["isallday"]) {
$notifications = array(array("rel" => "start", "type" => "duration", "period" => "hour", "period_val" => 24));
} else {
$notifications = array(array("rel" => "start", "type" => "duration", "period" => "hour", "period_val" => 1));
}
$recurrence = null;
$recurrentce_exdates = array();
} else {
$calendars = dav_get_current_user_calendars($server, DAV_ACL_WRITE);
$calendar = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_WRITE);
$event = array(
"id" => 0,
"Summary" => "",
"StartTime" => time(),
"EndTime" => time() + 3600,
"IsAllDayEvent" => "0",
"Description" => "",
"Location" => "",
"Color" => "#5858ff",
);
$notifications = array(array("rel" => "start", "type" => "duration", "period" => "hour", "period_val" => 1));
$recurrence = null;
$recurrentce_exdates = array();
}
$postto = $baseurl . "/dav/wdcal/" . ($uri == 0 ? "new/" : $calendar_id . "/" . $uri . "/edit/");
$out = "" . t("Go back to the calendar") . "
";
$out .= "
";
return $out;
}
/**
* @param Sabre_VObject_Component_VEvent $component
* @param wdcal_local $localization
*/
function wdcal_set_component_date(&$component, &$localization) {
if (isset($_REQUEST["allday"])) {
$ts_start = $localization->date_local2timestamp($_REQUEST["start_date"] . " 00:00");
$ts_end = $localization->date_local2timestamp($_REQUEST["end_date"] . " 00:00");
$type = Sabre_VObject_Property_DateTime::DATE;
} else {
$ts_start = $localization->date_local2timestamp($_REQUEST["start_date"] . " " . $_REQUEST["start_time"]);
$ts_end = $localization->date_local2timestamp($_REQUEST["end_date"] . " " . $_REQUEST["end_time"]);
$type = Sabre_VObject_Property_DateTime::LOCALTZ;
}
$datetime_start = new Sabre_VObject_Property_DateTime("DTSTART");
$datetime_start->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_start)), $type);
$datetime_end = new Sabre_VObject_Property_DateTime("DTEND");
$datetime_end->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_end)), $type);
$component->__unset("DTSTART");
$component->__unset("DTEND");
$component->add($datetime_start);
$component->add($datetime_end);
}
/**
* @param Sabre_VObject_Component_VEvent $component
* @param wdcal_local $localization
*/
function wdcal_set_component_recurrence(&$component, &$localization) {
$component->__unset("RRULE");
$component->__unset("EXRULE");
$component->__unset("EXDATE");
$component->__unset("RDATE");
$part_until = "";
switch ($_REQUEST["rec_until_type"]) {
case "date":
$date = $localization->date_local2timestamp($_REQUEST["rec_until_date"]);
$part_until = ";UNTIL=" . date("Ymd", $date);
$datetime_until = new Sabre_VObject_Property_DateTime("UNTIL");
$datetime_until->setDateTime(new DateTime(date("Y-m-d H:i:s", $date)), Sabre_VObject_Property_DateTime::DATE);
break;
case "count":
$part_until = ";COUNT=" . IntVal($_REQUEST["rec_until_count"]);
break;
}
switch ($_REQUEST["rec_frequency"]) {
case "daily":
$part_freq = "FREQ=DAILY";
if (isset($_REQUEST["rec_daily_byday"])) {
$days = array();
foreach ($_REQUEST["rec_daily_byday"] as $x) if (in_array($x, array("MO", "TU", "WE", "TH", "FR", "SA", "SU"))) $days[] = $x;
if (count($days) > 0) $part_freq .= ";BYDAY=" . implode(",", $days);
}
break;
case "weekly":
$part_freq = "FREQ=WEEKLY";
if (isset($_REQUEST["rec_weekly_wkst"]) && in_array($_REQUEST["rec_weekly_wkst"], array("MO", "SU"))) $part_freq .= ";WKST=" . $_REQUEST["rec_weekly_wkst"];
if (isset($_REQUEST["rec_weekly_byday"])) {
$days = array();
foreach ($_REQUEST["rec_weekly_byday"] as $x) if (in_array($x, array("MO", "TU", "WE", "TH", "FR", "SA", "SU"))) $days[] = $x;
if (count($days) > 0) $part_freq .= ";BYDAY=" . implode(",", $days);
}
break;
case "monthly":
$part_freq = "FREQ=MONTHLY";
break;
case "FREQ=yearly":
$part_freq = "FREQ=YEARLY";
break;
default:
$part_freq = "";
}
if ($part_freq == "") return;
if (isset($_REQUEST["rec_interval"])) $part_freq .= ";INTERVAL=" . InTVal($_REQUEST["rec_interval"]);
if (isset($_REQUEST["rec_exceptions"])) {
$arr = array();
foreach ($_REQUEST["rec_exceptions"] as $except) {
$arr[] = new DateTime(date("Y-m-d H:i:s", $except));
}
/** @var Sabre_VObject_Property_MultiDateTime $prop */
$prop = Sabre_VObject_Property::create("EXDATE");
$prop->setDateTimes($arr);
$component->add($prop);
}
$rrule = $part_freq . $part_until;
$component->add(new Sabre_VObject_Property("RRULE", $rrule));
}
/**
* @param string $uri
* @param string $recurr_uri
* @param int $uid
* @param string $timezone
* @param string $goaway_url
* @return array
*/
function wdcal_postEditPage($uri, $recurr_uri = "", $uid = 0, $timezone = "", $goaway_url = "")
{
$uid = IntVal($uid);
$localization = wdcal_local::getInstanceByUser($uid);
$server = dav_create_server(true, true, false);
if ($uri > 0) {
$calendar = dav_get_current_user_calendar_by_id($server, $_REQUEST["calendar"], DAV_ACL_READ);
$obj_uri = Sabre_CalDAV_Backend_Common::loadCalendarobjectById($uri);
$obj_uri = $obj_uri["uri"];
$vObject = dav_get_current_user_calendarobject($server, $calendar, $obj_uri, DAV_ACL_WRITE);
$component = dav_get_eventComponent($vObject);
if ($component == null) return array("ok" => false, "msg" => t('Could not open component for editing'));
} else {
$calendar = dav_get_current_user_calendar_by_id($server, $_REQUEST["calendar"], DAV_ACL_WRITE);
$vObject = dav_create_empty_vevent();
$component = dav_get_eventComponent($vObject);
$obj_uri = $component->__get("UID");
}
wdcal_set_component_date($component, $localization);
wdcal_set_component_recurrence($component, $localization);
$component->__unset("LOCATION");
$component->__unset("SUMMARY");
$component->__unset("DESCRIPTION");
$component->__unset("X-ANIMEXXCOLOR");
$component->add("SUMMARY", icalendar_sanitize_string(dav_compat_parse_text_serverside("summary")));
$component->add("LOCATION", icalendar_sanitize_string(dav_compat_parse_text_serverside("location")));
$component->add("DESCRIPTION", icalendar_sanitize_string(dav_compat_parse_text_serverside("wdcal_desc")));
$component->add("X-ANIMEXX-COLOR", $_REQUEST["color"]);
$data = $vObject->serialize();
if ($uri == 0) {
$calendar->createFile($obj_uri . ".ics", $data);
} else {
$obj = $calendar->getChild($obj_uri);
$obj->put($data);
}
return array("ok" => false, "msg" => t("Saved"));
}
/**
* @return string
*/
function wdcal_getEditPage_exception_selector() {
header("Content-type: application/json");
$a = get_app();
$localization = wdcal_local::getInstanceByUser($a->user["uid"]);
$vObject = wdcal_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 = "\n";
return $o;
}