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\Logger;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\System;
15 use Friendica\Core\Worker;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Event;
18 use Friendica\Model\Item;
19 use Friendica\Model\Profile;
20 use Friendica\Module\Login;
21 use Friendica\Util\DateTimeFormat;
22 use Friendica\Util\Strings;
23 use Friendica\Util\Temporal;
25 function events_init(App $a)
31 // If it's a json request abort here because we don't
32 // need the widget data
33 if ($a->argc > 1 && $a->argv[1] === 'json') {
37 if (empty($a->page['aside'])) {
38 $a->page['aside'] = '';
41 $cal_widget = CalendarExport::getHTML();
43 $a->page['aside'] .= $cal_widget;
48 function events_post(App $a)
51 Logger::log('post: ' . print_r($_REQUEST, true), Logger::DATA);
57 $event_id = !empty($_POST['event_id']) ? intval($_POST['event_id']) : 0;
58 $cid = !empty($_POST['cid']) ? intval($_POST['cid']) : 0;
61 $start_text = Strings::escapeHtml(defaults($_REQUEST, 'start_text', ''));
62 $finish_text = Strings::escapeHtml(defaults($_REQUEST, 'finish_text', ''));
64 $adjust = intval(defaults($_POST, 'adjust', 0));
65 $nofinish = intval(defaults($_POST, 'nofinish', 0));
67 // The default setting for the `private` field in event_store() is false, so mirror that
68 $private_event = false;
70 $start = DBA::NULL_DATETIME;
71 $finish = DBA::NULL_DATETIME;
78 $finish = $finish_text;
82 $start = DateTimeFormat::convert($start, 'UTC', date_default_timezone_get());
84 $finish = DateTimeFormat::convert($finish, 'UTC', date_default_timezone_get());
87 $start = DateTimeFormat::utc($start);
89 $finish = DateTimeFormat::utc($finish);
93 // Don't allow the event to finish before it begins.
94 // It won't hurt anything, but somebody will file a bug report
95 // and we'll waste a bunch of time responding to it. Time that
96 // could've been spent doing something else.
98 $summary = trim(defaults($_POST, 'summary' , ''));
99 $desc = trim(defaults($_POST, 'desc' , ''));
100 $location = trim(defaults($_POST, 'location', ''));
104 'summary' => $summary,
105 'description' => $desc,
106 'location' => $location,
107 'start' => $start_text,
108 'finish' => $finish_text,
110 'nofinish' => $nofinish,
113 $action = ($event_id == '') ? 'new' : 'event/' . $event_id;
114 $onerror_path = 'events/' . $action . '?' . http_build_query($params, null, null, PHP_QUERY_RFC3986);
116 if (strcmp($finish, $start) < 0 && !$nofinish) {
117 notice(L10n::t('Event can not end before it has started.') . EOL);
118 if (intval($_REQUEST['preview'])) {
119 echo L10n::t('Event can not end before it has started.');
122 $a->internalRedirect($onerror_path);
125 if (!$summary || ($start === DBA::NULL_DATETIME)) {
126 notice(L10n::t('Event title and start time are required.') . EOL);
127 if (intval($_REQUEST['preview'])) {
128 echo L10n::t('Event title and start time are required.');
131 $a->internalRedirect($onerror_path);
134 $share = intval(defaults($_POST, 'share', 0));
136 $c = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
140 if (DBA::isResult($c)) {
148 $str_group_allow = perms2str(defaults($_POST, 'group_allow' , ''));
149 $str_contact_allow = perms2str(defaults($_POST, 'contact_allow', ''));
150 $str_group_deny = perms2str(defaults($_POST, 'group_deny' , ''));
151 $str_contact_deny = perms2str(defaults($_POST, 'contact_deny' , ''));
153 // Undo the pseudo-contact of self, since there are real contacts now
154 if (strpos($str_contact_allow, '<' . $self . '>') !== false) {
155 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
157 // Make sure to set the `private` field as true. This is necessary to
158 // have the posts show up correctly in Diaspora if an event is created
159 // as visible only to self at first, but then edited to display to others.
160 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
161 $private_event = true;
164 // Note: do not set `private` field for self-only events. It will
165 // keep even you from seeing them!
166 $str_contact_allow = '<' . $self . '>';
167 $str_group_allow = $str_contact_deny = $str_group_deny = '';
172 $datarray['start'] = $start;
173 $datarray['finish'] = $finish;
174 $datarray['summary'] = $summary;
175 $datarray['desc'] = $desc;
176 $datarray['location'] = $location;
177 $datarray['type'] = $type;
178 $datarray['adjust'] = $adjust;
179 $datarray['nofinish'] = $nofinish;
180 $datarray['uid'] = $uid;
181 $datarray['cid'] = $cid;
182 $datarray['allow_cid'] = $str_contact_allow;
183 $datarray['allow_gid'] = $str_group_allow;
184 $datarray['deny_cid'] = $str_contact_deny;
185 $datarray['deny_gid'] = $str_group_deny;
186 $datarray['private'] = $private_event;
187 $datarray['id'] = $event_id;
189 if (intval($_REQUEST['preview'])) {
190 $html = Event::getHTML($datarray);
195 $item_id = Event::store($datarray);
198 Worker::add(PRIORITY_HIGH, "Notifier", "event", $item_id);
201 $a->internalRedirect('events');
204 function events_content(App $a)
207 notice(L10n::t('Permission denied.') . EOL);
208 return Login::form();
212 $_SESSION['return_path'] = $a->cmd;
215 if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
216 q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
222 if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
223 q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
229 if ($a->theme_events_in_profile) {
230 Nav::setSelected('home');
232 Nav::setSelected('events');
235 // get the translation strings for the callendar
236 $i18n = Event::getStrings();
238 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
239 $a->page['htmlhead'] .= Renderer::replaceMacros($htpl, [
240 '$module_url' => '/events',
248 if ($a->theme_events_in_profile) {
249 $tabs = Profile::getTabs($a, true);
255 $ignored = !empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0;
258 if ($a->argc > 2 && $a->argv[1] == 'event') {
260 $event_id = intval($a->argv[2]);
262 if ($a->argc > 2 && $a->argv[1] == 'drop') {
264 $event_id = intval($a->argv[2]);
266 if ($a->argc > 2 && $a->argv[1] == 'copy') {
268 $event_id = intval($a->argv[2]);
270 if ($a->argv[1] === 'new') {
274 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
276 $y = intval($a->argv[1]);
277 $m = intval($a->argv[2]);
281 // The view mode part is similiar to /mod/cal.php
282 if ($mode == 'view') {
283 $thisyear = DateTimeFormat::localNow('Y');
284 $thismonth = DateTimeFormat::localNow('m');
286 $y = intval($thisyear);
289 $m = intval($thismonth);
292 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
293 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
302 $dim = Temporal::getDaysInMonth($y, $m);
303 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
304 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
306 if ($a->argc > 1 && $a->argv[1] === 'json') {
307 if (!empty($_GET['start'])) {
308 $start = $_GET['start'];
310 if (!empty($_GET['end'])) {
311 $finish = $_GET['end'];
315 $start = DateTimeFormat::utc($start);
316 $finish = DateTimeFormat::utc($finish);
318 $adjust_start = DateTimeFormat::local($start);
319 $adjust_finish = DateTimeFormat::local($finish);
321 // put the event parametes in an array so we can better transmit them
323 'event_id' => intval(defaults($_GET, 'id', 0)),
326 'adjust_start' => $adjust_start,
327 'adjust_finish' => $adjust_finish,
328 'ignore' => $ignored,
331 // get events by id or by date
332 if ($event_params['event_id']) {
333 $r = Event::getListById(local_user(), $event_params['event_id']);
335 $r = Event::getListByDate(local_user(), $event_params);
340 if (DBA::isResult($r)) {
341 $r = Event::sortByDate($r);
342 foreach ($r as $rr) {
343 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
344 if (empty($links[$j])) {
345 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
352 // transform the event in a usable array
353 if (DBA::isResult($r)) {
354 $r = Event::sortByDate($r);
355 $events = Event::prepareListForTemplate($r);
358 if ($a->argc > 1 && $a->argv[1] === 'json') {
359 header('Content-Type: application/json');
360 echo json_encode($events);
364 if (!empty($_GET['id'])) {
365 $tpl = Renderer::getMarkupTemplate("event.tpl");
367 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
370 // Get rid of dashes in key names, Smarty3 can't handle them
371 foreach ($events as $key => $event) {
373 foreach ($event['item'] as $k => $v) {
374 $k = str_replace('-', '_', $k);
375 $event_item[$k] = $v;
377 $events[$key]['item'] = $event_item;
380 $o = Renderer::replaceMacros($tpl, [
382 '$title' => L10n::t('Events'),
383 '$view' => L10n::t('View'),
384 '$new_event' => [System::baseUrl() . '/events/new', L10n::t('Create New Event'), '', ''],
385 '$previous' => [System::baseUrl() . '/events/$prevyear/$prevmonth', L10n::t('Previous'), '', ''],
386 '$next' => [System::baseUrl() . '/events/$nextyear/$nextmonth', L10n::t('Next'), '', ''],
387 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
389 '$events' => $events,
391 '$today' => L10n::t('today'),
392 '$month' => L10n::t('month'),
393 '$week' => L10n::t('week'),
394 '$day' => L10n::t('day'),
395 '$list' => L10n::t('list'),
398 if (!empty($_GET['id'])) {
406 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
407 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
411 if (DBA::isResult($r)) {
416 // Passed parameters overrides anything found in the DB
417 if (in_array($mode, ['edit', 'new', 'copy'])) {
418 if (empty($orig_event)) {
422 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
423 if (!empty($_REQUEST['nofinish'])) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
424 if (!empty($_REQUEST['adjust'])) {$orig_event['adjust'] = $_REQUEST['adjust'];}
425 if (!empty($_REQUEST['summary'])) {$orig_event['summary'] = $_REQUEST['summary'];}
426 if (!empty($_REQUEST['description'])) {$orig_event['description'] = $_REQUEST['description'];}
427 if (!empty($_REQUEST['location'])) {$orig_event['location'] = $_REQUEST['location'];}
428 if (!empty($_REQUEST['start'])) {$orig_event['start'] = $_REQUEST['start'];}
429 if (!empty($_REQUEST['finish'])) {$orig_event['finish'] = $_REQUEST['finish'];}
431 $n_checked = (!empty($orig_event['nofinish']) ? ' checked="checked" ' : '');
432 $a_checked = (!empty($orig_event['adjust']) ? ' checked="checked" ' : '');
434 $t_orig = !empty($orig_event) ? $orig_event['summary'] : '';
435 $d_orig = !empty($orig_event) ? $orig_event['desc'] : '';
436 $l_orig = !empty($orig_event) ? $orig_event['location'] : '';
437 $eid = !empty($orig_event) ? $orig_event['id'] : 0;
438 $cid = !empty($orig_event) ? $orig_event['cid'] : 0;
439 $uri = !empty($orig_event) ? $orig_event['uri'] : '';
444 if (!empty($orig_event)
445 && ($orig_event['allow_cid'] !== '<' . local_user() . '>'
446 || $orig_event['allow_gid']
447 || $orig_event['deny_cid']
448 || $orig_event['deny_gid']))
450 $sh_checked = ' checked="checked" ';
453 if ($cid || $mode === 'edit') {
454 $sh_disabled = 'disabled="disabled"';
457 $sdt = !empty($orig_event) ? $orig_event['start'] : 'now';
458 $fdt = !empty($orig_event) ? $orig_event['finish'] : 'now';
460 $tz = date_default_timezone_get();
461 if (!empty($orig_event)) {
462 $tz = ($orig_event['adjust'] ? date_default_timezone_get() : 'UTC');
465 $syear = DateTimeFormat::convert($sdt, $tz, 'UTC', 'Y');
466 $smonth = DateTimeFormat::convert($sdt, $tz, 'UTC', 'm');
467 $sday = DateTimeFormat::convert($sdt, $tz, 'UTC', 'd');
469 $shour = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'H') : '00';
470 $sminute = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'i') : '00';
472 $fyear = DateTimeFormat::convert($fdt, $tz, 'UTC', 'Y');
473 $fmonth = DateTimeFormat::convert($fdt, $tz, 'UTC', 'm');
474 $fday = DateTimeFormat::convert($fdt, $tz, 'UTC', 'd');
476 $fhour = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'H') : '00';
477 $fminute = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'i') : '00';
479 $perms = ACL::getDefaultUserPermissions($orig_event);
481 if (!$cid && in_array($mode, ['new', 'copy'])) {
482 $acl = ACL::getFullSelectorHTML($a->user, false, $orig_event);
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 = Renderer::getMarkupTemplate('event_form.tpl');
496 $o .= Renderer::replaceMacros($tpl, [
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' => L10n::t('Event details'),
508 '$desc' => L10n::t('Starting date and Title are required.'),
509 '$s_text' => L10n::t('Event Starts:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
510 '$s_dsel' => Temporal::getDateTimeField(
512 DateTime::createFromFormat('Y', intval($syear) + 5),
513 DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"),
514 L10n::t('Event Starts:'),
522 '$n_text' => L10n::t('Finish date/time is not known or not relevant'),
523 '$n_checked' => $n_checked,
524 '$f_text' => L10n::t('Event Finishes:'),
525 '$f_dsel' => Temporal::getDateTimeField(
527 DateTime::createFromFormat('Y', intval($fyear) + 5),
528 DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"),
529 L10n::t('Event Finishes:'),
535 '$a_text' => L10n::t('Adjust for viewer timezone'),
536 '$a_checked' => $a_checked,
537 '$d_text' => L10n::t('Description:'),
538 '$d_orig' => $d_orig,
539 '$l_text' => L10n::t('Location:'),
540 '$l_orig' => $l_orig,
541 '$t_text' => L10n::t('Title:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
542 '$t_orig' => $t_orig,
543 '$summary' => ['summary', L10n::t('Title:'), $t_orig, '', '*'],
544 '$sh_text' => L10n::t('Share this event'),
545 '$share' => ['share', L10n::t('Share this event'), $sh_checked, '', $sh_disabled],
546 '$sh_checked' => $sh_checked,
547 '$nofinish' => ['nofinish', L10n::t('Finish date/time is not known or not relevant'), $n_checked],
548 '$adjust' => ['adjust', L10n::t('Adjust for viewer timezone'), $a_checked],
549 '$preview' => L10n::t('Preview'),
551 '$submit' => L10n::t('Submit'),
552 '$basic' => L10n::t('Basic'),
553 '$advanced' => L10n::t('Advanced'),
554 '$permissions' => L10n::t('Permissions'),
560 // Remove an event from the calendar and its related items
561 if ($mode === 'drop' && $event_id) {
562 $ev = Event::getListById(local_user(), $event_id);
564 // Delete only real events (no birthdays)
565 if (DBA::isResult($ev) && $ev[0]['type'] == 'event') {
566 Item::deleteForUser(['id' => $ev[0]['itemid']], local_user());
569 if (Item::exists(['id' => $ev[0]['itemid']])) {
570 notice(L10n::t('Failed to remove event') . EOL);
572 info(L10n::t('Event removed') . EOL);
575 $a->internalRedirect('events');