4 * @brief The events module
8 use Friendica\Content\Nav;
9 use Friendica\Core\L10n;
10 use Friendica\Core\System;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBM;
13 use Friendica\Model\Item;
14 use Friendica\Model\Profile;
15 use Friendica\Util\DateTimeFormat;
16 use Friendica\Util\Temporal;
18 require_once 'include/event.php';
19 require_once 'include/items.php';
21 function events_init(App $a) {
27 // If it's a json request abort here because we don't
28 // need the widget data
29 if ($a->argv[1] === 'json') {
33 $cal_widget = widget_events();
35 if (! x($a->page,'aside')) {
36 $a->page['aside'] = '';
39 $a->page['aside'] .= $cal_widget;
45 function events_post(App $a) {
47 logger('post: ' . print_r($_REQUEST, true), LOGGER_DATA);
53 $event_id = ((x($_POST, 'event_id')) ? intval($_POST['event_id']) : 0);
54 $cid = ((x($_POST, 'cid')) ? intval($_POST['cid']) : 0);
57 $start_text = escape_tags($_REQUEST['start_text']);
58 $finish_text = escape_tags($_REQUEST['finish_text']);
60 $adjust = intval($_POST['adjust']);
61 $nofinish = intval($_POST['nofinish']);
63 // The default setting for the `private` field in event_store() is false, so mirror that
64 $private_event = false;
74 $finish = $finish_text;
78 $start = DateTimeFormat::convert($start, 'UTC', date_default_timezone_get());
80 $finish = DateTimeFormat::convert($finish, 'UTC', date_default_timezone_get());
83 $start = DateTimeFormat::utc($start);
85 $finish = DateTimeFormat::utc($finish);
89 // Don't allow the event to finish before it begins.
90 // It won't hurt anything, but somebody will file a bug report
91 // and we'll waste a bunch of time responding to it. Time that
92 // could've been spent doing something else.
94 $summary = escape_tags(trim($_POST['summary']));
95 $desc = escape_tags(trim($_POST['desc']));
96 $location = escape_tags(trim($_POST['location']));
99 $action = ($event_id == '') ? 'new' : "event/" . $event_id;
100 $onerror_url = System::baseUrl() . "/events/" . $action . "?summary=$summary&description=$desc&location=$location&start=$start_text&finish=$finish_text&adjust=$adjust&nofinish=$nofinish";
102 if (strcmp($finish, $start) < 0 && !$nofinish) {
103 notice(L10n::t('Event can not end before it has started.') . EOL);
104 if (intval($_REQUEST['preview'])) {
105 echo L10n::t('Event can not end before it has started.');
108 goaway($onerror_url);
111 if ((! $summary) || ($start === NULL_DATE)) {
112 notice(L10n::t('Event title and start time are required.') . EOL);
113 if (intval($_REQUEST['preview'])) {
114 echo L10n::t('Event title and start time are required.');
117 goaway($onerror_url);
120 $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
122 $c = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
133 $str_group_allow = perms2str($_POST['group_allow']);
134 $str_contact_allow = perms2str($_POST['contact_allow']);
135 $str_group_deny = perms2str($_POST['group_deny']);
136 $str_contact_deny = perms2str($_POST['contact_deny']);
138 // Undo the pseudo-contact of self, since there are real contacts now
139 if (strpos($str_contact_allow, '<' . $self . '>') !== false ) {
140 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
142 // Make sure to set the `private` field as true. This is necessary to
143 // have the posts show up correctly in Diaspora if an event is created
144 // as visible only to self at first, but then edited to display to others.
145 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
146 $private_event = true;
149 // Note: do not set `private` field for self-only events. It will
150 // keep even you from seeing them!
151 $str_contact_allow = '<' . $self . '>';
152 $str_group_allow = $str_contact_deny = $str_group_deny = '';
157 $datarray['guid'] = get_guid(32);
158 $datarray['start'] = $start;
159 $datarray['finish'] = $finish;
160 $datarray['summary'] = $summary;
161 $datarray['desc'] = $desc;
162 $datarray['location'] = $location;
163 $datarray['type'] = $type;
164 $datarray['adjust'] = $adjust;
165 $datarray['nofinish'] = $nofinish;
166 $datarray['uid'] = $uid;
167 $datarray['cid'] = $cid;
168 $datarray['allow_cid'] = $str_contact_allow;
169 $datarray['allow_gid'] = $str_group_allow;
170 $datarray['deny_cid'] = $str_contact_deny;
171 $datarray['deny_gid'] = $str_group_deny;
172 $datarray['private'] = (($private_event) ? 1 : 0);
173 $datarray['id'] = $event_id;
174 $datarray['created'] = $created;
175 $datarray['edited'] = $edited;
177 if (intval($_REQUEST['preview'])) {
178 $html = format_event_html($datarray);
183 $item_id = event_store($datarray);
186 Worker::add(PRIORITY_HIGH, "Notifier", "event", $item_id);
189 goaway($_SESSION['return_url']);
192 function events_content(App $a) {
194 if (! local_user()) {
195 notice(L10n::t('Permission denied.') . EOL);
200 $_SESSION['return_url'] = System::baseUrl() . '/' . $a->cmd;
203 if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
204 $r = q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
210 if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
211 $r = q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
217 if ($a->theme_events_in_profile) {
218 Nav::setSelected('home');
220 Nav::setSelected('events');
223 // get the translation strings for the callendar
224 $i18n = get_event_strings();
226 $htpl = get_markup_template('event_head.tpl');
227 $a->page['htmlhead'] .= replace_macros($htpl, [
228 '$baseurl' => System::baseUrl(),
229 '$module_url' => '/events',
234 $etpl = get_markup_template('event_end.tpl');
235 $a->page['end'] .= replace_macros($etpl, [
236 '$baseurl' => System::baseUrl(),
242 if ($a->theme_events_in_profile) {
243 $tabs = Profile::getTabs($a, true);
249 $ignored = ((x($_REQUEST, 'ignored')) ? intval($_REQUEST['ignored']) : 0);
252 if ($a->argc > 2 && $a->argv[1] == 'event') {
254 $event_id = intval($a->argv[2]);
256 if ($a->argc > 2 && $a->argv[1] == 'drop') {
258 $event_id = intval($a->argv[2]);
260 if ($a->argc > 2 && $a->argv[1] == 'copy') {
262 $event_id = intval($a->argv[2]);
264 if ($a->argv[1] === 'new') {
268 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
270 $y = intval($a->argv[1]);
271 $m = intval($a->argv[2]);
275 // The view mode part is similiar to /mod/cal.php
276 if ($mode == 'view') {
278 $thisyear = DateTimeFormat::localNow('Y');
279 $thismonth = DateTimeFormat::localNow('m');
281 $y = intval($thisyear);
284 $m = intval($thismonth);
287 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
288 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
299 if ($nextmonth > 12) {
312 $dim = Temporal::getDaysInMonth($y, $m);
313 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
314 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
316 if ($a->argc > 1 && $a->argv[1] === 'json') {
317 if (x($_GET, 'start')) {
318 $start = $_GET['start'];
320 if (x($_GET, 'end')) {
321 $finish = $_GET['end'];
325 $start = DateTimeFormat::utc($start);
326 $finish = DateTimeFormat::utc($finish);
328 $adjust_start = DateTimeFormat::local($start);
329 $adjust_finish = DateTimeFormat::local($finish);
331 // put the event parametes in an array so we can better transmit them
333 'event_id' => (x($_GET, 'id') ? $_GET['id'] : 0),
336 'adjust_start' => $adjust_start,
337 'adjust_finish' => $adjust_finish,
338 'ignored' => $ignored,
341 // get events by id or by date
342 if (x($_GET, 'id')) {
343 $r = event_by_id(local_user(), $event_params);
345 $r = events_by_date(local_user(), $event_params);
350 if (DBM::is_result($r)) {
351 $r = sort_by_date($r);
352 foreach ($r as $rr) {
353 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
354 if (! x($links,$j)) {
355 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
362 // transform the event in a usable array
363 if (DBM::is_result($r)) {
364 $r = sort_by_date($r);
365 $events = process_events($r);
368 if ($a->argc > 1 && $a->argv[1] === 'json'){
369 echo json_encode($events);
373 if (x($_GET, 'id')) {
374 $tpl = get_markup_template("event.tpl");
376 $tpl = get_markup_template("events_js.tpl");
379 // Get rid of dashes in key names, Smarty3 can't handle them
380 foreach ($events as $key => $event) {
382 foreach ($event['item'] as $k => $v) {
383 $k = str_replace('-' ,'_', $k);
384 $event_item[$k] = $v;
386 $events[$key]['item'] = $event_item;
389 $o = replace_macros($tpl, [
390 '$baseurl' => System::baseUrl(),
392 '$title' => L10n::t('Events'),
393 '$view' => L10n::t('View'),
394 '$new_event' => [System::baseUrl() . '/events/new', L10n::t('Create New Event'), '', ''],
395 '$previous' => [System::baseUrl() . '/events/$prevyear/$prevmonth', L10n::t('Previous'), '', ''],
396 '$next' => [System::baseUrl() . '/events/$nextyear/$nextmonth', L10n::t('Next'), '', ''],
397 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
399 '$events' => $events,
401 '$today' => L10n::t('today'),
402 '$month' => L10n::t('month'),
403 '$week' => L10n::t('week'),
404 '$day' => L10n::t('day'),
405 '$list' => L10n::t('list'),
408 if (x($_GET, 'id')) {
416 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
417 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
421 if (DBM::is_result($r)) {
426 // Passed parameters overrides anything found in the DB
427 if (in_array($mode, ['edit', 'new', 'copy'])) {
428 if (!x($orig_event)) {$orig_event = [];}
429 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
430 if (x($_REQUEST, 'nofinish')) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
431 if (x($_REQUEST, 'adjust')) {$orig_event['adjust'] = $_REQUEST['adjust'];}
432 if (x($_REQUEST, 'summary')) {$orig_event['summary'] = $_REQUEST['summary'];}
433 if (x($_REQUEST, 'description')) {$orig_event['description'] = $_REQUEST['description'];}
434 if (x($_REQUEST, 'location')) {$orig_event['location'] = $_REQUEST['location'];}
435 if (x($_REQUEST, 'start')) {$orig_event['start'] = $_REQUEST['start'];}
436 if (x($_REQUEST, 'finish')) {$orig_event['finish'] = $_REQUEST['finish'];}
438 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
439 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
441 $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
442 $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
443 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
444 $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
445 $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
446 $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
451 if (x($orig_event)) {
452 $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ');
455 if ($cid || $mode === 'edit') {
456 $sh_disabled = 'disabled="disabled"';
459 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
460 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
462 $tz = date_default_timezone_get();
463 if (x($orig_event)) {
464 $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
467 $syear = DateTimeFormat::convert($sdt, $tz, 'UTC', 'Y');
468 $smonth = DateTimeFormat::convert($sdt, $tz, 'UTC', 'm');
469 $sday = DateTimeFormat::convert($sdt, $tz, 'UTC', 'd');
471 $shour = ((x($orig_event)) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'H') : '00');
472 $sminute = ((x($orig_event)) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'i') : '00');
474 $fyear = DateTimeFormat::convert($fdt, $tz, 'UTC', 'Y');
475 $fmonth = DateTimeFormat::convert($fdt, $tz, 'UTC', 'm');
476 $fday = DateTimeFormat::convert($fdt, $tz, 'UTC', 'd');
478 $fhour = ((x($orig_event)) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'H') : '00');
479 $fminute = ((x($orig_event)) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'i') : '00');
481 require_once 'include/acl_selectors.php' ;
483 $perms = get_acl_permissions($orig_event);
485 if ($mode === 'new' || $mode === 'copy') {
486 $acl = (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user)));
489 // If we copy an old event, we need to remove the ID and URI
490 // from the original event.
491 if ($mode === 'copy') {
496 $tpl = get_markup_template('event_form.tpl');
498 $o .= replace_macros($tpl,[
499 '$post' => System::baseUrl() . '/events',
504 '$allow_cid' => json_encode($perms['allow_cid']),
505 '$allow_gid' => json_encode($perms['allow_gid']),
506 '$deny_cid' => json_encode($perms['deny_cid']),
507 '$deny_gid' => json_encode($perms['deny_gid']),
509 '$title' => L10n::t('Event details'),
510 '$desc' => L10n::t('Starting date and Title are required.'),
511 '$s_text' => L10n::t('Event Starts:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
512 '$s_dsel' => Temporal::getDateTimeField(new DateTime(), DateTime::createFromFormat('Y', $syear+5), DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"), L10n::t('Event Starts:'), 'start_text', true, true, '', '', true),
513 '$n_text' => L10n::t('Finish date/time is not known or not relevant'),
514 '$n_checked' => $n_checked,
515 '$f_text' => L10n::t('Event Finishes:'),
516 '$f_dsel' => Temporal::getDateTimeField(new DateTime(), DateTime::createFromFormat('Y', $fyear+5), DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"), L10n::t('Event Finishes:'), 'finish_text', true, true, 'start_text'),
517 '$a_text' => L10n::t('Adjust for viewer timezone'),
518 '$a_checked' => $a_checked,
519 '$d_text' => L10n::t('Description:'),
520 '$d_orig' => $d_orig,
521 '$l_text' => L10n::t('Location:'),
522 '$l_orig' => $l_orig,
523 '$t_text' => L10n::t('Title:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
524 '$t_orig' => $t_orig,
525 '$summary' => ['summary', L10n::t('Title:'), $t_orig, '', '*'],
526 '$sh_text' => L10n::t('Share this event'),
527 '$share' => ['share', L10n::t('Share this event'), $sh_checked, '', $sh_disabled],
528 '$sh_checked' => $sh_checked,
529 '$nofinish' => ['nofinish', L10n::t('Finish date/time is not known or not relevant'), $n_checked],
530 '$adjust' => ['adjust', L10n::t('Adjust for viewer timezone'), $a_checked],
531 '$preview' => L10n::t('Preview'),
533 '$submit' => L10n::t('Submit'),
534 '$basic' => L10n::t('Basic'),
535 '$advanced' => L10n::t('Advanced'),
536 '$permissions' => L10n::t('Permissions'),
543 // Remove an event from the calendar and its related items
544 if ($mode === 'drop' && $event_id) {
547 $params = ['event_id' => ($event_id)];
548 $ev = event_by_id(local_user(), $params);
550 // Delete only real events (no birthdays)
551 if (DBM::is_result($ev) && $ev[0]['type'] == 'event') {
552 $del = Item::deleteById($ev[0]['itemid']);
556 notice(L10n::t('Failed to remove event') . EOL);
558 info(L10n::t('Event removed') . EOL);
561 goaway(System::baseUrl() . '/events');