4 * @brief The events module
8 use Friendica\Content\Nav;
9 use Friendica\Content\Widget\CalendarExport;
10 use Friendica\Core\ACL;
11 use Friendica\Core\L10n;
12 use Friendica\Core\System;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBA;
15 use Friendica\Model\Event;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Temporal;
21 require_once 'include/items.php';
23 function events_init(App $a) {
28 // If it's a json request abort here because we don't
29 // need the widget data
30 if ($a->argc > 1 && $a->argv[1] === 'json') {
34 if (empty($a->page['aside'])) {
35 $a->page['aside'] = '';
38 $cal_widget = CalendarExport::getHTML();
40 $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",
126 if (DBA::isResult($c)) {
134 $str_group_allow = !empty($_POST['group_allow']) ? perms2str($_POST['group_allow']) : '';
135 $str_contact_allow = !empty($_POST['contact_allow']) ? perms2str($_POST['contact_allow']) : '';
136 $str_group_deny = !empty($_POST['group_deny']) ? perms2str($_POST['group_deny']) : '';
137 $str_contact_deny = !empty($_POST['contact_deny']) ? perms2str($_POST['contact_deny']) : '';
139 // Undo the pseudo-contact of self, since there are real contacts now
140 if (strpos($str_contact_allow, '<' . $self . '>') !== false ) {
141 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
143 // Make sure to set the `private` field as true. This is necessary to
144 // have the posts show up correctly in Diaspora if an event is created
145 // as visible only to self at first, but then edited to display to others.
146 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
147 $private_event = true;
150 // Note: do not set `private` field for self-only events. It will
151 // keep even you from seeing them!
152 $str_contact_allow = '<' . $self . '>';
153 $str_group_allow = $str_contact_deny = $str_group_deny = '';
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;
173 $datarray['id'] = $event_id;
175 if (intval($_REQUEST['preview'])) {
176 $html = Event::getHTML($datarray);
181 $item_id = Event::store($datarray);
184 Worker::add(PRIORITY_HIGH, "Notifier", "event", $item_id);
187 goaway($_SESSION['return_url']);
190 function events_content(App $a) {
193 notice(L10n::t('Permission denied.') . EOL);
198 $_SESSION['return_url'] = System::baseUrl() . '/' . $a->cmd;
201 if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
202 $r = q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
208 if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
209 $r = q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
215 if ($a->theme_events_in_profile) {
216 Nav::setSelected('home');
218 Nav::setSelected('events');
221 // get the translation strings for the callendar
222 $i18n = Event::getStrings();
224 $htpl = get_markup_template('event_head.tpl');
225 $a->page['htmlhead'] .= replace_macros($htpl, [
226 '$baseurl' => System::baseUrl(),
227 '$module_url' => '/events',
235 if ($a->theme_events_in_profile) {
236 $tabs = Profile::getTabs($a, true);
242 $ignored = (x($_REQUEST, 'ignored') ? intval($_REQUEST['ignored']) : 0);
245 if ($a->argc > 2 && $a->argv[1] == 'event') {
247 $event_id = intval($a->argv[2]);
249 if ($a->argc > 2 && $a->argv[1] == 'drop') {
251 $event_id = intval($a->argv[2]);
253 if ($a->argc > 2 && $a->argv[1] == 'copy') {
255 $event_id = intval($a->argv[2]);
257 if ($a->argv[1] === 'new') {
261 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
263 $y = intval($a->argv[1]);
264 $m = intval($a->argv[2]);
268 // The view mode part is similiar to /mod/cal.php
269 if ($mode == 'view') {
271 $thisyear = DateTimeFormat::localNow('Y');
272 $thismonth = DateTimeFormat::localNow('m');
274 $y = intval($thisyear);
277 $m = intval($thismonth);
280 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
281 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
292 if ($nextmonth > 12) {
305 $dim = Temporal::getDaysInMonth($y, $m);
306 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
307 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
309 if ($a->argc > 1 && $a->argv[1] === 'json') {
310 if (x($_GET, 'start')) {
311 $start = $_GET['start'];
313 if (x($_GET, 'end')) {
314 $finish = $_GET['end'];
318 $start = DateTimeFormat::utc($start);
319 $finish = DateTimeFormat::utc($finish);
321 $adjust_start = DateTimeFormat::local($start);
322 $adjust_finish = DateTimeFormat::local($finish);
324 // put the event parametes in an array so we can better transmit them
326 'event_id' => intval(defaults($_GET, 'id', 0)),
329 'adjust_start' => $adjust_start,
330 'adjust_finish' => $adjust_finish,
331 'ignore' => $ignored,
334 // get events by id or by date
335 if ($event_params['event_id']) {
336 $r = Event::getListById(local_user(), $event_params['event_id']);
338 $r = Event::getListByDate(local_user(), $event_params);
343 if (DBA::isResult($r)) {
344 $r = Event::sortByDate($r);
345 foreach ($r as $rr) {
346 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
348 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
355 // transform the event in a usable array
356 if (DBA::isResult($r)) {
357 $r = Event::sortByDate($r);
358 $events = Event::prepareListForTemplate($r);
361 if ($a->argc > 1 && $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) {
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, [
383 '$baseurl' => System::baseUrl(),
385 '$title' => L10n::t('Events'),
386 '$view' => L10n::t('View'),
387 '$new_event' => [System::baseUrl() . '/events/new', L10n::t('Create New Event'), '', ''],
388 '$previous' => [System::baseUrl() . '/events/$prevyear/$prevmonth', L10n::t('Previous'), '', ''],
389 '$next' => [System::baseUrl() . '/events/$nextyear/$nextmonth', L10n::t('Next'), '', ''],
390 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
392 '$events' => $events,
394 '$today' => L10n::t('today'),
395 '$month' => L10n::t('month'),
396 '$week' => L10n::t('week'),
397 '$day' => L10n::t('day'),
398 '$list' => L10n::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 (DBA::isResult($r)) {
419 // Passed parameters overrides anything found in the DB
420 if (in_array($mode, ['edit', 'new', 'copy'])) {
421 if (empty($orig_event)) {
425 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
426 if (x($_REQUEST, 'nofinish')) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
427 if (x($_REQUEST, 'adjust')) {$orig_event['adjust'] = $_REQUEST['adjust'];}
428 if (x($_REQUEST, 'summary')) {$orig_event['summary'] = $_REQUEST['summary'];}
429 if (x($_REQUEST, 'description')) {$orig_event['description'] = $_REQUEST['description'];}
430 if (x($_REQUEST, 'location')) {$orig_event['location'] = $_REQUEST['location'];}
431 if (x($_REQUEST, 'start')) {$orig_event['start'] = $_REQUEST['start'];}
432 if (x($_REQUEST, 'finish')) {$orig_event['finish'] = $_REQUEST['finish'];}
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 = DateTimeFormat::convert($sdt, $tz, 'UTC', 'Y');
465 $smonth = DateTimeFormat::convert($sdt, $tz, 'UTC', 'm');
466 $sday = DateTimeFormat::convert($sdt, $tz, 'UTC', 'd');
468 $shour = (x($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'H') : '00');
469 $sminute = (x($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'i') : '00');
471 $fyear = DateTimeFormat::convert($fdt, $tz, 'UTC', 'Y');
472 $fmonth = DateTimeFormat::convert($fdt, $tz, 'UTC', 'm');
473 $fday = DateTimeFormat::convert($fdt, $tz, 'UTC', 'd');
475 $fhour = (x($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'H') : '00');
476 $fminute = (x($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'i') : '00');
478 $perms = ACL::getDefaultUserPermissions($orig_event);
480 if ($mode === 'new' || $mode === 'copy') {
481 $acl = ($cid ? '' : ACL::getFullSelectorHTML($a->user, false, $orig_event));
484 // If we copy an old event, we need to remove the ID and URI
485 // from the original event.
486 if ($mode === 'copy') {
491 $tpl = get_markup_template('event_form.tpl');
493 $o .= replace_macros($tpl,[
494 '$post' => System::baseUrl() . '/events',
499 '$allow_cid' => json_encode($perms['allow_cid']),
500 '$allow_gid' => json_encode($perms['allow_gid']),
501 '$deny_cid' => json_encode($perms['deny_cid']),
502 '$deny_gid' => json_encode($perms['deny_gid']),
504 '$title' => L10n::t('Event details'),
505 '$desc' => L10n::t('Starting date and Title are required.'),
506 '$s_text' => L10n::t('Event Starts:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
507 '$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),
508 '$n_text' => L10n::t('Finish date/time is not known or not relevant'),
509 '$n_checked' => $n_checked,
510 '$f_text' => L10n::t('Event Finishes:'),
511 '$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'),
512 '$a_text' => L10n::t('Adjust for viewer timezone'),
513 '$a_checked' => $a_checked,
514 '$d_text' => L10n::t('Description:'),
515 '$d_orig' => $d_orig,
516 '$l_text' => L10n::t('Location:'),
517 '$l_orig' => $l_orig,
518 '$t_text' => L10n::t('Title:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
519 '$t_orig' => $t_orig,
520 '$summary' => ['summary', L10n::t('Title:'), $t_orig, '', '*'],
521 '$sh_text' => L10n::t('Share this event'),
522 '$share' => ['share', L10n::t('Share this event'), $sh_checked, '', $sh_disabled],
523 '$sh_checked' => $sh_checked,
524 '$nofinish' => ['nofinish', L10n::t('Finish date/time is not known or not relevant'), $n_checked],
525 '$adjust' => ['adjust', L10n::t('Adjust for viewer timezone'), $a_checked],
526 '$preview' => L10n::t('Preview'),
528 '$submit' => L10n::t('Submit'),
529 '$basic' => L10n::t('Basic'),
530 '$advanced' => L10n::t('Advanced'),
531 '$permissions' => L10n::t('Permissions'),
538 // Remove an event from the calendar and its related items
539 if ($mode === 'drop' && $event_id) {
540 $ev = Event::getListById(local_user(), $event_id);
542 // Delete only real events (no birthdays)
543 if (DBA::isResult($ev) && $ev[0]['type'] == 'event') {
544 Item::deleteForUser(['id' => $ev[0]['itemid']], local_user());
547 if (Item::exists(['id' => $ev[0]['itemid']])) {
548 notice(L10n::t('Failed to remove event') . EOL);
550 info(L10n::t('Event removed') . EOL);
553 goaway(System::baseUrl() . '/events');