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 (empty(DI::page()['aside'])) {
53 DI::page()['aside'] = '';
56 $cal_widget = CalendarExport::getHTML(local_user());
58 DI::page()['aside'] .= $cal_widget;
63 function events_post(App $a)
65 Logger::debug('post', ['request' => $_REQUEST]);
70 $event_id = !empty($_POST['event_id']) ? intval($_POST['event_id']) : 0;
71 $cid = !empty($_POST['cid']) ? intval($_POST['cid']) : 0;
74 $start_text = Strings::escapeHtml($_REQUEST['start_text'] ?? '');
75 $finish_text = Strings::escapeHtml($_REQUEST['finish_text'] ?? '');
77 $nofinish = intval($_POST['nofinish'] ?? 0);
79 $share = intval($_POST['share'] ?? 0);
81 // The default setting for the `private` field in event_store() is false, so mirror that
82 $private_event = false;
84 $start = DBA::NULL_DATETIME;
85 $finish = DBA::NULL_DATETIME;
92 $finish = $finish_text;
95 $start = DateTimeFormat::convert($start, 'UTC', $a->getTimeZone());
97 $finish = DateTimeFormat::convert($finish, 'UTC', $a->getTimeZone());
100 // Don't allow the event to finish before it begins.
101 // It won't hurt anything, but somebody will file a bug report
102 // and we'll waste a bunch of time responding to it. Time that
103 // could've been spent doing something else.
105 $summary = trim($_POST['summary'] ?? '');
106 $desc = trim($_POST['desc'] ?? '');
107 $location = trim($_POST['location'] ?? '');
111 'summary' => $summary,
112 'description' => $desc,
113 'location' => $location,
114 'start' => $start_text,
115 'finish' => $finish_text,
116 'nofinish' => $nofinish,
119 $action = ($event_id == '') ? 'new' : 'event/' . $event_id;
120 $onerror_path = 'events/' . $action . '?' . http_build_query($params, '', '&', PHP_QUERY_RFC3986);
122 if (strcmp($finish, $start) < 0 && !$nofinish) {
123 notice(DI::l10n()->t('Event can not end before it has started.'));
124 if (intval($_REQUEST['preview'])) {
125 echo DI::l10n()->t('Event can not end before it has started.');
128 DI::baseUrl()->redirect($onerror_path);
131 if (!$summary || ($start === DBA::NULL_DATETIME)) {
132 notice(DI::l10n()->t('Event title and start time are required.'));
133 if (intval($_REQUEST['preview'])) {
134 echo DI::l10n()->t('Event title and start time are required.');
137 DI::baseUrl()->redirect($onerror_path);
140 $self = \Friendica\Model\Contact::getPublicIdByUserId($uid);
142 $aclFormatter = DI::aclFormatter();
145 $user = User::getById($uid, ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']);
146 if (!DBA::isResult($user)) {
150 $str_contact_allow = isset($_REQUEST['contact_allow']) ? $aclFormatter->toString($_REQUEST['contact_allow']) : $user['allow_cid'] ?? '';
151 $str_group_allow = isset($_REQUEST['group_allow']) ? $aclFormatter->toString($_REQUEST['group_allow']) : $user['allow_gid'] ?? '';
152 $str_contact_deny = isset($_REQUEST['contact_deny']) ? $aclFormatter->toString($_REQUEST['contact_deny']) : $user['deny_cid'] ?? '';
153 $str_group_deny = isset($_REQUEST['group_deny']) ? $aclFormatter->toString($_REQUEST['group_deny']) : $user['deny_gid'] ?? '';
155 $visibility = $_REQUEST['visibility'] ?? '';
156 if ($visibility === 'public') {
157 // The ACL selector introduced in version 2019.12 sends ACL input data even when the Public visibility is selected
158 $str_contact_allow = $str_group_allow = $str_contact_deny = $str_group_deny = '';
159 } else if ($visibility === 'custom') {
160 // Since we know from the visibility parameter the item should be private, we have to prevent the empty ACL
161 // case that would make it public. So we always append the author's contact id to the allowed contacts.
162 // See https://github.com/friendica/friendica/issues/9672
163 $str_contact_allow .= $aclFormatter->toString($self);
166 $str_contact_allow = $aclFormatter->toString($self);
167 $str_group_allow = $str_contact_deny = $str_group_deny = '';
170 // Make sure to set the `private` field as true. This is necessary to
171 // have the posts show up correctly in Diaspora if an event is created
172 // as visible only to self at first, but then edited to display to others.
173 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
174 $private_event = true;
178 $datarray['start'] = $start;
179 $datarray['finish'] = $finish;
180 $datarray['summary'] = $summary;
181 $datarray['desc'] = $desc;
182 $datarray['location'] = $location;
183 $datarray['type'] = $type;
184 $datarray['nofinish'] = $nofinish;
185 $datarray['uid'] = $uid;
186 $datarray['cid'] = $cid;
187 $datarray['allow_cid'] = $str_contact_allow;
188 $datarray['allow_gid'] = $str_group_allow;
189 $datarray['deny_cid'] = $str_contact_deny;
190 $datarray['deny_gid'] = $str_group_deny;
191 $datarray['private'] = $private_event;
192 $datarray['id'] = $event_id;
194 if (intval($_REQUEST['preview'])) {
195 $html = Event::getHTML($datarray);
200 $event_id = Event::store($datarray);
202 $item = ['network' => Protocol::DFRN, 'protocol' => Conversation::PARCEL_DIRECT, 'direction' => Conversation::PUSH];
203 $item = Event::getItemArrayForId($event_id, $item);
204 if (Item::insert($item)) {
205 $uri_id = $item['uri-id'];
210 if (!$cid && $uri_id) {
211 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, (int)$uri_id, (int)$uid);
214 DI::baseUrl()->redirect('events');
217 function events_content(App $a)
220 notice(DI::l10n()->t('Permission denied.'));
221 return Login::form();
224 if (DI::args()->getArgc() == 1) {
225 $_SESSION['return_path'] = DI::args()->getCommand();
228 if ((DI::args()->getArgc() > 2) && (DI::args()->getArgv()[1] === 'ignore') && intval(DI::args()->getArgv()[2])) {
229 DBA::update('event', ['ignore' => true], ['id' => DI::args()->getArgv()[2], 'uid' => local_user()]);
232 if ((DI::args()->getArgc() > 2) && (DI::args()->getArgv()[1] === 'unignore') && intval(DI::args()->getArgv()[2])) {
233 DBA::update('event', ['ignore' => false], ['id' => DI::args()->getArgv()[2], 'uid' => local_user()]);
236 if ($a->getThemeInfoValue('events_in_profile')) {
237 Nav::setSelected('home');
239 Nav::setSelected('events');
242 // get the translation strings for the callendar
243 $i18n = Event::getStrings();
245 DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.min.css');
246 DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.print.min.css', 'print');
247 DI::page()->registerFooterScript('view/asset/moment/min/moment-with-locales.min.js');
248 DI::page()->registerFooterScript('view/asset/fullcalendar/dist/fullcalendar.min.js');
250 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
251 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
252 '$module_url' => '/events',
260 if ($a->getThemeInfoValue('events_in_profile')) {
261 $tabs = BaseProfile::getTabsHTML($a, 'events', true, $a->getLoggedInUserNickname(), false);
267 $ignored = !empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0;
269 if (DI::args()->getArgc() > 1) {
270 if (DI::args()->getArgc() > 2 && DI::args()->getArgv()[1] == 'event') {
272 $event_id = intval(DI::args()->getArgv()[2]);
274 if (DI::args()->getArgc() > 2 && DI::args()->getArgv()[1] == 'drop') {
276 $event_id = intval(DI::args()->getArgv()[2]);
278 if (DI::args()->getArgc() > 2 && DI::args()->getArgv()[1] == 'copy') {
280 $event_id = intval(DI::args()->getArgv()[2]);
282 if (DI::args()->getArgv()[1] === 'new') {
286 if (DI::args()->getArgc() > 2 && intval(DI::args()->getArgv()[1]) && intval(DI::args()->getArgv()[2])) {
288 $y = intval(DI::args()->getArgv()[1]);
289 $m = intval(DI::args()->getArgv()[2]);
293 // The view mode part is similiar to /mod/cal.php
294 if ($mode == 'view') {
295 $thisyear = DateTimeFormat::localNow('Y');
296 $thismonth = DateTimeFormat::localNow('m');
298 $y = intval($thisyear);
301 $m = intval($thismonth);
304 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
305 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
314 $dim = Temporal::getDaysInMonth($y, $m);
315 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
316 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
318 // put the event parametes in an array so we can better transmit them
320 'event_id' => intval($_GET['id'] ?? 0),
323 'ignore' => $ignored,
326 // get events by id or by date
327 if ($event_params['event_id']) {
328 $r = Event::getListById(local_user(), $event_params['event_id']);
330 $r = Event::getListByDate(local_user(), $event_params);
335 if (DBA::isResult($r)) {
336 $r = Event::sortByDate($r);
337 foreach ($r as $rr) {
338 $j = DateTimeFormat::local($rr['start'], 'j');
339 if (empty($links[$j])) {
340 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
347 // transform the event in a usable array
348 if (DBA::isResult($r)) {
349 $r = Event::sortByDate($r);
350 $events = Event::prepareListForTemplate($r);
353 if (!empty($_GET['id'])) {
354 $tpl = Renderer::getMarkupTemplate("event.tpl");
356 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
359 // Get rid of dashes in key names, Smarty3 can't handle them
360 foreach ($events as $key => $event) {
362 foreach ($event['item'] as $k => $v) {
363 $k = str_replace('-', '_', $k);
364 $event_item[$k] = $v;
366 $events[$key]['item'] = $event_item;
369 // ACL blocks are loaded in modals in frio
370 DI::page()->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
371 DI::page()->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
372 DI::page()->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
373 DI::page()->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
375 $o = Renderer::replaceMacros($tpl, [
377 '$title' => DI::l10n()->t('Events'),
378 '$view' => DI::l10n()->t('View'),
379 '$new_event' => [DI::baseUrl() . '/events/new', DI::l10n()->t('Create New Event'), '', ''],
380 '$previous' => [DI::baseUrl() . '/events/$prevyear/$prevmonth', DI::l10n()->t('Previous'), '', ''],
381 '$next' => [DI::baseUrl() . '/events/$nextyear/$nextmonth', DI::l10n()->t('Next'), '', ''],
382 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
384 '$events' => $events,
386 '$today' => DI::l10n()->t('today'),
387 '$month' => DI::l10n()->t('month'),
388 '$week' => DI::l10n()->t('week'),
389 '$day' => DI::l10n()->t('day'),
390 '$list' => DI::l10n()->t('list'),
393 if (!empty($_GET['id'])) {
401 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
402 $orig_event = DBA::selectFirst('event', [], ['id' => $event_id, 'uid' => local_user()]);
405 // Passed parameters overrides anything found in the DB
406 if (in_array($mode, ['edit', 'new', 'copy'])) {
408 $share_disabled = '';
410 if (empty($orig_event)) {
411 $orig_event = User::getById(local_user(), ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']);;
412 } elseif ($orig_event['allow_cid'] !== '<' . local_user() . '>'
413 || $orig_event['allow_gid']
414 || $orig_event['deny_cid']
415 || $orig_event['deny_gid']) {
416 $share_checked = ' checked="checked" ';
419 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
420 if (!empty($_REQUEST['nofinish'])) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
421 if (!empty($_REQUEST['summary'])) {$orig_event['summary'] = $_REQUEST['summary'];}
422 if (!empty($_REQUEST['desc'])) {$orig_event['desc'] = $_REQUEST['desc'];}
423 if (!empty($_REQUEST['location'])) {$orig_event['location'] = $_REQUEST['location'];}
424 if (!empty($_REQUEST['start'])) {$orig_event['start'] = $_REQUEST['start'];}
425 if (!empty($_REQUEST['finish'])) {$orig_event['finish'] = $_REQUEST['finish'];}
427 $n_checked = (!empty($orig_event['nofinish']) ? ' checked="checked" ' : '');
429 $t_orig = $orig_event['summary'] ?? '';
430 $d_orig = $orig_event['desc'] ?? '';
431 $l_orig = $orig_event['location'] ?? '';
432 $eid = $orig_event['id'] ?? 0;
433 $cid = $orig_event['cid'] ?? 0;
434 $uri = $orig_event['uri'] ?? '';
436 if ($cid || $mode === 'edit') {
437 $share_disabled = 'disabled="disabled"';
440 $sdt = $orig_event['start'] ?? 'now';
441 $fdt = $orig_event['finish'] ?? 'now';
443 $syear = DateTimeFormat::local($sdt, 'Y');
444 $smonth = DateTimeFormat::local($sdt, 'm');
445 $sday = DateTimeFormat::local($sdt, 'd');
447 $shour = !empty($orig_event) ? DateTimeFormat::local($sdt, 'H') : '00';
448 $sminute = !empty($orig_event) ? DateTimeFormat::local($sdt, 'i') : '00';
450 $fyear = DateTimeFormat::local($fdt, 'Y');
451 $fmonth = DateTimeFormat::local($fdt, 'm');
452 $fday = DateTimeFormat::local($fdt, 'd');
454 $fhour = !empty($orig_event) ? DateTimeFormat::local($fdt, 'H') : '00';
455 $fminute = !empty($orig_event) ? DateTimeFormat::local($fdt, 'i') : '00';
457 if (!$cid && in_array($mode, ['new', 'copy'])) {
458 $acl = ACL::getFullSelectorHTML(DI::page(), $a->getLoggedInUserId(), false, ACL::getDefaultUserPermissions($orig_event));
463 // If we copy an old event, we need to remove the ID and URI
464 // from the original event.
465 if ($mode === 'copy') {
470 $tpl = Renderer::getMarkupTemplate('event_form.tpl');
472 $o .= Renderer::replaceMacros($tpl, [
473 '$post' => DI::baseUrl() . '/events',
478 '$title' => DI::l10n()->t('Event details'),
479 '$desc' => DI::l10n()->t('Starting date and Title are required.'),
480 '$s_text' => DI::l10n()->t('Event Starts:') . ' <span class="required" title="' . DI::l10n()->t('Required') . '">*</span>',
481 '$s_dsel' => Temporal::getDateTimeField(
483 DateTime::createFromFormat('Y', intval($syear) + 5),
484 DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"),
485 DI::l10n()->t('Event Starts:'),
493 '$n_text' => DI::l10n()->t('Finish date/time is not known or not relevant'),
494 '$n_checked' => $n_checked,
495 '$f_text' => DI::l10n()->t('Event Finishes:'),
496 '$f_dsel' => Temporal::getDateTimeField(
498 DateTime::createFromFormat('Y', intval($fyear) + 5),
499 DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"),
500 DI::l10n()->t('Event Finishes:'),
506 '$d_text' => DI::l10n()->t('Description:'),
507 '$d_orig' => $d_orig,
508 '$l_text' => DI::l10n()->t('Location:'),
509 '$l_orig' => $l_orig,
510 '$t_text' => DI::l10n()->t('Title:') . ' <span class="required" title="' . DI::l10n()->t('Required') . '">*</span>',
511 '$t_orig' => $t_orig,
512 '$summary' => ['summary', DI::l10n()->t('Title:'), $t_orig, '', '*'],
513 '$sh_text' => DI::l10n()->t('Share this event'),
514 '$share' => ['share', DI::l10n()->t('Share this event'), $share_checked, '', $share_disabled],
515 '$sh_checked' => $share_checked,
516 '$nofinish' => ['nofinish', DI::l10n()->t('Finish date/time is not known or not relevant'), $n_checked],
517 '$preview' => DI::l10n()->t('Preview'),
519 '$submit' => DI::l10n()->t('Submit'),
520 '$basic' => DI::l10n()->t('Basic'),
521 '$advanced' => DI::l10n()->t('Advanced'),
522 '$permissions' => DI::l10n()->t('Permissions'),
528 // Remove an event from the calendar and its related items
529 if ($mode === 'drop' && $event_id) {
530 $ev = Event::getListById(local_user(), $event_id);
532 // Delete only real events (no birthdays)
533 if (DBA::isResult($ev) && $ev[0]['type'] == 'event') {
534 Item::deleteForUser(['id' => $ev[0]['itemid']], local_user());
537 if (Post::exists(['id' => $ev[0]['itemid']])) {
538 notice(DI::l10n()->t('Failed to remove event'));
541 DI::baseUrl()->redirect('events');