3 * @copyright Copyright (C) 2010-2021, the Friendica project
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\Protocol;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Theme;
31 use Friendica\Core\Worker;
32 use Friendica\Database\DBA;
34 use Friendica\Model\Conversation;
35 use Friendica\Model\Event;
36 use Friendica\Model\Item;
37 use Friendica\Model\Post;
38 use Friendica\Model\User;
39 use Friendica\Module\BaseProfile;
40 use Friendica\Module\Security\Login;
41 use Friendica\Util\DateTimeFormat;
42 use Friendica\Util\Strings;
43 use Friendica\Util\Temporal;
44 use Friendica\Worker\Delivery;
46 function events_init(App $a)
52 // If it's a json request abort here because we don't
53 // need the widget data
54 if (DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] === 'json') {
58 if (empty(DI::page()['aside'])) {
59 DI::page()['aside'] = '';
62 $cal_widget = CalendarExport::getHTML(local_user());
64 DI::page()['aside'] .= $cal_widget;
69 function events_post(App $a)
71 Logger::debug('post', ['request' => $_REQUEST]);
76 $event_id = !empty($_POST['event_id']) ? intval($_POST['event_id']) : 0;
77 $cid = !empty($_POST['cid']) ? intval($_POST['cid']) : 0;
80 $start_text = Strings::escapeHtml($_REQUEST['start_text'] ?? '');
81 $finish_text = Strings::escapeHtml($_REQUEST['finish_text'] ?? '');
83 $adjust = intval($_POST['adjust'] ?? 0);
84 $nofinish = intval($_POST['nofinish'] ?? 0);
86 $share = intval($_POST['share'] ?? 0);
88 // The default setting for the `private` field in event_store() is false, so mirror that
89 $private_event = false;
91 $start = DBA::NULL_DATETIME;
92 $finish = DBA::NULL_DATETIME;
99 $finish = $finish_text;
103 $start = DateTimeFormat::convert($start, 'UTC', $a->getTimeZone());
105 $finish = DateTimeFormat::convert($finish, 'UTC', $a->getTimeZone());
108 $start = DateTimeFormat::utc($start);
110 $finish = DateTimeFormat::utc($finish);
114 // Don't allow the event to finish before it begins.
115 // It won't hurt anything, but somebody will file a bug report
116 // and we'll waste a bunch of time responding to it. Time that
117 // could've been spent doing something else.
119 $summary = trim($_POST['summary'] ?? '');
120 $desc = trim($_POST['desc'] ?? '');
121 $location = trim($_POST['location'] ?? '');
125 'summary' => $summary,
126 'description' => $desc,
127 'location' => $location,
128 'start' => $start_text,
129 'finish' => $finish_text,
131 'nofinish' => $nofinish,
134 $action = ($event_id == '') ? 'new' : 'event/' . $event_id;
135 $onerror_path = 'events/' . $action . '?' . http_build_query($params, '', '&', PHP_QUERY_RFC3986);
137 if (strcmp($finish, $start) < 0 && !$nofinish) {
138 notice(DI::l10n()->t('Event can not end before it has started.'));
139 if (intval($_REQUEST['preview'])) {
140 echo DI::l10n()->t('Event can not end before it has started.');
143 DI::baseUrl()->redirect($onerror_path);
146 if (!$summary || ($start === DBA::NULL_DATETIME)) {
147 notice(DI::l10n()->t('Event title and start time are required.'));
148 if (intval($_REQUEST['preview'])) {
149 echo DI::l10n()->t('Event title and start time are required.');
152 DI::baseUrl()->redirect($onerror_path);
155 $self = \Friendica\Model\Contact::getPublicIdByUserId($uid);
157 $aclFormatter = DI::aclFormatter();
160 $user = User::getById($uid, ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']);
161 if (!DBA::isResult($user)) {
165 $str_contact_allow = isset($_REQUEST['contact_allow']) ? $aclFormatter->toString($_REQUEST['contact_allow']) : $user['allow_cid'] ?? '';
166 $str_group_allow = isset($_REQUEST['group_allow']) ? $aclFormatter->toString($_REQUEST['group_allow']) : $user['allow_gid'] ?? '';
167 $str_contact_deny = isset($_REQUEST['contact_deny']) ? $aclFormatter->toString($_REQUEST['contact_deny']) : $user['deny_cid'] ?? '';
168 $str_group_deny = isset($_REQUEST['group_deny']) ? $aclFormatter->toString($_REQUEST['group_deny']) : $user['deny_gid'] ?? '';
170 $visibility = $_REQUEST['visibility'] ?? '';
171 if ($visibility === 'public') {
172 // The ACL selector introduced in version 2019.12 sends ACL input data even when the Public visibility is selected
173 $str_contact_allow = $str_group_allow = $str_contact_deny = $str_group_deny = '';
174 } else if ($visibility === 'custom') {
175 // Since we know from the visibility parameter the item should be private, we have to prevent the empty ACL
176 // case that would make it public. So we always append the author's contact id to the allowed contacts.
177 // See https://github.com/friendica/friendica/issues/9672
178 $str_contact_allow .= $aclFormatter->toString($self);
181 $str_contact_allow = $aclFormatter->toString($self);
182 $str_group_allow = $str_contact_deny = $str_group_deny = '';
185 // Make sure to set the `private` field as true. This is necessary to
186 // have the posts show up correctly in Diaspora if an event is created
187 // as visible only to self at first, but then edited to display to others.
188 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
189 $private_event = true;
193 $datarray['start'] = $start;
194 $datarray['finish'] = $finish;
195 $datarray['summary'] = $summary;
196 $datarray['desc'] = $desc;
197 $datarray['location'] = $location;
198 $datarray['type'] = $type;
199 $datarray['adjust'] = $adjust;
200 $datarray['nofinish'] = $nofinish;
201 $datarray['uid'] = $uid;
202 $datarray['cid'] = $cid;
203 $datarray['allow_cid'] = $str_contact_allow;
204 $datarray['allow_gid'] = $str_group_allow;
205 $datarray['deny_cid'] = $str_contact_deny;
206 $datarray['deny_gid'] = $str_group_deny;
207 $datarray['private'] = $private_event;
208 $datarray['id'] = $event_id;
210 if (intval($_REQUEST['preview'])) {
211 $html = Event::getHTML($datarray);
216 $event_id = Event::store($datarray);
218 $item = ['network' => Protocol::DFRN, 'protocol' => Conversation::PARCEL_DIRECT, 'direction' => Conversation::PUSH];
219 $item = Event::getItemArrayForId($event_id, $item);
220 if (Item::insert($item)) {
221 $uri_id = $item['uri-id'];
226 if (!$cid && $uri_id) {
227 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, (int)$uri_id, (int)$uid);
230 DI::baseUrl()->redirect('events');
233 function events_content(App $a)
236 notice(DI::l10n()->t('Permission denied.'));
237 return Login::form();
240 if (DI::args()->getArgc() == 1) {
241 $_SESSION['return_path'] = DI::args()->getCommand();
244 if ((DI::args()->getArgc() > 2) && (DI::args()->getArgv()[1] === 'ignore') && intval(DI::args()->getArgv()[2])) {
245 DBA::update('event', ['ignore' => true], ['id' => DI::args()->getArgv()[2], 'uid' => local_user()]);
248 if ((DI::args()->getArgc() > 2) && (DI::args()->getArgv()[1] === 'unignore') && intval(DI::args()->getArgv()[2])) {
249 DBA::update('event', ['ignore' => false], ['id' => DI::args()->getArgv()[2], 'uid' => local_user()]);
252 if ($a->getThemeInfoValue('events_in_profile')) {
253 Nav::setSelected('home');
255 Nav::setSelected('events');
258 // get the translation strings for the callendar
259 $i18n = Event::getStrings();
261 DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.min.css');
262 DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.print.min.css', 'print');
263 DI::page()->registerFooterScript('view/asset/moment/min/moment-with-locales.min.js');
264 DI::page()->registerFooterScript('view/asset/fullcalendar/dist/fullcalendar.min.js');
266 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
267 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
268 '$module_url' => '/events',
276 if ($a->getThemeInfoValue('events_in_profile')) {
277 $tabs = BaseProfile::getTabsHTML($a, 'events', true, $a->getLoggedInUserNickname(), false);
283 $ignored = !empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0;
285 if (DI::args()->getArgc() > 1) {
286 if (DI::args()->getArgc() > 2 && DI::args()->getArgv()[1] == 'event') {
288 $event_id = intval(DI::args()->getArgv()[2]);
290 if (DI::args()->getArgc() > 2 && DI::args()->getArgv()[1] == 'drop') {
292 $event_id = intval(DI::args()->getArgv()[2]);
294 if (DI::args()->getArgc() > 2 && DI::args()->getArgv()[1] == 'copy') {
296 $event_id = intval(DI::args()->getArgv()[2]);
298 if (DI::args()->getArgv()[1] === 'new') {
302 if (DI::args()->getArgc() > 2 && intval(DI::args()->getArgv()[1]) && intval(DI::args()->getArgv()[2])) {
304 $y = intval(DI::args()->getArgv()[1]);
305 $m = intval(DI::args()->getArgv()[2]);
309 // The view mode part is similiar to /mod/cal.php
310 if ($mode == 'view') {
311 $thisyear = DateTimeFormat::localNow('Y');
312 $thismonth = DateTimeFormat::localNow('m');
314 $y = intval($thisyear);
317 $m = intval($thismonth);
320 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
321 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
330 $dim = Temporal::getDaysInMonth($y, $m);
331 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
332 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
334 if (DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] === 'json') {
335 if (!empty($_GET['start'])) {
336 $start = $_GET['start'];
338 if (!empty($_GET['end'])) {
339 $finish = $_GET['end'];
343 $start = DateTimeFormat::utc($start);
344 $finish = DateTimeFormat::utc($finish);
346 $adjust_start = DateTimeFormat::local($start);
347 $adjust_finish = DateTimeFormat::local($finish);
349 // put the event parametes in an array so we can better transmit them
351 'event_id' => intval($_GET['id'] ?? 0),
354 'adjust_start' => $adjust_start,
355 'adjust_finish' => $adjust_finish,
356 'ignore' => $ignored,
359 // get events by id or by date
360 if ($event_params['event_id']) {
361 $r = Event::getListById(local_user(), $event_params['event_id']);
363 $r = Event::getListByDate(local_user(), $event_params);
368 if (DBA::isResult($r)) {
369 $r = Event::sortByDate($r);
370 foreach ($r as $rr) {
371 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
372 if (empty($links[$j])) {
373 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
380 // transform the event in a usable array
381 if (DBA::isResult($r)) {
382 $r = Event::sortByDate($r);
383 $events = Event::prepareListForTemplate($r);
386 if (DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] === 'json') {
387 header('Content-Type: application/json');
388 echo json_encode($events);
392 if (!empty($_GET['id'])) {
393 $tpl = Renderer::getMarkupTemplate("event.tpl");
395 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
398 // Get rid of dashes in key names, Smarty3 can't handle them
399 foreach ($events as $key => $event) {
401 foreach ($event['item'] as $k => $v) {
402 $k = str_replace('-', '_', $k);
403 $event_item[$k] = $v;
405 $events[$key]['item'] = $event_item;
408 // ACL blocks are loaded in modals in frio
409 DI::page()->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
410 DI::page()->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
411 DI::page()->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
412 DI::page()->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
414 $o = Renderer::replaceMacros($tpl, [
416 '$title' => DI::l10n()->t('Events'),
417 '$view' => DI::l10n()->t('View'),
418 '$new_event' => [DI::baseUrl() . '/events/new', DI::l10n()->t('Create New Event'), '', ''],
419 '$previous' => [DI::baseUrl() . '/events/$prevyear/$prevmonth', DI::l10n()->t('Previous'), '', ''],
420 '$next' => [DI::baseUrl() . '/events/$nextyear/$nextmonth', DI::l10n()->t('Next'), '', ''],
421 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
423 '$events' => $events,
425 '$today' => DI::l10n()->t('today'),
426 '$month' => DI::l10n()->t('month'),
427 '$week' => DI::l10n()->t('week'),
428 '$day' => DI::l10n()->t('day'),
429 '$list' => DI::l10n()->t('list'),
432 if (!empty($_GET['id'])) {
440 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
441 $orig_event = DBA::selectFirst('event', [], ['id' => $event_id, 'uid' => local_user()]);
444 // Passed parameters overrides anything found in the DB
445 if (in_array($mode, ['edit', 'new', 'copy'])) {
447 $share_disabled = '';
449 if (empty($orig_event)) {
450 $orig_event = User::getById(local_user(), ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']);;
451 } elseif ($orig_event['allow_cid'] !== '<' . local_user() . '>'
452 || $orig_event['allow_gid']
453 || $orig_event['deny_cid']
454 || $orig_event['deny_gid']) {
455 $share_checked = ' checked="checked" ';
458 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
459 if (!empty($_REQUEST['nofinish'])) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
460 if (!empty($_REQUEST['adjust'])) {$orig_event['adjust'] = $_REQUEST['adjust'];}
461 if (!empty($_REQUEST['summary'])) {$orig_event['summary'] = $_REQUEST['summary'];}
462 if (!empty($_REQUEST['desc'])) {$orig_event['desc'] = $_REQUEST['desc'];}
463 if (!empty($_REQUEST['location'])) {$orig_event['location'] = $_REQUEST['location'];}
464 if (!empty($_REQUEST['start'])) {$orig_event['start'] = $_REQUEST['start'];}
465 if (!empty($_REQUEST['finish'])) {$orig_event['finish'] = $_REQUEST['finish'];}
467 $n_checked = (!empty($orig_event['nofinish']) ? ' checked="checked" ' : '');
468 $a_checked = (!empty($orig_event['adjust']) ? ' checked="checked" ' : '');
470 $t_orig = $orig_event['summary'] ?? '';
471 $d_orig = $orig_event['desc'] ?? '';
472 $l_orig = $orig_event['location'] ?? '';
473 $eid = $orig_event['id'] ?? 0;
474 $cid = $orig_event['cid'] ?? 0;
475 $uri = $orig_event['uri'] ?? '';
477 if ($cid || $mode === 'edit') {
478 $share_disabled = 'disabled="disabled"';
481 $sdt = $orig_event['start'] ?? 'now';
482 $fdt = $orig_event['finish'] ?? 'now';
484 $tz = $a->getTimeZone();
485 if (isset($orig_event['adjust'])) {
486 $tz = ($orig_event['adjust'] ? $a->getTimeZone() : 'UTC');
489 $syear = DateTimeFormat::convert($sdt, $tz, 'UTC', 'Y');
490 $smonth = DateTimeFormat::convert($sdt, $tz, 'UTC', 'm');
491 $sday = DateTimeFormat::convert($sdt, $tz, 'UTC', 'd');
493 $shour = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'H') : '00';
494 $sminute = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'i') : '00';
496 $fyear = DateTimeFormat::convert($fdt, $tz, 'UTC', 'Y');
497 $fmonth = DateTimeFormat::convert($fdt, $tz, 'UTC', 'm');
498 $fday = DateTimeFormat::convert($fdt, $tz, 'UTC', 'd');
500 $fhour = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'H') : '00';
501 $fminute = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'i') : '00';
503 if (!$cid && in_array($mode, ['new', 'copy'])) {
504 $acl = ACL::getFullSelectorHTML(DI::page(), $a->getLoggedInUserId(), false, ACL::getDefaultUserPermissions($orig_event));
509 // If we copy an old event, we need to remove the ID and URI
510 // from the original event.
511 if ($mode === 'copy') {
516 $tpl = Renderer::getMarkupTemplate('event_form.tpl');
518 $o .= Renderer::replaceMacros($tpl, [
519 '$post' => DI::baseUrl() . '/events',
524 '$title' => DI::l10n()->t('Event details'),
525 '$desc' => DI::l10n()->t('Starting date and Title are required.'),
526 '$s_text' => DI::l10n()->t('Event Starts:') . ' <span class="required" title="' . DI::l10n()->t('Required') . '">*</span>',
527 '$s_dsel' => Temporal::getDateTimeField(
529 DateTime::createFromFormat('Y', intval($syear) + 5),
530 DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"),
531 DI::l10n()->t('Event Starts:'),
539 '$n_text' => DI::l10n()->t('Finish date/time is not known or not relevant'),
540 '$n_checked' => $n_checked,
541 '$f_text' => DI::l10n()->t('Event Finishes:'),
542 '$f_dsel' => Temporal::getDateTimeField(
544 DateTime::createFromFormat('Y', intval($fyear) + 5),
545 DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"),
546 DI::l10n()->t('Event Finishes:'),
552 '$a_text' => DI::l10n()->t('Adjust for viewer timezone'),
553 '$a_checked' => $a_checked,
554 '$d_text' => DI::l10n()->t('Description:'),
555 '$d_orig' => $d_orig,
556 '$l_text' => DI::l10n()->t('Location:'),
557 '$l_orig' => $l_orig,
558 '$t_text' => DI::l10n()->t('Title:') . ' <span class="required" title="' . DI::l10n()->t('Required') . '">*</span>',
559 '$t_orig' => $t_orig,
560 '$summary' => ['summary', DI::l10n()->t('Title:'), $t_orig, '', '*'],
561 '$sh_text' => DI::l10n()->t('Share this event'),
562 '$share' => ['share', DI::l10n()->t('Share this event'), $share_checked, '', $share_disabled],
563 '$sh_checked' => $share_checked,
564 '$nofinish' => ['nofinish', DI::l10n()->t('Finish date/time is not known or not relevant'), $n_checked],
565 '$adjust' => ['adjust', DI::l10n()->t('Adjust for viewer timezone'), $a_checked],
566 '$preview' => DI::l10n()->t('Preview'),
568 '$submit' => DI::l10n()->t('Submit'),
569 '$basic' => DI::l10n()->t('Basic'),
570 '$advanced' => DI::l10n()->t('Advanced'),
571 '$permissions' => DI::l10n()->t('Permissions'),
577 // Remove an event from the calendar and its related items
578 if ($mode === 'drop' && $event_id) {
579 $ev = Event::getListById(local_user(), $event_id);
581 // Delete only real events (no birthdays)
582 if (DBA::isResult($ev) && $ev[0]['type'] == 'event') {
583 Item::deleteForUser(['id' => $ev[0]['itemid']], local_user());
586 if (Post::exists(['id' => $ev[0]['itemid']])) {
587 notice(DI::l10n()->t('Failed to remove event'));
590 DI::baseUrl()->redirect('events');