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