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