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\Theme;
15 use Friendica\Core\Worker;
16 use Friendica\Database\DBA;
18 use Friendica\Model\Event;
19 use Friendica\Model\Item;
20 use Friendica\Model\Profile;
21 use Friendica\Module\Security\Login;
22 use Friendica\Util\DateTimeFormat;
23 use Friendica\Util\Strings;
24 use Friendica\Util\Temporal;
25 use Friendica\Worker\Delivery;
27 function events_init(App $a)
33 // If it's a json request abort here because we don't
34 // need the widget data
35 if ($a->argc > 1 && $a->argv[1] === 'json') {
39 if (empty(DI::page()['aside'])) {
40 DI::page()['aside'] = '';
43 $cal_widget = CalendarExport::getHTML();
45 DI::page()['aside'] .= $cal_widget;
50 function events_post(App $a)
53 Logger::log('post: ' . print_r($_REQUEST, true), Logger::DATA);
59 $event_id = !empty($_POST['event_id']) ? intval($_POST['event_id']) : 0;
60 $cid = !empty($_POST['cid']) ? intval($_POST['cid']) : 0;
63 $start_text = Strings::escapeHtml($_REQUEST['start_text'] ?? '');
64 $finish_text = Strings::escapeHtml($_REQUEST['finish_text'] ?? '');
66 $adjust = intval($_POST['adjust'] ?? 0);
67 $nofinish = intval($_POST['nofinish'] ?? 0);
69 // The default setting for the `private` field in event_store() is false, so mirror that
70 $private_event = false;
72 $start = DBA::NULL_DATETIME;
73 $finish = DBA::NULL_DATETIME;
80 $finish = $finish_text;
84 $start = DateTimeFormat::convert($start, 'UTC', date_default_timezone_get());
86 $finish = DateTimeFormat::convert($finish, 'UTC', date_default_timezone_get());
89 $start = DateTimeFormat::utc($start);
91 $finish = DateTimeFormat::utc($finish);
95 // Don't allow the event to finish before it begins.
96 // It won't hurt anything, but somebody will file a bug report
97 // and we'll waste a bunch of time responding to it. Time that
98 // could've been spent doing something else.
100 $summary = trim($_POST['summary'] ?? '');
101 $desc = trim($_POST['desc'] ?? '');
102 $location = trim($_POST['location'] ?? '');
106 'summary' => $summary,
107 'description' => $desc,
108 'location' => $location,
109 'start' => $start_text,
110 'finish' => $finish_text,
112 'nofinish' => $nofinish,
115 $action = ($event_id == '') ? 'new' : 'event/' . $event_id;
116 $onerror_path = 'events/' . $action . '?' . http_build_query($params, null, null, PHP_QUERY_RFC3986);
118 if (strcmp($finish, $start) < 0 && !$nofinish) {
119 notice(L10n::t('Event can not end before it has started.') . EOL);
120 if (intval($_REQUEST['preview'])) {
121 echo L10n::t('Event can not end before it has started.');
124 DI::baseUrl()->redirect($onerror_path);
127 if (!$summary || ($start === DBA::NULL_DATETIME)) {
128 notice(L10n::t('Event title and start time are required.') . EOL);
129 if (intval($_REQUEST['preview'])) {
130 echo L10n::t('Event title and start time are required.');
133 DI::baseUrl()->redirect($onerror_path);
136 $share = intval($_POST['share'] ?? 0);
138 $c = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
142 if (DBA::isResult($c)) {
151 $aclFormatter = DI::aclFormatter();
153 $str_group_allow = $aclFormatter->toString($_POST['group_allow'] ?? '');
154 $str_contact_allow = $aclFormatter->toString($_POST['contact_allow'] ?? '');
155 $str_group_deny = $aclFormatter->toString($_POST['group_deny'] ?? '');
156 $str_contact_deny = $aclFormatter->toString($_POST['contact_deny'] ?? '');
158 // Undo the pseudo-contact of self, since there are real contacts now
159 if (strpos($str_contact_allow, '<' . $self . '>') !== false) {
160 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
162 // Make sure to set the `private` field as true. This is necessary to
163 // have the posts show up correctly in Diaspora if an event is created
164 // as visible only to self at first, but then edited to display to others.
165 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
166 $private_event = true;
169 // Note: do not set `private` field for self-only events. It will
170 // keep even you from seeing them!
171 $str_contact_allow = '<' . $self . '>';
172 $str_group_allow = $str_contact_deny = $str_group_deny = '';
177 $datarray['start'] = $start;
178 $datarray['finish'] = $finish;
179 $datarray['summary'] = $summary;
180 $datarray['desc'] = $desc;
181 $datarray['location'] = $location;
182 $datarray['type'] = $type;
183 $datarray['adjust'] = $adjust;
184 $datarray['nofinish'] = $nofinish;
185 $datarray['uid'] = $uid;
186 $datarray['cid'] = $cid;
187 $datarray['allow_cid'] = $str_contact_allow;
188 $datarray['allow_gid'] = $str_group_allow;
189 $datarray['deny_cid'] = $str_contact_deny;
190 $datarray['deny_gid'] = $str_group_deny;
191 $datarray['private'] = $private_event;
192 $datarray['id'] = $event_id;
194 if (intval($_REQUEST['preview'])) {
195 $html = Event::getHTML($datarray);
200 $item_id = Event::store($datarray);
203 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $item_id);
206 DI::baseUrl()->redirect('events');
209 function events_content(App $a)
212 notice(L10n::t('Permission denied.') . EOL);
213 return Login::form();
217 $_SESSION['return_path'] = DI::args()->getCommand();
220 if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
221 q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
227 if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
228 q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
234 if ($a->theme_events_in_profile) {
235 Nav::setSelected('home');
237 Nav::setSelected('events');
240 // get the translation strings for the callendar
241 $i18n = Event::getStrings();
243 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
244 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
245 '$module_url' => '/events',
253 if ($a->theme_events_in_profile) {
254 $tabs = Profile::getTabs($a, 'events', true);
260 $ignored = !empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0;
263 if ($a->argc > 2 && $a->argv[1] == 'event') {
265 $event_id = intval($a->argv[2]);
267 if ($a->argc > 2 && $a->argv[1] == 'drop') {
269 $event_id = intval($a->argv[2]);
271 if ($a->argc > 2 && $a->argv[1] == 'copy') {
273 $event_id = intval($a->argv[2]);
275 if ($a->argv[1] === 'new') {
279 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
281 $y = intval($a->argv[1]);
282 $m = intval($a->argv[2]);
286 // The view mode part is similiar to /mod/cal.php
287 if ($mode == 'view') {
288 $thisyear = DateTimeFormat::localNow('Y');
289 $thismonth = DateTimeFormat::localNow('m');
291 $y = intval($thisyear);
294 $m = intval($thismonth);
297 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
298 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
307 $dim = Temporal::getDaysInMonth($y, $m);
308 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
309 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
311 if ($a->argc > 1 && $a->argv[1] === 'json') {
312 if (!empty($_GET['start'])) {
313 $start = $_GET['start'];
315 if (!empty($_GET['end'])) {
316 $finish = $_GET['end'];
320 $start = DateTimeFormat::utc($start);
321 $finish = DateTimeFormat::utc($finish);
323 $adjust_start = DateTimeFormat::local($start);
324 $adjust_finish = DateTimeFormat::local($finish);
326 // put the event parametes in an array so we can better transmit them
328 'event_id' => intval($_GET['id'] ?? 0),
331 'adjust_start' => $adjust_start,
332 'adjust_finish' => $adjust_finish,
333 'ignore' => $ignored,
336 // get events by id or by date
337 if ($event_params['event_id']) {
338 $r = Event::getListById(local_user(), $event_params['event_id']);
340 $r = Event::getListByDate(local_user(), $event_params);
345 if (DBA::isResult($r)) {
346 $r = Event::sortByDate($r);
347 foreach ($r as $rr) {
348 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
349 if (empty($links[$j])) {
350 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
357 // transform the event in a usable array
358 if (DBA::isResult($r)) {
359 $r = Event::sortByDate($r);
360 $events = Event::prepareListForTemplate($r);
363 if ($a->argc > 1 && $a->argv[1] === 'json') {
364 header('Content-Type: application/json');
365 echo json_encode($events);
369 if (!empty($_GET['id'])) {
370 $tpl = Renderer::getMarkupTemplate("event.tpl");
372 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
375 // Get rid of dashes in key names, Smarty3 can't handle them
376 foreach ($events as $key => $event) {
378 foreach ($event['item'] as $k => $v) {
379 $k = str_replace('-', '_', $k);
380 $event_item[$k] = $v;
382 $events[$key]['item'] = $event_item;
385 // ACL blocks are loaded in modals in frio
386 DI::page()->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
387 DI::page()->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
388 DI::page()->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
389 DI::page()->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
391 $o = Renderer::replaceMacros($tpl, [
393 '$title' => L10n::t('Events'),
394 '$view' => L10n::t('View'),
395 '$new_event' => [DI::baseUrl() . '/events/new', L10n::t('Create New Event'), '', ''],
396 '$previous' => [DI::baseUrl() . '/events/$prevyear/$prevmonth', L10n::t('Previous'), '', ''],
397 '$next' => [DI::baseUrl() . '/events/$nextyear/$nextmonth', L10n::t('Next'), '', ''],
398 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
400 '$events' => $events,
402 '$today' => L10n::t('today'),
403 '$month' => L10n::t('month'),
404 '$week' => L10n::t('week'),
405 '$day' => L10n::t('day'),
406 '$list' => L10n::t('list'),
409 if (!empty($_GET['id'])) {
417 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
418 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
422 if (DBA::isResult($r)) {
427 // Passed parameters overrides anything found in the DB
428 if (in_array($mode, ['edit', 'new', 'copy'])) {
429 if (empty($orig_event)) {
433 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
434 if (!empty($_REQUEST['nofinish'])) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
435 if (!empty($_REQUEST['adjust'])) {$orig_event['adjust'] = $_REQUEST['adjust'];}
436 if (!empty($_REQUEST['summary'])) {$orig_event['summary'] = $_REQUEST['summary'];}
437 if (!empty($_REQUEST['desc'])) {$orig_event['desc'] = $_REQUEST['desc'];}
438 if (!empty($_REQUEST['location'])) {$orig_event['location'] = $_REQUEST['location'];}
439 if (!empty($_REQUEST['start'])) {$orig_event['start'] = $_REQUEST['start'];}
440 if (!empty($_REQUEST['finish'])) {$orig_event['finish'] = $_REQUEST['finish'];}
442 $n_checked = (!empty($orig_event['nofinish']) ? ' checked="checked" ' : '');
443 $a_checked = (!empty($orig_event['adjust']) ? ' checked="checked" ' : '');
445 $t_orig = $orig_event['summary'] ?? '';
446 $d_orig = $orig_event['desc'] ?? '';
447 $l_orig = $orig_event['location'] ?? '';
448 $eid = !empty($orig_event) ? $orig_event['id'] : 0;
449 $cid = !empty($orig_event) ? $orig_event['cid'] : 0;
450 $uri = !empty($orig_event) ? $orig_event['uri'] : '';
455 if (!empty($orig_event)
456 && ($orig_event['allow_cid'] !== '<' . local_user() . '>'
457 || $orig_event['allow_gid']
458 || $orig_event['deny_cid']
459 || $orig_event['deny_gid']))
461 $sh_checked = ' checked="checked" ';
464 if ($cid || $mode === 'edit') {
465 $sh_disabled = 'disabled="disabled"';
468 $sdt = !empty($orig_event) ? $orig_event['start'] : 'now';
469 $fdt = !empty($orig_event) ? $orig_event['finish'] : 'now';
471 $tz = date_default_timezone_get();
472 if (!empty($orig_event)) {
473 $tz = ($orig_event['adjust'] ? date_default_timezone_get() : 'UTC');
476 $syear = DateTimeFormat::convert($sdt, $tz, 'UTC', 'Y');
477 $smonth = DateTimeFormat::convert($sdt, $tz, 'UTC', 'm');
478 $sday = DateTimeFormat::convert($sdt, $tz, 'UTC', 'd');
480 $shour = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'H') : '00';
481 $sminute = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'i') : '00';
483 $fyear = DateTimeFormat::convert($fdt, $tz, 'UTC', 'Y');
484 $fmonth = DateTimeFormat::convert($fdt, $tz, 'UTC', 'm');
485 $fday = DateTimeFormat::convert($fdt, $tz, 'UTC', 'd');
487 $fhour = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'H') : '00';
488 $fminute = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'i') : '00';
490 if (!$cid && in_array($mode, ['new', 'copy'])) {
491 $acl = ACL::getFullSelectorHTML(DI::page(), $a->user, false, ACL::getDefaultUserPermissions($orig_event));
496 // If we copy an old event, we need to remove the ID and URI
497 // from the original event.
498 if ($mode === 'copy') {
503 $tpl = Renderer::getMarkupTemplate('event_form.tpl');
505 $o .= Renderer::replaceMacros($tpl, [
506 '$post' => DI::baseUrl() . '/events',
511 '$title' => L10n::t('Event details'),
512 '$desc' => L10n::t('Starting date and Title are required.'),
513 '$s_text' => L10n::t('Event Starts:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
514 '$s_dsel' => Temporal::getDateTimeField(
516 DateTime::createFromFormat('Y', intval($syear) + 5),
517 DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"),
518 L10n::t('Event Starts:'),
526 '$n_text' => L10n::t('Finish date/time is not known or not relevant'),
527 '$n_checked' => $n_checked,
528 '$f_text' => L10n::t('Event Finishes:'),
529 '$f_dsel' => Temporal::getDateTimeField(
531 DateTime::createFromFormat('Y', intval($fyear) + 5),
532 DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"),
533 L10n::t('Event Finishes:'),
539 '$a_text' => L10n::t('Adjust for viewer timezone'),
540 '$a_checked' => $a_checked,
541 '$d_text' => L10n::t('Description:'),
542 '$d_orig' => $d_orig,
543 '$l_text' => L10n::t('Location:'),
544 '$l_orig' => $l_orig,
545 '$t_text' => L10n::t('Title:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
546 '$t_orig' => $t_orig,
547 '$summary' => ['summary', L10n::t('Title:'), $t_orig, '', '*'],
548 '$sh_text' => L10n::t('Share this event'),
549 '$share' => ['share', L10n::t('Share this event'), $sh_checked, '', $sh_disabled],
550 '$sh_checked' => $sh_checked,
551 '$nofinish' => ['nofinish', L10n::t('Finish date/time is not known or not relevant'), $n_checked],
552 '$adjust' => ['adjust', L10n::t('Adjust for viewer timezone'), $a_checked],
553 '$preview' => L10n::t('Preview'),
555 '$submit' => L10n::t('Submit'),
556 '$basic' => L10n::t('Basic'),
557 '$advanced' => L10n::t('Advanced'),
558 '$permissions' => L10n::t('Permissions'),
564 // Remove an event from the calendar and its related items
565 if ($mode === 'drop' && $event_id) {
566 $ev = Event::getListById(local_user(), $event_id);
568 // Delete only real events (no birthdays)
569 if (DBA::isResult($ev) && $ev[0]['type'] == 'event') {
570 Item::deleteForUser(['id' => $ev[0]['itemid']], local_user());
573 if (Item::exists(['id' => $ev[0]['itemid']])) {
574 notice(L10n::t('Failed to remove event') . EOL);
576 info(L10n::t('Event removed') . EOL);
579 DI::baseUrl()->redirect('events');