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 = Strings::escapeHtml(trim(defaults($_POST, 'summary', '')));
99 $desc = Strings::escapeHtml(trim(defaults($_POST, 'desc', '')));
100 $location = Strings::escapeHtml(trim(defaults($_POST, 'location', '')));
103 $action = ($event_id == '') ? 'new' : "event/" . $event_id;
104 $onerror_path = "events/" . $action . "?summary=$summary&description=$desc&location=$location&start=$start_text&finish=$finish_text&adjust=$adjust&nofinish=$nofinish";
106 if (strcmp($finish, $start) < 0 && !$nofinish) {
107 notice(L10n::t('Event can not end before it has started.') . EOL);
108 if (intval($_REQUEST['preview'])) {
109 echo L10n::t('Event can not end before it has started.');
112 $a->internalRedirect($onerror_path);
115 if (!$summary || ($start === DBA::NULL_DATETIME)) {
116 notice(L10n::t('Event title and start time are required.') . EOL);
117 if (intval($_REQUEST['preview'])) {
118 echo L10n::t('Event title and start time are required.');
121 $a->internalRedirect($onerror_path);
124 $share = intval(defaults($_POST, 'share', 0));
126 $c = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
130 if (DBA::isResult($c)) {
138 $str_group_allow = !empty($_POST['group_allow']) ? perms2str($_POST['group_allow']) : '';
139 $str_contact_allow = !empty($_POST['contact_allow']) ? perms2str($_POST['contact_allow']) : '';
140 $str_group_deny = !empty($_POST['group_deny']) ? perms2str($_POST['group_deny']) : '';
141 $str_contact_deny = !empty($_POST['contact_deny']) ? perms2str($_POST['contact_deny']) : '';
143 // Undo the pseudo-contact of self, since there are real contacts now
144 if (strpos($str_contact_allow, '<' . $self . '>') !== false) {
145 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
147 // Make sure to set the `private` field as true. This is necessary to
148 // have the posts show up correctly in Diaspora if an event is created
149 // as visible only to self at first, but then edited to display to others.
150 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
151 $private_event = true;
154 // Note: do not set `private` field for self-only events. It will
155 // keep even you from seeing them!
156 $str_contact_allow = '<' . $self . '>';
157 $str_group_allow = $str_contact_deny = $str_group_deny = '';
162 $datarray['start'] = $start;
163 $datarray['finish'] = $finish;
164 $datarray['summary'] = $summary;
165 $datarray['desc'] = $desc;
166 $datarray['location'] = $location;
167 $datarray['type'] = $type;
168 $datarray['adjust'] = $adjust;
169 $datarray['nofinish'] = $nofinish;
170 $datarray['uid'] = $uid;
171 $datarray['cid'] = $cid;
172 $datarray['allow_cid'] = $str_contact_allow;
173 $datarray['allow_gid'] = $str_group_allow;
174 $datarray['deny_cid'] = $str_contact_deny;
175 $datarray['deny_gid'] = $str_group_deny;
176 $datarray['private'] = $private_event;
177 $datarray['id'] = $event_id;
179 if (intval($_REQUEST['preview'])) {
180 $html = Event::getHTML($datarray);
185 $item_id = Event::store($datarray);
188 Worker::add(PRIORITY_HIGH, "Notifier", "event", $item_id);
191 $a->internalRedirect('events');
194 function events_content(App $a)
197 notice(L10n::t('Permission denied.') . EOL);
198 return Login::form();
202 $_SESSION['return_path'] = $a->cmd;
205 if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
206 $r = q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
212 if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
213 $r = q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
219 if ($a->theme_events_in_profile) {
220 Nav::setSelected('home');
222 Nav::setSelected('events');
225 // get the translation strings for the callendar
226 $i18n = Event::getStrings();
228 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
229 $a->page['htmlhead'] .= Renderer::replaceMacros($htpl, [
230 '$baseurl' => System::baseUrl(),
231 '$module_url' => '/events',
239 if ($a->theme_events_in_profile) {
240 $tabs = Profile::getTabs($a, true);
246 $ignored = !empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0;
249 if ($a->argc > 2 && $a->argv[1] == 'event') {
251 $event_id = intval($a->argv[2]);
253 if ($a->argc > 2 && $a->argv[1] == 'drop') {
255 $event_id = intval($a->argv[2]);
257 if ($a->argc > 2 && $a->argv[1] == 'copy') {
259 $event_id = intval($a->argv[2]);
261 if ($a->argv[1] === 'new') {
265 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
267 $y = intval($a->argv[1]);
268 $m = intval($a->argv[2]);
272 // The view mode part is similiar to /mod/cal.php
273 if ($mode == 'view') {
274 $thisyear = DateTimeFormat::localNow('Y');
275 $thismonth = DateTimeFormat::localNow('m');
277 $y = intval($thisyear);
280 $m = intval($thismonth);
283 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
284 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
295 if ($nextmonth > 12) {
308 $dim = Temporal::getDaysInMonth($y, $m);
309 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
310 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
312 if ($a->argc > 1 && $a->argv[1] === 'json') {
313 if (!empty($_GET['start'])) {
314 $start = $_GET['start'];
316 if (!empty($_GET['end'])) {
317 $finish = $_GET['end'];
321 $start = DateTimeFormat::utc($start);
322 $finish = DateTimeFormat::utc($finish);
324 $adjust_start = DateTimeFormat::local($start);
325 $adjust_finish = DateTimeFormat::local($finish);
327 // put the event parametes in an array so we can better transmit them
329 'event_id' => intval(defaults($_GET, 'id', 0)),
332 'adjust_start' => $adjust_start,
333 'adjust_finish' => $adjust_finish,
334 'ignore' => $ignored,
337 // get events by id or by date
338 if ($event_params['event_id']) {
339 $r = Event::getListById(local_user(), $event_params['event_id']);
341 $r = Event::getListByDate(local_user(), $event_params);
346 if (DBA::isResult($r)) {
347 $r = Event::sortByDate($r);
348 foreach ($r as $rr) {
349 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
350 if (empty($links[$j])) {
351 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
358 // transform the event in a usable array
359 if (DBA::isResult($r)) {
360 $r = Event::sortByDate($r);
361 $events = Event::prepareListForTemplate($r);
364 if ($a->argc > 1 && $a->argv[1] === '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 $o = Renderer::replaceMacros($tpl, [
386 '$baseurl' => System::baseUrl(),
388 '$title' => L10n::t('Events'),
389 '$view' => L10n::t('View'),
390 '$new_event' => [System::baseUrl() . '/events/new', L10n::t('Create New Event'), '', ''],
391 '$previous' => [System::baseUrl() . '/events/$prevyear/$prevmonth', L10n::t('Previous'), '', ''],
392 '$next' => [System::baseUrl() . '/events/$nextyear/$nextmonth', L10n::t('Next'), '', ''],
393 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
395 '$events' => $events,
397 '$today' => L10n::t('today'),
398 '$month' => L10n::t('month'),
399 '$week' => L10n::t('week'),
400 '$day' => L10n::t('day'),
401 '$list' => L10n::t('list'),
404 if (!empty($_GET['id'])) {
412 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
413 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
417 if (DBA::isResult($r)) {
422 // Passed parameters overrides anything found in the DB
423 if (in_array($mode, ['edit', 'new', 'copy'])) {
424 if (empty($orig_event)) {
428 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
429 if (!empty($_REQUEST['nofinish'])) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
430 if (!empty($_REQUEST['adjust'])) {$orig_event['adjust'] = $_REQUEST['adjust'];}
431 if (!empty($_REQUEST['summary'])) {$orig_event['summary'] = $_REQUEST['summary'];}
432 if (!empty($_REQUEST['description'])) {$orig_event['description'] = $_REQUEST['description'];}
433 if (!empty($_REQUEST['location'])) {$orig_event['location'] = $_REQUEST['location'];}
434 if (!empty($_REQUEST['start'])) {$orig_event['start'] = $_REQUEST['start'];}
435 if (!empty($_REQUEST['finish'])) {$orig_event['finish'] = $_REQUEST['finish'];}
437 $n_checked = (!empty($orig_event['nofinish']) ? ' checked="checked" ' : '');
438 $a_checked = (!empty($orig_event['adjust']) ? ' checked="checked" ' : '');
440 $t_orig = !empty($orig_event) ? $orig_event['summary'] : '';
441 $d_orig = !empty($orig_event) ? $orig_event['desc'] : '';
442 $l_orig = !empty($orig_event) ? $orig_event['location'] : '';
443 $eid = !empty($orig_event) ? $orig_event['id'] : 0;
444 $cid = !empty($orig_event) ? $orig_event['cid'] : 0;
445 $uri = !empty($orig_event) ? $orig_event['uri'] : '';
450 if (!empty($orig_event)
451 && ($orig_event['allow_cid'] !== '<' . local_user() . '>'
452 || $orig_event['allow_gid']
453 || $orig_event['deny_cid']
454 || $orig_event['deny_gid']))
456 $sh_checked = ' checked="checked" ';
459 if ($cid || $mode === 'edit') {
460 $sh_disabled = 'disabled="disabled"';
463 $sdt = !empty($orig_event) ? $orig_event['start'] : 'now';
464 $fdt = !empty($orig_event) ? $orig_event['finish'] : 'now';
466 $tz = date_default_timezone_get();
467 if (!empty($orig_event)) {
468 $tz = ($orig_event['adjust'] ? date_default_timezone_get() : 'UTC');
471 $syear = DateTimeFormat::convert($sdt, $tz, 'UTC', 'Y');
472 $smonth = DateTimeFormat::convert($sdt, $tz, 'UTC', 'm');
473 $sday = DateTimeFormat::convert($sdt, $tz, 'UTC', 'd');
475 $shour = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'H') : '00';
476 $sminute = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'i') : '00';
478 $fyear = DateTimeFormat::convert($fdt, $tz, 'UTC', 'Y');
479 $fmonth = DateTimeFormat::convert($fdt, $tz, 'UTC', 'm');
480 $fday = DateTimeFormat::convert($fdt, $tz, 'UTC', 'd');
482 $fhour = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'H') : '00';
483 $fminute = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'i') : '00';
485 $perms = ACL::getDefaultUserPermissions($orig_event);
487 if (!$cid && in_array($mode, ['new', 'copy'])) {
488 $acl = ACL::getFullSelectorHTML($a->user, false, $orig_event);
493 // If we copy an old event, we need to remove the ID and URI
494 // from the original event.
495 if ($mode === 'copy') {
500 $tpl = Renderer::getMarkupTemplate('event_form.tpl');
502 $o .= Renderer::replaceMacros($tpl, [
503 '$post' => System::baseUrl() . '/events',
508 '$allow_cid' => json_encode($perms['allow_cid']),
509 '$allow_gid' => json_encode($perms['allow_gid']),
510 '$deny_cid' => json_encode($perms['deny_cid']),
511 '$deny_gid' => json_encode($perms['deny_gid']),
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', $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', $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');