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