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 '$baseurl' => System::baseUrl(),
241 '$module_url' => '/events',
249 if ($a->theme_events_in_profile) {
250 $tabs = Profile::getTabs($a, true);
256 $ignored = !empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0;
259 if ($a->argc > 2 && $a->argv[1] == 'event') {
261 $event_id = intval($a->argv[2]);
263 if ($a->argc > 2 && $a->argv[1] == 'drop') {
265 $event_id = intval($a->argv[2]);
267 if ($a->argc > 2 && $a->argv[1] == 'copy') {
269 $event_id = intval($a->argv[2]);
271 if ($a->argv[1] === 'new') {
275 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
277 $y = intval($a->argv[1]);
278 $m = intval($a->argv[2]);
282 // The view mode part is similiar to /mod/cal.php
283 if ($mode == 'view') {
284 $thisyear = DateTimeFormat::localNow('Y');
285 $thismonth = DateTimeFormat::localNow('m');
287 $y = intval($thisyear);
290 $m = intval($thismonth);
293 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
294 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
303 $dim = Temporal::getDaysInMonth($y, $m);
304 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
305 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
307 if ($a->argc > 1 && $a->argv[1] === 'json') {
308 if (!empty($_GET['start'])) {
309 $start = $_GET['start'];
311 if (!empty($_GET['end'])) {
312 $finish = $_GET['end'];
316 $start = DateTimeFormat::utc($start);
317 $finish = DateTimeFormat::utc($finish);
319 $adjust_start = DateTimeFormat::local($start);
320 $adjust_finish = DateTimeFormat::local($finish);
322 // put the event parametes in an array so we can better transmit them
324 'event_id' => intval(defaults($_GET, 'id', 0)),
327 'adjust_start' => $adjust_start,
328 'adjust_finish' => $adjust_finish,
329 'ignore' => $ignored,
332 // get events by id or by date
333 if ($event_params['event_id']) {
334 $r = Event::getListById(local_user(), $event_params['event_id']);
336 $r = Event::getListByDate(local_user(), $event_params);
341 if (DBA::isResult($r)) {
342 $r = Event::sortByDate($r);
343 foreach ($r as $rr) {
344 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
345 if (empty($links[$j])) {
346 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
353 // transform the event in a usable array
354 if (DBA::isResult($r)) {
355 $r = Event::sortByDate($r);
356 $events = Event::prepareListForTemplate($r);
359 if ($a->argc > 1 && $a->argv[1] === 'json') {
360 header('Content-Type: application/json');
361 echo json_encode($events);
365 if (!empty($_GET['id'])) {
366 $tpl = Renderer::getMarkupTemplate("event.tpl");
368 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
371 // Get rid of dashes in key names, Smarty3 can't handle them
372 foreach ($events as $key => $event) {
374 foreach ($event['item'] as $k => $v) {
375 $k = str_replace('-', '_', $k);
376 $event_item[$k] = $v;
378 $events[$key]['item'] = $event_item;
381 $o = Renderer::replaceMacros($tpl, [
382 '$baseurl' => System::baseUrl(),
384 '$title' => L10n::t('Events'),
385 '$view' => L10n::t('View'),
386 '$new_event' => [System::baseUrl() . '/events/new', L10n::t('Create New Event'), '', ''],
387 '$previous' => [System::baseUrl() . '/events/$prevyear/$prevmonth', L10n::t('Previous'), '', ''],
388 '$next' => [System::baseUrl() . '/events/$nextyear/$nextmonth', L10n::t('Next'), '', ''],
389 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
391 '$events' => $events,
393 '$today' => L10n::t('today'),
394 '$month' => L10n::t('month'),
395 '$week' => L10n::t('week'),
396 '$day' => L10n::t('day'),
397 '$list' => L10n::t('list'),
400 if (!empty($_GET['id'])) {
408 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
409 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
413 if (DBA::isResult($r)) {
418 // Passed parameters overrides anything found in the DB
419 if (in_array($mode, ['edit', 'new', 'copy'])) {
420 if (empty($orig_event)) {
424 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
425 if (!empty($_REQUEST['nofinish'])) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
426 if (!empty($_REQUEST['adjust'])) {$orig_event['adjust'] = $_REQUEST['adjust'];}
427 if (!empty($_REQUEST['summary'])) {$orig_event['summary'] = $_REQUEST['summary'];}
428 if (!empty($_REQUEST['description'])) {$orig_event['description'] = $_REQUEST['description'];}
429 if (!empty($_REQUEST['location'])) {$orig_event['location'] = $_REQUEST['location'];}
430 if (!empty($_REQUEST['start'])) {$orig_event['start'] = $_REQUEST['start'];}
431 if (!empty($_REQUEST['finish'])) {$orig_event['finish'] = $_REQUEST['finish'];}
433 $n_checked = (!empty($orig_event['nofinish']) ? ' checked="checked" ' : '');
434 $a_checked = (!empty($orig_event['adjust']) ? ' checked="checked" ' : '');
436 $t_orig = !empty($orig_event) ? $orig_event['summary'] : '';
437 $d_orig = !empty($orig_event) ? $orig_event['desc'] : '';
438 $l_orig = !empty($orig_event) ? $orig_event['location'] : '';
439 $eid = !empty($orig_event) ? $orig_event['id'] : 0;
440 $cid = !empty($orig_event) ? $orig_event['cid'] : 0;
441 $uri = !empty($orig_event) ? $orig_event['uri'] : '';
446 if (!empty($orig_event)
447 && ($orig_event['allow_cid'] !== '<' . local_user() . '>'
448 || $orig_event['allow_gid']
449 || $orig_event['deny_cid']
450 || $orig_event['deny_gid']))
452 $sh_checked = ' checked="checked" ';
455 if ($cid || $mode === 'edit') {
456 $sh_disabled = 'disabled="disabled"';
459 $sdt = !empty($orig_event) ? $orig_event['start'] : 'now';
460 $fdt = !empty($orig_event) ? $orig_event['finish'] : 'now';
462 $tz = date_default_timezone_get();
463 if (!empty($orig_event)) {
464 $tz = ($orig_event['adjust'] ? date_default_timezone_get() : 'UTC');
467 $syear = DateTimeFormat::convert($sdt, $tz, 'UTC', 'Y');
468 $smonth = DateTimeFormat::convert($sdt, $tz, 'UTC', 'm');
469 $sday = DateTimeFormat::convert($sdt, $tz, 'UTC', 'd');
471 $shour = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'H') : '00';
472 $sminute = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'i') : '00';
474 $fyear = DateTimeFormat::convert($fdt, $tz, 'UTC', 'Y');
475 $fmonth = DateTimeFormat::convert($fdt, $tz, 'UTC', 'm');
476 $fday = DateTimeFormat::convert($fdt, $tz, 'UTC', 'd');
478 $fhour = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'H') : '00';
479 $fminute = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'i') : '00';
481 $perms = ACL::getDefaultUserPermissions($orig_event);
483 if (!$cid && in_array($mode, ['new', 'copy'])) {
484 $acl = ACL::getFullSelectorHTML($a->user, false, $orig_event);
489 // If we copy an old event, we need to remove the ID and URI
490 // from the original event.
491 if ($mode === 'copy') {
496 $tpl = Renderer::getMarkupTemplate('event_form.tpl');
498 $o .= Renderer::replaceMacros($tpl, [
499 '$post' => System::baseUrl() . '/events',
504 '$allow_cid' => json_encode($perms['allow_cid']),
505 '$allow_gid' => json_encode($perms['allow_gid']),
506 '$deny_cid' => json_encode($perms['deny_cid']),
507 '$deny_gid' => json_encode($perms['deny_gid']),
509 '$title' => L10n::t('Event details'),
510 '$desc' => L10n::t('Starting date and Title are required.'),
511 '$s_text' => L10n::t('Event Starts:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
512 '$s_dsel' => Temporal::getDateTimeField(
514 DateTime::createFromFormat('Y', intval($syear) + 5),
515 DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"),
516 L10n::t('Event Starts:'),
524 '$n_text' => L10n::t('Finish date/time is not known or not relevant'),
525 '$n_checked' => $n_checked,
526 '$f_text' => L10n::t('Event Finishes:'),
527 '$f_dsel' => Temporal::getDateTimeField(
529 DateTime::createFromFormat('Y', intval($fyear) + 5),
530 DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"),
531 L10n::t('Event Finishes:'),
537 '$a_text' => L10n::t('Adjust for viewer timezone'),
538 '$a_checked' => $a_checked,
539 '$d_text' => L10n::t('Description:'),
540 '$d_orig' => $d_orig,
541 '$l_text' => L10n::t('Location:'),
542 '$l_orig' => $l_orig,
543 '$t_text' => L10n::t('Title:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
544 '$t_orig' => $t_orig,
545 '$summary' => ['summary', L10n::t('Title:'), $t_orig, '', '*'],
546 '$sh_text' => L10n::t('Share this event'),
547 '$share' => ['share', L10n::t('Share this event'), $sh_checked, '', $sh_disabled],
548 '$sh_checked' => $sh_checked,
549 '$nofinish' => ['nofinish', L10n::t('Finish date/time is not known or not relevant'), $n_checked],
550 '$adjust' => ['adjust', L10n::t('Adjust for viewer timezone'), $a_checked],
551 '$preview' => L10n::t('Preview'),
553 '$submit' => L10n::t('Submit'),
554 '$basic' => L10n::t('Basic'),
555 '$advanced' => L10n::t('Advanced'),
556 '$permissions' => L10n::t('Permissions'),
562 // Remove an event from the calendar and its related items
563 if ($mode === 'drop' && $event_id) {
564 $ev = Event::getListById(local_user(), $event_id);
566 // Delete only real events (no birthdays)
567 if (DBA::isResult($ev) && $ev[0]['type'] == 'event') {
568 Item::deleteForUser(['id' => $ev[0]['itemid']], local_user());
571 if (Item::exists(['id' => $ev[0]['itemid']])) {
572 notice(L10n::t('Failed to remove event') . EOL);
574 info(L10n::t('Event removed') . EOL);
577 $a->internalRedirect('events');