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