3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 use Friendica\Content\Nav;
25 use Friendica\Content\Widget\CalendarExport;
26 use Friendica\Core\ACL;
27 use Friendica\Core\Logger;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Theme;
30 use Friendica\Core\Worker;
31 use Friendica\Database\DBA;
33 use Friendica\Model\Contact;
34 use Friendica\Model\Event;
35 use Friendica\Model\Item;
36 use Friendica\Model\User;
37 use Friendica\Module\BaseProfile;
38 use Friendica\Module\Security\Login;
39 use Friendica\Util\DateTimeFormat;
40 use Friendica\Util\Strings;
41 use Friendica\Util\Temporal;
42 use Friendica\Worker\Delivery;
44 function events_init(App $a)
50 // If it's a json request abort here because we don't
51 // need the widget data
52 if ($a->argc > 1 && $a->argv[1] === 'json') {
56 if (empty(DI::page()['aside'])) {
57 DI::page()['aside'] = '';
60 $cal_widget = CalendarExport::getHTML();
62 DI::page()['aside'] .= $cal_widget;
67 function events_post(App $a)
69 Logger::debug('post', ['request' => $_REQUEST]);
74 $event_id = !empty($_POST['event_id']) ? intval($_POST['event_id']) : 0;
75 $cid = !empty($_POST['cid']) ? intval($_POST['cid']) : 0;
78 $start_text = Strings::escapeHtml($_REQUEST['start_text'] ?? '');
79 $finish_text = Strings::escapeHtml($_REQUEST['finish_text'] ?? '');
81 $adjust = intval($_POST['adjust'] ?? 0);
82 $nofinish = intval($_POST['nofinish'] ?? 0);
84 $share = intval($_POST['share'] ?? 0);
86 // The default setting for the `private` field in event_store() is false, so mirror that
87 $private_event = false;
89 $start = DBA::NULL_DATETIME;
90 $finish = DBA::NULL_DATETIME;
97 $finish = $finish_text;
101 $start = DateTimeFormat::convert($start, 'UTC', date_default_timezone_get());
103 $finish = DateTimeFormat::convert($finish, 'UTC', date_default_timezone_get());
106 $start = DateTimeFormat::utc($start);
108 $finish = DateTimeFormat::utc($finish);
112 // Don't allow the event to finish before it begins.
113 // It won't hurt anything, but somebody will file a bug report
114 // and we'll waste a bunch of time responding to it. Time that
115 // could've been spent doing something else.
117 $summary = trim($_POST['summary'] ?? '');
118 $desc = trim($_POST['desc'] ?? '');
119 $location = trim($_POST['location'] ?? '');
123 'summary' => $summary,
124 'description' => $desc,
125 'location' => $location,
126 'start' => $start_text,
127 'finish' => $finish_text,
129 'nofinish' => $nofinish,
132 $action = ($event_id == '') ? 'new' : 'event/' . $event_id;
133 $onerror_path = 'events/' . $action . '?' . http_build_query($params, '', '&', PHP_QUERY_RFC3986);
135 if (strcmp($finish, $start) < 0 && !$nofinish) {
136 notice(DI::l10n()->t('Event can not end before it has started.'));
137 if (intval($_REQUEST['preview'])) {
138 echo DI::l10n()->t('Event can not end before it has started.');
141 DI::baseUrl()->redirect($onerror_path);
144 if (!$summary || ($start === DBA::NULL_DATETIME)) {
145 notice(DI::l10n()->t('Event title and start time are required.'));
146 if (intval($_REQUEST['preview'])) {
147 echo DI::l10n()->t('Event title and start time are required.');
150 DI::baseUrl()->redirect($onerror_path);
153 $self = \Friendica\Model\Contact::getPublicIdByUserId($uid);
155 $aclFormatter = DI::aclFormatter();
158 $user = User::getById($uid, ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']);
159 if (!DBA::isResult($user)) {
163 $str_contact_allow = isset($_REQUEST['contact_allow']) ? $aclFormatter->toString($_REQUEST['contact_allow']) : $user['allow_cid'] ?? '';
164 $str_group_allow = isset($_REQUEST['group_allow']) ? $aclFormatter->toString($_REQUEST['group_allow']) : $user['allow_gid'] ?? '';
165 $str_contact_deny = isset($_REQUEST['contact_deny']) ? $aclFormatter->toString($_REQUEST['contact_deny']) : $user['deny_cid'] ?? '';
166 $str_group_deny = isset($_REQUEST['group_deny']) ? $aclFormatter->toString($_REQUEST['group_deny']) : $user['deny_gid'] ?? '';
168 $visibility = $_REQUEST['visibility'] ?? '';
169 if ($visibility === 'public') {
170 // The ACL selector introduced in version 2019.12 sends ACL input data even when the Public visibility is selected
171 $str_contact_allow = $str_group_allow = $str_contact_deny = $str_group_deny = '';
172 } else if ($visibility === 'custom') {
173 // Since we know from the visibility parameter the item should be private, we have to prevent the empty ACL
174 // case that would make it public. So we always append the author's contact id to the allowed contacts.
175 // See https://github.com/friendica/friendica/issues/9672
176 $str_contact_allow .= $aclFormatter->toString($self);
179 $str_contact_allow = $aclFormatter->toString($self);
180 $str_group_allow = $str_contact_deny = $str_group_deny = '';
183 // Make sure to set the `private` field as true. This is necessary to
184 // have the posts show up correctly in Diaspora if an event is created
185 // as visible only to self at first, but then edited to display to others.
186 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
187 $private_event = true;
191 $datarray['start'] = $start;
192 $datarray['finish'] = $finish;
193 $datarray['summary'] = $summary;
194 $datarray['desc'] = $desc;
195 $datarray['location'] = $location;
196 $datarray['type'] = $type;
197 $datarray['adjust'] = $adjust;
198 $datarray['nofinish'] = $nofinish;
199 $datarray['uid'] = $uid;
200 $datarray['cid'] = $cid;
201 $datarray['allow_cid'] = $str_contact_allow;
202 $datarray['allow_gid'] = $str_group_allow;
203 $datarray['deny_cid'] = $str_contact_deny;
204 $datarray['deny_gid'] = $str_group_deny;
205 $datarray['private'] = $private_event;
206 $datarray['id'] = $event_id;
208 if (intval($_REQUEST['preview'])) {
209 $html = Event::getHTML($datarray);
214 $item_id = Event::store($datarray);
217 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $item_id);
220 DI::baseUrl()->redirect('events');
223 function events_content(App $a)
226 notice(DI::l10n()->t('Permission denied.'));
227 return Login::form();
231 $_SESSION['return_path'] = DI::args()->getCommand();
234 if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
235 q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
241 if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
242 q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
248 if ($a->theme_events_in_profile) {
249 Nav::setSelected('home');
251 Nav::setSelected('events');
254 // get the translation strings for the callendar
255 $i18n = Event::getStrings();
257 DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.min.css');
258 DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.print.min.css', 'print');
259 DI::page()->registerFooterScript('view/asset/moment/min/moment-with-locales.min.js');
260 DI::page()->registerFooterScript('view/asset/fullcalendar/dist/fullcalendar.min.js');
262 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
263 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
264 '$module_url' => '/events',
272 if ($a->theme_events_in_profile) {
273 $tabs = BaseProfile::getTabsHTML($a, 'events', true);
279 $ignored = !empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0;
282 if ($a->argc > 2 && $a->argv[1] == 'event') {
284 $event_id = intval($a->argv[2]);
286 if ($a->argc > 2 && $a->argv[1] == 'drop') {
288 $event_id = intval($a->argv[2]);
290 if ($a->argc > 2 && $a->argv[1] == 'copy') {
292 $event_id = intval($a->argv[2]);
294 if ($a->argv[1] === 'new') {
298 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
300 $y = intval($a->argv[1]);
301 $m = intval($a->argv[2]);
305 // The view mode part is similiar to /mod/cal.php
306 if ($mode == 'view') {
307 $thisyear = DateTimeFormat::localNow('Y');
308 $thismonth = DateTimeFormat::localNow('m');
310 $y = intval($thisyear);
313 $m = intval($thismonth);
316 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
317 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
326 $dim = Temporal::getDaysInMonth($y, $m);
327 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
328 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
330 if ($a->argc > 1 && $a->argv[1] === 'json') {
331 if (!empty($_GET['start'])) {
332 $start = $_GET['start'];
334 if (!empty($_GET['end'])) {
335 $finish = $_GET['end'];
339 $start = DateTimeFormat::utc($start);
340 $finish = DateTimeFormat::utc($finish);
342 $adjust_start = DateTimeFormat::local($start);
343 $adjust_finish = DateTimeFormat::local($finish);
345 // put the event parametes in an array so we can better transmit them
347 'event_id' => intval($_GET['id'] ?? 0),
350 'adjust_start' => $adjust_start,
351 'adjust_finish' => $adjust_finish,
352 'ignore' => $ignored,
355 // get events by id or by date
356 if ($event_params['event_id']) {
357 $r = Event::getListById(local_user(), $event_params['event_id']);
359 $r = Event::getListByDate(local_user(), $event_params);
364 if (DBA::isResult($r)) {
365 $r = Event::sortByDate($r);
366 foreach ($r as $rr) {
367 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
368 if (empty($links[$j])) {
369 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
376 // transform the event in a usable array
377 if (DBA::isResult($r)) {
378 $r = Event::sortByDate($r);
379 $events = Event::prepareListForTemplate($r);
382 if ($a->argc > 1 && $a->argv[1] === 'json') {
383 header('Content-Type: application/json');
384 echo json_encode($events);
388 if (!empty($_GET['id'])) {
389 $tpl = Renderer::getMarkupTemplate("event.tpl");
391 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
394 // Get rid of dashes in key names, Smarty3 can't handle them
395 foreach ($events as $key => $event) {
397 foreach ($event['item'] as $k => $v) {
398 $k = str_replace('-', '_', $k);
399 $event_item[$k] = $v;
401 $events[$key]['item'] = $event_item;
404 // ACL blocks are loaded in modals in frio
405 DI::page()->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
406 DI::page()->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
407 DI::page()->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
408 DI::page()->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
410 $o = Renderer::replaceMacros($tpl, [
412 '$title' => DI::l10n()->t('Events'),
413 '$view' => DI::l10n()->t('View'),
414 '$new_event' => [DI::baseUrl() . '/events/new', DI::l10n()->t('Create New Event'), '', ''],
415 '$previous' => [DI::baseUrl() . '/events/$prevyear/$prevmonth', DI::l10n()->t('Previous'), '', ''],
416 '$next' => [DI::baseUrl() . '/events/$nextyear/$nextmonth', DI::l10n()->t('Next'), '', ''],
417 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
419 '$events' => $events,
421 '$today' => DI::l10n()->t('today'),
422 '$month' => DI::l10n()->t('month'),
423 '$week' => DI::l10n()->t('week'),
424 '$day' => DI::l10n()->t('day'),
425 '$list' => DI::l10n()->t('list'),
428 if (!empty($_GET['id'])) {
436 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
437 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
441 if (DBA::isResult($r)) {
446 // Passed parameters overrides anything found in the DB
447 if (in_array($mode, ['edit', 'new', 'copy'])) {
449 $share_disabled = '';
451 if (empty($orig_event)) {
452 $orig_event = User::getById(local_user(), ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']);;
453 } elseif ($orig_event['allow_cid'] !== '<' . local_user() . '>'
454 || $orig_event['allow_gid']
455 || $orig_event['deny_cid']
456 || $orig_event['deny_gid']) {
457 $share_checked = ' checked="checked" ';
460 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
461 if (!empty($_REQUEST['nofinish'])) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
462 if (!empty($_REQUEST['adjust'])) {$orig_event['adjust'] = $_REQUEST['adjust'];}
463 if (!empty($_REQUEST['summary'])) {$orig_event['summary'] = $_REQUEST['summary'];}
464 if (!empty($_REQUEST['desc'])) {$orig_event['desc'] = $_REQUEST['desc'];}
465 if (!empty($_REQUEST['location'])) {$orig_event['location'] = $_REQUEST['location'];}
466 if (!empty($_REQUEST['start'])) {$orig_event['start'] = $_REQUEST['start'];}
467 if (!empty($_REQUEST['finish'])) {$orig_event['finish'] = $_REQUEST['finish'];}
469 $n_checked = (!empty($orig_event['nofinish']) ? ' checked="checked" ' : '');
470 $a_checked = (!empty($orig_event['adjust']) ? ' checked="checked" ' : '');
472 $t_orig = $orig_event['summary'] ?? '';
473 $d_orig = $orig_event['desc'] ?? '';
474 $l_orig = $orig_event['location'] ?? '';
475 $eid = $orig_event['id'] ?? 0;
476 $cid = $orig_event['cid'] ?? 0;
477 $uri = $orig_event['uri'] ?? '';
479 if ($cid || $mode === 'edit') {
480 $share_disabled = 'disabled="disabled"';
483 $sdt = $orig_event['start'] ?? 'now';
484 $fdt = $orig_event['finish'] ?? 'now';
486 $tz = date_default_timezone_get();
487 if (!empty($orig_event)) {
488 $tz = ($orig_event['adjust'] ? date_default_timezone_get() : 'UTC');
491 $syear = DateTimeFormat::convert($sdt, $tz, 'UTC', 'Y');
492 $smonth = DateTimeFormat::convert($sdt, $tz, 'UTC', 'm');
493 $sday = DateTimeFormat::convert($sdt, $tz, 'UTC', 'd');
495 $shour = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'H') : '00';
496 $sminute = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'i') : '00';
498 $fyear = DateTimeFormat::convert($fdt, $tz, 'UTC', 'Y');
499 $fmonth = DateTimeFormat::convert($fdt, $tz, 'UTC', 'm');
500 $fday = DateTimeFormat::convert($fdt, $tz, 'UTC', 'd');
502 $fhour = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'H') : '00';
503 $fminute = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'i') : '00';
505 if (!$cid && in_array($mode, ['new', 'copy'])) {
506 $acl = ACL::getFullSelectorHTML(DI::page(), $a->user, false, ACL::getDefaultUserPermissions($orig_event));
511 // If we copy an old event, we need to remove the ID and URI
512 // from the original event.
513 if ($mode === 'copy') {
518 $tpl = Renderer::getMarkupTemplate('event_form.tpl');
520 $o .= Renderer::replaceMacros($tpl, [
521 '$post' => DI::baseUrl() . '/events',
526 '$title' => DI::l10n()->t('Event details'),
527 '$desc' => DI::l10n()->t('Starting date and Title are required.'),
528 '$s_text' => DI::l10n()->t('Event Starts:') . ' <span class="required" title="' . DI::l10n()->t('Required') . '">*</span>',
529 '$s_dsel' => Temporal::getDateTimeField(
531 DateTime::createFromFormat('Y', intval($syear) + 5),
532 DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"),
533 DI::l10n()->t('Event Starts:'),
541 '$n_text' => DI::l10n()->t('Finish date/time is not known or not relevant'),
542 '$n_checked' => $n_checked,
543 '$f_text' => DI::l10n()->t('Event Finishes:'),
544 '$f_dsel' => Temporal::getDateTimeField(
546 DateTime::createFromFormat('Y', intval($fyear) + 5),
547 DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"),
548 DI::l10n()->t('Event Finishes:'),
554 '$a_text' => DI::l10n()->t('Adjust for viewer timezone'),
555 '$a_checked' => $a_checked,
556 '$d_text' => DI::l10n()->t('Description:'),
557 '$d_orig' => $d_orig,
558 '$l_text' => DI::l10n()->t('Location:'),
559 '$l_orig' => $l_orig,
560 '$t_text' => DI::l10n()->t('Title:') . ' <span class="required" title="' . DI::l10n()->t('Required') . '">*</span>',
561 '$t_orig' => $t_orig,
562 '$summary' => ['summary', DI::l10n()->t('Title:'), $t_orig, '', '*'],
563 '$sh_text' => DI::l10n()->t('Share this event'),
564 '$share' => ['share', DI::l10n()->t('Share this event'), $share_checked, '', $share_disabled],
565 '$sh_checked' => $share_checked,
566 '$nofinish' => ['nofinish', DI::l10n()->t('Finish date/time is not known or not relevant'), $n_checked],
567 '$adjust' => ['adjust', DI::l10n()->t('Adjust for viewer timezone'), $a_checked],
568 '$preview' => DI::l10n()->t('Preview'),
570 '$submit' => DI::l10n()->t('Submit'),
571 '$basic' => DI::l10n()->t('Basic'),
572 '$advanced' => DI::l10n()->t('Advanced'),
573 '$permissions' => DI::l10n()->t('Permissions'),
579 // Remove an event from the calendar and its related items
580 if ($mode === 'drop' && $event_id) {
581 $ev = Event::getListById(local_user(), $event_id);
583 // Delete only real events (no birthdays)
584 if (DBA::isResult($ev) && $ev[0]['type'] == 'event') {
585 Item::deleteForUser(['id' => $ev[0]['itemid']], local_user());
588 if (Item::exists(['id' => $ev[0]['itemid']])) {
589 notice(DI::l10n()->t('Failed to remove event'));
592 DI::baseUrl()->redirect('events');