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 require_once 'include/items.php';
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($a->page['aside'])) {
40 $a->page['aside'] = '';
43 $cal_widget = CalendarExport::getHTML();
45 $a->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(defaults($_REQUEST, 'start_text', ''));
64 $finish_text = Strings::escapeHtml(defaults($_REQUEST, 'finish_text', ''));
66 $adjust = intval(defaults($_POST, 'adjust', 0));
67 $nofinish = intval(defaults($_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 = Strings::escapeHtml(trim(defaults($_POST, 'summary', '')));
101 $desc = Strings::escapeHtml(trim(defaults($_POST, 'desc', '')));
102 $location = Strings::escapeHtml(trim(defaults($_POST, 'location', '')));
105 $action = ($event_id == '') ? 'new' : "event/" . $event_id;
106 $onerror_path = "events/" . $action . "?summary=$summary&description=$desc&location=$location&start=$start_text&finish=$finish_text&adjust=$adjust&nofinish=$nofinish";
108 if (strcmp($finish, $start) < 0 && !$nofinish) {
109 notice(L10n::t('Event can not end before it has started.') . EOL);
110 if (intval($_REQUEST['preview'])) {
111 echo L10n::t('Event can not end before it has started.');
114 $a->internalRedirect($onerror_path);
117 if (!$summary || ($start === DBA::NULL_DATETIME)) {
118 notice(L10n::t('Event title and start time are required.') . EOL);
119 if (intval($_REQUEST['preview'])) {
120 echo L10n::t('Event title and start time are required.');
123 $a->internalRedirect($onerror_path);
126 $share = intval(defaults($_POST, 'share', 0));
128 $c = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
132 if (DBA::isResult($c)) {
140 $str_group_allow = !empty($_POST['group_allow']) ? perms2str($_POST['group_allow']) : '';
141 $str_contact_allow = !empty($_POST['contact_allow']) ? perms2str($_POST['contact_allow']) : '';
142 $str_group_deny = !empty($_POST['group_deny']) ? perms2str($_POST['group_deny']) : '';
143 $str_contact_deny = !empty($_POST['contact_deny']) ? perms2str($_POST['contact_deny']) : '';
145 // Undo the pseudo-contact of self, since there are real contacts now
146 if (strpos($str_contact_allow, '<' . $self . '>') !== false) {
147 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
149 // Make sure to set the `private` field as true. This is necessary to
150 // have the posts show up correctly in Diaspora if an event is created
151 // as visible only to self at first, but then edited to display to others.
152 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
153 $private_event = true;
156 // Note: do not set `private` field for self-only events. It will
157 // keep even you from seeing them!
158 $str_contact_allow = '<' . $self . '>';
159 $str_group_allow = $str_contact_deny = $str_group_deny = '';
164 $datarray['start'] = $start;
165 $datarray['finish'] = $finish;
166 $datarray['summary'] = $summary;
167 $datarray['desc'] = $desc;
168 $datarray['location'] = $location;
169 $datarray['type'] = $type;
170 $datarray['adjust'] = $adjust;
171 $datarray['nofinish'] = $nofinish;
172 $datarray['uid'] = $uid;
173 $datarray['cid'] = $cid;
174 $datarray['allow_cid'] = $str_contact_allow;
175 $datarray['allow_gid'] = $str_group_allow;
176 $datarray['deny_cid'] = $str_contact_deny;
177 $datarray['deny_gid'] = $str_group_deny;
178 $datarray['private'] = $private_event;
179 $datarray['id'] = $event_id;
181 if (intval($_REQUEST['preview'])) {
182 $html = Event::getHTML($datarray);
187 $item_id = Event::store($datarray);
190 Worker::add(PRIORITY_HIGH, "Notifier", "event", $item_id);
193 $a->internalRedirect('events');
196 function events_content(App $a)
199 notice(L10n::t('Permission denied.') . EOL);
200 return Login::form();
204 $_SESSION['return_path'] = $a->cmd;
207 if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
208 $r = q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
214 if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
215 $r = q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
221 if ($a->theme_events_in_profile) {
222 Nav::setSelected('home');
224 Nav::setSelected('events');
227 // get the translation strings for the callendar
228 $i18n = Event::getStrings();
230 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
231 $a->page['htmlhead'] .= Renderer::replaceMacros($htpl, [
232 '$baseurl' => System::baseUrl(),
233 '$module_url' => '/events',
241 if ($a->theme_events_in_profile) {
242 $tabs = Profile::getTabs($a, true);
248 $ignored = !empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0;
251 if ($a->argc > 2 && $a->argv[1] == 'event') {
253 $event_id = intval($a->argv[2]);
255 if ($a->argc > 2 && $a->argv[1] == 'drop') {
257 $event_id = intval($a->argv[2]);
259 if ($a->argc > 2 && $a->argv[1] == 'copy') {
261 $event_id = intval($a->argv[2]);
263 if ($a->argv[1] === 'new') {
267 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
269 $y = intval($a->argv[1]);
270 $m = intval($a->argv[2]);
274 // The view mode part is similiar to /mod/cal.php
275 if ($mode == 'view') {
276 $thisyear = DateTimeFormat::localNow('Y');
277 $thismonth = DateTimeFormat::localNow('m');
279 $y = intval($thisyear);
282 $m = intval($thismonth);
285 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
286 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
297 if ($nextmonth > 12) {
310 $dim = Temporal::getDaysInMonth($y, $m);
311 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
312 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
314 if ($a->argc > 1 && $a->argv[1] === 'json') {
315 if (!empty($_GET['start'])) {
316 $start = $_GET['start'];
318 if (!empty($_GET['end'])) {
319 $finish = $_GET['end'];
323 $start = DateTimeFormat::utc($start);
324 $finish = DateTimeFormat::utc($finish);
326 $adjust_start = DateTimeFormat::local($start);
327 $adjust_finish = DateTimeFormat::local($finish);
329 // put the event parametes in an array so we can better transmit them
331 'event_id' => intval(defaults($_GET, 'id', 0)),
334 'adjust_start' => $adjust_start,
335 'adjust_finish' => $adjust_finish,
336 'ignore' => $ignored,
339 // get events by id or by date
340 if ($event_params['event_id']) {
341 $r = Event::getListById(local_user(), $event_params['event_id']);
343 $r = Event::getListByDate(local_user(), $event_params);
348 if (DBA::isResult($r)) {
349 $r = Event::sortByDate($r);
350 foreach ($r as $rr) {
351 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
352 if (empty($links[$j])) {
353 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
360 // transform the event in a usable array
361 if (DBA::isResult($r)) {
362 $r = Event::sortByDate($r);
363 $events = Event::prepareListForTemplate($r);
366 if ($a->argc > 1 && $a->argv[1] === '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 $o = Renderer::replaceMacros($tpl, [
388 '$baseurl' => System::baseUrl(),
390 '$title' => L10n::t('Events'),
391 '$view' => L10n::t('View'),
392 '$new_event' => [System::baseUrl() . '/events/new', L10n::t('Create New Event'), '', ''],
393 '$previous' => [System::baseUrl() . '/events/$prevyear/$prevmonth', L10n::t('Previous'), '', ''],
394 '$next' => [System::baseUrl() . '/events/$nextyear/$nextmonth', L10n::t('Next'), '', ''],
395 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
397 '$events' => $events,
399 '$today' => L10n::t('today'),
400 '$month' => L10n::t('month'),
401 '$week' => L10n::t('week'),
402 '$day' => L10n::t('day'),
403 '$list' => L10n::t('list'),
406 if (!empty($_GET['id'])) {
414 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
415 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
419 if (DBA::isResult($r)) {
424 // Passed parameters overrides anything found in the DB
425 if (in_array($mode, ['edit', 'new', 'copy'])) {
426 if (empty($orig_event)) {
430 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
431 if (!empty($_REQUEST['nofinish'])) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
432 if (!empty($_REQUEST['adjust'])) {$orig_event['adjust'] = $_REQUEST['adjust'];}
433 if (!empty($_REQUEST['summary'])) {$orig_event['summary'] = $_REQUEST['summary'];}
434 if (!empty($_REQUEST['description'])) {$orig_event['description'] = $_REQUEST['description'];}
435 if (!empty($_REQUEST['location'])) {$orig_event['location'] = $_REQUEST['location'];}
436 if (!empty($_REQUEST['start'])) {$orig_event['start'] = $_REQUEST['start'];}
437 if (!empty($_REQUEST['finish'])) {$orig_event['finish'] = $_REQUEST['finish'];}
439 $n_checked = (!empty($orig_event['nofinish']) ? ' checked="checked" ' : '');
440 $a_checked = (!empty($orig_event['adjust']) ? ' checked="checked" ' : '');
442 $t_orig = !empty($orig_event) ? $orig_event['summary'] : '';
443 $d_orig = !empty($orig_event) ? $orig_event['desc'] : '';
444 $l_orig = !empty($orig_event) ? $orig_event['location'] : '';
445 $eid = !empty($orig_event) ? $orig_event['id'] : 0;
446 $cid = !empty($orig_event) ? $orig_event['cid'] : 0;
447 $uri = !empty($orig_event) ? $orig_event['uri'] : '';
452 if (!empty($orig_event)
453 && ($orig_event['allow_cid'] !== '<' . local_user() . '>'
454 || $orig_event['allow_gid']
455 || $orig_event['deny_cid']
456 || $orig_event['deny_gid']))
458 $sh_checked = ' checked="checked" ';
461 if ($cid || $mode === 'edit') {
462 $sh_disabled = 'disabled="disabled"';
465 $sdt = !empty($orig_event) ? $orig_event['start'] : 'now';
466 $fdt = !empty($orig_event) ? $orig_event['finish'] : 'now';
468 $tz = date_default_timezone_get();
469 if (!empty($orig_event)) {
470 $tz = ($orig_event['adjust'] ? date_default_timezone_get() : 'UTC');
473 $syear = DateTimeFormat::convert($sdt, $tz, 'UTC', 'Y');
474 $smonth = DateTimeFormat::convert($sdt, $tz, 'UTC', 'm');
475 $sday = DateTimeFormat::convert($sdt, $tz, 'UTC', 'd');
477 $shour = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'H') : '00';
478 $sminute = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'i') : '00';
480 $fyear = DateTimeFormat::convert($fdt, $tz, 'UTC', 'Y');
481 $fmonth = DateTimeFormat::convert($fdt, $tz, 'UTC', 'm');
482 $fday = DateTimeFormat::convert($fdt, $tz, 'UTC', 'd');
484 $fhour = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'H') : '00';
485 $fminute = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'i') : '00';
487 $perms = ACL::getDefaultUserPermissions($orig_event);
489 if (!$cid && in_array($mode, ['new', 'copy'])) {
490 $acl = ACL::getFullSelectorHTML($a->user, false, $orig_event);
495 // If we copy an old event, we need to remove the ID and URI
496 // from the original event.
497 if ($mode === 'copy') {
502 $tpl = Renderer::getMarkupTemplate('event_form.tpl');
504 $o .= Renderer::replaceMacros($tpl, [
505 '$post' => System::baseUrl() . '/events',
510 '$allow_cid' => json_encode($perms['allow_cid']),
511 '$allow_gid' => json_encode($perms['allow_gid']),
512 '$deny_cid' => json_encode($perms['deny_cid']),
513 '$deny_gid' => json_encode($perms['deny_gid']),
515 '$title' => L10n::t('Event details'),
516 '$desc' => L10n::t('Starting date and Title are required.'),
517 '$s_text' => L10n::t('Event Starts:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
518 '$s_dsel' => Temporal::getDateTimeField(
520 DateTime::createFromFormat('Y', $syear+5),
521 DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"),
522 L10n::t('Event Starts:'),
530 '$n_text' => L10n::t('Finish date/time is not known or not relevant'),
531 '$n_checked' => $n_checked,
532 '$f_text' => L10n::t('Event Finishes:'),
533 '$f_dsel' => Temporal::getDateTimeField(
535 DateTime::createFromFormat('Y', $fyear+5),
536 DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"),
537 L10n::t('Event Finishes:'),
543 '$a_text' => L10n::t('Adjust for viewer timezone'),
544 '$a_checked' => $a_checked,
545 '$d_text' => L10n::t('Description:'),
546 '$d_orig' => $d_orig,
547 '$l_text' => L10n::t('Location:'),
548 '$l_orig' => $l_orig,
549 '$t_text' => L10n::t('Title:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
550 '$t_orig' => $t_orig,
551 '$summary' => ['summary', L10n::t('Title:'), $t_orig, '', '*'],
552 '$sh_text' => L10n::t('Share this event'),
553 '$share' => ['share', L10n::t('Share this event'), $sh_checked, '', $sh_disabled],
554 '$sh_checked' => $sh_checked,
555 '$nofinish' => ['nofinish', L10n::t('Finish date/time is not known or not relevant'), $n_checked],
556 '$adjust' => ['adjust', L10n::t('Adjust for viewer timezone'), $a_checked],
557 '$preview' => L10n::t('Preview'),
559 '$submit' => L10n::t('Submit'),
560 '$basic' => L10n::t('Basic'),
561 '$advanced' => L10n::t('Advanced'),
562 '$permissions' => L10n::t('Permissions'),
568 // Remove an event from the calendar and its related items
569 if ($mode === 'drop' && $event_id) {
570 $ev = Event::getListById(local_user(), $event_id);
572 // Delete only real events (no birthdays)
573 if (DBA::isResult($ev) && $ev[0]['type'] == 'event') {
574 Item::deleteForUser(['id' => $ev[0]['itemid']], local_user());
577 if (Item::exists(['id' => $ev[0]['itemid']])) {
578 notice(L10n::t('Failed to remove event') . EOL);
580 info(L10n::t('Event removed') . EOL);
583 $a->internalRedirect('events');