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