]> git.mxchange.org Git - friendica.git/blob - mod/events.php
Merge remote-tracking branch 'origin/Nav-#3878' into Nav-#3878
[friendica.git] / mod / events.php
1 <?php
2 /**
3  * @file mod/events.php
4  * @brief The events module
5  */
6 use Friendica\App;
7 use Friendica\Content\Nav;
8 use Friendica\Core\Config;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBM;
12 use Friendica\Model\Profile;
13
14 require_once 'include/bbcode.php';
15 require_once 'include/datetime.php';
16 require_once 'include/event.php';
17 require_once 'include/items.php';
18
19 function events_init(App $a) {
20         if (! local_user()) {
21                 return;
22         }
23
24         if ($a->argc > 1) {
25                 // If it's a json request abort here because we don't
26                 // need the widget data
27                 if ($a->argv[1] === 'json') {
28                         return;
29                 }
30
31                 $cal_widget = widget_events();
32
33                 if (! x($a->page,'aside')) {
34                         $a->page['aside'] = '';
35                 }
36
37                 $a->page['aside'] .= $cal_widget;
38         }
39
40         return;
41 }
42
43 function events_post(App $a) {
44
45         logger('post: ' . print_r($_REQUEST, true), LOGGER_DATA);
46
47         if (! local_user()) {
48                 return;
49         }
50
51         $event_id = ((x($_POST, 'event_id')) ? intval($_POST['event_id']) : 0);
52         $cid = ((x($_POST, 'cid')) ? intval($_POST['cid']) : 0);
53         $uid = local_user();
54
55         $start_text  = escape_tags($_REQUEST['start_text']);
56         $finish_text = escape_tags($_REQUEST['finish_text']);
57
58         $adjust   = intval($_POST['adjust']);
59         $nofinish = intval($_POST['nofinish']);
60
61         // The default setting for the `private` field in event_store() is false, so mirror that
62         $private_event = false;
63
64         $start  = NULL_DATE;
65         $finish = NULL_DATE;
66
67         if ($start_text) {
68                 $start = $start_text;
69         }
70
71         if ($finish_text) {
72                 $finish = $finish_text;
73         }
74
75         if ($adjust) {
76                 $start = datetime_convert(date_default_timezone_get(), 'UTC', $start);
77                 if (! $nofinish) {
78                         $finish = datetime_convert(date_default_timezone_get(), 'UTC', $finish);
79                 }
80         } else {
81                 $start = datetime_convert('UTC', 'UTC', $start);
82                 if (! $nofinish) {
83                         $finish = datetime_convert('UTC', 'UTC', $finish);
84                 }
85         }
86
87         // Don't allow the event to finish before it begins.
88         // It won't hurt anything, but somebody will file a bug report
89         // and we'll waste a bunch of time responding to it. Time that
90         // could've been spent doing something else.
91
92         $summary  = escape_tags(trim($_POST['summary']));
93         $desc     = escape_tags(trim($_POST['desc']));
94         $location = escape_tags(trim($_POST['location']));
95         $type     = 'event';
96
97         $action = ($event_id == '') ? 'new' : "event/" . $event_id;
98         $onerror_url = System::baseUrl() . "/events/" . $action . "?summary=$summary&description=$desc&location=$location&start=$start_text&finish=$finish_text&adjust=$adjust&nofinish=$nofinish";
99
100         if (strcmp($finish, $start) < 0 && !$nofinish) {
101                 notice(t('Event can not end before it has started.') . EOL);
102                 if (intval($_REQUEST['preview'])) {
103                         echo t('Event can not end before it has started.');
104                         killme();
105                 }
106                 goaway($onerror_url);
107         }
108
109         if ((! $summary) || ($start === NULL_DATE)) {
110                 notice(t('Event title and start time are required.') . EOL);
111                 if (intval($_REQUEST['preview'])) {
112                         echo t('Event title and start time are required.');
113                         killme();
114                 }
115                 goaway($onerror_url);
116         }
117
118         $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
119
120         $c = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
121                 intval(local_user())
122         );
123         if (count($c)) {
124                 $self = $c[0]['id'];
125         } else {
126                 $self = 0;
127         }
128
129
130         if ($share) {
131                 $str_group_allow   = perms2str($_POST['group_allow']);
132                 $str_contact_allow = perms2str($_POST['contact_allow']);
133                 $str_group_deny    = perms2str($_POST['group_deny']);
134                 $str_contact_deny  = perms2str($_POST['contact_deny']);
135
136                 // Undo the pseudo-contact of self, since there are real contacts now
137                 if (strpos($str_contact_allow, '<' . $self . '>') !== false ) {
138                         $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
139                 }
140                 // Make sure to set the `private` field as true. This is necessary to
141                 // have the posts show up correctly in Diaspora if an event is created
142                 // as visible only to self at first, but then edited to display to others.
143                 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
144                         $private_event = true;
145                 }
146         } else {
147                 // Note: do not set `private` field for self-only events. It will
148                 // keep even you from seeing them!
149                 $str_contact_allow = '<' . $self . '>';
150                 $str_group_allow = $str_contact_deny = $str_group_deny = '';
151         }
152
153
154         $datarray = [];
155         $datarray['guid']      = get_guid(32);
156         $datarray['start']     = $start;
157         $datarray['finish']    = $finish;
158         $datarray['summary']   = $summary;
159         $datarray['desc']      = $desc;
160         $datarray['location']  = $location;
161         $datarray['type']      = $type;
162         $datarray['adjust']    = $adjust;
163         $datarray['nofinish']  = $nofinish;
164         $datarray['uid']       = $uid;
165         $datarray['cid']       = $cid;
166         $datarray['allow_cid'] = $str_contact_allow;
167         $datarray['allow_gid'] = $str_group_allow;
168         $datarray['deny_cid']  = $str_contact_deny;
169         $datarray['deny_gid']  = $str_group_deny;
170         $datarray['private']   = (($private_event) ? 1 : 0);
171         $datarray['id']        = $event_id;
172         $datarray['created']   = $created;
173         $datarray['edited']    = $edited;
174
175         if (intval($_REQUEST['preview'])) {
176                 $html = format_event_html($datarray);
177                 echo $html;
178                 killme();
179         }
180
181         $item_id = event_store($datarray);
182
183         if (! $cid) {
184                 Worker::add(PRIORITY_HIGH, "Notifier", "event", $item_id);
185         }
186
187         goaway($_SESSION['return_url']);
188 }
189
190 function events_content(App $a) {
191
192         if (! local_user()) {
193                 notice(t('Permission denied.') . EOL);
194                 return;
195         }
196
197         if ($a->argc == 1) {
198                 $_SESSION['return_url'] = System::baseUrl() . '/' . $a->cmd;
199         }
200
201         if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
202                 $r = q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
203                         intval($a->argv[2]),
204                         intval(local_user())
205                 );
206         }
207
208         if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
209                 $r = q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
210                         intval($a->argv[2]),
211                         intval(local_user())
212                 );
213         }
214
215         if ($a->theme_events_in_profile) {
216                 Nav::setSelected('home');
217         } else {
218                 Nav::setSelected('events');
219         }
220
221         // get the translation strings for the callendar
222         $i18n = get_event_strings();
223
224         $htpl = get_markup_template('event_head.tpl');
225         $a->page['htmlhead'] .= replace_macros($htpl, [
226                 '$baseurl' => System::baseUrl(),
227                 '$module_url' => '/events',
228                 '$modparams' => 1,
229                 '$i18n' => $i18n,
230         ]);
231
232         $etpl = get_markup_template('event_end.tpl');
233         $a->page['end'] .= replace_macros($etpl, [
234                 '$baseurl' => System::baseUrl(),
235         ]);
236
237         $o = '';
238         $tabs = '';
239         // tabs
240         if ($a->theme_events_in_profile) {
241                 $tabs = Profile::getTabs($a, true);
242         }
243
244         $mode = 'view';
245         $y = 0;
246         $m = 0;
247         $ignored = ((x($_REQUEST, 'ignored')) ? intval($_REQUEST['ignored']) : 0);
248
249         if ($a->argc > 1) {
250                 if ($a->argc > 2 && $a->argv[1] == 'event') {
251                         $mode = 'edit';
252                         $event_id = intval($a->argv[2]);
253                 }
254                 if ($a->argc > 2 && $a->argv[1] == 'drop') {
255                         $mode = 'drop';
256                         $event_id = intval($a->argv[2]);
257                 }
258                 if ($a->argc > 2 && $a->argv[1] == 'copy') {
259                         $mode = 'copy';
260                         $event_id = intval($a->argv[2]);
261                 }
262                 if ($a->argv[1] === 'new') {
263                         $mode = 'new';
264                         $event_id = 0;
265                 }
266                 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
267                         $mode = 'view';
268                         $y = intval($a->argv[1]);
269                         $m = intval($a->argv[2]);
270                 }
271         }
272
273         // The view mode part is similiar to /mod/cal.php
274         if ($mode == 'view') {
275
276                 $thisyear  = datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y');
277                 $thismonth = datetime_convert('UTC', date_default_timezone_get(), 'now', 'm');
278                 if (! $y) {
279                         $y = intval($thisyear);
280                 }
281                 if (! $m) {
282                         $m = intval($thismonth);
283                 }
284
285                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
286                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
287
288                 if ($y < 1901) {
289                         $y = 1900;
290                 }
291                 if ($y > 2099) {
292                         $y = 2100;
293                 }
294
295                 $nextyear = $y;
296                 $nextmonth = $m + 1;
297                 if ($nextmonth > 12) {
298                         $nextmonth = 1;
299                         $nextyear ++;
300                 }
301
302                 $prevyear = $y;
303                 if ($m > 1) {
304                         $prevmonth = $m - 1;
305                 } else {
306                         $prevmonth = 12;
307                         $prevyear --;
308                 }
309
310                 $dim    = get_dim($y, $m);
311                 $start  = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
312                 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
313
314                 if ($a->argc > 1 && $a->argv[1] === 'json') {
315                         if (x($_GET, 'start')) {
316                                 $start  = $_GET['start'];
317                         }
318                         if (x($_GET, 'end'))   {
319                                 $finish = $_GET['end'];
320                         }
321                 }
322
323                 $start  = datetime_convert('UTC', 'UTC', $start);
324                 $finish = datetime_convert('UTC', 'UTC', $finish);
325
326                 $adjust_start  = datetime_convert('UTC', date_default_timezone_get(), $start);
327                 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
328
329                 // put the event parametes in an array so we can better transmit them
330                 $event_params = [
331                         'event_id'      => (x($_GET, 'id') ? $_GET['id'] : 0),
332                         'start'         => $start,
333                         'finish'        => $finish,
334                         'adjust_start'  => $adjust_start,
335                         'adjust_finish' => $adjust_finish,
336                         'ignored'       => $ignored,
337                 ];
338
339                 // get events by id or by date
340                 if (x($_GET, 'id')) {
341                         $r = event_by_id(local_user(), $event_params);
342                 } else {
343                         $r = events_by_date(local_user(), $event_params);
344                 }
345
346                 $links = [];
347
348                 if (DBM::is_result($r)) {
349                         $r = sort_by_date($r);
350                         foreach ($r as $rr) {
351                                 $j = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'j') : datetime_convert('UTC', 'UTC', $rr['start'], 'j'));
352                                 if (! x($links,$j)) {
353                                         $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
354                                 }
355                         }
356                 }
357
358                 $events = [];
359
360                 // transform the event in a usable array
361                 if (DBM::is_result($r)) {
362                         $r = sort_by_date($r);
363                         $events = process_events($r);
364                 }
365
366                 if ($a->argc > 1 && $a->argv[1] === 'json'){
367                         echo json_encode($events);
368                         killme();
369                 }
370
371                 if (x($_GET, 'id')) {
372                         $tpl =  get_markup_template("event.tpl");
373                 } else {
374                         $tpl = get_markup_template("events_js.tpl");
375                 }
376
377                 // Get rid of dashes in key names, Smarty3 can't handle them
378                 foreach ($events as $key => $event) {
379                         $event_item = [];
380                         foreach ($event['item'] as $k => $v) {
381                                 $k = str_replace('-' ,'_', $k);
382                                 $event_item[$k] = $v;
383                         }
384                         $events[$key]['item'] = $event_item;
385                 }
386
387                 $o = replace_macros($tpl, [
388                         '$baseurl'   => System::baseUrl(),
389                         '$tabs'      => $tabs,
390                         '$title'     => t('Events'),
391                         '$view'      => t('View'),
392                         '$new_event' => [System::baseUrl() . '/events/new', t('Create New Event'), '', ''],
393                         '$previous'  => [System::baseUrl() . '/events/$prevyear/$prevmonth', t('Previous'), '', ''],
394                         '$next'      => [System::baseUrl() . '/events/$nextyear/$nextmonth', t('Next'), '', ''],
395                         '$calendar'  => cal($y, $m, $links, ' eventcal'),
396
397                         '$events'    => $events,
398
399                         '$today' => t('today'),
400                         '$month' => t('month'),
401                         '$week'  => t('week'),
402                         '$day'   => t('day'),
403                         '$list'  => t('list'),
404                 ]);
405
406                 if (x($_GET, 'id')) {
407                         echo $o;
408                         killme();
409                 }
410
411                 return $o;
412         }
413
414         if (($mode === 'edit' || $mode === 'copy') && $event_id) {
415                 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
416                         intval($event_id),
417                         intval(local_user())
418                 );
419                 if (DBM::is_result($r)) {
420                         $orig_event = $r[0];
421                 }
422         }
423
424         // Passed parameters overrides anything found in the DB
425         if (in_array($mode, ['edit', 'new', 'copy'])) {
426                 if (!x($orig_event)) {$orig_event = [];}
427                 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
428                 if (x($_REQUEST, 'nofinish'))    {$orig_event['nofinish']    = $_REQUEST['nofinish'];}
429                 if (x($_REQUEST, 'adjust'))      {$orig_event['adjust']      = $_REQUEST['adjust'];}
430                 if (x($_REQUEST, 'summary'))     {$orig_event['summary']     = $_REQUEST['summary'];}
431                 if (x($_REQUEST, 'description')) {$orig_event['description'] = $_REQUEST['description'];}
432                 if (x($_REQUEST, 'location'))    {$orig_event['location']    = $_REQUEST['location'];}
433                 if (x($_REQUEST, 'start'))       {$orig_event['start']       = $_REQUEST['start'];}
434                 if (x($_REQUEST, 'finish'))      {$orig_event['finish']      = $_REQUEST['finish'];}
435
436                 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
437                 $a_checked = ((x($orig_event) && $orig_event['adjust'])   ? ' checked="checked" ' : '');
438
439                 $t_orig = ((x($orig_event)) ? $orig_event['summary']  : '');
440                 $d_orig = ((x($orig_event)) ? $orig_event['desc']     : '');
441                 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
442                 $eid    = ((x($orig_event)) ? $orig_event['id']       : 0);
443                 $cid    = ((x($orig_event)) ? $orig_event['cid']      : 0);
444                 $uri    = ((x($orig_event)) ? $orig_event['uri']      : '');
445
446                 $sh_disabled = '';
447                 $sh_checked  = '';
448
449                 if (x($orig_event)) {
450                         $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ');
451                 }
452
453                 if ($cid || $mode === 'edit') {
454                         $sh_disabled = 'disabled="disabled"';
455                 }
456
457                 $sdt = ((x($orig_event)) ? $orig_event['start']  : 'now');
458                 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
459
460                 $tz = date_default_timezone_get();
461                 if (x($orig_event)) {
462                         $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
463                 }
464
465                 $syear  = datetime_convert('UTC', $tz, $sdt, 'Y');
466                 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
467                 $sday   = datetime_convert('UTC', $tz, $sdt, 'd');
468
469                 $shour   = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
470                 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
471
472                 $fyear  = datetime_convert('UTC', $tz, $fdt, 'Y');
473                 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
474                 $fday   = datetime_convert('UTC', $tz, $fdt, 'd');
475
476                 $fhour   = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
477                 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
478
479                 require_once 'include/acl_selectors.php' ;
480
481                 $perms = get_acl_permissions($orig_event);
482
483                 if ($mode === 'new' || $mode === 'copy') {
484                         $acl = (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user)));
485                 }
486
487                 // If we copy an old event, we need to remove the ID and URI
488                 // from the original event.
489                 if ($mode === 'copy') {
490                         $eid = 0;
491                         $uri = '';
492                 }
493
494                 $tpl = get_markup_template('event_form.tpl');
495
496                 $o .= replace_macros($tpl,[
497                         '$post' => System::baseUrl() . '/events',
498                         '$eid'  => $eid,
499                         '$cid'  => $cid,
500                         '$uri'  => $uri,
501
502                         '$allow_cid' => json_encode($perms['allow_cid']),
503                         '$allow_gid' => json_encode($perms['allow_gid']),
504                         '$deny_cid'  => json_encode($perms['deny_cid']),
505                         '$deny_gid'  => json_encode($perms['deny_gid']),
506
507                         '$title' => t('Event details'),
508                         '$desc' => t('Starting date and Title are required.'),
509                         '$s_text' => t('Event Starts:') . ' <span class="required" title="' . t('Required') . '">*</span>',
510                         '$s_dsel' => datetimesel(new DateTime(), DateTime::createFromFormat('Y', $syear+5), DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"), t('Event Starts:'), 'start_text', true, true, '', '', true),
511                         '$n_text' => t('Finish date/time is not known or not relevant'),
512                         '$n_checked' => $n_checked,
513                         '$f_text' => t('Event Finishes:'),
514                         '$f_dsel' => datetimesel(new DateTime(), DateTime::createFromFormat('Y', $fyear+5), DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"), t('Event Finishes:'), 'finish_text', true, true, 'start_text'),
515                         '$a_text' => t('Adjust for viewer timezone'),
516                         '$a_checked' => $a_checked,
517                         '$d_text' => t('Description:'),
518                         '$d_orig' => $d_orig,
519                         '$l_text' => t('Location:'),
520                         '$l_orig' => $l_orig,
521                         '$t_text' => t('Title:') . ' <span class="required" title="' . t('Required') . '">*</span>',
522                         '$t_orig' => $t_orig,
523                         '$summary' => ['summary', t('Title:'), $t_orig, '', '*'],
524                         '$sh_text' => t('Share this event'),
525                         '$share' => ['share', t('Share this event'), $sh_checked, '', $sh_disabled],
526                         '$sh_checked' => $sh_checked,
527                         '$nofinish' => ['nofinish', t('Finish date/time is not known or not relevant'), $n_checked],
528                         '$adjust' => ['adjust', t('Adjust for viewer timezone'), $a_checked],
529                         '$preview' => t('Preview'),
530                         '$acl' => $acl,
531                         '$submit' => t('Submit'),
532                         '$basic' => t('Basic'),
533                         '$advanced' => t('Advanced'),
534                         '$permissions' => t('Permissions'),
535
536                 ]);
537
538                 return $o;
539         }
540
541         // Remove an event from the calendar and its related items
542         if ($mode === 'drop' && $event_id) {
543                 $del = 0;
544
545                 $params = ['event_id' => ($event_id)];
546                 $ev = event_by_id(local_user(), $params);
547
548                 // Delete only real events (no birthdays)
549                 if (DBM::is_result($ev) && $ev[0]['type'] == 'event') {
550                         $del = drop_item($ev[0]['itemid'], false);
551                 }
552
553                 if ($del == 0) {
554                         notice(t('Failed to remove event' ) . EOL);
555                 } else {
556                         info(t('Event removed') . EOL);
557                 }
558
559                 goaway(System::baseUrl() . '/events');
560         }
561 }