]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/wdcal_edit.inc.php
Set notifications of events; the actual notification routine is not yet implemented
[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 $calendar_id
7  * @param int $uri
8  * @return string
9  */
10 function wdcal_getEditPage_str(&$localization, $baseurl, $calendar_id, $uri)
11 {
12         $server = dav_create_server(true, true, false);
13
14         if ($uri > 0) {
15                 $calendar = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_WRITE);
16                 if (!$calendar) {
17                         $calendar  = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_READ);
18                         $calendars = array();
19                 } else {
20                         $calendars = dav_get_current_user_calendars($server, DAV_ACL_WRITE);
21                 }
22
23                 if ($calendar == null) return "Calendar not found";
24
25                 $obj_uri = Sabre_CalDAV_Backend_Common::loadCalendarobjectById($uri);
26
27                 $vObject   = dav_get_current_user_calendarobject($server, $calendar, $obj_uri["uri"], DAV_ACL_WRITE);
28                 $component = dav_get_eventComponent($vObject);
29
30                 if ($component == null) return t('Could not open component for editing');
31
32                 /** @var Sabre_VObject_Property_DateTime $dtstart  */
33                 $dtstart = $component->__get("DTSTART");
34                 $event   = array(
35                         "id"            => IntVal($uri),
36                         "Summary"       => ($component->__get("SUMMARY") ? $component->__get("SUMMARY")->value : null),
37                         "StartTime"     => $dtstart->getDateTime()->getTimeStamp(),
38                         "EndTime"       => Sabre_CalDAV_Backend_Common::getDtEndTimeStamp($component),
39                         "IsAllDayEvent" => (strlen($dtstart->value) == 8),
40                         "Description"   => ($component->__get("DESCRIPTION") ? $component->__get("DESCRIPTION")->value : null),
41                         "Location"      => ($component->__get("LOCATION") ? $component->__get("LOCATION")->value : null),
42                         "Color"         => ($component->__get("X-ANIMEXX-COLOR") ? $component->__get("X-ANIMEXX-COLOR")->value : null),
43                 );
44
45                 $exdates             = $component->select("EXDATE");
46                 $recurrentce_exdates = array();
47                 /** @var Sabre_VObject_Property_MultiDateTime $x */
48                 foreach ($exdates as $x) {
49                         /** @var DateTime $y */
50                         $z = $x->getDateTimes();
51                         foreach ($z as $y) $recurrentce_exdates[] = $y->getTimeStamp();
52                 }
53
54                 $notifications = array();
55                 $alarms = $component->select("VALARM");
56                 foreach ($alarms as $alarm)  {
57                         /** @var Sabre_VObject_Component_VAlarm $alarm */
58                         $action = $alarm->__get("ACTION")->value;
59                         $trigger = $alarm->__get("TRIGGER");
60
61                         if(isset($trigger['VALUE']) && strtoupper($trigger['VALUE']) !== 'DURATION') {
62                                 notice("The notification of this event cannot be parsed");
63                                 continue;
64                         }
65
66                         /** @var DateInterval $triggerDuration  */
67                         $triggerDuration = Sabre_VObject_DateTimeParser::parseDuration($trigger);
68                         $unit = "hour";
69                         $value = 1;
70                         if ($triggerDuration->s > 0) {
71                                 $unit = "second";
72                                 $value = $triggerDuration->s + $triggerDuration->i * 60 + $triggerDuration->h * 3600 + $triggerDuration->d * 3600 * 24; // @TODO support more than days?
73                         }   elseif ($triggerDuration->m) {
74                                 $unit = "minute";
75                                 $value = $triggerDuration->i + $triggerDuration->h * 60 + $triggerDuration->d * 60 * 24;
76                         } elseif ($triggerDuration->h) {
77                                 $unit = "hour";
78                                 $value = $triggerDuration->h + $triggerDuration->d * 24;
79                         } elseif ($triggerDuration->d > 0) {
80                                 $unit = "day";
81                                 $value = $triggerDuration->d;
82                         }
83
84                         $rel = (isset($trigger['RELATED']) && strtoupper($trigger['RELATED']) == 'END') ? 'end' : 'start';
85
86
87                         $notifications[] = array(
88                                 "action" => strtolower($action),
89                                 "rel" => $rel,
90                                 "trigger_unit" => $unit,
91                                 "trigger_value" => $value,
92                         );
93                 }
94
95                 if ($component->select("RRULE")) $recurrence = new Sabre_VObject_RecurrenceIterator($vObject, (string)$component->__get("UID"));
96                 else $recurrence = null;
97
98         } elseif (isset($_REQUEST["start"]) && $_REQUEST["start"] > 0) {
99                 $calendars = dav_get_current_user_calendars($server, DAV_ACL_WRITE);
100                 //$calendar  = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_WRITE);
101
102                 $event = array(
103                         "id"            => 0,
104                         "Summary"       => $_REQUEST["title"],
105                         "StartTime"     => InTVal($_REQUEST["start"]),
106                         "EndTime"       => IntVal($_REQUEST["end"]),
107                         "IsAllDayEvent" => $_REQUEST["isallday"],
108                         "Description"   => "",
109                         "Location"      => "",
110                         "Color"         => null,
111                 );
112                 if ($_REQUEST["isallday"]) {
113                         $notifications = array();
114                 } else {
115                         $notifications = array(array("action" => "email", "rel" => "start", "trigger_unit" => "hour", "trigger_value" => 1));
116                 }
117                 $recurrence          = null;
118                 $recurrentce_exdates = array();
119         } else {
120                 $calendars = dav_get_current_user_calendars($server, DAV_ACL_WRITE);
121                 //$calendar  = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_WRITE);
122
123                 $event               = array(
124                         "id"            => 0,
125                         "Summary"       => "",
126                         "StartTime"     => time(),
127                         "EndTime"       => time() + 3600,
128                         "IsAllDayEvent" => "0",
129                         "Description"   => "",
130                         "Location"      => "",
131                         "Color"         => null,
132                 );
133                 $notifications = array(array("action" => "email", "rel" => "start", "trigger_unit" => "hour", "trigger_value" => 1));
134                 $recurrence          = null;
135                 $recurrentce_exdates = array();
136         }
137
138         $postto = $baseurl . "/dav/wdcal/" . ($uri == 0 ? "new/" : $calendar_id . "/" . $uri . "/edit/");
139
140         $out = "<a href='" . $baseurl . "/dav/wdcal/'>" . t("Go back to the calendar") . "</a><br><br>";
141         $out .= "<form method='POST' action='$postto'>
142                 <input type='hidden' name='form_security_token' value='" . get_form_security_token('caledit') . "'>\n";
143
144         $out .= "<h2>" . t("Event data") . "</h2>";
145
146         $out .= "<label for='calendar'>" . t("Calendar") . ":</label><select id='calendar' name='calendar' size='1'>";
147         $found   = false;
148         $cal_col = "aaaaaa";
149         foreach ($calendars as $cal) {
150                 $prop = $cal->getProperties(array("id", DAV_DISPLAYNAME, DAV_CALENDARCOLOR));
151                 $out .= "<option value='" . $prop["id"] . "' ";
152                 if ($prop["id"] == $calendar_id) {
153                         $out .= "selected";
154                         $cal_col = $prop[DAV_CALENDARCOLOR];
155                         $found   = true;
156                 } elseif (!$found) $cal_col = $prop[DAV_CALENDARCOLOR];
157                 $out .= ">" . escape_tags($prop[DAV_DISPLAYNAME]) . "</option>\n";
158         }
159
160         $out .= "</select>";
161         $out .= "&nbsp; &nbsp; <label class='plain'><input type='checkbox' name='color_override' id='color_override' ";
162         if (!is_null($event["Color"])) $out .= "checked";
163         $out .= "> " . t("Special color") . ":</label>";
164         $out .= "<span id='cal_color_holder' ";
165         if (is_null($event["Color"])) $out .= "style='display: none;'";
166         $out .= "><input name='color' id='cal_color' value='" . (is_null($event["Color"]) ? "#" . $cal_col : escape_tags($event["Color"])) . "'></span>";
167         $out .= "<br>\n";
168
169         $out .= "<label class='block' for='cal_summary'>" . t("Subject") . ":</label>
170                 <input name='summary' id='cal_summary' value=\"" . escape_tags($event["Summary"]) . "\"><br>\n";
171         $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";
172
173         $out .= "<label class='block' for='cal_start_date'>" . t("Starts") . ":</label>";
174         $out .= "<input name='start_date' value='" . $localization->dateformat_datepicker_php($event["StartTime"]) . "' id='cal_start_date'>";
175         $out .= "<input name='start_time' value='" . date("H:i", $event["StartTime"]) . "' id='cal_start_time'>";
176         $out .= "<br>\n";
177
178         $out .= "<label class='block' for='cal_end_date'>" . t("Ends") . ":</label>";
179         $out .= "<input name='end_date' value='" . $localization->dateformat_datepicker_php($event["EndTime"]) . "' id='cal_end_date'>";
180         $out .= "<input name='end_time' value='" . date("H:i", $event["EndTime"]) . "' id='cal_end_time'>";
181         $out .= "<br>\n";
182
183         $out .= "<label class='block' for='cal_location'>" . t("Location") . ":</label><input name='location' id='cal_location' value=\"" . escape_tags($event["Location"]) . "\"><br>\n";
184
185         $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>";
186         $out .= "<br style='clear: both;'>";
187
188         $out .= "<h2>" . t("Recurrence") . "</h2>";
189
190         $out .= "<label class='block' for='rec_frequency'>" . t("Frequency") . ":</label> <select id='rec_frequency' name='rec_frequency' size='1'>";
191         $out .= "<option value=''>" . t("None") . "</option>\n";
192         $out .= "<option value='daily' ";
193         if ($recurrence && $recurrence->frequency == "daily") $out .= "selected";
194         $out .= ">" . t("Daily") . "</option>\n";
195         $out .= "<option value='weekly' ";
196         if ($recurrence && $recurrence->frequency == "weekly") $out .= "selected";
197         $out .= ">" . t("Weekly") . "</option>\n";
198         $out .= "<option value='monthly' ";
199         if ($recurrence && $recurrence->frequency == "monthly") $out .= "selected";
200         $out .= ">" . t("Monthly") . "</option>\n";
201         $out .= "<option value='yearly' ";
202         if ($recurrence && $recurrence->frequency == "yearly") $out .= "selected";
203         $out .= ">" . t("Yearly") . "</option>\n";
204         $out .= "</select><br>\n";
205         $out .= "<div id='rec_details'>";
206
207         $select = "<select id='rec_interval' name='rec_interval' size='1'>";
208         for ($i = 1; $i < 50; $i++) {
209                 $select .= "<option value='$i' ";
210                 if ($recurrence && $i == $recurrence->interval) $select .= "selected";
211                 $select .= ">$i</option>\n";
212         }
213         $select .= "</select>";
214         $time = "<span class='rec_daily'>" . t("days") . "</span>";
215         $time .= "<span class='rec_weekly'>" . t("weeks") . "</span>";
216         $time .= "<span class='rec_monthly'>" . t("months") . "</span>";
217         $time .= "<span class='rec_yearly'>" . t("years") . "</span>";
218         $out .= "<label class='block'>" . t("Interval") . ":</label> " . str_replace(array("%select%", "%time%"), array($select, $time), t("All %select% %time%")) . "<br>";
219
220
221         $out .= "<div class='rec_daily'>";
222         $out .= "<label class='block'>" . t("Days") . ":</label>";
223         if ($recurrence && $recurrence->byDay) {
224                 $byday = $recurrence->byDay;
225         } else {
226                 $byday = array("MO", "TU", "WE", "TH", "FR", "SA", "SU");
227         }
228         if ($localization->getFirstDayOfWeek() == 0) {
229                 $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='SU' ";
230                 if (in_array("SU", $byday)) $out .= "checked";
231                 $out .= ">" . t("Sunday") . "</label> &nbsp; ";
232         }
233         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='MO' ";
234         if (in_array("MO", $byday)) $out .= "checked";
235         $out .= ">" . t("Monday") . "</label> &nbsp; ";
236         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='TU' ";
237         if (in_array("TU", $byday)) $out .= "checked";
238         $out .= ">" . t("Tuesday") . "</label> &nbsp; ";
239         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='WE' ";
240         if (in_array("WE", $byday)) $out .= "checked";
241         $out .= ">" . t("Wednesday") . "</label> &nbsp; ";
242         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='TH' ";
243         if (in_array("TH", $byday)) $out .= "checked";
244         $out .= ">" . t("Thursday") . "</label> &nbsp; ";
245         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='FR' ";
246         if (in_array("FR", $byday)) $out .= "checked";
247         $out .= ">" . t("Friday") . "</label> &nbsp; ";
248         $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='SA' ";
249         if (in_array("SA", $byday)) $out .= "checked";
250         $out .= ">" . t("Saturday") . "</label> &nbsp; ";
251         if ($localization->getFirstDayOfWeek() != 0) {
252                 $out .= "<label class='plain'><input class='rec_daily_byday' type='checkbox' name='rec_daily_byday[]' value='SU' ";
253                 if (in_array("SU", $byday)) $out .= "checked";
254                 $out .= ">" . t("Sunday") . "</label> &nbsp; ";
255         }
256         $out .= "</div>";
257
258
259         $out .= "<div class='rec_weekly'>";
260         $out .= "<label class='block'>" . t("Days") . ":</label>";
261         if ($recurrence && $recurrence->byDay) {
262                 $byday = $recurrence->byDay;
263         } else {
264                 $byday = array("MO", "TU", "WE", "TH", "FR", "SA", "SU");
265         }
266         if ($localization->getFirstDayOfWeek() == 0) {
267                 $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='SU' ";
268                 if (in_array("SU", $byday)) $out .= "checked";
269                 $out .= ">" . t("Sunday") . "</label> &nbsp; ";
270         }
271         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='MO' ";
272         if (in_array("MO", $byday)) $out .= "checked";
273         $out .= ">" . t("Monday") . "</label> &nbsp; ";
274         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='TU' ";
275         if (in_array("TU", $byday)) $out .= "checked";
276         $out .= ">" . t("Tuesday") . "</label> &nbsp; ";
277         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='WE' ";
278         if (in_array("WE", $byday)) $out .= "checked";
279         $out .= ">" . t("Wednesday") . "</label> &nbsp; ";
280         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='TH' ";
281         if (in_array("TH", $byday)) $out .= "checked";
282         $out .= ">" . t("Thursday") . "</label> &nbsp; ";
283         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='FR' ";
284         if (in_array("FR", $byday)) $out .= "checked";
285         $out .= ">" . t("Friday") . "</label> &nbsp; ";
286         $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='SA' ";
287         if (in_array("SA", $byday)) $out .= "checked";
288         $out .= ">" . t("Saturday") . "</label> &nbsp; ";
289         if ($localization->getFirstDayOfWeek() != 0) {
290                 $out .= "<label class='plain'><input class='rec_weekly_byday' type='checkbox' name='rec_weekly_byday[]' value='SU' ";
291                 if (in_array("SU", $byday)) $out .= "checked";
292                 $out .= ">" . t("Sunday") . "</label> &nbsp; ";
293         }
294         $out .= "<br>";
295
296         $out .= "<label class='block'>" . t("First day of week:") . "</label>";
297         if ($recurrence && $recurrence->weekStart != "") $wkst = $recurrence->weekStart;
298         else {
299                 if ($localization->getFirstDayOfWeek() == 0) $wkst = "SU";
300                 else $wkst = "MO";
301         }
302         $out .= "<label class='plain'><input type='radio' name='rec_weekly_wkst' value='SU' ";
303         if ($wkst == "SU") $out .= "checked";
304         $out .= ">" . t("Sunday") . "</label> &nbsp; ";
305         $out .= "<label class='plain'><input type='radio' name='rec_weekly_wkst' value='MO' ";
306         if ($wkst == "MO") $out .= "checked";
307         $out .= ">" . t("Monday") . "</label><br>\n";
308
309         $out .= "</div>";
310
311         $monthly_rule = "";
312         if ($recurrence && ($recurrence->frequency == "monthly" || $recurrence->frequency == "yearly")) {
313                 if (is_null($recurrence->byDay) && !is_null($recurrence->byMonthDay) && count($recurrence->byMonthDay) == 1) {
314                         $day = date("j", $event["StartTime"]);
315                         if ($recurrence->byMonthDay[0] == $day) $monthly_rule = "bymonthday";
316                         else {
317                                 $lastday = date("t", $event["StartTime"]);
318                                 if ($recurrence->byMonthDay[0] == -1 * ($lastday - $day + 1)) $monthly_rule = "bymonthday_neg";
319                         }
320                 }
321                 if (is_null($recurrence->byMonthDay) && !is_null($recurrence->byDay) && count($recurrence->byDay) == 1) {
322                         $num = IntVal($recurrence->byDay[0]);
323                         /*
324                         $dayMap = array(
325                                 'SU' => 0,
326                                 'MO' => 1,
327                                 'TU' => 2,
328                                 'WE' => 3,
329                                 'TH' => 4,
330                                 'FR' => 5,
331                                 'SA' => 6,
332                         );
333                         if ($num == 0) {
334                                 $num = 1;
335                                 $weekday = $dayMap[$recurrence->byDay[0]];
336                         } else {
337                                 $weekday = $dayMap[substr($recurrence->byDay[0], strlen($num))];
338                         }
339
340                         echo $num . " - " . $weekday;
341                         */
342                         if ($num > 0) $monthly_rule = "byday";
343                         if ($num < 0) $monthly_rule = "byday_neg";
344                 }
345                 if ($monthly_rule == "") notice("The recurrence of this event cannot be parsed");
346         }
347
348         $out .= "<div class='rec_monthly'>";
349         $out .= "<label class='block' for='rec_monthly_day'>" . t("Day of month") . ":</label>";
350         $out .= "<select id='rec_monthly_day' name='rec_monthly_day' size='1'>";
351         $out .= "<option value='bymonthday' ";
352         if ($monthly_rule == "bymonthday") $out .= "selected";
353         $out .= ">" . t("#num#th of each month") . "</option>\n";
354         $out .= "<option value='bymonthday_neg' ";
355         if ($monthly_rule == "bymonthday_neg") $out .= "selected";
356         $out .= ">" . t("#num#th-last of each month") . "</option>\n";
357         $out .= "<option value='byday' ";
358         if ($monthly_rule == "byday") $out .= "selected";
359         $out .= ">" . t("#num#th #wkday# of each month") . "</option>\n";
360         $out .= "<option value='byday_neg' ";
361         if ($monthly_rule == "byday_neg") $out .= "selected";
362         $out .= ">" . t("#num#th-last #wkday# of each month") . "</option>\n";
363         $out .= "</select>";
364         $out .= "</div>\n";
365
366         if ($recurrence && $recurrence->frequency == "yearly") {
367                 if (count($recurrence->byMonth) != 1 || $recurrence->byMonth[0] != date("n", $event["StartTime"])) notice("The recurrence of this event cannot be parsed!");
368         }
369
370         $out .= "<div class='rec_yearly'>";
371         $out .= "<label class='block'>" . t("Month") . ":</label> <span class='rec_month_name'>#month#</span><br>\n";
372         $out .= "<label class='block' for='rec_yearly_day'>" . t("Day of month") . ":</label>";
373         $out .= "<select id='rec_yearly_day' name='rec_yearly_day' size='1'>";
374         $out .= "<option value='bymonthday' ";
375         if ($monthly_rule == "bymonthday") $out .= "selected";
376         $out .= ">" . t("#num#th of the given month") . "</option>\n";
377         $out .= "<option value='bymonthday_neg' ";
378         if ($monthly_rule == "bymonthday_neg") $out .= "selected";
379         $out .= ">" . t("#num#th-last of the given month") . "</option>\n";
380         $out .= "<option value='byday' ";
381         if ($monthly_rule == "byday") $out .= "selected";
382         $out .= ">" . t("#num#th #wkday# of the given month") . "</option>\n";
383         $out .= "<option value='byday_neg' ";
384         if ($monthly_rule == "byday_neg") $out .= "selected";
385         $out .= ">" . t("#num#th-last #wkday# of the given month") . "</option>\n";
386         $out .= "</select>";
387         $out .= "</div>\n";
388
389
390         if ($recurrence) {
391                 $until = $recurrence->until;
392                 $count = $recurrence->count;
393                 if (is_a($until, "DateTime")) {
394                         /** @var DateTime $until */
395                         $rule_type        = "date";
396                         $rule_until_date  = $until->getTimestamp();
397                         $rule_until_count = 1;
398                 } elseif ($count > 0) {
399                         $rule_type        = "count";
400                         $rule_until_date  = time();
401                         $rule_until_count = $count;
402                 } else {
403                         $rule_type        = "infinite";
404                         $rule_until_date  = time();
405                         $rule_until_count = 1;
406                 }
407         } else {
408                 $rule_type        = "infinite";
409                 $rule_until_date  = time();
410                 $rule_until_count = 1;
411         }
412         $out .= "<label class='block' for='rec_until_type'>" . t("Repeat until") . ":</label> ";
413         $out .= "<select name='rec_until_type' id='rec_until_type' size='1'>";
414         $out .= "<option value='infinite' ";
415         if ($rule_type == "infinite") $out .= "selected";
416         $out .= ">" . t("Infinite") . "</option>\n";
417         $out .= "<option value='date' ";
418         if ($rule_type == "date") $out .= "selected";
419         $out .= ">" . t("Until the following date") . ":</option>\n";
420         $out .= "<option value='count' ";
421         if ($rule_type == "count") $out .= "selected";
422         $out .= ">" . t("Number of times") . ":</option>\n";
423         $out .= "</select>";
424
425         $out .= "<input name='rec_until_date' value='" . $localization->dateformat_datepicker_php($rule_until_date) . "' id='rec_until_date'>";
426         $out .= "<input name='rec_until_count' value='$rule_until_count' id='rec_until_count'><br>";
427
428         $out .= "<label class='block'>" . t("Exceptions") . ":</label><div class='rec_exceptions'>";
429         $out .= "<div class='rec_exceptions_none' ";
430         if (count($recurrentce_exdates) > 0) $out .= "style='display: none;'";
431         $out .= ">" . t("none") . "</div>";
432         $out .= "<div class='rec_exceptions_holder' ";
433         if (count($recurrentce_exdates) == 0) $out .= "style='display: none;'";
434         $out .= ">";
435
436         foreach ($recurrentce_exdates as $exdate) {
437                 $out .= "<div data-timestamp='$exdate' class='except'><input type='hidden' class='rec_exception' name='rec_exceptions[]' value='$exdate'>";
438                 $out .= "<a href='#' class='exception_remover'>[remove]</a> ";
439                 $out .= $localization->date_timestamp2localDate($exdate);
440                 $out .= "</div>\n";
441         }
442         $out .= "</div><div><a href='#' class='exception_adder'>[add]</a></div>";
443         $out .= "</div>\n";
444         $out .= "<br>\n";
445
446         $out .= "</div><br>";
447
448         $out .= "<h2>" . t("Notification") . "</h2>";
449
450         if (!$notifications) $notifications = array();
451         $notifications["new"] = array(
452                 "action" => "email",
453                 "trigger_value" => 60,
454                 "trigger_unit" => "minute",
455                 "rel" => "start",
456         );
457
458         foreach ($notifications as $index => $noti) {
459
460                 $unparsable = false;
461                 if (!in_array($noti["action"], array("email", "display"))) $unparsable = true;
462
463                 $out .= "<div class='noti_holder' ";
464                 if (!is_numeric($index) && $index == "new") $out .= "style='display: none;' id='noti_new_row'";
465                 $out .= "><label class='plain'>" . t("Notify by");
466                 $out .= "<select name='noti_type[$index]' size='1'>";
467                 $out .= "<option value=''>- " . t("Remove") . " -</option>\n";
468                 $out .= "<option value='email' "; if (!$unparsable && $noti["action"] == "email") $out .= "selected"; $out .= ">" . t("E-Mail") . "</option>\n";
469                 $out .= "<option value='display' "; if (!$unparsable && $noti["action"] == "display") $out .= "selected"; $out .= ">" . t("On Friendica / Display") . "</option>\n";
470                 //$out .= "<option value='other' "; if ($unparsable) $out .= "selected"; $out .= ">- " . t("other (leave it untouched)") . " -</option>\n"; // @TODO
471                 $out .= "</select></label>";
472
473                 $out .= "<input name='noti_value[$index]' size='5' style='width: 5em;' value='" . $noti["trigger_value"] . "'>";
474
475                 $out .= "<select name='noti_unit[$index]' size='1'>";
476                 $out .= "<option value='H' "; if ($noti["trigger_unit"] == "hour") $out .= "selected"; $out .= ">" . t("Hours") . "</option>\n";
477                 $out .= "<option value='M' "; if ($noti["trigger_unit"] == "minute") $out .= "selected"; $out .= ">" . t("Minutes") . "</option>\n";
478                 $out .= "<option value='S' "; if ($noti["trigger_unit"] == "second") $out .= "selected"; $out .= ">" . t("Seconds") . "</option>\n";
479                 $out .= "<option value='D' "; if ($noti["trigger_unit"] == "day") $out .= "selected"; $out .= ">" . t("Days") . "</option>\n";
480                 $out .= "<option value='W' "; if ($noti["trigger_unit"] == "week") $out .= "selected"; $out .= ">" . t("Weeks") . "</option>\n";
481                 $out .= "</select>";
482
483                 $out .= " <label class='plain'>" . t("before the") . " <select name='noti_ref[$index]' size='1'>";
484                 $out .= "<option value='start' "; if ($noti["rel"] == "start") $out .= "selected"; $out .= ">" . t("start of the event") . "</option>\n";
485                 $out .= "<option value='end' "; if ($noti["rel"] == "end") $out .= "selected"; $out .= ">" . t("end of the event") . "</option>\n";
486                 $out .= "</select></label>\n";
487
488                 $out .= "</div>";
489         }
490         $out .= "<input type='hidden' name='new_alarm' id='new_alarm' value='0'><div id='new_alarm_adder'><a href='#'>" . t("Add a notification") . "</a></div>";
491
492         $out .= "<script>\$(function() {
493                 wdcal_edit_init('" . $localization->dateformat_datepicker_js() . "', '${baseurl}/dav/');
494         });</script>";
495
496         $out .= "<br><input type='submit' name='save' value='Save'></form>";
497
498         return $out;
499 }
500
501
502 /**
503  * @param Sabre_VObject_Component_VEvent $component
504  * @param wdcal_local $localization
505  * @return int
506  */
507 function wdcal_set_component_date(&$component, &$localization)
508 {
509         if (isset($_REQUEST["allday"])) {
510                 $ts_start = $localization->date_local2timestamp($_REQUEST["start_date"] . " 00:00");
511                 $ts_end   = $localization->date_local2timestamp($_REQUEST["end_date"] . " 00:00");
512                 $type     = Sabre_VObject_Property_DateTime::DATE;
513         } else {
514                 $ts_start = $localization->date_local2timestamp($_REQUEST["start_date"] . " " . $_REQUEST["start_time"]);
515                 $ts_end   = $localization->date_local2timestamp($_REQUEST["end_date"] . " " . $_REQUEST["end_time"]);
516                 $type     = Sabre_VObject_Property_DateTime::LOCALTZ;
517         }
518         $datetime_start = new Sabre_VObject_Property_DateTime("DTSTART");
519         $datetime_start->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_start)), $type);
520         $datetime_end = new Sabre_VObject_Property_DateTime("DTEND");
521         $datetime_end->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_end)), $type);
522
523         $component->__unset("DTSTART");
524         $component->__unset("DTEND");
525         $component->add($datetime_start);
526         $component->add($datetime_end);
527
528         return $ts_start;
529 }
530
531 /**
532  * @param Sabre_VObject_Component_VEvent $component
533  * @param string $str
534  * @return string
535  */
536
537 function wdcal_set_component_recurrence_special(&$component, $str) {
538         $ret = "";
539
540         /** @var Sabre_VObject_Property_DateTime $start  */
541         $start  = $component->__get("DTSTART");
542         $dayMap = array(
543                 0 => 'SU',
544                 1 => 'MO',
545                 2 => 'TU',
546                 3 => 'WE',
547                 4 => 'TH',
548                 5 => 'FR',
549                 6 => 'SA',
550         );
551
552         switch ($str) {
553                 case "bymonthday":
554                         $day = $start->getDateTime()->format("j");
555                         $ret = ";BYMONTHDAY=" . $day;
556                         break;
557                 case "bymonthday_neg":
558                         $day     = $start->getDateTime()->format("j");
559                         $day_max = $start->getDateTime()->format("t");
560                         $ret = ";BYMONTHDAY=" . (-1 * ($day_max - $day + 1));
561                         break;
562                 case "byday":
563                         $day     = $start->getDateTime()->format("j");
564                         $weekday = $dayMap[$start->getDateTime()->format("w")];
565                         $num     = IntVal(ceil($day / 7));
566                         $ret = ";BYDAY=${num}${weekday}";
567                         break;
568                 case "byday_neg":
569                         $day     = $start->getDateTime()->format("j");
570                         $weekday = $dayMap[$start->getDateTime()->format("w")];
571                         $day_max = $start->getDateTime()->format("t");
572                         $day_last = ($day_max - $day + 1);
573                         $num     = IntVal(ceil($day_last / 7));
574                         $ret = ";BYDAY=-${num}${weekday}";
575                         break;
576         }
577         return $ret;
578 }
579
580 /**
581  * @param Sabre_VObject_Component_VEvent $component
582  * @param wdcal_local $localization
583  */
584 function wdcal_set_component_recurrence(&$component, &$localization)
585 {
586         $component->__unset("RRULE");
587         $component->__unset("EXRULE");
588         $component->__unset("EXDATE");
589         $component->__unset("RDATE");
590
591         $part_until = "";
592         switch ($_REQUEST["rec_until_type"]) {
593                 case "date":
594                         $date           = $localization->date_local2timestamp($_REQUEST["rec_until_date"]);
595                         $part_until     = ";UNTIL=" . date("Ymd", $date);
596                         $datetime_until = new Sabre_VObject_Property_DateTime("UNTIL");
597                         $datetime_until->setDateTime(new DateTime(date("Y-m-d H:i:s", $date)), Sabre_VObject_Property_DateTime::DATE);
598                         break;
599                 case "count":
600                         $part_until = ";COUNT=" . IntVal($_REQUEST["rec_until_count"]);
601                         break;
602         }
603
604         switch ($_REQUEST["rec_frequency"]) {
605                 case "daily":
606                         $part_freq = "FREQ=DAILY";
607                         if (isset($_REQUEST["rec_daily_byday"])) {
608                                 $days = array();
609                                 foreach ($_REQUEST["rec_daily_byday"] as $x) if (in_array($x, array("MO", "TU", "WE", "TH", "FR", "SA", "SU"))) $days[] = $x;
610                                 if (count($days) > 0) $part_freq .= ";BYDAY=" . implode(",", $days);
611                         }
612                         break;
613                 case "weekly":
614                         $part_freq = "FREQ=WEEKLY";
615                         if (isset($_REQUEST["rec_weekly_wkst"]) && in_array($_REQUEST["rec_weekly_wkst"], array("MO", "SU"))) $part_freq .= ";WKST=" . $_REQUEST["rec_weekly_wkst"];
616                         if (isset($_REQUEST["rec_weekly_byday"])) {
617                                 $days = array();
618                                 foreach ($_REQUEST["rec_weekly_byday"] as $x) if (in_array($x, array("MO", "TU", "WE", "TH", "FR", "SA", "SU"))) $days[] = $x;
619                                 if (count($days) > 0) $part_freq .= ";BYDAY=" . implode(",", $days);
620                         }
621                         break;
622                 case "monthly":
623                         $part_freq = "FREQ=MONTHLY";
624                         $part_freq .= wdcal_set_component_recurrence_special($component, $_REQUEST["rec_monthly_day"]);
625                         break;
626                 case "yearly":
627                         /** @var Sabre_VObject_Property_DateTime $start  */
628                         $start  = $component->__get("DTSTART");
629                         $part_freq = "FREQ=YEARLY";
630                         $part_freq .= ";BYMONTH=" . $start->getDateTime()->format("n");
631                         $part_freq .= wdcal_set_component_recurrence_special($component, $_REQUEST["rec_yearly_day"]);
632                         break;
633                 default:
634                         $part_freq = "";
635         }
636
637         if ($part_freq == "") return;
638
639         if (isset($_REQUEST["rec_interval"])) $part_freq .= ";INTERVAL=" . InTVal($_REQUEST["rec_interval"]);
640
641         if (isset($_REQUEST["rec_exceptions"])) {
642                 $arr = array();
643                 foreach ($_REQUEST["rec_exceptions"] as $except) {
644                         $arr[] = new DateTime(date("Y-m-d H:i:s", $except));
645                 }
646                 /** @var Sabre_VObject_Property_MultiDateTime $prop */
647                 $prop = Sabre_VObject_Property::create("EXDATE");
648                 $prop->setDateTimes($arr);
649                 $component->add($prop);
650         }
651
652         $rrule = $part_freq . $part_until;
653         $component->add(new Sabre_VObject_Property("RRULE", $rrule));
654
655 }
656
657
658         /**
659          * @param Sabre_VObject_Component_VEvent $component
660          * @param wdcal_local $localization
661          * @param string $summary
662          * @param int $dtstart
663          */
664 function wdcal_set_component_alerts(&$component, &$localization, $summary, $dtstart)
665 {
666         $a = get_app();
667
668         $prev_alarms = $component->select("VALARM");
669         $component->__unset("VALARM");
670
671         foreach ($prev_alarms as $al) {
672                 /** @var Sabre_VObject_Component_VAlarm $al */
673         }
674
675         foreach (array_keys($_REQUEST["noti_type"]) as $key) if (is_numeric($key) || ($key == "new" && $_REQUEST["new_alarm"] == 1)) {
676                 $alarm = new Sabre_VObject_Component_VAlarm("VALARM");
677
678                 switch ($_REQUEST["noti_type"][$key]) {
679                         case "email":
680                                 $mailtext = str_replace(array(
681                                         "#date#", "#name",
682                                 ), array(
683                                         $localization->date_timestamp2local($dtstart), $summary,
684                                 ), t("The event #name# will start at #date"));
685
686                                 $alarm->add(new Sabre_VObject_Property("ACTION", "EMAIL"));
687                                 $alarm->add(new Sabre_VObject_Property("SUMMARY", $summary));
688                                 $alarm->add(new Sabre_VObject_Property("DESCRIPTION", $mailtext));
689                                 $alarm->add(new Sabre_VObject_Property("ATTENDEE", "MAILTO:" . $a->user["email"]));
690                                 break;
691                         case "display":
692                                 $alarm->add(new Sabre_VObject_Property("ACTION", "DISPLAY"));
693                                 $text = str_replace("#name#", $summary, t("#name# is about to begin."));
694                                 $alarm->add(new Sabre_VObject_Property("DESCRIPTION", $text));
695                                 break;
696                         default:
697                                 continue;
698                 }
699
700                 $trigger_name = "TRIGGER";
701                 $trigger_val = ""; // @TODO Bugfix : und ; sind evtl. vertauscht vgl. http://www.kanzaki.com/docs/ical/trigger.html
702                 if ($_REQUEST["noti_ref"][$key] == "end") $trigger_name .= ";RELATED=END";
703                 $trigger_val .= "-P";
704                 if (in_array($_REQUEST["noti_unit"][$key], array("H", "M", "S"))) $trigger_val .= "T";
705                 $trigger_val .= IntVal($_REQUEST["noti_value"][$key]) . $_REQUEST["noti_unit"][$key];
706                 $alarm->add(new Sabre_VObject_Property($trigger_name, $trigger_val));
707
708                 $component->add($alarm);
709         }
710
711 }
712
713         /**
714  * @param string $uri
715  * @param int $uid
716  * @param string $timezone
717  * @param string $goaway_url
718  * @return array
719  */
720 function wdcal_postEditPage($uri, $uid = 0, $timezone = "", $goaway_url = "")
721 {
722         $uid          = IntVal($uid);
723         $localization = wdcal_local::getInstanceByUser($uid);
724
725         $server = dav_create_server(true, true, false);
726
727         if ($uri > 0) {
728                 $calendar = dav_get_current_user_calendar_by_id($server, $_REQUEST["calendar"], DAV_ACL_READ);
729                 $obj_uri  = Sabre_CalDAV_Backend_Common::loadCalendarobjectById($uri);
730                 $obj_uri  = $obj_uri["uri"];
731
732                 $vObject   = dav_get_current_user_calendarobject($server, $calendar, $obj_uri, DAV_ACL_WRITE);
733                 $component = dav_get_eventComponent($vObject);
734
735                 if ($component == null) return array("ok" => false, "msg" => t('Could not open component for editing'));
736         } else {
737                 $calendar  = dav_get_current_user_calendar_by_id($server, $_REQUEST["calendar"], DAV_ACL_WRITE);
738                 $vObject   = dav_create_empty_vevent();
739                 $component = dav_get_eventComponent($vObject);
740                 $obj_uri   = $component->__get("UID");
741         }
742
743         $ts_start = wdcal_set_component_date($component, $localization);
744         wdcal_set_component_recurrence($component, $localization);
745         wdcal_set_component_alerts($component, $localization, icalendar_sanitize_string(dav_compat_parse_text_serverside("summary")), $ts_start);
746
747         $component->__unset("LOCATION");
748         $component->__unset("SUMMARY");
749         $component->__unset("DESCRIPTION");
750         $component->__unset("X-ANIMEXX-COLOR");
751         $component->add("SUMMARY", icalendar_sanitize_string(dav_compat_parse_text_serverside("summary")));
752         $component->add("LOCATION", icalendar_sanitize_string(dav_compat_parse_text_serverside("location")));
753         $component->add("DESCRIPTION", icalendar_sanitize_string(dav_compat_parse_text_serverside("wdcal_desc")));
754         if (isset($_REQUEST["color_override"])) {
755                 $component->add("X-ANIMEXX-COLOR", $_REQUEST["color"]);
756         }
757
758         $data = $vObject->serialize();
759
760         if ($uri == 0) {
761                 $calendar->createFile($obj_uri . ".ics", $data);
762         } else {
763                 $obj = $calendar->getChild($obj_uri);
764                 $obj->put($data);
765         }
766         return array("ok" => false, "msg" => t("Saved"));
767 }
768
769
770 /**
771  * @return string
772  */
773 function wdcal_getEditPage_exception_selector()
774 {
775         header("Content-type: application/json");
776
777         $a            = get_app();
778         $localization = wdcal_local::getInstanceByUser($a->user["uid"]);
779
780         $vObject = dav_create_empty_vevent();
781
782         foreach ($vObject->getComponents() as $component) {
783                 if ($component->name !== 'VTIMEZONE') break;
784         }
785         /** @var Sabre_VObject_Component_VEvent $component */
786         wdcal_set_component_date($component, $localization);
787         wdcal_set_component_recurrence($component, $localization);
788
789
790         $it         = new Sabre_VObject_RecurrenceIterator($vObject, (string)$component->__get("UID"));
791         $max_ts     = mktime(0, 0, 0, 1, 1, CALDAV_MAX_YEAR + 1);
792         $last_start = 0;
793
794         $o = "<ul>";
795
796         $i = 0;
797         while ($it->valid() && $last_start < $max_ts && $i++ < 1000) {
798                 $last_start = $it->getDtStart()->getTimestamp();
799                 $o .= "<li><a href='#' class='exception_selector_link' data-timestamp='$last_start'>" . $localization->date_timestamp2localDate($last_start) . "</a></li>\n";
800                 $it->next();
801         }
802         $o .= "</ul>\n";
803
804         return $o;
805 }