]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/wdcal_edit.inc.php
Exporting and Importing ICS-Files
[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 id='calendar' 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_start_date'>" . 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_end_date'>" . 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'>" . 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 = "";
273         if ($recurrence->frequency == "monthly" || $recurrence->frequency == "yearly") {
274                 if (is_null($recurrence->byDay) && !is_null($recurrence->byMonthDay) && count($recurrence->byMonthDay) == 1) {
275                         $day = date("j", $event["StartTime"]);
276                         if ($recurrence->byMonthDay[0] == $day) $monthly_rule = "bymonthday";
277                         else {
278                                 $lastday = date("t", $event["StartTime"]);
279                                 if ($recurrence->byMonthDay[0] == -1 * ($lastday - $day + 1)) $monthly_rule = "bymonthday_neg";
280                         }
281                 }
282                 if (is_null($recurrence->byMonthDay) && !is_null($recurrence->byDay) && count($recurrence->byDay) == 1) {
283                         $num = IntVal($recurrence->byDay[0]);
284                         /*
285                         $dayMap = array(
286                                 'SU' => 0,
287                                 'MO' => 1,
288                                 'TU' => 2,
289                                 'WE' => 3,
290                                 'TH' => 4,
291                                 'FR' => 5,
292                                 'SA' => 6,
293                         );
294                         if ($num == 0) {
295                                 $num = 1;
296                                 $weekday = $dayMap[$recurrence->byDay[0]];
297                         } else {
298                                 $weekday = $dayMap[substr($recurrence->byDay[0], strlen($num))];
299                         }
300
301                         echo $num . " - " . $weekday;
302                         */
303                         if ($num > 0) $monthly_rule = "byday";
304                         if ($num < 0) $monthly_rule = "byday_neg";
305                 }
306                 if ($monthly_rule == "") notice("The recurrence of this event cannot be parsed");
307         }
308
309         $out .= "<div class='rec_monthly'>";
310         $out .= "<label class='block' for='rec_monthly_day'>" . t("Day of month") . ":</label>";
311         $out .= "<select id='rec_monthly_day' name='rec_monthly_day' size='1'>";
312         $out .= "<option value='bymonthday' ";
313         if ($monthly_rule == "bymonthday") $out .= "selected";
314         $out .= ">" . t("#num#th of each month") . "</option>\n";
315         $out .= "<option value='bymonthday_neg' ";
316         if ($monthly_rule == "bymonthday_neg") $out .= "selected";
317         $out .= ">" . t("#num#th-last of each month") . "</option>\n";
318         $out .= "<option value='byday' ";
319         if ($monthly_rule == "byday") $out .= "selected";
320         $out .= ">" . t("#num#th #wkday# of each month") . "</option>\n";
321         $out .= "<option value='byday_neg' ";
322         if ($monthly_rule == "byday_neg") $out .= "selected";
323         $out .= ">" . t("#num#th-last #wkday# of each month") . "</option>\n";
324         $out .= "</select>";
325         $out .= "</div>\n";
326
327         if ($recurrence->frequency == "yearly") {
328                 if (count($recurrence->byMonth) != 1 || $recurrence->byMonth[0] != date("n", $event["StartTime"])) notice("The recurrence of this event cannot be parsed!");
329         }
330
331         $out .= "<div class='rec_yearly'>";
332         $out .= "<label class='block'>" . t("Month") . ":</label> <span class='rec_month_name'>#month#</span><br>\n";
333         $out .= "<label class='block' for='rec_yearly_day'>" . t("Day of month") . ":</label>";
334         $out .= "<select id='rec_yearly_day' name='rec_yearly_day' size='1'>";
335         $out .= "<option value='bymonthday' ";
336         if ($monthly_rule == "bymonthday") $out .= "selected";
337         $out .= ">" . t("#num#th of the given month") . "</option>\n";
338         $out .= "<option value='bymonthday_neg' ";
339         if ($monthly_rule == "bymonthday_neg") $out .= "selected";
340         $out .= ">" . t("#num#th-last of the given month") . "</option>\n";
341         $out .= "<option value='byday' ";
342         if ($monthly_rule == "byday") $out .= "selected";
343         $out .= ">" . t("#num#th #wkday# of the given month") . "</option>\n";
344         $out .= "<option value='byday_neg' ";
345         if ($monthly_rule == "byday_neg") $out .= "selected";
346         $out .= ">" . t("#num#th-last #wkday# of the given month") . "</option>\n";
347         $out .= "</select>";
348         $out .= "</div>\n";
349
350
351         if ($recurrence) {
352                 $until = $recurrence->until;
353                 $count = $recurrence->count;
354                 if (is_a($until, "DateTime")) {
355                         /** @var DateTime $until */
356                         $rule_type        = "date";
357                         $rule_until_date  = $until->getTimestamp();
358                         $rule_until_count = 1;
359                 } elseif ($count > 0) {
360                         $rule_type        = "count";
361                         $rule_until_date  = time();
362                         $rule_until_count = $count;
363                 } else {
364                         $rule_type        = "infinite";
365                         $rule_until_date  = time();
366                         $rule_until_count = 1;
367                 }
368         } else {
369                 $rule_type        = "infinite";
370                 $rule_until_date  = time();
371                 $rule_until_count = 1;
372         }
373         $out .= "<label class='block' for='rec_until_type'>" . t("Repeat until") . ":</label> ";
374         $out .= "<select name='rec_until_type' id='rec_until_type' size='1'>";
375         $out .= "<option value='infinite' ";
376         if ($rule_type == "infinite") $out .= "selected";
377         $out .= ">" . t("Infinite") . "</option>\n";
378         $out .= "<option value='date' ";
379         if ($rule_type == "date") $out .= "selected";
380         $out .= ">" . t("Until the following date") . ":</option>\n";
381         $out .= "<option value='count' ";
382         if ($rule_type == "count") $out .= "selected";
383         $out .= ">" . t("Number of times") . ":</option>\n";
384         $out .= "</select>";
385
386         $out .= "<input name='rec_until_date' value='" . $localization->dateformat_datepicker_php($rule_until_date) . "' id='rec_until_date'>";
387         $out .= "<input name='rec_until_count' value='$rule_until_count' id='rec_until_count'><br>";
388
389         $out .= "<label class='block'>" . t("Exceptions") . ":</label><div class='rec_exceptions'>";
390         $out .= "<div class='rec_exceptions_none' ";
391         if (count($recurrentce_exdates) > 0) $out .= "style='display: none;'";
392         $out .= ">" . t("none") . "</div>";
393         $out .= "<div class='rec_exceptions_holder' ";
394         if (count($recurrentce_exdates) == 0) $out .= "style='display: none;'";
395         $out .= ">";
396
397         foreach ($recurrentce_exdates as $exdate) {
398                 $out .= "<div data-timestamp='$exdate' class='except'><input type='hidden' class='rec_exception' name='rec_exceptions[]' value='$exdate'>";
399                 $out .= "<a href='#' class='exception_remover'>[remove]</a> ";
400                 $out .= $localization->date_timestamp2localDate($exdate);
401                 $out .= "</div>\n";
402         }
403         $out .= "</div><div><a href='#' class='exception_adder'>[add]</a></div>";
404         $out .= "</div>\n";
405         $out .= "<br>\n";
406
407         $out .= "</div><br>";
408
409         $out .= "<h2>" . t("Notification") . "</h2>";
410
411         /*
412         $out .= '<input type="checkbox" name="notification" id="notification" ';
413         if ($notification) $out .= "checked";
414         $out .= '> ';
415         $out .= '<span id="notification_detail" style="display: none;">
416                         <input name="notification_value" value="' . $notification_value . '" size="3">
417                         <select name="notification_type" size="1">
418                                 <option value="minute" ';
419         if ($notification_type == "minute") $out .= "selected";
420         $out .= '> ' . t('Minutes') . '</option>
421                                 <option value="hour" ';
422         if ($notification_type == "hour") $out .= "selected";
423         $out .= '> ' . t('Hours') . '</option>
424                                 <option value="day" ';
425         if ($notification_type == "day") echo "selected";
426         $out .= '> ' . t('Days') . '</option>
427                         </select> ' . t('before') . '
428                 </span><br><br>';
429         */
430
431         $out .= "<script>\$(function() {
432                 wdcal_edit_init('" . $localization->dateformat_datepicker_js() . "', '${baseurl}/dav/');
433         });</script>";
434
435         $out .= "<input type='submit' name='save' value='Save'></form>";
436
437         return $out;
438 }
439
440
441 /**
442  * @param Sabre_VObject_Component_VEvent $component
443  * @param wdcal_local $localization
444  */
445 function wdcal_set_component_date(&$component, &$localization)
446 {
447         if (isset($_REQUEST["allday"])) {
448                 $ts_start = $localization->date_local2timestamp($_REQUEST["start_date"] . " 00:00");
449                 $ts_end   = $localization->date_local2timestamp($_REQUEST["end_date"] . " 00:00");
450                 $type     = Sabre_VObject_Property_DateTime::DATE;
451         } else {
452                 $ts_start = $localization->date_local2timestamp($_REQUEST["start_date"] . " " . $_REQUEST["start_time"]);
453                 $ts_end   = $localization->date_local2timestamp($_REQUEST["end_date"] . " " . $_REQUEST["end_time"]);
454                 $type     = Sabre_VObject_Property_DateTime::LOCALTZ;
455         }
456         $datetime_start = new Sabre_VObject_Property_DateTime("DTSTART");
457         $datetime_start->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_start)), $type);
458         $datetime_end = new Sabre_VObject_Property_DateTime("DTEND");
459         $datetime_end->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_end)), $type);
460
461         $component->__unset("DTSTART");
462         $component->__unset("DTEND");
463         $component->add($datetime_start);
464         $component->add($datetime_end);
465 }
466
467 /**
468  * @param Sabre_VObject_Component_VEvent $component
469  * @param string $str
470  * @return string
471  */
472
473 function wdcal_set_component_recurrence_special(&$component, $str) {
474         $ret = "";
475
476         /** @var Sabre_VObject_Property_DateTime $start  */
477         $start  = $component->__get("DTSTART");
478         $dayMap = array(
479                 0 => 'SU',
480                 1 => 'MO',
481                 2 => 'TU',
482                 3 => 'WE',
483                 4 => 'TH',
484                 5 => 'FR',
485                 6 => 'SA',
486         );
487
488         switch ($str) {
489                 case "bymonthday":
490                         $day = $start->getDateTime()->format("j");
491                         $ret = ";BYMONTHDAY=" . $day;
492                         break;
493                 case "bymonthday_neg":
494                         $day     = $start->getDateTime()->format("j");
495                         $day_max = $start->getDateTime()->format("t");
496                         $ret = ";BYMONTHDAY=" . (-1 * ($day_max - $day + 1));
497                         break;
498                 case "byday":
499                         $day     = $start->getDateTime()->format("j");
500                         $weekday = $dayMap[$start->getDateTime()->format("w")];
501                         $num     = IntVal(ceil($day / 7));
502                         $ret = ";BYDAY=${num}${weekday}";
503                         break;
504                 case "byday_neg":
505                         $day     = $start->getDateTime()->format("j");
506                         $weekday = $dayMap[$start->getDateTime()->format("w")];
507                         $day_max = $start->getDateTime()->format("t");
508                         $day_last = ($day_max - $day + 1);
509                         $num     = IntVal(ceil($day_last / 7));
510                         $ret = ";BYDAY=-${num}${weekday}";
511                         break;
512         }
513         return $ret;
514 }
515
516 /**
517  * @param Sabre_VObject_Component_VEvent $component
518  * @param wdcal_local $localization
519  */
520 function wdcal_set_component_recurrence(&$component, &$localization)
521 {
522         $component->__unset("RRULE");
523         $component->__unset("EXRULE");
524         $component->__unset("EXDATE");
525         $component->__unset("RDATE");
526
527         $part_until = "";
528         switch ($_REQUEST["rec_until_type"]) {
529                 case "date":
530                         $date           = $localization->date_local2timestamp($_REQUEST["rec_until_date"]);
531                         $part_until     = ";UNTIL=" . date("Ymd", $date);
532                         $datetime_until = new Sabre_VObject_Property_DateTime("UNTIL");
533                         $datetime_until->setDateTime(new DateTime(date("Y-m-d H:i:s", $date)), Sabre_VObject_Property_DateTime::DATE);
534                         break;
535                 case "count":
536                         $part_until = ";COUNT=" . IntVal($_REQUEST["rec_until_count"]);
537                         break;
538         }
539
540         switch ($_REQUEST["rec_frequency"]) {
541                 case "daily":
542                         $part_freq = "FREQ=DAILY";
543                         if (isset($_REQUEST["rec_daily_byday"])) {
544                                 $days = array();
545                                 foreach ($_REQUEST["rec_daily_byday"] as $x) if (in_array($x, array("MO", "TU", "WE", "TH", "FR", "SA", "SU"))) $days[] = $x;
546                                 if (count($days) > 0) $part_freq .= ";BYDAY=" . implode(",", $days);
547                         }
548                         break;
549                 case "weekly":
550                         $part_freq = "FREQ=WEEKLY";
551                         if (isset($_REQUEST["rec_weekly_wkst"]) && in_array($_REQUEST["rec_weekly_wkst"], array("MO", "SU"))) $part_freq .= ";WKST=" . $_REQUEST["rec_weekly_wkst"];
552                         if (isset($_REQUEST["rec_weekly_byday"])) {
553                                 $days = array();
554                                 foreach ($_REQUEST["rec_weekly_byday"] as $x) if (in_array($x, array("MO", "TU", "WE", "TH", "FR", "SA", "SU"))) $days[] = $x;
555                                 if (count($days) > 0) $part_freq .= ";BYDAY=" . implode(",", $days);
556                         }
557                         break;
558                 case "monthly":
559                         $part_freq = "FREQ=MONTHLY";
560                         $part_freq .= wdcal_set_component_recurrence_special($component, $_REQUEST["rec_monthly_day"]);
561                         break;
562                 case "yearly":
563                         /** @var Sabre_VObject_Property_DateTime $start  */
564                         $start  = $component->__get("DTSTART");
565                         $part_freq = "FREQ=YEARLY";
566                         $part_freq .= ";BYMONTH=" . $start->getDateTime()->format("n");
567                         $part_freq .= wdcal_set_component_recurrence_special($component, $_REQUEST["rec_yearly_day"]);
568                         break;
569                 default:
570                         $part_freq = "";
571         }
572 /*
573          echo "<pre>!";
574          echo $part_freq . "\n";
575          var_dump($_REQUEST);
576          echo "</pre>";
577          die();
578 */
579
580         if ($part_freq == "") return;
581
582         if (isset($_REQUEST["rec_interval"])) $part_freq .= ";INTERVAL=" . InTVal($_REQUEST["rec_interval"]);
583
584         if (isset($_REQUEST["rec_exceptions"])) {
585                 $arr = array();
586                 foreach ($_REQUEST["rec_exceptions"] as $except) {
587                         $arr[] = new DateTime(date("Y-m-d H:i:s", $except));
588                 }
589                 /** @var Sabre_VObject_Property_MultiDateTime $prop */
590                 $prop = Sabre_VObject_Property::create("EXDATE");
591                 $prop->setDateTimes($arr);
592                 $component->add($prop);
593         }
594
595         $rrule = $part_freq . $part_until;
596         $component->add(new Sabre_VObject_Property("RRULE", $rrule));
597
598 }
599
600
601 /**
602  * @param string $uri
603  * @param string $recurr_uri
604  * @param int $uid
605  * @param string $timezone
606  * @param string $goaway_url
607  * @return array
608  */
609 function wdcal_postEditPage($uri, $recurr_uri = "", $uid = 0, $timezone = "", $goaway_url = "")
610 {
611         $uid          = IntVal($uid);
612         $localization = wdcal_local::getInstanceByUser($uid);
613
614         $server = dav_create_server(true, true, false);
615
616         if ($uri > 0) {
617                 $calendar = dav_get_current_user_calendar_by_id($server, $_REQUEST["calendar"], DAV_ACL_READ);
618                 $obj_uri  = Sabre_CalDAV_Backend_Common::loadCalendarobjectById($uri);
619                 $obj_uri  = $obj_uri["uri"];
620
621                 $vObject   = dav_get_current_user_calendarobject($server, $calendar, $obj_uri, DAV_ACL_WRITE);
622                 $component = dav_get_eventComponent($vObject);
623
624                 if ($component == null) return array("ok" => false, "msg" => t('Could not open component for editing'));
625         } else {
626                 $calendar  = dav_get_current_user_calendar_by_id($server, $_REQUEST["calendar"], DAV_ACL_WRITE);
627                 $vObject   = dav_create_empty_vevent();
628                 $component = dav_get_eventComponent($vObject);
629                 $obj_uri   = $component->__get("UID");
630         }
631
632         wdcal_set_component_date($component, $localization);
633         wdcal_set_component_recurrence($component, $localization);
634
635         $component->__unset("LOCATION");
636         $component->__unset("SUMMARY");
637         $component->__unset("DESCRIPTION");
638         $component->__unset("X-ANIMEXX-COLOR");
639         $component->add("SUMMARY", icalendar_sanitize_string(dav_compat_parse_text_serverside("summary")));
640         $component->add("LOCATION", icalendar_sanitize_string(dav_compat_parse_text_serverside("location")));
641         $component->add("DESCRIPTION", icalendar_sanitize_string(dav_compat_parse_text_serverside("wdcal_desc")));
642         if (isset($_REQUEST["color_override"])) {
643                 $component->add("X-ANIMEXX-COLOR", $_REQUEST["color"]);
644         }
645
646         $data = $vObject->serialize();
647
648         if ($uri == 0) {
649                 $calendar->createFile($obj_uri . ".ics", $data);
650         } else {
651                 $obj = $calendar->getChild($obj_uri);
652                 $obj->put($data);
653         }
654         return array("ok" => false, "msg" => t("Saved"));
655 }
656
657
658 /**
659  * @return string
660  */
661 function wdcal_getEditPage_exception_selector()
662 {
663         header("Content-type: application/json");
664
665         $a            = get_app();
666         $localization = wdcal_local::getInstanceByUser($a->user["uid"]);
667
668         $vObject = dav_create_empty_vevent();
669
670         foreach ($vObject->getComponents() as $component) {
671                 if ($component->name !== 'VTIMEZONE') break;
672         }
673         /** @var Sabre_VObject_Component_VEvent $component */
674         wdcal_set_component_date($component, $localization);
675         wdcal_set_component_recurrence($component, $localization);
676
677
678         $it         = new Sabre_VObject_RecurrenceIterator($vObject, (string)$component->__get("UID"));
679         $max_ts     = mktime(0, 0, 0, 1, 1, CALDAV_MAX_YEAR + 1);
680         $last_start = 0;
681
682         $o = "<ul>";
683
684         $i = 0;
685         while ($it->valid() && $last_start < $max_ts && $i++ < 1000) {
686                 $last_start = $it->getDtStart()->getTimestamp();
687                 $o .= "<li><a href='#' class='exception_selector_link' data-timestamp='$last_start'>" . $localization->date_timestamp2localDate($last_start) . "</a></li>\n";
688                 $it->next();
689         }
690         $o .= "</ul>\n";
691
692         return $o;
693 }