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