]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/wdcal_edit.inc.php
Adding and removing calendars
[friendica-addons.git] / dav / common / wdcal_edit.inc.php
1 <?php
2
3 /**
4  * @param wdcal_local $localization
5  * @param string $baseurl
6  * @param int $uid
7  * @param int $calendar_id
8  * @param int $uri
9  * @param string $recurr_uri
10  * @return string
11  */
12 function wdcal_getEditPage_str(&$localization, $baseurl, $uid, $calendar_id, $uri, $recurr_uri = "")
13 {
14         $server = dav_create_server(true, true, false);
15
16         if ($uri > 0) {
17                 $calendar = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_WRITE);
18                 if (!$calendar) {
19                         $calendar  = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_READ);
20                         $calendars = array();
21                 } else {
22                         $calendars = dav_get_current_user_calendars($server, DAV_ACL_WRITE);
23                 }
24
25                 if ($calendar == null) return "Calendar not found";
26
27                 $obj_uri = Sabre_CalDAV_Backend_Common::loadCalendarobjectById($uri);
28
29                 $vObject   = dav_get_current_user_calendarobject($server, $calendar, $obj_uri["uri"], DAV_ACL_WRITE);
30                 $component = dav_get_eventComponent($vObject);
31
32                 if ($component == null) return t('Could not open component for editing');
33
34                 /** @var Sabre_VObject_Property_DateTime $dtstart  */
35                 $dtstart = $component->__get("DTSTART");
36                 $event   = array(
37                         "id"            => IntVal($uri),
38                         "Summary"       => ($component->__get("SUMMARY") ? $component->__get("SUMMARY")->value : null),
39                         "StartTime"     => $dtstart->getDateTime()->getTimeStamp(),
40                         "EndTime"       => Sabre_CalDAV_Backend_Common::getDtEndTimeStamp($component),
41                         "IsAllDayEvent" => (strlen($dtstart->value) == 8),
42                         "Description"   => ($component->__get("DESCRIPTION") ? $component->__get("DESCRIPTION")->value : null),
43                         "Location"      => ($component->__get("LOCATION") ? $component->__get("LOCATION")->value : null),
44                         "Color"         => ($component->__get("X-ANIMEXX-COLOR") ? $component->__get("X-ANIMEXX-COLOR")->value : null),
45                 );
46
47                 $exdates             = $component->select("EXDATE");
48                 $recurrentce_exdates = array();
49                 /** @var Sabre_VObject_Property_MultiDateTime $x */
50                 foreach ($exdates as $x) {
51                         /** @var DateTime $y */
52                         $z = $x->getDateTimes();
53                         foreach ($z as $y) $recurrentce_exdates[] = $y->getTimeStamp();
54                 }
55
56                 if ($component->select("RRULE")) $recurrence = new Sabre_VObject_RecurrenceIterator($vObject, (string)$component->__get("UID"));
57                 else $recurrence = null;
58
59         } elseif (isset($_REQUEST["start"]) && $_REQUEST["start"] > 0) {
60                 $calendars = dav_get_current_user_calendars($server, DAV_ACL_WRITE);
61                 $calendar  = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_WRITE);
62
63                 $event = array(
64                         "id"            => 0,
65                         "Summary"       => $_REQUEST["title"],
66                         "StartTime"     => InTVal($_REQUEST["start"]),
67                         "EndTime"       => IntVal($_REQUEST["end"]),
68                         "IsAllDayEvent" => $_REQUEST["isallday"],
69                         "Description"   => "",
70                         "Location"      => "",
71                         "Color"         => null,
72                 );
73                 if ($_REQUEST["isallday"]) {
74                         $notifications = array(array("rel" => "start", "type" => "duration", "period" => "hour", "period_val" => 24));
75                 } else {
76                         $notifications = array(array("rel" => "start", "type" => "duration", "period" => "hour", "period_val" => 1));
77                 }
78                 $recurrence          = null;
79                 $recurrentce_exdates = array();
80         } else {
81                 $calendars = dav_get_current_user_calendars($server, DAV_ACL_WRITE);
82                 $calendar  = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_WRITE);
83
84                 $event               = array(
85                         "id"            => 0,
86                         "Summary"       => "",
87                         "StartTime"     => time(),
88                         "EndTime"       => time() + 3600,
89                         "IsAllDayEvent" => "0",
90                         "Description"   => "",
91                         "Location"      => "",
92                         "Color"         => null,
93                 );
94                 $notifications       = array(array("rel" => "start", "type" => "duration", "period" => "hour", "period_val" => 1));
95                 $recurrence          = null;
96                 $recurrentce_exdates = array();
97         }
98
99         $postto = $baseurl . "/dav/wdcal/" . ($uri == 0 ? "new/" : $calendar_id . "/" . $uri . "/edit/");
100
101         $out = "<a href='" . $baseurl . "/dav/wdcal/'>" . t("Go back to the calendar") . "</a><br><br>";
102         $out .= "<form method='POST' action='$postto'>
103                 <input type='hidden' name='form_security_token' value='" . get_form_security_token('caledit') . "'>\n";
104
105         $out .= "<h2>" . t("Event data") . "</h2>";
106
107         $out .= "<label for='calendar'>" . t("Calendar") . ":</label><select name='calendar' size='1'>";
108         $found   = false;
109         $cal_col = "aaaaaa";
110         foreach ($calendars as $cal) {
111                 $prop = $cal->getProperties(array("id", DAV_DISPLAYNAME, DAV_CALENDARCOLOR));
112                 $out .= "<option value='" . $prop["id"] . "' ";
113                 if ($prop["id"] == $calendar_id) {
114                         $out .= "selected";
115                         $cal_col = $prop[DAV_CALENDARCOLOR];
116                         $found   = true;
117                 } elseif (!$found) $cal_col = $prop[DAV_CALENDARCOLOR];
118                 $out .= ">" . escape_tags($prop[DAV_DISPLAYNAME]) . "</option>\n";
119         }
120
121         $out .= "</select>";
122         $out .= "&nbsp; &nbsp; <label class='plain'><input type='checkbox' name='color_override' id='color_override' ";
123         if (!is_null($event["Color"])) $out .= "checked";
124         $out .= "> " . t("Special color") . ":</label>";
125         $out .= "<span id='cal_color_holder' ";
126         if (is_null($event["Color"])) $out .= "style='display: none;'";
127         $out .= "><input name='color' id='cal_color' value='" . (is_null($event["Color"]) ? "#" . $cal_col : escape_tags($event["Color"])) . "'></span>";
128         $out .= "<br>\n";
129
130         $out .= "<label class='block' for='cal_summary'>" . t("Subject") . ":</label>
131                 <input name='summary' id='cal_summary' value=\"" . escape_tags($event["Summary"]) . "\"><br>\n";
132         $out .= "<label class='block' for='cal_allday'>Is All-Day event:</label><input type='checkbox' name='allday' id='cal_allday' " . ($event["IsAllDayEvent"] ? "checked" : "") . "><br>\n";
133
134         $out .= "<label class='block' for='cal_startdate'>" . t("Starts") . ":</label>";
135         $out .= "<input name='start_date' value='" . $localization->dateformat_datepicker_php($event["StartTime"]) . "' id='cal_start_date'>";
136         $out .= "<input name='start_time' value='" . date("H:i", $event["StartTime"]) . "' id='cal_start_time'>";
137         $out .= "<br>\n";
138
139         $out .= "<label class='block' for='cal_enddate'>" . t("Ends") . ":</label>";
140         $out .= "<input name='end_date' value='" . $localization->dateformat_datepicker_php($event["EndTime"]) . "' id='cal_end_date'>";
141         $out .= "<input name='end_time' value='" . date("H:i", $event["EndTime"]) . "' id='cal_end_time'>";
142         $out .= "<br>\n";
143
144         $out .= "<label class='block' for='cal_location'>" . t("Location") . ":</label><input name='location' id='cal_location' value=\"" . escape_tags($event["Location"]) . "\"><br>\n";
145
146         $out .= "<label class='block' for='event-desc-textarea'>" . t("Description") . ":</label> <textarea id='event-desc-textarea' name='wdcal_desc' style='vertical-align: top; width: 400px; height: 100px;'>" . escape_tags($event["Description"]) . "</textarea>";
147         $out .= "<br style='clear: both;'>";
148
149         $out .= "<h2>" . t("Recurrence") . "</h2>";
150
151         $out .= "<label class='block' for='rec_frequency'>" . t("Frequency") . ":</label> <select id='rec_frequency' name='rec_frequency' size='1'>";
152         $out .= "<option value=''>" . t("None") . "</option>\n";
153         $out .= "<option value='daily' ";
154         if ($recurrence && $recurrence->frequency == "daily") $out .= "selected";
155         $out .= ">" . t("Daily") . "</option>\n";
156         $out .= "<option value='weekly' ";
157         if ($recurrence && $recurrence->frequency == "weekly") $out .= "selected";
158         $out .= ">" . t("Weekly") . "</option>\n";
159         $out .= "<option value='monthly' ";
160         if ($recurrence && $recurrence->frequency == "monthly") $out .= "selected";
161         $out .= ">" . t("Monthly") . "</option>\n";
162         $out .= "<option value='yearly' ";
163         if ($recurrence && $recurrence->frequency == "yearly") $out .= "selected";
164         $out .= ">" . t("Yearly") . "</option>\n";
165         $out .= "</select><br>\n";
166         $out .= "<div id='rec_details'>";
167
168         $select = "<select id='rec_interval' name='rec_interval' size='1'>";
169         for ($i = 1; $i < 50; $i++) {
170                 $select .= "<option value='$i' ";
171                 if ($recurrence && $i == $recurrence->interval) $select .= "selected";
172                 $select .= ">$i</option>\n";
173         }
174         $select .= "</select>";
175         $time = "<span class='rec_daily'>" . t("days") . "</span>";
176         $time .= "<span class='rec_weekly'>" . t("weeks") . "</span>";
177         $time .= "<span class='rec_monthly'>" . t("months") . "</span>";
178         $time .= "<span class='rec_yearly'>" . t("years") . "</span>";
179         $out .= "<label class='block' for='rev_interval'>" . t("Interval") . ":</label> " . str_replace(array("%select%", "%time%"), array($select, $time), t("All %select% %time%")) . "<br>";
180
181
182         $out .= "<div class='rec_daily'>";
183         $out .= "<label class='block'>" . t("Days") . ":</label>";
184         if ($recurrence && $recurrence->byDay) {
185                 $byday = $recurrence->byDay;
186         } else {
187                 $byday = array("MO", "TU", "WE", "TH", "FR", "SA", "SU");
188         }
189         if ($localization->getFirstDayOfWeek() == 0) {
190                 $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='SU' ";
191                 if (in_array("SU", $byday)) $out .= "checked";
192                 $out .= ">" . t("Sunday") . "</label> &nbsp; ";
193         }
194         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='MO' ";
195         if (in_array("MO", $byday)) $out .= "checked";
196         $out .= ">" . t("Monday") . "</label> &nbsp; ";
197         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='TU' ";
198         if (in_array("TU", $byday)) $out .= "checked";
199         $out .= ">" . t("Tuesday") . "</label> &nbsp; ";
200         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='WE' ";
201         if (in_array("WE", $byday)) $out .= "checked";
202         $out .= ">" . t("Wednesday") . "</label> &nbsp; ";
203         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='TH' ";
204         if (in_array("TH", $byday)) $out .= "checked";
205         $out .= ">" . t("Thursday") . "</label> &nbsp; ";
206         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='FR' ";
207         if (in_array("FR", $byday)) $out .= "checked";
208         $out .= ">" . t("Friday") . "</label> &nbsp; ";
209         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='SA' ";
210         if (in_array("SA", $byday)) $out .= "checked";
211         $out .= ">" . t("Saturday") . "</label> &nbsp; ";
212         if ($localization->getFirstDayOfWeek() != 0) {
213                 $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='SU' ";
214                 if (in_array("SU", $byday)) $out .= "checked";
215                 $out .= ">" . t("Sunday") . "</label> &nbsp; ";
216         }
217         $out .= "</div>";
218
219
220         $out .= "<div class='rec_weekly'>";
221         $out .= "<label class='block'>" . t("Days") . ":</label>";
222         if ($recurrence && $recurrence->byDay) {
223                 $byday = $recurrence->byDay;
224         } else {
225                 $byday = array("MO", "TU", "WE", "TH", "FR", "SA", "SU");
226         }
227         if ($localization->getFirstDayOfWeek() == 0) {
228                 $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='SU' ";
229                 if (in_array("SU", $byday)) $out .= "checked";
230                 $out .= ">" . t("Sunday") . "</label> &nbsp; ";
231         }
232         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='MO' ";
233         if (in_array("MO", $byday)) $out .= "checked";
234         $out .= ">" . t("Monday") . "</label> &nbsp; ";
235         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='TU' ";
236         if (in_array("TU", $byday)) $out .= "checked";
237         $out .= ">" . t("Tuesday") . "</label> &nbsp; ";
238         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='WE' ";
239         if (in_array("WE", $byday)) $out .= "checked";
240         $out .= ">" . t("Wednesday") . "</label> &nbsp; ";
241         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='TH' ";
242         if (in_array("TH", $byday)) $out .= "checked";
243         $out .= ">" . t("Thursday") . "</label> &nbsp; ";
244         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='FR' ";
245         if (in_array("FR", $byday)) $out .= "checked";
246         $out .= ">" . t("Friday") . "</label> &nbsp; ";
247         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='SA' ";
248         if (in_array("SA", $byday)) $out .= "checked";
249         $out .= ">" . t("Saturday") . "</label> &nbsp; ";
250         if ($localization->getFirstDayOfWeek() != 0) {
251                 $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='SU' ";
252                 if (in_array("SU", $byday)) $out .= "checked";
253                 $out .= ">" . t("Sunday") . "</label> &nbsp; ";
254         }
255         $out .= "<br>";
256
257         $out .= "<label class='block'>" . t("First day of week:") . "</label>";
258         if ($recurrence && $recurrence->weekStart != "") $wkst = $recurrence->weekStart;
259         else {
260                 if ($localization->getFirstDayOfWeek() == 0) $wkst = "SU";
261                 else $wkst = "MO";
262         }
263         $out .= "<label class='plain'><input type='radio' name='rec_weekly_wkst' value='SU' ";
264         if ($wkst == "SU") $out .= "checked";
265         $out .= ">" . t("Sunday") . "</label> &nbsp; ";
266         $out .= "<label class='plain'><input type='radio' name='rec_weekly_wkst' value='MO' ";
267         if ($wkst == "MO") $out .= "checked";
268         $out .= ">" . t("Monday") . "</label><br>\n";
269
270         $out .= "</div>";
271
272         $monthly_rule = "bymonthday"; // @TODO
273         $out .= "<div class='rec_monthly'>";
274         $out .= "<label class='block' name='rec_monthly_day'>" . t("Day of month") . ":</label>";
275         $out .= "<select id='rec_monthly_day' name='rec_monthly_day' size='1'>";
276         $out .= "<option value='bymonthday' ";
277         if ($monthly_rule == "bymonthday") $out .= "selected";
278         $out .= ">" . t("#num#th of each month") . "</option>\n";
279         $out .= "<option value='bymonthday_neg' ";
280         if ($monthly_rule == "bymonthday_neg") $out .= "selected";
281         $out .= ">" . t("#num#th-last of each month") . "</option>\n";
282         $out .= "<option value='byday' ";
283         if ($monthly_rule == "byday") $out .= "selected";
284         $out .= ">" . t("#num#th #wkday# of each month") . "</option>\n";
285         $out .= "<option value='byday_neg' ";
286         if ($monthly_rule == "byday_neg") $out .= "selected";
287         $out .= ">" . t("#num#th-last #wkday# of each month") . "</option>\n";
288         $out .= "</select>";
289         $out .= "</div>\n";
290
291
292         $out .= "<div class='rec_yearly'>";
293         $out .= "<label class='block' name='rec_yearly_day'>" . t("Month") . ":</label> <span class='rec_month_name'>#month#</span><br>\n";
294         $out .= "<label class='block' name='rec_yearly_day'>" . t("Day of month") . ":</label>";
295         $out .= "<select id='rec_yearly_day' name='rec_yearly_day' size='1'>";
296         $out .= "<option value='bymonthday' ";
297         if ($monthly_rule == "bymonthday") $out .= "selected";
298         $out .= ">" . t("#num#th of each month") . "</option>\n";
299         $out .= "<option value='bymonthday_neg' ";
300         if ($monthly_rule == "bymonthday_neg") $out .= "selected";
301         $out .= ">" . t("#num#th-last of each month") . "</option>\n";
302         $out .= "<option value='byday' ";
303         if ($monthly_rule == "byday") $out .= "selected";
304         $out .= ">" . t("#num#th #wkday# of each month") . "</option>\n";
305         $out .= "<option value='byday_neg' ";
306         if ($monthly_rule == "byday_neg") $out .= "selected";
307         $out .= ">" . t("#num#th-last #wkday# of each month") . "</option>\n";
308         $out .= "</select>";
309         $out .= "</div>\n";
310
311
312         if ($recurrence) {
313                 $until = $recurrence->until;
314                 $count = $recurrence->count;
315                 if (is_a($until, "DateTime")) {
316                         /** @var DateTime $until */
317                         $rule_type        = "date";
318                         $rule_until_date  = $until->getTimestamp();
319                         $rule_until_count = 1;
320                 } elseif ($count > 0) {
321                         $rule_type        = "count";
322                         $rule_until_date  = time();
323                         $rule_until_count = $count;
324                 } else {
325                         $rule_type        = "infinite";
326                         $rule_until_date  = time();
327                         $rule_until_count = 1;
328                 }
329         } else {
330                 $rule_type        = "infinite";
331                 $rule_until_date  = time();
332                 $rule_until_count = 1;
333         }
334         $out .= "<label class='block' for='rec_until_type'>" . t("Repeat until") . ":</label> ";
335         $out .= "<select name='rec_until_type' id='rec_until_type' size='1'>";
336         $out .= "<option value='infinite' ";
337         if ($rule_type == "infinite") $out .= "selected";
338         $out .= ">" . t("Infinite") . "</option>\n";
339         $out .= "<option value='date' ";
340         if ($rule_type == "date") $out .= "selected";
341         $out .= ">" . t("Until the following date") . ":</option>\n";
342         $out .= "<option value='count' ";
343         if ($rule_type == "count") $out .= "selected";
344         $out .= ">" . t("Number of times") . ":</option>\n";
345         $out .= "</select>";
346
347         $out .= "<input name='rec_until_date' value='" . $localization->dateformat_datepicker_php($rule_until_date) . "' id='rec_until_date'>";
348         $out .= "<input name='rec_until_count' value='$rule_until_count' id='rec_until_count'><br>";
349
350         $out .= "<label class='block'>" . t("Exceptions") . ":</label><div class='rec_exceptions'>";
351         $out .= "<div class='rec_exceptions_none' ";
352         if (count($recurrentce_exdates) > 0) $out .= "style='display: none;'";
353         $out .= ">" . t("none") . "</div>";
354         $out .= "<div class='rec_exceptions_holder' ";
355         if (count($recurrentce_exdates) == 0) $out .= "style='display: none;'";
356         $out .= ">";
357
358         foreach ($recurrentce_exdates as $exdate) {
359                 $out .= "<div data-timestamp='$exdate' class='except'><input type='hidden' class='rec_exception' name='rec_exceptions[]' value='$exdate'>";
360                 $out .= "<a href='#' class='exception_remover'>[remove]</a> ";
361                 $out .= $localization->date_timestamp2localDate($exdate);
362                 $out .= "</div>\n";
363         }
364         $out .= "</div><div><a href='#' class='exception_adder'>[add]</a></div>";
365         $out .= "</div>\n";
366         $out .= "<br>\n";
367
368         $out .= "</div><br>";
369
370         $out .= "<h2>" . t("Notification") . "</h2>";
371
372         /*
373         $out .= '<input type="checkbox" name="notification" id="notification" ';
374         if ($notification) $out .= "checked";
375         $out .= '> ';
376         $out .= '<span id="notification_detail" style="display: none;">
377                         <input name="notification_value" value="' . $notification_value . '" size="3">
378                         <select name="notification_type" size="1">
379                                 <option value="minute" ';
380         if ($notification_type == "minute") $out .= "selected";
381         $out .= '> ' . t('Minutes') . '</option>
382                                 <option value="hour" ';
383         if ($notification_type == "hour") $out .= "selected";
384         $out .= '> ' . t('Hours') . '</option>
385                                 <option value="day" ';
386         if ($notification_type == "day") echo "selected";
387         $out .= '> ' . t('Days') . '</option>
388                         </select> ' . t('before') . '
389                 </span><br><br>';
390         */
391
392         $out .= "<script>\$(function() {
393                 wdcal_edit_init('" . $localization->dateformat_datepicker_js() . "', '${baseurl}/dav/');
394         });</script>";
395
396         $out .= "<input type='submit' name='save' value='Save'></form>";
397
398         return $out;
399 }
400
401
402 /**
403  * @param Sabre_VObject_Component_VEvent $component
404  * @param wdcal_local $localization
405  */
406 function wdcal_set_component_date(&$component, &$localization)
407 {
408         if (isset($_REQUEST["allday"])) {
409                 $ts_start = $localization->date_local2timestamp($_REQUEST["start_date"] . " 00:00");
410                 $ts_end   = $localization->date_local2timestamp($_REQUEST["end_date"] . " 00:00");
411                 $type     = Sabre_VObject_Property_DateTime::DATE;
412         } else {
413                 $ts_start = $localization->date_local2timestamp($_REQUEST["start_date"] . " " . $_REQUEST["start_time"]);
414                 $ts_end   = $localization->date_local2timestamp($_REQUEST["end_date"] . " " . $_REQUEST["end_time"]);
415                 $type     = Sabre_VObject_Property_DateTime::LOCALTZ;
416         }
417         $datetime_start = new Sabre_VObject_Property_DateTime("DTSTART");
418         $datetime_start->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_start)), $type);
419         $datetime_end = new Sabre_VObject_Property_DateTime("DTEND");
420         $datetime_end->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_end)), $type);
421
422         $component->__unset("DTSTART");
423         $component->__unset("DTEND");
424         $component->add($datetime_start);
425         $component->add($datetime_end);
426 }
427
428 /**
429  * @param Sabre_VObject_Component_VEvent $component
430  * @param wdcal_local $localization
431  */
432 function wdcal_set_component_recurrence(&$component, &$localization)
433 {
434         $component->__unset("RRULE");
435         $component->__unset("EXRULE");
436         $component->__unset("EXDATE");
437         $component->__unset("RDATE");
438
439         $part_until = "";
440         switch ($_REQUEST["rec_until_type"]) {
441                 case "date":
442                         $date           = $localization->date_local2timestamp($_REQUEST["rec_until_date"]);
443                         $part_until     = ";UNTIL=" . date("Ymd", $date);
444                         $datetime_until = new Sabre_VObject_Property_DateTime("UNTIL");
445                         $datetime_until->setDateTime(new DateTime(date("Y-m-d H:i:s", $date)), Sabre_VObject_Property_DateTime::DATE);
446                         break;
447                 case "count":
448                         $part_until = ";COUNT=" . IntVal($_REQUEST["rec_until_count"]);
449                         break;
450         }
451
452         switch ($_REQUEST["rec_frequency"]) {
453                 case "daily":
454                         $part_freq = "FREQ=DAILY";
455                         if (isset($_REQUEST["rec_daily_byday"])) {
456                                 $days = array();
457                                 foreach ($_REQUEST["rec_daily_byday"] as $x) if (in_array($x, array("MO", "TU", "WE", "TH", "FR", "SA", "SU"))) $days[] = $x;
458                                 if (count($days) > 0) $part_freq .= ";BYDAY=" . implode(",", $days);
459                         }
460                         break;
461                 case "weekly":
462                         $part_freq = "FREQ=WEEKLY";
463                         if (isset($_REQUEST["rec_weekly_wkst"]) && in_array($_REQUEST["rec_weekly_wkst"], array("MO", "SU"))) $part_freq .= ";WKST=" . $_REQUEST["rec_weekly_wkst"];
464                         if (isset($_REQUEST["rec_weekly_byday"])) {
465                                 $days = array();
466                                 foreach ($_REQUEST["rec_weekly_byday"] as $x) if (in_array($x, array("MO", "TU", "WE", "TH", "FR", "SA", "SU"))) $days[] = $x;
467                                 if (count($days) > 0) $part_freq .= ";BYDAY=" . implode(",", $days);
468                         }
469                         break;
470                 case "monthly":
471                         $part_freq = "FREQ=MONTHLY";
472                         break;
473                 case "FREQ=yearly":
474                         $part_freq = "FREQ=YEARLY";
475                         break;
476                 default:
477                         $part_freq = "";
478         }
479
480         if ($part_freq == "") return;
481
482         if (isset($_REQUEST["rec_interval"])) $part_freq .= ";INTERVAL=" . InTVal($_REQUEST["rec_interval"]);
483
484         if (isset($_REQUEST["rec_exceptions"])) {
485                 $arr = array();
486                 foreach ($_REQUEST["rec_exceptions"] as $except) {
487                         $arr[] = new DateTime(date("Y-m-d H:i:s", $except));
488                 }
489                 /** @var Sabre_VObject_Property_MultiDateTime $prop */
490                 $prop = Sabre_VObject_Property::create("EXDATE");
491                 $prop->setDateTimes($arr);
492                 $component->add($prop);
493         }
494
495         $rrule = $part_freq . $part_until;
496         $component->add(new Sabre_VObject_Property("RRULE", $rrule));
497
498 }
499
500
501 /**
502  * @param string $uri
503  * @param string $recurr_uri
504  * @param int $uid
505  * @param string $timezone
506  * @param string $goaway_url
507  * @return array
508  */
509 function wdcal_postEditPage($uri, $recurr_uri = "", $uid = 0, $timezone = "", $goaway_url = "")
510 {
511         $uid          = IntVal($uid);
512         $localization = wdcal_local::getInstanceByUser($uid);
513
514         $server = dav_create_server(true, true, false);
515
516         if ($uri > 0) {
517                 $calendar = dav_get_current_user_calendar_by_id($server, $_REQUEST["calendar"], DAV_ACL_READ);
518                 $obj_uri  = Sabre_CalDAV_Backend_Common::loadCalendarobjectById($uri);
519                 $obj_uri  = $obj_uri["uri"];
520
521                 $vObject   = dav_get_current_user_calendarobject($server, $calendar, $obj_uri, DAV_ACL_WRITE);
522                 $component = dav_get_eventComponent($vObject);
523
524                 if ($component == null) return array("ok" => false, "msg" => t('Could not open component for editing'));
525         } else {
526                 $calendar  = dav_get_current_user_calendar_by_id($server, $_REQUEST["calendar"], DAV_ACL_WRITE);
527                 $vObject   = dav_create_empty_vevent();
528                 $component = dav_get_eventComponent($vObject);
529                 $obj_uri   = $component->__get("UID");
530         }
531
532         wdcal_set_component_date($component, $localization);
533         wdcal_set_component_recurrence($component, $localization);
534
535         $component->__unset("LOCATION");
536         $component->__unset("SUMMARY");
537         $component->__unset("DESCRIPTION");
538         $component->__unset("X-ANIMEXX-COLOR");
539         $component->add("SUMMARY", icalendar_sanitize_string(dav_compat_parse_text_serverside("summary")));
540         $component->add("LOCATION", icalendar_sanitize_string(dav_compat_parse_text_serverside("location")));
541         $component->add("DESCRIPTION", icalendar_sanitize_string(dav_compat_parse_text_serverside("wdcal_desc")));
542         if (isset($_REQUEST["color_override"])) {
543                 $component->add("X-ANIMEXX-COLOR", $_REQUEST["color"]);
544         }
545
546         $data = $vObject->serialize();
547
548         if ($uri == 0) {
549                 $calendar->createFile($obj_uri . ".ics", $data);
550         } else {
551                 $obj = $calendar->getChild($obj_uri);
552                 $obj->put($data);
553         }
554         return array("ok" => false, "msg" => t("Saved"));
555 }
556
557
558 /**
559  * @return string
560  */
561 function wdcal_getEditPage_exception_selector()
562 {
563         header("Content-type: application/json");
564
565         $a            = get_app();
566         $localization = wdcal_local::getInstanceByUser($a->user["uid"]);
567
568         $vObject = dav_create_empty_vevent();
569
570         foreach ($vObject->getComponents() as $component) {
571                 if ($component->name !== 'VTIMEZONE') break;
572         }
573         /** @var Sabre_VObject_Component_VEvent $component */
574         wdcal_set_component_date($component, $localization);
575         wdcal_set_component_recurrence($component, $localization);
576
577
578         $it         = new Sabre_VObject_RecurrenceIterator($vObject, (string)$component->__get("UID"));
579         $max_ts     = mktime(0, 0, 0, 1, 1, CALDAV_MAX_YEAR + 1);
580         $last_start = 0;
581
582         $o = "<ul>";
583
584         $i = 0;
585         while ($it->valid() && $last_start < $max_ts && $i++ < 1000) {
586                 $last_start = $it->getDtStart()->getTimestamp();
587                 $o .= "<li><a href='#' class='exception_selector_link' data-timestamp='$last_start'>" . $localization->date_timestamp2localDate($last_start) . "</a></li>\n";
588                 $it->next();
589         }
590         $o .= "</ul>\n";
591
592         return $o;
593 }