]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/wdcal_edit.inc.php
Notifications by E-Mail 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->i) {
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' class='block'>" . 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='block' for='noti_type_" . $index . "'>" . t("Notify by") . ":</label>";
466                 $out .= "<select name='noti_type[$index]' size='1' id='noti_type_" . $index . "'>";
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><br>";
472
473                 $out .= "<label class='block'>" . t("Time") . ":</label>";
474                 $out .= "<input name='noti_value[$index]' size='5' style='width: 5em;' value='" . $noti["trigger_value"] . "'>";
475
476                 $out .= "<select name='noti_unit[$index]' size='1'>";
477                 $out .= "<option value='H' "; if ($noti["trigger_unit"] == "hour") $out .= "selected"; $out .= ">" . t("Hours") . "</option>\n";
478                 $out .= "<option value='M' "; if ($noti["trigger_unit"] == "minute") $out .= "selected"; $out .= ">" . t("Minutes") . "</option>\n";
479                 $out .= "<option value='S' "; if ($noti["trigger_unit"] == "second") $out .= "selected"; $out .= ">" . t("Seconds") . "</option>\n";
480                 $out .= "<option value='D' "; if ($noti["trigger_unit"] == "day") $out .= "selected"; $out .= ">" . t("Days") . "</option>\n";
481                 $out .= "<option value='W' "; if ($noti["trigger_unit"] == "week") $out .= "selected"; $out .= ">" . t("Weeks") . "</option>\n";
482                 $out .= "</select>";
483
484                 $out .= " <label class='plain'>" . t("before the") . " <select name='noti_ref[$index]' size='1'>";
485                 $out .= "<option value='start' "; if ($noti["rel"] == "start") $out .= "selected"; $out .= ">" . t("start of the event") . "</option>\n";
486                 $out .= "<option value='end' "; if ($noti["rel"] == "end") $out .= "selected"; $out .= ">" . t("end of the event") . "</option>\n";
487                 $out .= "</select></label>\n";
488
489                 $out .= "</div>";
490         }
491         $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>";
492
493         $out .= "<script>\$(function() {
494                 wdcal_edit_init('" . $localization->dateformat_datepicker_js() . "', '${baseurl}/dav/');
495         });</script>";
496
497         $out .= "<br><input type='submit' name='save' value='Save'></form>";
498
499         return $out;
500 }
501
502
503 /**
504  * @param Sabre_VObject_Component_VEvent $component
505  * @param wdcal_local $localization
506  * @return int
507  */
508 function wdcal_set_component_date(&$component, &$localization)
509 {
510         if (isset($_REQUEST["allday"])) {
511                 $ts_start = $localization->date_local2timestamp($_REQUEST["start_date"] . " 00:00");
512                 $ts_end   = $localization->date_local2timestamp($_REQUEST["end_date"] . " 00:00");
513                 $type     = Sabre_VObject_Property_DateTime::DATE;
514         } else {
515                 $ts_start = $localization->date_local2timestamp($_REQUEST["start_date"] . " " . $_REQUEST["start_time"]);
516                 $ts_end   = $localization->date_local2timestamp($_REQUEST["end_date"] . " " . $_REQUEST["end_time"]);
517                 $type     = Sabre_VObject_Property_DateTime::LOCALTZ;
518         }
519         $datetime_start = new Sabre_VObject_Property_DateTime("DTSTART");
520         $datetime_start->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_start)), $type);
521         $datetime_end = new Sabre_VObject_Property_DateTime("DTEND");
522         $datetime_end->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_end)), $type);
523
524         $component->__unset("DTSTART");
525         $component->__unset("DTEND");
526         $component->add($datetime_start);
527         $component->add($datetime_end);
528
529         return $ts_start;
530 }
531
532 /**
533  * @param Sabre_VObject_Component_VEvent $component
534  * @param string $str
535  * @return string
536  */
537
538 function wdcal_set_component_recurrence_special(&$component, $str) {
539         $ret = "";
540
541         /** @var Sabre_VObject_Property_DateTime $start  */
542         $start  = $component->__get("DTSTART");
543         $dayMap = array(
544                 0 => 'SU',
545                 1 => 'MO',
546                 2 => 'TU',
547                 3 => 'WE',
548                 4 => 'TH',
549                 5 => 'FR',
550                 6 => 'SA',
551         );
552
553         switch ($str) {
554                 case "bymonthday":
555                         $day = $start->getDateTime()->format("j");
556                         $ret = ";BYMONTHDAY=" . $day;
557                         break;
558                 case "bymonthday_neg":
559                         $day     = $start->getDateTime()->format("j");
560                         $day_max = $start->getDateTime()->format("t");
561                         $ret = ";BYMONTHDAY=" . (-1 * ($day_max - $day + 1));
562                         break;
563                 case "byday":
564                         $day     = $start->getDateTime()->format("j");
565                         $weekday = $dayMap[$start->getDateTime()->format("w")];
566                         $num     = IntVal(ceil($day / 7));
567                         $ret = ";BYDAY=${num}${weekday}";
568                         break;
569                 case "byday_neg":
570                         $day     = $start->getDateTime()->format("j");
571                         $weekday = $dayMap[$start->getDateTime()->format("w")];
572                         $day_max = $start->getDateTime()->format("t");
573                         $day_last = ($day_max - $day + 1);
574                         $num     = IntVal(ceil($day_last / 7));
575                         $ret = ";BYDAY=-${num}${weekday}";
576                         break;
577         }
578         return $ret;
579 }
580
581 /**
582  * @param Sabre_VObject_Component_VEvent $component
583  * @param wdcal_local $localization
584  */
585 function wdcal_set_component_recurrence(&$component, &$localization)
586 {
587         $component->__unset("RRULE");
588         $component->__unset("EXRULE");
589         $component->__unset("EXDATE");
590         $component->__unset("RDATE");
591
592         $part_until = "";
593         switch ($_REQUEST["rec_until_type"]) {
594                 case "date":
595                         $date           = $localization->date_local2timestamp($_REQUEST["rec_until_date"]);
596                         $part_until     = ";UNTIL=" . date("Ymd", $date);
597                         $datetime_until = new Sabre_VObject_Property_DateTime("UNTIL");
598                         $datetime_until->setDateTime(new DateTime(date("Y-m-d H:i:s", $date)), Sabre_VObject_Property_DateTime::DATE);
599                         break;
600                 case "count":
601                         $part_until = ";COUNT=" . IntVal($_REQUEST["rec_until_count"]);
602                         break;
603         }
604
605         switch ($_REQUEST["rec_frequency"]) {
606                 case "daily":
607                         $part_freq = "FREQ=DAILY";
608                         if (isset($_REQUEST["rec_daily_byday"])) {
609                                 $days = array();
610                                 foreach ($_REQUEST["rec_daily_byday"] as $x) if (in_array($x, array("MO", "TU", "WE", "TH", "FR", "SA", "SU"))) $days[] = $x;
611                                 if (count($days) > 0) $part_freq .= ";BYDAY=" . implode(",", $days);
612                         }
613                         break;
614                 case "weekly":
615                         $part_freq = "FREQ=WEEKLY";
616                         if (isset($_REQUEST["rec_weekly_wkst"]) && in_array($_REQUEST["rec_weekly_wkst"], array("MO", "SU"))) $part_freq .= ";WKST=" . $_REQUEST["rec_weekly_wkst"];
617                         if (isset($_REQUEST["rec_weekly_byday"])) {
618                                 $days = array();
619                                 foreach ($_REQUEST["rec_weekly_byday"] as $x) if (in_array($x, array("MO", "TU", "WE", "TH", "FR", "SA", "SU"))) $days[] = $x;
620                                 if (count($days) > 0) $part_freq .= ";BYDAY=" . implode(",", $days);
621                         }
622                         break;
623                 case "monthly":
624                         $part_freq = "FREQ=MONTHLY";
625                         $part_freq .= wdcal_set_component_recurrence_special($component, $_REQUEST["rec_monthly_day"]);
626                         break;
627                 case "yearly":
628                         /** @var Sabre_VObject_Property_DateTime $start  */
629                         $start  = $component->__get("DTSTART");
630                         $part_freq = "FREQ=YEARLY";
631                         $part_freq .= ";BYMONTH=" . $start->getDateTime()->format("n");
632                         $part_freq .= wdcal_set_component_recurrence_special($component, $_REQUEST["rec_yearly_day"]);
633                         break;
634                 default:
635                         $part_freq = "";
636         }
637
638         if ($part_freq == "") return;
639
640         if (isset($_REQUEST["rec_interval"])) $part_freq .= ";INTERVAL=" . InTVal($_REQUEST["rec_interval"]);
641
642         if (isset($_REQUEST["rec_exceptions"])) {
643                 $arr = array();
644                 foreach ($_REQUEST["rec_exceptions"] as $except) {
645                         $arr[] = new DateTime(date("Y-m-d H:i:s", $except));
646                 }
647                 /** @var Sabre_VObject_Property_MultiDateTime $prop */
648                 $prop = Sabre_VObject_Property::create("EXDATE");
649                 $prop->setDateTimes($arr);
650                 $component->add($prop);
651         }
652
653         $rrule = $part_freq . $part_until;
654         $component->add(new Sabre_VObject_Property("RRULE", $rrule));
655
656 }
657
658
659         /**
660          * @param Sabre_VObject_Component_VEvent $component
661          * @param wdcal_local $localization
662          * @param string $summary
663          * @param int $dtstart
664          */
665 function wdcal_set_component_alerts(&$component, &$localization, $summary, $dtstart)
666 {
667         $a = get_app();
668
669         $prev_alarms = $component->select("VALARM");
670         $component->__unset("VALARM");
671
672         foreach ($prev_alarms as $al) {
673                 /** @var Sabre_VObject_Component_VAlarm $al */
674         }
675
676         foreach (array_keys($_REQUEST["noti_type"]) as $key) if (is_numeric($key) || ($key == "new" && $_REQUEST["new_alarm"] == 1)) {
677                 $alarm = new Sabre_VObject_Component_VAlarm("VALARM");
678
679                 switch ($_REQUEST["noti_type"][$key]) {
680                         case "email":
681                                 $mailtext = str_replace(array(
682                                         "#date#", "#name",
683                                 ), array(
684                                         $localization->date_timestamp2local($dtstart), $summary,
685                                 ), t("The event #name# will start at #date"));
686
687                                 $alarm->add(new Sabre_VObject_Property("ACTION", "EMAIL"));
688                                 $alarm->add(new Sabre_VObject_Property("SUMMARY", $summary));
689                                 $alarm->add(new Sabre_VObject_Property("DESCRIPTION", $mailtext));
690                                 $alarm->add(new Sabre_VObject_Property("ATTENDEE", "MAILTO:" . $a->user["email"]));
691                                 break;
692                         case "display":
693                                 $alarm->add(new Sabre_VObject_Property("ACTION", "DISPLAY"));
694                                 $text = str_replace("#name#", $summary, t("#name# is about to begin."));
695                                 $alarm->add(new Sabre_VObject_Property("DESCRIPTION", $text));
696                                 break;
697                         default:
698                                 continue;
699                 }
700
701                 $trigger_name = "TRIGGER";
702                 $trigger_val = ""; // @TODO Bugfix : und ; sind evtl. vertauscht vgl. http://www.kanzaki.com/docs/ical/trigger.html
703                 if ($_REQUEST["noti_ref"][$key] == "end") $trigger_name .= ";RELATED=END";
704                 $trigger_val .= "-P";
705                 if (in_array($_REQUEST["noti_unit"][$key], array("H", "M", "S"))) $trigger_val .= "T";
706                 $trigger_val .= IntVal($_REQUEST["noti_value"][$key]) . $_REQUEST["noti_unit"][$key];
707                 $alarm->add(new Sabre_VObject_Property($trigger_name, $trigger_val));
708
709                 $component->add($alarm);
710         }
711
712 }
713
714         /**
715  * @param string $uri
716  * @param int $uid
717  * @param string $timezone
718  * @param string $goaway_url
719  * @return array
720  */
721 function wdcal_postEditPage($uri, $uid = 0, $timezone = "", $goaway_url = "")
722 {
723         $uid          = IntVal($uid);
724         $localization = wdcal_local::getInstanceByUser($uid);
725
726         $server = dav_create_server(true, true, false);
727
728         if ($uri > 0) {
729                 $calendar = dav_get_current_user_calendar_by_id($server, $_REQUEST["calendar"], DAV_ACL_READ);
730                 $obj_uri  = Sabre_CalDAV_Backend_Common::loadCalendarobjectById($uri);
731                 $obj_uri  = $obj_uri["uri"];
732
733                 $vObject   = dav_get_current_user_calendarobject($server, $calendar, $obj_uri, DAV_ACL_WRITE);
734                 $component = dav_get_eventComponent($vObject);
735
736                 if ($component == null) return array("ok" => false, "msg" => t('Could not open component for editing'));
737         } else {
738                 $calendar  = dav_get_current_user_calendar_by_id($server, $_REQUEST["calendar"], DAV_ACL_WRITE);
739                 $vObject   = dav_create_empty_vevent();
740                 $component = dav_get_eventComponent($vObject);
741                 $obj_uri   = $component->__get("UID");
742         }
743
744         $ts_start = wdcal_set_component_date($component, $localization);
745         wdcal_set_component_recurrence($component, $localization);
746         wdcal_set_component_alerts($component, $localization, icalendar_sanitize_string(dav_compat_parse_text_serverside("summary")), $ts_start);
747
748         $component->__unset("LOCATION");
749         $component->__unset("SUMMARY");
750         $component->__unset("DESCRIPTION");
751         $component->__unset("X-ANIMEXX-COLOR");
752         $component->add("SUMMARY", icalendar_sanitize_string(dav_compat_parse_text_serverside("summary")));
753         $component->add("LOCATION", icalendar_sanitize_string(dav_compat_parse_text_serverside("location")));
754         $component->add("DESCRIPTION", icalendar_sanitize_string(dav_compat_parse_text_serverside("wdcal_desc")));
755         if (isset($_REQUEST["color_override"])) {
756                 $component->add("X-ANIMEXX-COLOR", $_REQUEST["color"]);
757         }
758
759         $data = $vObject->serialize();
760
761         if ($uri == 0) {
762                 $calendar->createFile($obj_uri . ".ics", $data);
763         } else {
764                 $obj = $calendar->getChild($obj_uri);
765                 $obj->put($data);
766         }
767         return array("ok" => false, "msg" => t("Saved"));
768 }
769
770
771 /**
772  * @return string
773  */
774 function wdcal_getEditPage_exception_selector()
775 {
776         header("Content-type: application/json");
777
778         $a            = get_app();
779         $localization = wdcal_local::getInstanceByUser($a->user["uid"]);
780
781         $vObject = dav_create_empty_vevent();
782
783         foreach ($vObject->getComponents() as $component) {
784                 if ($component->name !== 'VTIMEZONE') break;
785         }
786         /** @var Sabre_VObject_Component_VEvent $component */
787         wdcal_set_component_date($component, $localization);
788         wdcal_set_component_recurrence($component, $localization);
789
790
791         $it         = new Sabre_VObject_RecurrenceIterator($vObject, (string)$component->__get("UID"));
792         $max_ts     = mktime(0, 0, 0, 1, 1, CALDAV_MAX_YEAR + 1);
793         $last_start = 0;
794
795         $o = "<ul>";
796
797         $i = 0;
798         while ($it->valid() && $last_start < $max_ts && $i++ < 1000) {
799                 $last_start = $it->getDtStart()->getTimestamp();
800                 $o .= "<li><a href='#' class='exception_selector_link' data-timestamp='$last_start'>" . $localization->date_timestamp2localDate($last_start) . "</a></li>\n";
801                 $it->next();
802         }
803         $o .= "</ul>\n";
804
805         return $o;
806 }