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(),
239 if ($a->theme_events_in_profile) {
240 $tabs = profile_tabs($a, true);
246 $ignored = ((x($_REQUEST, 'ignored')) ? intval($_REQUEST['ignored']) : 0);
249 if ($a->argc > 2 && $a->argv[1] == 'event') {
251 $event_id = intval($a->argv[2]);
253 if ($a->argc > 2 && $a->argv[1] == 'drop') {
255 $event_id = intval($a->argv[2]);
257 if ($a->argc > 2 && $a->argv[1] == 'copy') {
259 $event_id = intval($a->argv[2]);
261 if ($a->argv[1] === 'new') {
265 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
267 $y = intval($a->argv[1]);
268 $m = intval($a->argv[2]);
272 // The view mode part is similiar to /mod/cal.php
273 if ($mode == 'view') {
275 $thisyear = datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y');
276 $thismonth = datetime_convert('UTC', date_default_timezone_get(), 'now', 'm');
278 $y = intval($thisyear);
281 $m = intval($thismonth);
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.
296 if ($nextmonth > 12) {
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);
313 if ($a->argc > 1 && $a->argv[1] === 'json') {
314 if (x($_GET, 'start')) {
315 $start = $_GET['start'];
317 if (x($_GET, 'end')) {
318 $finish = $_GET['end'];
322 $start = datetime_convert('UTC', 'UTC', $start);
323 $finish = datetime_convert('UTC', 'UTC', $finish);
325 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
326 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
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),
333 'adjust_start' => $adjust_start,
334 'adjust_finish' => $adjust_finish,
335 'ignored' => $ignored,
338 // get events by id or by date
339 if (x($_GET, 'id')) {
340 $r = event_by_id(local_user(), $event_params);
342 $r = events_by_date(local_user(), $event_params);
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;
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);
365 if ($a->argc > 1 && $a->argv[1] === 'json'){
366 echo json_encode($events);
370 if (x($_GET, 'id')) {
371 $tpl = get_markup_template("event.tpl");
373 $tpl = get_markup_template("events_js.tpl");
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;
383 $events[$key]['item'] = $event_item;
386 $o = replace_macros($tpl, array(
387 '$baseurl' => System::baseUrl(),
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'),
396 '$events' => $events,
398 '$today' => t('today'),
399 '$month' => t('month'),
400 '$week' => t('week'),
402 '$list' => t('list'),
405 if (x($_GET, 'id')) {
413 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
414 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
418 if (DBM::is_result($r)) {
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'];}
435 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
436 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
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'] : '');
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" ');
452 if ($cid || $mode === 'edit') {
453 $sh_disabled = 'disabled="disabled"';
456 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
457 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
459 $tz = date_default_timezone_get();
460 if (x($orig_event)) {
461 $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
464 $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
465 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
466 $sday = datetime_convert('UTC', $tz, $sdt, 'd');
468 $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
469 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
471 $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
472 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
473 $fday = datetime_convert('UTC', $tz, $fdt, 'd');
475 $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
476 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
478 require_once 'include/acl_selectors.php' ;
480 $perms = get_acl_permissions($orig_event);
482 if ($mode === 'new' || $mode === 'copy') {
483 $acl = (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user)));
486 // If we copy an old event, we need to remove the ID and URI
487 // from the original event.
488 if ($mode === 'copy') {
493 $tpl = get_markup_template('event_form.tpl');
495 $o .= replace_macros($tpl,array(
496 '$post' => System::baseUrl() . '/events',
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']),
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'),
530 '$submit' => t('Submit'),
531 '$basic' => t('Basic'),
532 '$advanced' => t('Advanced'),
533 '$permissions' => t('Permissions'),
540 // Remove an event from the calendar and its related items
541 if ($mode === 'drop' && $event_id) {
544 $params = array('event_id' => ($event_id));
545 $ev = event_by_id(local_user(), $params);
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);
553 notice(t('Failed to remove event' ) . EOL);
555 info(t('Event removed') . EOL);
558 goaway(System::baseUrl() . '/events');