]> git.mxchange.org Git - friendica.git/blob - mod/events.php
Merge pull request #4240 from annando/sql-definition
[friendica.git] / mod / events.php
1 <?php
2 /**
3  * @file mod/events.php
4  * @brief The events module
5  */
6 use Friendica\App;
7 use Friendica\Core\Config;
8 use Friendica\Core\System;
9 use Friendica\Core\Worker;
10 use Friendica\Database\DBM;
11 use Friendica\Model\Profile;
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         // tabs
239         if ($a->theme_events_in_profile) {
240                 $tabs = Profile::getTabs($a, true);
241         }
242
243         $mode = 'view';
244         $y = 0;
245         $m = 0;
246         $ignored = ((x($_REQUEST, 'ignored')) ? intval($_REQUEST['ignored']) : 0);
247
248         if ($a->argc > 1) {
249                 if ($a->argc > 2 && $a->argv[1] == 'event') {
250                         $mode = 'edit';
251                         $event_id = intval($a->argv[2]);
252                 }
253                 if ($a->argc > 2 && $a->argv[1] == 'drop') {
254                         $mode = 'drop';
255                         $event_id = intval($a->argv[2]);
256                 }
257                 if ($a->argc > 2 && $a->argv[1] == 'copy') {
258                         $mode = 'copy';
259                         $event_id = intval($a->argv[2]);
260                 }
261                 if ($a->argv[1] === 'new') {
262                         $mode = 'new';
263                         $event_id = 0;
264                 }
265                 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
266                         $mode = 'view';
267                         $y = intval($a->argv[1]);
268                         $m = intval($a->argv[2]);
269                 }
270         }
271
272         // The view mode part is similiar to /mod/cal.php
273         if ($mode == 'view') {
274
275                 $thisyear  = datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y');
276                 $thismonth = datetime_convert('UTC', date_default_timezone_get(), 'now', 'm');
277                 if (! $y) {
278                         $y = intval($thisyear);
279                 }
280                 if (! $m) {
281                         $m = intval($thismonth);
282                 }
283
284                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
285                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
286
287                 if ($y < 1901) {
288                         $y = 1900;
289                 }
290                 if ($y > 2099) {
291                         $y = 2100;
292                 }
293
294                 $nextyear = $y;
295                 $nextmonth = $m + 1;
296                 if ($nextmonth > 12) {
297                         $nextmonth = 1;
298                         $nextyear ++;
299                 }
300
301                 $prevyear = $y;
302                 if ($m > 1) {
303                         $prevmonth = $m - 1;
304                 } else {
305                         $prevmonth = 12;
306                         $prevyear --;
307                 }
308
309                 $dim    = get_dim($y, $m);
310                 $start  = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
311                 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
312
313                 if ($a->argc > 1 && $a->argv[1] === 'json') {
314                         if (x($_GET, 'start')) {
315                                 $start  = $_GET['start'];
316                         }
317                         if (x($_GET, 'end'))   {
318                                 $finish = $_GET['end'];
319                         }
320                 }
321
322                 $start  = datetime_convert('UTC', 'UTC', $start);
323                 $finish = datetime_convert('UTC', 'UTC', $finish);
324
325                 $adjust_start  = datetime_convert('UTC', date_default_timezone_get(), $start);
326                 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
327
328                 // put the event parametes in an array so we can better transmit them
329                 $event_params = array(
330                         'event_id'      => (x($_GET, 'id') ? $_GET['id'] : 0),
331                         'start'         => $start,
332                         'finish'        => $finish,
333                         'adjust_start'  => $adjust_start,
334                         'adjust_finish' => $adjust_finish,
335                         'ignored'       => $ignored,
336                 );
337
338                 // get events by id or by date
339                 if (x($_GET, 'id')) {
340                         $r = event_by_id(local_user(), $event_params);
341                 } else {
342                         $r = events_by_date(local_user(), $event_params);
343                 }
344
345                 $links = array();
346
347                 if (DBM::is_result($r)) {
348                         $r = sort_by_date($r);
349                         foreach ($r as $rr) {
350                                 $j = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'j') : datetime_convert('UTC', 'UTC', $rr['start'], 'j'));
351                                 if (! x($links,$j)) {
352                                         $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
353                                 }
354                         }
355                 }
356
357                 $events = array();
358
359                 // transform the event in a usable array
360                 if (DBM::is_result($r)) {
361                         $r = sort_by_date($r);
362                         $events = process_events($r);
363                 }
364
365                 if ($a->argc > 1 && $a->argv[1] === 'json'){
366                         echo json_encode($events);
367                         killme();
368                 }
369
370                 if (x($_GET, 'id')) {
371                         $tpl =  get_markup_template("event.tpl");
372                 } else {
373                         $tpl = get_markup_template("events_js.tpl");
374                 }
375
376                 // Get rid of dashes in key names, Smarty3 can't handle them
377                 foreach ($events as $key => $event) {
378                         $event_item = array();
379                         foreach ($event['item'] as $k => $v) {
380                                 $k = str_replace('-' ,'_', $k);
381                                 $event_item[$k] = $v;
382                         }
383                         $events[$key]['item'] = $event_item;
384                 }
385
386                 $o = replace_macros($tpl, array(
387                         '$baseurl'   => System::baseUrl(),
388                         '$tabs'      => $tabs,
389                         '$title'     => t('Events'),
390                         '$view'      => t('View'),
391                         '$new_event' => array(System::baseUrl() . '/events/new', t('Create New Event'), '', ''),
392                         '$previous'  => array(System::baseUrl() . '/events/$prevyear/$prevmonth', t('Previous'), '', ''),
393                         '$next'      => array(System::baseUrl() . '/events/$nextyear/$nextmonth', t('Next'), '', ''),
394                         '$calendar'  => cal($y, $m, $links, ' eventcal'),
395
396                         '$events'    => $events,
397
398                         '$today' => t('today'),
399                         '$month' => t('month'),
400                         '$week'  => t('week'),
401                         '$day'   => t('day'),
402                         '$list'  => t('list'),
403                 ));
404
405                 if (x($_GET, 'id')) {
406                         echo $o;
407                         killme();
408                 }
409
410                 return $o;
411         }
412
413         if (($mode === 'edit' || $mode === 'copy') && $event_id) {
414                 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
415                         intval($event_id),
416                         intval(local_user())
417                 );
418                 if (DBM::is_result($r)) {
419                         $orig_event = $r[0];
420                 }
421         }
422
423         // Passed parameters overrides anything found in the DB
424         if (in_array($mode, array('edit', 'new', 'copy'))) {
425                 if (!x($orig_event)) {$orig_event = array();}
426                 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
427                 if (x($_REQUEST, 'nofinish'))    {$orig_event['nofinish']    = $_REQUEST['nofinish'];}
428                 if (x($_REQUEST, 'adjust'))      {$orig_event['adjust']      = $_REQUEST['adjust'];}
429                 if (x($_REQUEST, 'summary'))     {$orig_event['summary']     = $_REQUEST['summary'];}
430                 if (x($_REQUEST, 'description')) {$orig_event['description'] = $_REQUEST['description'];}
431                 if (x($_REQUEST, 'location'))    {$orig_event['location']    = $_REQUEST['location'];}
432                 if (x($_REQUEST, 'start'))       {$orig_event['start']       = $_REQUEST['start'];}
433                 if (x($_REQUEST, 'finish'))      {$orig_event['finish']      = $_REQUEST['finish'];}
434
435                 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
436                 $a_checked = ((x($orig_event) && $orig_event['adjust'])   ? ' checked="checked" ' : '');
437
438                 $t_orig = ((x($orig_event)) ? $orig_event['summary']  : '');
439                 $d_orig = ((x($orig_event)) ? $orig_event['desc']     : '');
440                 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
441                 $eid    = ((x($orig_event)) ? $orig_event['id']       : 0);
442                 $cid    = ((x($orig_event)) ? $orig_event['cid']      : 0);
443                 $uri    = ((x($orig_event)) ? $orig_event['uri']      : '');
444
445                 $sh_disabled = '';
446                 $sh_checked  = '';
447
448                 if (x($orig_event)) {
449                         $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ');
450                 }
451
452                 if ($cid || $mode === 'edit') {
453                         $sh_disabled = 'disabled="disabled"';
454                 }
455
456                 $sdt = ((x($orig_event)) ? $orig_event['start']  : 'now');
457                 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
458
459                 $tz = date_default_timezone_get();
460                 if (x($orig_event)) {
461                         $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
462                 }
463
464                 $syear  = datetime_convert('UTC', $tz, $sdt, 'Y');
465                 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
466                 $sday   = datetime_convert('UTC', $tz, $sdt, 'd');
467
468                 $shour   = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
469                 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
470
471                 $fyear  = datetime_convert('UTC', $tz, $fdt, 'Y');
472                 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
473                 $fday   = datetime_convert('UTC', $tz, $fdt, 'd');
474
475                 $fhour   = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
476                 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
477
478                 require_once 'include/acl_selectors.php' ;
479
480                 $perms = get_acl_permissions($orig_event);
481
482                 if ($mode === 'new' || $mode === 'copy') {
483                         $acl = (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user)));
484                 }
485
486                 // If we copy an old event, we need to remove the ID and URI
487                 // from the original event.
488                 if ($mode === 'copy') {
489                         $eid = 0;
490                         $uri = '';
491                 }
492
493                 $tpl = get_markup_template('event_form.tpl');
494
495                 $o .= replace_macros($tpl,array(
496                         '$post' => System::baseUrl() . '/events',
497                         '$eid'  => $eid,
498                         '$cid'  => $cid,
499                         '$uri'  => $uri,
500
501                         '$allow_cid' => json_encode($perms['allow_cid']),
502                         '$allow_gid' => json_encode($perms['allow_gid']),
503                         '$deny_cid'  => json_encode($perms['deny_cid']),
504                         '$deny_gid'  => json_encode($perms['deny_gid']),
505
506                         '$title' => t('Event details'),
507                         '$desc' => t('Starting date and Title are required.'),
508                         '$s_text' => t('Event Starts:') . ' <span class="required" title="' . t('Required') . '">*</span>',
509                         '$s_dsel' => datetimesel(new DateTime(), DateTime::createFromFormat('Y', $syear+5), DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"), t('Event Starts:'), 'start_text', true, true, '', '', true),
510                         '$n_text' => t('Finish date/time is not known or not relevant'),
511                         '$n_checked' => $n_checked,
512                         '$f_text' => t('Event Finishes:'),
513                         '$f_dsel' => datetimesel(new DateTime(), DateTime::createFromFormat('Y', $fyear+5), DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"), t('Event Finishes:'), 'finish_text', true, true, 'start_text'),
514                         '$a_text' => t('Adjust for viewer timezone'),
515                         '$a_checked' => $a_checked,
516                         '$d_text' => t('Description:'),
517                         '$d_orig' => $d_orig,
518                         '$l_text' => t('Location:'),
519                         '$l_orig' => $l_orig,
520                         '$t_text' => t('Title:') . ' <span class="required" title="' . t('Required') . '">*</span>',
521                         '$t_orig' => $t_orig,
522                         '$summary' => array('summary', t('Title:'), $t_orig, '', '*'),
523                         '$sh_text' => t('Share this event'),
524                         '$share' => array('share', t('Share this event'), $sh_checked, '', $sh_disabled),
525                         '$sh_checked' => $sh_checked,
526                         '$nofinish' => array('nofinish', t('Finish date/time is not known or not relevant'), $n_checked),
527                         '$adjust' => array('adjust', t('Adjust for viewer timezone'), $a_checked),
528                         '$preview' => t('Preview'),
529                         '$acl' => $acl,
530                         '$submit' => t('Submit'),
531                         '$basic' => t('Basic'),
532                         '$advanced' => t('Advanced'),
533                         '$permissions' => t('Permissions'),
534
535                 ));
536
537                 return $o;
538         }
539
540         // Remove an event from the calendar and its related items
541         if ($mode === 'drop' && $event_id) {
542                 $del = 0;
543
544                 $params = array('event_id' => ($event_id));
545                 $ev = event_by_id(local_user(), $params);
546
547                 // Delete only real events (no birthdays)
548                 if (DBM::is_result($ev) && $ev[0]['type'] == 'event') {
549                         $del = drop_item($ev[0]['itemid'], false);
550                 }
551
552                 if ($del == 0) {
553                         notice(t('Failed to remove event' ) . EOL);
554                 } else {
555                         info(t('Event removed') . EOL);
556                 }
557
558                 goaway(System::baseUrl() . '/events');
559         }
560 }