4 * @brief The events module
8 use Friendica\Core\Config;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBM;
13 require_once 'include/bbcode.php';
14 require_once 'include/datetime.php';
15 require_once 'include/event.php';
16 require_once 'include/items.php';
18 function events_init(App $a) {
24 // If it's a json request abort here because we don't
25 // need the widget data
26 if ($a->argv[1] === 'json') {
30 $cal_widget = widget_events();
32 if (! x($a->page,'aside')) {
33 $a->page['aside'] = '';
36 $a->page['aside'] .= $cal_widget;
42 function events_post(App $a) {
44 logger('post: ' . print_r($_REQUEST, true), LOGGER_DATA);
50 $event_id = ((x($_POST, 'event_id')) ? intval($_POST['event_id']) : 0);
51 $cid = ((x($_POST, 'cid')) ? intval($_POST['cid']) : 0);
54 $start_text = escape_tags($_REQUEST['start_text']);
55 $finish_text = escape_tags($_REQUEST['finish_text']);
57 $adjust = intval($_POST['adjust']);
58 $nofinish = intval($_POST['nofinish']);
60 // The default setting for the `private` field in event_store() is false, so mirror that
61 $private_event = false;
71 $finish = $finish_text;
75 $start = datetime_convert(date_default_timezone_get(), 'UTC', $start);
77 $finish = datetime_convert(date_default_timezone_get(), 'UTC', $finish);
80 $start = datetime_convert('UTC', 'UTC', $start);
82 $finish = datetime_convert('UTC', 'UTC', $finish);
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.
91 $summary = escape_tags(trim($_POST['summary']));
92 $desc = escape_tags(trim($_POST['desc']));
93 $location = escape_tags(trim($_POST['location']));
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";
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.');
105 goaway($onerror_url);
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.');
114 goaway($onerror_url);
117 $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
119 $c = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
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']);
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);
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;
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 = '';
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;
174 if (intval($_REQUEST['preview'])) {
175 $html = format_event_html($datarray);
180 $item_id = event_store($datarray);
183 Worker::add(PRIORITY_HIGH, "notifier", "event", $item_id);
186 goaway($_SESSION['return_url']);
189 function events_content(App $a) {
191 if (! local_user()) {
192 notice(t('Permission denied.') . EOL);
197 $_SESSION['return_url'] = System::baseUrl() . '/' . $a->cmd;
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",
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",
214 if ($a->theme_events_in_profile) {
215 nav_set_selected('home');
217 nav_set_selected('events');
220 // get the translation strings for the callendar
221 $i18n = get_event_strings();
223 $htpl = get_markup_template('event_head.tpl');
224 $a->page['htmlhead'] .= replace_macros($htpl, array(
225 '$baseurl' => System::baseUrl(),
226 '$module_url' => '/events',
231 $etpl = get_markup_template('event_end.tpl');
232 $a->page['end'] .= replace_macros($etpl, array(
233 '$baseurl' => System::baseUrl(),
238 if ($a->theme_events_in_profile) {
239 $tabs = profile_tabs($a, true);
245 $ignored = ((x($_REQUEST, 'ignored')) ? intval($_REQUEST['ignored']) : 0);
248 if ($a->argc > 2 && $a->argv[1] == 'event') {
250 $event_id = intval($a->argv[2]);
252 if ($a->argc > 2 && $a->argv[1] == 'drop') {
254 $event_id = intval($a->argv[2]);
256 if ($a->argc > 2 && $a->argv[1] == 'copy') {
258 $event_id = intval($a->argv[2]);
260 if ($a->argv[1] === 'new') {
264 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
266 $y = intval($a->argv[1]);
267 $m = intval($a->argv[2]);
271 // The view mode part is similiar to /mod/cal.php
272 if ($mode == 'view') {
274 $thisyear = datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y');
275 $thismonth = datetime_convert('UTC', date_default_timezone_get(), 'now', 'm');
277 $y = intval($thisyear);
280 $m = intval($thismonth);
283 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
284 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
295 if ($nextmonth > 12) {
308 $dim = get_dim($y, $m);
309 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
310 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
313 if ($a->argv[1] === 'json') {
314 if (x($_GET, 'start')) {$start = $_GET['start'];}
315 if (x($_GET, 'end')) {$finish = $_GET['end'];}
318 $start = datetime_convert('UTC', 'UTC', $start);
319 $finish = datetime_convert('UTC', 'UTC', $finish);
321 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
322 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
324 // put the event parametes in an array so we can better transmit them
325 $event_params = array(
326 'event_id' => (x($_GET, 'id') ? $_GET['id'] : 0),
329 'adjust_start' => $adjust_start,
330 'adjust_finish' => $adjust_finish,
331 'ignored' => $ignored,
334 // get events by id or by date
335 if (x($_GET, 'id')) {
336 $r = event_by_id(local_user(), $event_params);
338 $r = events_by_date(local_user(), $event_params);
343 if (DBM::is_result($r)) {
344 $r = sort_by_date($r);
345 foreach ($r as $rr) {
346 $j = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'j') : datetime_convert('UTC', 'UTC', $rr['start'], 'j'));
347 if (! x($links,$j)) {
348 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
355 // transform the event in a usable array
356 if (DBM::is_result($r)) {
357 $r = sort_by_date($r);
358 $events = process_events($r);
361 if ($a->argv[1] === 'json'){
362 echo json_encode($events);
366 if (x($_GET, 'id')) {
367 $tpl = get_markup_template("event.tpl");
369 $tpl = get_markup_template("events_js.tpl");
372 // Get rid of dashes in key names, Smarty3 can't handle them
373 foreach ($events as $key => $event) {
374 $event_item = array();
375 foreach ($event['item'] as $k => $v) {
376 $k = str_replace('-' ,'_', $k);
377 $event_item[$k] = $v;
379 $events[$key]['item'] = $event_item;
382 $o = replace_macros($tpl, array(
383 '$baseurl' => System::baseUrl(),
385 '$title' => t('Events'),
386 '$view' => t('View'),
387 '$new_event' => array(System::baseUrl() . '/events/new', t('Create New Event'), '', ''),
388 '$previous' => array(System::baseUrl() . '/events/$prevyear/$prevmonth', t('Previous'), '', ''),
389 '$next' => array(System::baseUrl() . '/events/$nextyear/$nextmonth', t('Next'), '', ''),
390 '$calendar' => cal($y, $m, $links, ' eventcal'),
392 '$events' => $events,
394 '$today' => t('today'),
395 '$month' => t('month'),
396 '$week' => t('week'),
398 '$list' => t('list'),
401 if (x($_GET, 'id')) {
409 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
410 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
414 if (DBM::is_result($r)) {
419 // Passed parameters overrides anything found in the DB
420 if (in_array($mode, array('edit', 'new', 'copy'))) {
421 if (!x($orig_event)) {$orig_event = array();}
422 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
423 if (x($_REQUEST, 'nofinish')) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
424 if (x($_REQUEST, 'adjust')) {$orig_event['adjust'] = $_REQUEST['adjust'];}
425 if (x($_REQUEST, 'summary')) {$orig_event['summary'] = $_REQUEST['summary'];}
426 if (x($_REQUEST, 'description')) {$orig_event['description'] = $_REQUEST['description'];}
427 if (x($_REQUEST, 'location')) {$orig_event['location'] = $_REQUEST['location'];}
428 if (x($_REQUEST, 'start')) {$orig_event['start'] = $_REQUEST['start'];}
429 if (x($_REQUEST, 'finish')) {$orig_event['finish'] = $_REQUEST['finish'];}
431 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
432 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
434 $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
435 $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
436 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
437 $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
438 $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
439 $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
444 if (x($orig_event)) {
445 $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ');
448 if ($cid || $mode === 'edit') {
449 $sh_disabled = 'disabled="disabled"';
452 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
453 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
455 $tz = date_default_timezone_get();
456 if (x($orig_event)) {
457 $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
460 $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
461 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
462 $sday = datetime_convert('UTC', $tz, $sdt, 'd');
464 $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
465 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
467 $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
468 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
469 $fday = datetime_convert('UTC', $tz, $fdt, 'd');
471 $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
472 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
474 $f = Config::get('system','event_input_format');
479 require_once 'include/acl_selectors.php' ;
481 $perms = get_acl_permissions($orig_event);
483 if ($mode === 'new' || $mode === 'copy') {
484 $acl = (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user)));
487 // If we copy an old event, we need to remove the ID and URI
488 // from the original event.
489 if ($mode === 'copy') {
494 $tpl = get_markup_template('event_form.tpl');
496 $o .= replace_macros($tpl,array(
497 '$post' => System::baseUrl() . '/events',
502 '$allow_cid' => json_encode($perms['allow_cid']),
503 '$allow_gid' => json_encode($perms['allow_gid']),
504 '$deny_cid' => json_encode($perms['deny_cid']),
505 '$deny_gid' => json_encode($perms['deny_gid']),
507 '$title' => t('Event details'),
508 '$desc' => t('Starting date and Title are required.'),
509 '$s_text' => t('Event Starts:') . ' <span class="required" title="' . t('Required') . '">*</span>',
510 '$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),
511 '$n_text' => t('Finish date/time is not known or not relevant'),
512 '$n_checked' => $n_checked,
513 '$f_text' => t('Event Finishes:'),
514 '$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'),
515 '$a_text' => t('Adjust for viewer timezone'),
516 '$a_checked' => $a_checked,
517 '$d_text' => t('Description:'),
518 '$d_orig' => $d_orig,
519 '$l_text' => t('Location:'),
520 '$l_orig' => $l_orig,
521 '$t_text' => t('Title:') . ' <span class="required" title="' . t('Required') . '">*</span>',
522 '$t_orig' => $t_orig,
523 '$summary' => array('summary', t('Title:'), $t_orig, '', '*'),
524 '$sh_text' => t('Share this event'),
525 '$share' => array('share', t('Share this event'), $sh_checked, '', $sh_disabled),
526 '$sh_checked' => $sh_checked,
527 '$nofinish' => array('nofinish', t('Finish date/time is not known or not relevant'), $n_checked),
528 '$adjust' => array('adjust', t('Adjust for viewer timezone'), $a_checked),
529 '$preview' => t('Preview'),
531 '$submit' => t('Submit'),
532 '$basic' => t('Basic'),
533 '$advanced' => t('Advanced'),
534 '$permissions' => t('Permissions'),
541 // Remove an event from the calendar and its related items
542 if ($mode === 'drop' && $event_id) {
545 $params = array('event_id' => ($event_id));
546 $ev = event_by_id(local_user(), $params);
548 // Delete only real events (no birthdays)
549 if (DBM::is_result($ev) && $ev[0]['type'] == 'event') {
550 $del = drop_item($ev[0]['itemid'], false);
554 notice(t('Failed to remove event' ) . EOL);
556 info(t('Event removed') . EOL);
559 goaway(System::baseUrl() . '/events');