]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/wdcal_edit.inc.php
Heavily refactored, including multiple calendars per user and recurring events. Not...
[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"         => "#5858ff",
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"         => "#5858ff",
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         foreach ($calendars as $cal) {
109                 $prop = $cal->getProperties(array("id", DAV_DISPLAYNAME));
110                 $out .= "<option value='" . $prop["id"] . "' ";
111                 if ($prop["id"] == $calendar_id) $out .= "selected";
112                 $out .= ">" . escape_tags($prop[DAV_DISPLAYNAME]) . "</option>\n";
113         }
114         $out .= "</select><br>\n";
115
116         $out .= "<label class='block' for='cal_summary'>" . t("Subject") . ":</label>
117                 <input name='color' id='cal_color' value='" . (strlen($event["Color"]) != 7 ? "#5858ff" : escape_tags($event["Color"])) . "'>
118                 <input name='summary' id='cal_summary' value='" . escape_tags($event["Summary"]) . "'><br>\n";
119         $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";
120
121         $out .= "<label class='block' for='cal_startdate'>" . t("Starts") . ":</label>";
122         $out .= "<input name='start_date' value='" . $localization->dateformat_datepicker_php($event["StartTime"]) . "' id='cal_start_date'>";
123         $out .= "<input name='start_time' value='" . date("H:i", $event["StartTime"]) . "' id='cal_start_time'>";
124         $out .= "<br>\n";
125
126         $out .= "<label class='block' for='cal_enddate'>" . t("Ends") . ":</label>";
127         $out .= "<input name='end_date' value='" . $localization->dateformat_datepicker_php($event["EndTime"]) . "' id='cal_end_date'>";
128         $out .= "<input name='end_time' value='" . date("H:i", $event["EndTime"]) . "' id='cal_end_time'>";
129         $out .= "<br>\n";
130
131         $out .= "<label class='block' for='cal_location'>" . t("Location") . ":</label><input name='location' id='cal_location' value='" . escape_tags($event["Location"]) . "'><br>\n";
132
133         $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>";
134         $out .= "<br style='clear: both;'>";
135
136         $out .= "<h2>" .t("Recurrence") . "</h2>";
137
138         $out .= "<label class='block' for='rec_frequency'>" . t("Frequency") . ":</label> <select id='rec_frequency' name='rec_frequency' size='1'>";
139         $out .= "<option value=''>" . t("None") . "</option>\n";
140         $out .= "<option value='daily' "; if ($recurrence && $recurrence->frequency == "daily") $out .= "selected"; $out .= ">" . t("Daily") . "</option>\n";
141         $out .= "<option value='weekly' "; if ($recurrence && $recurrence->frequency == "weekly") $out .= "selected"; $out .= ">" . t("Weekly") . "</option>\n";
142         $out .= "<option value='monthly' "; if ($recurrence && $recurrence->frequency == "monthly") $out .= "selected"; $out .= ">" . t("Monthly") . "</option>\n";
143         $out .= "<option value='yearly' "; if ($recurrence && $recurrence->frequency == "yearly") $out .= "selected"; $out .= ">" . t("Yearly") . "</option>\n";
144         $out .="</select><br>\n";
145         $out .= "<div id='rec_details'>";
146
147         $select = "<select id='rec_interval' name='rec_interval' size='1'>";
148         for ($i = 1; $i < 50; $i++) {
149                 $select .= "<option value='$i' ";
150                 if ($recurrence && $i == $recurrence->interval) $select .= "selected";
151                 $select .= ">$i</option>\n";
152         }
153         $select .= "</select>";
154         $time  = "<span class='rec_daily'>" . t("days") . "</span>";
155         $time .= "<span class='rec_weekly'>" . t("weeks") . "</span>";
156         $time .= "<span class='rec_monthly'>" . t("months") . "</span>";
157         $time .= "<span class='rec_yearly'>" . t("years") . "</span>";
158         $out .= "<label class='block' for='rev_interval'>" . t("Interval") . ":</label> " . str_replace(array("%select%", "%time%"), array($select, $time), t("All %select% %time%")) . "<br>";
159
160
161         $out .= "<div class='rec_daily'>";
162         $out .= "<label class='block'>" . t("Days") . ":</label>";
163         if ($recurrence && $recurrence->byDay) {
164                 $byday = $recurrence->byDay;
165         } else {
166                 $byday = array("MO", "TU", "WE", "TH", "FR", "SA", "SU");
167         }
168         if ($localization->getFirstDayOfWeek() == 0) {
169                 $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='SU' "; if (in_array("SU", $byday)) $out .= "checked"; $out .= ">" . t("Sunday") . "</label> &nbsp; ";
170         }
171         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='MO' "; if (in_array("MO", $byday)) $out .= "checked"; $out .= ">" . t("Monday") . "</label> &nbsp; ";
172         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='TU' "; if (in_array("TU", $byday)) $out .= "checked"; $out .= ">" . t("Tuesday") . "</label> &nbsp; ";
173         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='WE' "; if (in_array("WE", $byday)) $out .= "checked"; $out .= ">" . t("Wednesday") . "</label> &nbsp; ";
174         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='TH' "; if (in_array("TH", $byday)) $out .= "checked"; $out .= ">" . t("Thursday") . "</label> &nbsp; ";
175         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='FR' "; if (in_array("FR", $byday)) $out .= "checked"; $out .= ">" . t("Friday") . "</label> &nbsp; ";
176         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='SA' "; if (in_array("SA", $byday)) $out .= "checked"; $out .= ">" . t("Saturday") . "</label> &nbsp; ";
177         if ($localization->getFirstDayOfWeek() != 0) {
178                 $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='SU' "; if (in_array("SU", $byday)) $out .= "checked"; $out .= ">" . t("Sunday") . "</label> &nbsp; ";
179         }
180         $out .= "</div>";
181
182
183         $out .= "<div class='rec_weekly'>";
184         $out .= "<label class='block'>" . t("Days") . ":</label>";
185         if ($recurrence && $recurrence->byDay) {
186                 $byday = $recurrence->byDay;
187         } else {
188                 $byday = array("MO", "TU", "WE", "TH", "FR", "SA", "SU");
189         }
190         if ($localization->getFirstDayOfWeek() == 0) {
191                 $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='SU' "; if (in_array("SU", $byday)) $out .= "checked"; $out .= ">" . t("Sunday") . "</label> &nbsp; ";
192         }
193         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='MO' "; if (in_array("MO", $byday)) $out .= "checked"; $out .= ">" . t("Monday") . "</label> &nbsp; ";
194         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='TU' "; if (in_array("TU", $byday)) $out .= "checked"; $out .= ">" . t("Tuesday") . "</label> &nbsp; ";
195         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='WE' "; if (in_array("WE", $byday)) $out .= "checked"; $out .= ">" . t("Wednesday") . "</label> &nbsp; ";
196         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='TH' "; if (in_array("TH", $byday)) $out .= "checked"; $out .= ">" . t("Thursday") . "</label> &nbsp; ";
197         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='FR' "; if (in_array("FR", $byday)) $out .= "checked"; $out .= ">" . t("Friday") . "</label> &nbsp; ";
198         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='SA' "; if (in_array("SA", $byday)) $out .= "checked"; $out .= ">" . t("Saturday") . "</label> &nbsp; ";
199         if ($localization->getFirstDayOfWeek() != 0) {
200                 $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='SU' "; if (in_array("SU", $byday)) $out .= "checked"; $out .= ">" . t("Sunday") . "</label> &nbsp; ";
201         }
202         $out .= "<br>";
203
204         $out .= "<label class='block'>" . t("First day of week:") . "</label>";
205         if ($recurrence && $recurrence->weekStart != "") $wkst = $recurrence->weekStart;
206         else {
207                 if ($localization->getFirstDayOfWeek() == 0) $wkst = "SU";
208                 else $wkst = "MO";
209         }
210         $out .= "<label class='plain'><input type='radio' name='rec_weekly_wkst' value='SU' "; if ($wkst == "SU") $out .= "checked"; $out .= ">" . t("Sunday") . "</label> &nbsp; ";
211         $out .= "<label class='plain'><input type='radio' name='rec_weekly_wkst' value='MO' "; if ($wkst == "MO") $out .= "checked"; $out .= ">" . t("Monday") . "</label><br>\n";
212
213         $out .= "</div>";
214
215         $monthly_rule = "bymonthday"; // @TODO
216         $out .= "<div class='rec_monthly'>";
217         $out .= "<label class='block' name='rec_monthly_day'>" . t("Day of month") . ":</label>";
218         $out .= "<select id='rec_monthly_day' name='rec_monthly_day' size='1'>";
219         $out .= "<option value='bymonthday' "; if ($monthly_rule == "bymonthday") $out .= "selected"; $out .= ">" . t("#num#th of each month") . "</option>\n";
220         $out .= "<option value='bymonthday_neg' "; if ($monthly_rule == "bymonthday_neg") $out .= "selected"; $out .= ">" . t("#num#th-last of each month") . "</option>\n";
221         $out .= "<option value='byday' "; if ($monthly_rule == "byday") $out .= "selected"; $out .= ">" . t("#num#th #wkday# of each month") . "</option>\n";
222         $out .= "<option value='byday_neg' "; if ($monthly_rule == "byday_neg") $out .= "selected"; $out .= ">" . t("#num#th-last #wkday# of each month") . "</option>\n";
223         $out .= "</select>";
224         $out .= "</div>\n";
225
226
227         $out .= "<div class='rec_yearly'>";
228         $out .= "<label class='block' name='rec_yearly_day'>" . t("Month") . ":</label> <span class='rec_month_name'>#month#</span><br>\n";
229         $out .= "<label class='block' name='rec_yearly_day'>" . t("Day of month") . ":</label>";
230         $out .= "<select id='rec_yearly_day' name='rec_yearly_day' size='1'>";
231         $out .= "<option value='bymonthday' "; if ($monthly_rule == "bymonthday") $out .= "selected"; $out .= ">" . t("#num#th of each month") . "</option>\n";
232         $out .= "<option value='bymonthday_neg' "; if ($monthly_rule == "bymonthday_neg") $out .= "selected"; $out .= ">" . t("#num#th-last of each month") . "</option>\n";
233         $out .= "<option value='byday' "; if ($monthly_rule == "byday") $out .= "selected"; $out .= ">" . t("#num#th #wkday# of each month") . "</option>\n";
234         $out .= "<option value='byday_neg' "; if ($monthly_rule == "byday_neg") $out .= "selected"; $out .= ">" . t("#num#th-last #wkday# of each month") . "</option>\n";
235         $out .= "</select>";
236         $out .= "</div>\n";
237
238
239         if ($recurrence) {
240                 $until = $recurrence->until;
241                 $count = $recurrence->count;
242                 if (is_a($until, "DateTime")) {
243                         /** @var DateTime $until */
244                         $rule_type = "date";
245                         $rule_until_date = $until->getTimestamp();
246                         $rule_until_count = 1;
247                 } elseif ($count > 0) {
248                         $rule_type = "count";
249                         $rule_until_date = time();
250                         $rule_until_count = $count;
251                 } else {
252                         $rule_type = "infinite";
253                         $rule_until_date = time();
254                         $rule_until_count = 1;
255                 }
256         } else {
257                 $rule_type = "infinite";
258                 $rule_until_date = time();
259                 $rule_until_count = 1;
260         }
261         $out .= "<label class='block' for='rec_until_type'>" . t("Repeat until") . ":</label> ";
262         $out .= "<select name='rec_until_type' id='rec_until_type' size='1'>";
263         $out .= "<option value='infinite' "; if ($rule_type == "infinite") $out .= "selected"; $out .= ">" . t("Infinite") . "</option>\n";
264         $out .= "<option value='date' "; if ($rule_type == "date") $out .= "selected"; $out .= ">" . t("Until the following date") . ":</option>\n";
265         $out .= "<option value='count' "; if ($rule_type == "count") $out .= "selected"; $out .= ">" . t("Number of times") . ":</option>\n";
266         $out .= "</select>";
267
268         $out .= "<input name='rec_until_date' value='" . $localization->dateformat_datepicker_php($rule_until_date) . "' id='rec_until_date'>";
269         $out .= "<input name='rec_until_count' value='$rule_until_count' id='rec_until_count'><br>";
270
271         $out .= "<label class='block'>" . t("Exceptions") . ":</label><div class='rec_exceptions'>";
272         $out .= "<div class='rec_exceptions_none' ";
273         if (count($recurrentce_exdates) > 0) $out .= "style='display: none;'";
274         $out .= ">" . t("none") . "</div>";
275         $out .= "<div class='rec_exceptions_holder' ";
276         if (count($recurrentce_exdates) == 0) $out .= "style='display: none;'";
277         $out .= ">";
278
279         foreach ($recurrentce_exdates as $exdate) {
280                 $out .= "<div data-timestamp='$exdate' class='except'><input type='hidden' class='rec_exception' name='rec_exceptions[]' value='$exdate'>";
281                 $out .= "<a href='#' class='exception_remover'>[remove]</a> ";
282                 $out .= $localization->date_timestamp2localDate($exdate);
283                 $out .= "</div>\n";
284         }
285         $out .= "</div><div><a href='#' class='exception_adder'>[add]</a></div>";
286         $out .= "</div>\n";
287         $out .= "<br>\n";
288
289         $out .= "</div><br>";
290
291         $out .= "<h2>" .t("Notification") . "</h2>";
292
293         /*
294         $out .= '<input type="checkbox" name="notification" id="notification" ';
295         if ($notification) $out .= "checked";
296         $out .= '> ';
297         $out .= '<span id="notification_detail" style="display: none;">
298                         <input name="notification_value" value="' . $notification_value . '" size="3">
299                         <select name="notification_type" size="1">
300                                 <option value="minute" ';
301         if ($notification_type == "minute") $out .= "selected";
302         $out .= '> ' . t('Minutes') . '</option>
303                                 <option value="hour" ';
304         if ($notification_type == "hour") $out .= "selected";
305         $out .= '> ' . t('Hours') . '</option>
306                                 <option value="day" ';
307         if ($notification_type == "day") echo "selected";
308         $out .= '> ' . t('Days') . '</option>
309                         </select> ' . t('before') . '
310                 </span><br><br>';
311         */
312
313         $out .= "<script>\$(function() {
314                 wdcal_edit_init('" . $localization->dateformat_datepicker_js() . "', '${baseurl}/dav/');
315         });</script>";
316
317         $out .= "<input type='submit' name='save' value='Save'></form>";
318
319         return $out;
320 }
321
322
323 /**
324  * @param Sabre_VObject_Component_VEvent $component
325  * @param wdcal_local $localization
326  */
327 function wdcal_set_component_date(&$component, &$localization) {
328         if (isset($_REQUEST["allday"])) {
329                 $ts_start = $localization->date_local2timestamp($_REQUEST["start_date"] . " 00:00");
330                 $ts_end = $localization->date_local2timestamp($_REQUEST["end_date"] . " 00:00");
331                 $type = Sabre_VObject_Property_DateTime::DATE;
332         } else {
333                 $ts_start = $localization->date_local2timestamp($_REQUEST["start_date"] . " " . $_REQUEST["start_time"]);
334                 $ts_end = $localization->date_local2timestamp($_REQUEST["end_date"] . " " . $_REQUEST["end_time"]);
335                 $type = Sabre_VObject_Property_DateTime::LOCALTZ;
336         }
337         $datetime_start = new Sabre_VObject_Property_DateTime("DTSTART");
338         $datetime_start->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_start)), $type);
339         $datetime_end = new Sabre_VObject_Property_DateTime("DTEND");
340         $datetime_end->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_end)), $type);
341
342         $component->__unset("DTSTART");
343         $component->__unset("DTEND");
344         $component->add($datetime_start);
345         $component->add($datetime_end);
346 }
347
348         /**
349          * @param Sabre_VObject_Component_VEvent $component
350          * @param wdcal_local $localization
351          */
352 function wdcal_set_component_recurrence(&$component, &$localization) {
353         $component->__unset("RRULE");
354         $component->__unset("EXRULE");
355         $component->__unset("EXDATE");
356         $component->__unset("RDATE");
357
358         $part_until = "";
359         switch ($_REQUEST["rec_until_type"]) {
360                 case "date":
361                         $date = $localization->date_local2timestamp($_REQUEST["rec_until_date"]);
362                         $part_until = ";UNTIL=" . date("Ymd", $date);
363                         $datetime_until = new Sabre_VObject_Property_DateTime("UNTIL");
364                         $datetime_until->setDateTime(new DateTime(date("Y-m-d H:i:s", $date)), Sabre_VObject_Property_DateTime::DATE);
365                         break;
366                 case "count":
367                         $part_until = ";COUNT=" . IntVal($_REQUEST["rec_until_count"]);
368                         break;
369         }
370
371         switch ($_REQUEST["rec_frequency"]) {
372                 case "daily":
373                         $part_freq = "FREQ=DAILY";
374                         if (isset($_REQUEST["rec_daily_byday"])) {
375                                 $days = array();
376                                 foreach ($_REQUEST["rec_daily_byday"] as $x) if (in_array($x, array("MO", "TU", "WE", "TH", "FR", "SA", "SU"))) $days[] = $x;
377                                 if (count($days) > 0) $part_freq .= ";BYDAY=" . implode(",", $days);
378                         }
379                         break;
380                 case "weekly":
381                         $part_freq = "FREQ=WEEKLY";
382                         if (isset($_REQUEST["rec_weekly_wkst"]) && in_array($_REQUEST["rec_weekly_wkst"], array("MO", "SU"))) $part_freq .= ";WKST=" . $_REQUEST["rec_weekly_wkst"];
383                         if (isset($_REQUEST["rec_weekly_byday"])) {
384                                 $days = array();
385                                 foreach ($_REQUEST["rec_weekly_byday"] as $x) if (in_array($x, array("MO", "TU", "WE", "TH", "FR", "SA", "SU"))) $days[] = $x;
386                                 if (count($days) > 0) $part_freq .= ";BYDAY=" . implode(",", $days);
387                         }
388                         break;
389                 case "monthly":
390                         $part_freq = "FREQ=MONTHLY";
391                         break;
392                 case "FREQ=yearly":
393                         $part_freq = "FREQ=YEARLY";
394                         break;
395                 default:
396                         $part_freq = "";
397         }
398
399         if ($part_freq == "") return;
400
401         if (isset($_REQUEST["rec_interval"])) $part_freq .= ";INTERVAL=" . InTVal($_REQUEST["rec_interval"]);
402
403         if (isset($_REQUEST["rec_exceptions"])) {
404                 $arr = array();
405                 foreach ($_REQUEST["rec_exceptions"] as $except)  {
406                         $arr[] = new DateTime(date("Y-m-d H:i:s", $except));
407                 }
408                 /** @var Sabre_VObject_Property_MultiDateTime $prop */
409                 $prop = Sabre_VObject_Property::create("EXDATE");
410                 $prop->setDateTimes($arr);
411                 $component->add($prop);
412         }
413
414         $rrule = $part_freq . $part_until;
415         $component->add(new Sabre_VObject_Property("RRULE", $rrule));
416
417 }
418
419
420
421 /**
422  * @param string $uri
423  * @param string $recurr_uri
424  * @param int $uid
425  * @param string $timezone
426  * @param string $goaway_url
427  * @return array
428  */
429 function wdcal_postEditPage($uri, $recurr_uri = "", $uid = 0, $timezone = "", $goaway_url = "")
430 {
431         $uid = IntVal($uid);
432         $localization = wdcal_local::getInstanceByUser($uid);
433
434         $server = dav_create_server(true, true, false);
435
436         if ($uri > 0) {
437                 $calendar = dav_get_current_user_calendar_by_id($server, $_REQUEST["calendar"], DAV_ACL_READ);
438                 $obj_uri = Sabre_CalDAV_Backend_Common::loadCalendarobjectById($uri);
439                 $obj_uri = $obj_uri["uri"];
440
441                 $vObject = dav_get_current_user_calendarobject($server, $calendar, $obj_uri, DAV_ACL_WRITE);
442                 $component = dav_get_eventComponent($vObject);
443
444                 if ($component == null) return array("ok" => false, "msg" => t('Could not open component for editing'));
445         } else {
446                 $calendar = dav_get_current_user_calendar_by_id($server, $_REQUEST["calendar"], DAV_ACL_WRITE);
447                 $vObject = dav_create_empty_vevent();
448                 $component = dav_get_eventComponent($vObject);
449                 $obj_uri = $component->__get("UID");
450         }
451
452         wdcal_set_component_date($component, $localization);
453         wdcal_set_component_recurrence($component, $localization);
454
455         $component->__unset("LOCATION");
456         $component->__unset("SUMMARY");
457         $component->__unset("DESCRIPTION");
458         $component->__unset("X-ANIMEXXCOLOR");
459         $component->add("SUMMARY", icalendar_sanitize_string(dav_compat_parse_text_serverside("summary")));
460         $component->add("LOCATION", icalendar_sanitize_string(dav_compat_parse_text_serverside("location")));
461         $component->add("DESCRIPTION", icalendar_sanitize_string(dav_compat_parse_text_serverside("wdcal_desc")));
462         $component->add("X-ANIMEXX-COLOR", $_REQUEST["color"]);
463
464         $data = $vObject->serialize();
465
466         if ($uri == 0) {
467                 $calendar->createFile($obj_uri . ".ics", $data);
468         } else {
469                 $obj = $calendar->getChild($obj_uri);
470                 $obj->put($data);
471         }
472         return array("ok" => false, "msg" => t("Saved"));
473 }
474
475
476 /**
477  * @return string
478  */
479 function wdcal_getEditPage_exception_selector() {
480         header("Content-type: application/json");
481
482         $a            = get_app();
483         $localization = wdcal_local::getInstanceByUser($a->user["uid"]);
484
485         $vObject = wdcal_create_empty_vevent();
486
487         foreach($vObject->getComponents() as $component) {
488                 if ($component->name!=='VTIMEZONE') break;
489         }
490         /** @var Sabre_VObject_Component_VEvent $component */
491         wdcal_set_component_date($component, $localization);
492         wdcal_set_component_recurrence($component, $localization);
493
494
495         $it = new Sabre_VObject_RecurrenceIterator($vObject, (string)$component->__get("UID"));
496         $max_ts = mktime(0, 0, 0, 1, 1, CALDAV_MAX_YEAR + 1);
497         $last_start = 0;
498
499         $o = "<ul>";
500
501         $i = 0;
502         while($it->valid() && $last_start < $max_ts && $i++ < 1000) {
503                 $last_start = $it->getDtStart()->getTimestamp();
504                 $o .= "<li><a href='#' class='exception_selector_link' data-timestamp='$last_start'>" . $localization->date_timestamp2localDate($last_start) . "</a></li>\n";
505                 $it->next();
506         }
507         $o .= "</ul>\n";
508
509         return $o;
510 }