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