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\Theme;
16 use Friendica\Core\Worker;
17 use Friendica\Database\DBA;
18 use Friendica\Model\Event;
19 use Friendica\Model\Item;
20 use Friendica\Model\Profile;
21 use Friendica\Module\Login;
22 use Friendica\Module\Security\Login;
23 use Friendica\Util\ACLFormatter;
24 use Friendica\Util\DateTimeFormat;
25 use Friendica\Util\Strings;
26 use Friendica\Util\Temporal;
27 use Friendica\Worker\Delivery;
29 function events_init(App $a)
35 // If it's a json request abort here because we don't
36 // need the widget data
37 if ($a->argc > 1 && $a->argv[1] === 'json') {
41 if (empty($a->page['aside'])) {
42 $a->page['aside'] = '';
45 $cal_widget = CalendarExport::getHTML();
47 $a->page['aside'] .= $cal_widget;
52 function events_post(App $a)
55 Logger::log('post: ' . print_r($_REQUEST, true), Logger::DATA);
61 $event_id = !empty($_POST['event_id']) ? intval($_POST['event_id']) : 0;
62 $cid = !empty($_POST['cid']) ? intval($_POST['cid']) : 0;
65 $start_text = Strings::escapeHtml($_REQUEST['start_text'] ?? '');
66 $finish_text = Strings::escapeHtml($_REQUEST['finish_text'] ?? '');
68 $adjust = intval($_POST['adjust'] ?? 0);
69 $nofinish = intval($_POST['nofinish'] ?? 0);
71 // The default setting for the `private` field in event_store() is false, so mirror that
72 $private_event = false;
74 $start = DBA::NULL_DATETIME;
75 $finish = DBA::NULL_DATETIME;
82 $finish = $finish_text;
86 $start = DateTimeFormat::convert($start, 'UTC', date_default_timezone_get());
88 $finish = DateTimeFormat::convert($finish, 'UTC', date_default_timezone_get());
91 $start = DateTimeFormat::utc($start);
93 $finish = DateTimeFormat::utc($finish);
97 // Don't allow the event to finish before it begins.
98 // It won't hurt anything, but somebody will file a bug report
99 // and we'll waste a bunch of time responding to it. Time that
100 // could've been spent doing something else.
102 $summary = trim($_POST['summary'] ?? '');
103 $desc = trim($_POST['desc'] ?? '');
104 $location = trim($_POST['location'] ?? '');
108 'summary' => $summary,
109 'description' => $desc,
110 'location' => $location,
111 'start' => $start_text,
112 'finish' => $finish_text,
114 'nofinish' => $nofinish,
117 $action = ($event_id == '') ? 'new' : 'event/' . $event_id;
118 $onerror_path = 'events/' . $action . '?' . http_build_query($params, null, null, PHP_QUERY_RFC3986);
120 if (strcmp($finish, $start) < 0 && !$nofinish) {
121 notice(L10n::t('Event can not end before it has started.') . EOL);
122 if (intval($_REQUEST['preview'])) {
123 echo L10n::t('Event can not end before it has started.');
126 $a->internalRedirect($onerror_path);
129 if (!$summary || ($start === DBA::NULL_DATETIME)) {
130 notice(L10n::t('Event title and start time are required.') . EOL);
131 if (intval($_REQUEST['preview'])) {
132 echo L10n::t('Event title and start time are required.');
135 $a->internalRedirect($onerror_path);
138 $share = intval($_POST['share'] ?? 0);
140 $c = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
144 if (DBA::isResult($c)) {
153 $aclFormatter = \Friendica\DI::aclFormatter();
155 $str_group_allow = $aclFormatter->toString($_POST['group_allow'] ?? '');
156 $str_contact_allow = $aclFormatter->toString($_POST['contact_allow'] ?? '');
157 $str_group_deny = $aclFormatter->toString($_POST['group_deny'] ?? '');
158 $str_contact_deny = $aclFormatter->toString($_POST['contact_deny'] ?? '');
160 // Undo the pseudo-contact of self, since there are real contacts now
161 if (strpos($str_contact_allow, '<' . $self . '>') !== false) {
162 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
164 // Make sure to set the `private` field as true. This is necessary to
165 // have the posts show up correctly in Diaspora if an event is created
166 // as visible only to self at first, but then edited to display to others.
167 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
168 $private_event = true;
171 // Note: do not set `private` field for self-only events. It will
172 // keep even you from seeing them!
173 $str_contact_allow = '<' . $self . '>';
174 $str_group_allow = $str_contact_deny = $str_group_deny = '';
179 $datarray['start'] = $start;
180 $datarray['finish'] = $finish;
181 $datarray['summary'] = $summary;
182 $datarray['desc'] = $desc;
183 $datarray['location'] = $location;
184 $datarray['type'] = $type;
185 $datarray['adjust'] = $adjust;
186 $datarray['nofinish'] = $nofinish;
187 $datarray['uid'] = $uid;
188 $datarray['cid'] = $cid;
189 $datarray['allow_cid'] = $str_contact_allow;
190 $datarray['allow_gid'] = $str_group_allow;
191 $datarray['deny_cid'] = $str_contact_deny;
192 $datarray['deny_gid'] = $str_group_deny;
193 $datarray['private'] = $private_event;
194 $datarray['id'] = $event_id;
196 if (intval($_REQUEST['preview'])) {
197 $html = Event::getHTML($datarray);
202 $item_id = Event::store($datarray);
205 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $item_id);
208 $a->internalRedirect('events');
211 function events_content(App $a)
214 notice(L10n::t('Permission denied.') . EOL);
215 return Login::form();
219 $_SESSION['return_path'] = $a->cmd;
222 if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
223 q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
229 if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
230 q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
236 if ($a->theme_events_in_profile) {
237 Nav::setSelected('home');
239 Nav::setSelected('events');
242 // get the translation strings for the callendar
243 $i18n = Event::getStrings();
245 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
246 $a->page['htmlhead'] .= Renderer::replaceMacros($htpl, [
247 '$module_url' => '/events',
255 if ($a->theme_events_in_profile) {
256 $tabs = Profile::getTabs($a, 'events', true);
262 $ignored = !empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0;
265 if ($a->argc > 2 && $a->argv[1] == 'event') {
267 $event_id = intval($a->argv[2]);
269 if ($a->argc > 2 && $a->argv[1] == 'drop') {
271 $event_id = intval($a->argv[2]);
273 if ($a->argc > 2 && $a->argv[1] == 'copy') {
275 $event_id = intval($a->argv[2]);
277 if ($a->argv[1] === 'new') {
281 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
283 $y = intval($a->argv[1]);
284 $m = intval($a->argv[2]);
288 // The view mode part is similiar to /mod/cal.php
289 if ($mode == 'view') {
290 $thisyear = DateTimeFormat::localNow('Y');
291 $thismonth = DateTimeFormat::localNow('m');
293 $y = intval($thisyear);
296 $m = intval($thismonth);
299 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
300 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
309 $dim = Temporal::getDaysInMonth($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 (!empty($_GET['start'])) {
315 $start = $_GET['start'];
317 if (!empty($_GET['end'])) {
318 $finish = $_GET['end'];
322 $start = DateTimeFormat::utc($start);
323 $finish = DateTimeFormat::utc($finish);
325 $adjust_start = DateTimeFormat::local($start);
326 $adjust_finish = DateTimeFormat::local($finish);
328 // put the event parametes in an array so we can better transmit them
330 'event_id' => intval($_GET['id'] ?? 0),
333 'adjust_start' => $adjust_start,
334 'adjust_finish' => $adjust_finish,
335 'ignore' => $ignored,
338 // get events by id or by date
339 if ($event_params['event_id']) {
340 $r = Event::getListById(local_user(), $event_params['event_id']);
342 $r = Event::getListByDate(local_user(), $event_params);
347 if (DBA::isResult($r)) {
348 $r = Event::sortByDate($r);
349 foreach ($r as $rr) {
350 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
351 if (empty($links[$j])) {
352 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
359 // transform the event in a usable array
360 if (DBA::isResult($r)) {
361 $r = Event::sortByDate($r);
362 $events = Event::prepareListForTemplate($r);
365 if ($a->argc > 1 && $a->argv[1] === 'json') {
366 header('Content-Type: application/json');
367 echo json_encode($events);
371 if (!empty($_GET['id'])) {
372 $tpl = Renderer::getMarkupTemplate("event.tpl");
374 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
377 // Get rid of dashes in key names, Smarty3 can't handle them
378 foreach ($events as $key => $event) {
380 foreach ($event['item'] as $k => $v) {
381 $k = str_replace('-', '_', $k);
382 $event_item[$k] = $v;
384 $events[$key]['item'] = $event_item;
387 // ACL blocks are loaded in modals in frio
388 $a->page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
389 $a->page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
390 $a->page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
391 $a->page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
393 $o = Renderer::replaceMacros($tpl, [
395 '$title' => L10n::t('Events'),
396 '$view' => L10n::t('View'),
397 '$new_event' => [System::baseUrl() . '/events/new', L10n::t('Create New Event'), '', ''],
398 '$previous' => [System::baseUrl() . '/events/$prevyear/$prevmonth', L10n::t('Previous'), '', ''],
399 '$next' => [System::baseUrl() . '/events/$nextyear/$nextmonth', L10n::t('Next'), '', ''],
400 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
402 '$events' => $events,
404 '$today' => L10n::t('today'),
405 '$month' => L10n::t('month'),
406 '$week' => L10n::t('week'),
407 '$day' => L10n::t('day'),
408 '$list' => L10n::t('list'),
411 if (!empty($_GET['id'])) {
419 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
420 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
424 if (DBA::isResult($r)) {
429 // Passed parameters overrides anything found in the DB
430 if (in_array($mode, ['edit', 'new', 'copy'])) {
431 if (empty($orig_event)) {
435 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
436 if (!empty($_REQUEST['nofinish'])) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
437 if (!empty($_REQUEST['adjust'])) {$orig_event['adjust'] = $_REQUEST['adjust'];}
438 if (!empty($_REQUEST['summary'])) {$orig_event['summary'] = $_REQUEST['summary'];}
439 if (!empty($_REQUEST['desc'])) {$orig_event['desc'] = $_REQUEST['desc'];}
440 if (!empty($_REQUEST['location'])) {$orig_event['location'] = $_REQUEST['location'];}
441 if (!empty($_REQUEST['start'])) {$orig_event['start'] = $_REQUEST['start'];}
442 if (!empty($_REQUEST['finish'])) {$orig_event['finish'] = $_REQUEST['finish'];}
444 $n_checked = (!empty($orig_event['nofinish']) ? ' checked="checked" ' : '');
445 $a_checked = (!empty($orig_event['adjust']) ? ' checked="checked" ' : '');
447 $t_orig = $orig_event['summary'] ?? '';
448 $d_orig = $orig_event['desc'] ?? '';
449 $l_orig = $orig_event['location'] ?? '';
450 $eid = !empty($orig_event) ? $orig_event['id'] : 0;
451 $cid = !empty($orig_event) ? $orig_event['cid'] : 0;
452 $uri = !empty($orig_event) ? $orig_event['uri'] : '';
457 if (!empty($orig_event)
458 && ($orig_event['allow_cid'] !== '<' . local_user() . '>'
459 || $orig_event['allow_gid']
460 || $orig_event['deny_cid']
461 || $orig_event['deny_gid']))
463 $sh_checked = ' checked="checked" ';
466 if ($cid || $mode === 'edit') {
467 $sh_disabled = 'disabled="disabled"';
470 $sdt = !empty($orig_event) ? $orig_event['start'] : 'now';
471 $fdt = !empty($orig_event) ? $orig_event['finish'] : 'now';
473 $tz = date_default_timezone_get();
474 if (!empty($orig_event)) {
475 $tz = ($orig_event['adjust'] ? date_default_timezone_get() : 'UTC');
478 $syear = DateTimeFormat::convert($sdt, $tz, 'UTC', 'Y');
479 $smonth = DateTimeFormat::convert($sdt, $tz, 'UTC', 'm');
480 $sday = DateTimeFormat::convert($sdt, $tz, 'UTC', 'd');
482 $shour = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'H') : '00';
483 $sminute = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'i') : '00';
485 $fyear = DateTimeFormat::convert($fdt, $tz, 'UTC', 'Y');
486 $fmonth = DateTimeFormat::convert($fdt, $tz, 'UTC', 'm');
487 $fday = DateTimeFormat::convert($fdt, $tz, 'UTC', 'd');
489 $fhour = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'H') : '00';
490 $fminute = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'i') : '00';
492 if (!$cid && in_array($mode, ['new', 'copy'])) {
493 $acl = ACL::getFullSelectorHTML($a->page, $a->user, false, ACL::getDefaultUserPermissions($orig_event));
498 // If we copy an old event, we need to remove the ID and URI
499 // from the original event.
500 if ($mode === 'copy') {
505 $tpl = Renderer::getMarkupTemplate('event_form.tpl');
507 $o .= Renderer::replaceMacros($tpl, [
508 '$post' => System::baseUrl() . '/events',
513 '$title' => L10n::t('Event details'),
514 '$desc' => L10n::t('Starting date and Title are required.'),
515 '$s_text' => L10n::t('Event Starts:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
516 '$s_dsel' => Temporal::getDateTimeField(
518 DateTime::createFromFormat('Y', intval($syear) + 5),
519 DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"),
520 L10n::t('Event Starts:'),
528 '$n_text' => L10n::t('Finish date/time is not known or not relevant'),
529 '$n_checked' => $n_checked,
530 '$f_text' => L10n::t('Event Finishes:'),
531 '$f_dsel' => Temporal::getDateTimeField(
533 DateTime::createFromFormat('Y', intval($fyear) + 5),
534 DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"),
535 L10n::t('Event Finishes:'),
541 '$a_text' => L10n::t('Adjust for viewer timezone'),
542 '$a_checked' => $a_checked,
543 '$d_text' => L10n::t('Description:'),
544 '$d_orig' => $d_orig,
545 '$l_text' => L10n::t('Location:'),
546 '$l_orig' => $l_orig,
547 '$t_text' => L10n::t('Title:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
548 '$t_orig' => $t_orig,
549 '$summary' => ['summary', L10n::t('Title:'), $t_orig, '', '*'],
550 '$sh_text' => L10n::t('Share this event'),
551 '$share' => ['share', L10n::t('Share this event'), $sh_checked, '', $sh_disabled],
552 '$sh_checked' => $sh_checked,
553 '$nofinish' => ['nofinish', L10n::t('Finish date/time is not known or not relevant'), $n_checked],
554 '$adjust' => ['adjust', L10n::t('Adjust for viewer timezone'), $a_checked],
555 '$preview' => L10n::t('Preview'),
557 '$submit' => L10n::t('Submit'),
558 '$basic' => L10n::t('Basic'),
559 '$advanced' => L10n::t('Advanced'),
560 '$permissions' => L10n::t('Permissions'),
566 // Remove an event from the calendar and its related items
567 if ($mode === 'drop' && $event_id) {
568 $ev = Event::getListById(local_user(), $event_id);
570 // Delete only real events (no birthdays)
571 if (DBA::isResult($ev) && $ev[0]['type'] == 'event') {
572 Item::deleteForUser(['id' => $ev[0]['itemid']], local_user());
575 if (Item::exists(['id' => $ev[0]['itemid']])) {
576 notice(L10n::t('Failed to remove event') . EOL);
578 info(L10n::t('Event removed') . EOL);
581 $a->internalRedirect('events');