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