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\System;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBM;
15 use Friendica\Model\Event;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Temporal;
21 require_once 'include/items.php';
23 function events_init(App $a) {
28 // If it's a json request abort here because we don't
29 // need the widget data
30 if ($a->argc > 1 && $a->argv[1] === 'json') {
34 if (empty($a->page['aside'])) {
35 $a->page['aside'] = '';
38 $a->data['user'] = $_SESSION['user'];
40 $cal_widget = CalendarExport::getHTML();
42 $a->page['aside'] .= $cal_widget;
47 function events_post(App $a) {
49 logger('post: ' . print_r($_REQUEST, true), LOGGER_DATA);
55 $event_id = ((x($_POST, 'event_id')) ? intval($_POST['event_id']) : 0);
56 $cid = ((x($_POST, 'cid')) ? intval($_POST['cid']) : 0);
59 $start_text = escape_tags($_REQUEST['start_text']);
60 $finish_text = escape_tags($_REQUEST['finish_text']);
62 $adjust = intval($_POST['adjust']);
63 $nofinish = intval($_POST['nofinish']);
65 // The default setting for the `private` field in event_store() is false, so mirror that
66 $private_event = false;
76 $finish = $finish_text;
80 $start = DateTimeFormat::convert($start, 'UTC', date_default_timezone_get());
82 $finish = DateTimeFormat::convert($finish, 'UTC', date_default_timezone_get());
85 $start = DateTimeFormat::utc($start);
87 $finish = DateTimeFormat::utc($finish);
91 // Don't allow the event to finish before it begins.
92 // It won't hurt anything, but somebody will file a bug report
93 // and we'll waste a bunch of time responding to it. Time that
94 // could've been spent doing something else.
96 $summary = escape_tags(trim($_POST['summary']));
97 $desc = escape_tags(trim($_POST['desc']));
98 $location = escape_tags(trim($_POST['location']));
101 $action = ($event_id == '') ? 'new' : "event/" . $event_id;
102 $onerror_url = System::baseUrl() . "/events/" . $action . "?summary=$summary&description=$desc&location=$location&start=$start_text&finish=$finish_text&adjust=$adjust&nofinish=$nofinish";
104 if (strcmp($finish, $start) < 0 && !$nofinish) {
105 notice(L10n::t('Event can not end before it has started.') . EOL);
106 if (intval($_REQUEST['preview'])) {
107 echo L10n::t('Event can not end before it has started.');
110 goaway($onerror_url);
113 if ((! $summary) || ($start === NULL_DATE)) {
114 notice(L10n::t('Event title and start time are required.') . EOL);
115 if (intval($_REQUEST['preview'])) {
116 echo L10n::t('Event title and start time are required.');
119 goaway($onerror_url);
122 $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
124 $c = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
135 $str_group_allow = perms2str($_POST['group_allow']);
136 $str_contact_allow = perms2str($_POST['contact_allow']);
137 $str_group_deny = perms2str($_POST['group_deny']);
138 $str_contact_deny = perms2str($_POST['contact_deny']);
140 // Undo the pseudo-contact of self, since there are real contacts now
141 if (strpos($str_contact_allow, '<' . $self . '>') !== false ) {
142 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
144 // Make sure to set the `private` field as true. This is necessary to
145 // have the posts show up correctly in Diaspora if an event is created
146 // as visible only to self at first, but then edited to display to others.
147 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
148 $private_event = true;
151 // Note: do not set `private` field for self-only events. It will
152 // keep even you from seeing them!
153 $str_contact_allow = '<' . $self . '>';
154 $str_group_allow = $str_contact_deny = $str_group_deny = '';
159 $datarray['start'] = $start;
160 $datarray['finish'] = $finish;
161 $datarray['summary'] = $summary;
162 $datarray['desc'] = $desc;
163 $datarray['location'] = $location;
164 $datarray['type'] = $type;
165 $datarray['adjust'] = $adjust;
166 $datarray['nofinish'] = $nofinish;
167 $datarray['uid'] = $uid;
168 $datarray['cid'] = $cid;
169 $datarray['allow_cid'] = $str_contact_allow;
170 $datarray['allow_gid'] = $str_group_allow;
171 $datarray['deny_cid'] = $str_contact_deny;
172 $datarray['deny_gid'] = $str_group_deny;
173 $datarray['private'] = $private_event;
174 $datarray['id'] = $event_id;
176 if (intval($_REQUEST['preview'])) {
177 $html = Event::getHTML($datarray);
182 $item_id = Event::store($datarray);
185 Worker::add(PRIORITY_HIGH, "Notifier", "event", $item_id);
188 goaway($_SESSION['return_url']);
191 function events_content(App $a) {
193 if (! local_user()) {
194 notice(L10n::t('Permission denied.') . EOL);
199 $_SESSION['return_url'] = System::baseUrl() . '/' . $a->cmd;
202 if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
203 $r = q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
209 if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
210 $r = q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
216 if ($a->theme_events_in_profile) {
217 Nav::setSelected('home');
219 Nav::setSelected('events');
222 // get the translation strings for the callendar
223 $i18n = Event::getStrings();
225 $htpl = get_markup_template('event_head.tpl');
226 $a->page['htmlhead'] .= replace_macros($htpl, [
227 '$baseurl' => System::baseUrl(),
228 '$module_url' => '/events',
233 $etpl = get_markup_template('event_end.tpl');
234 $a->page['end'] .= replace_macros($etpl, [
235 '$baseurl' => System::baseUrl(),
241 if ($a->theme_events_in_profile) {
242 $tabs = Profile::getTabs($a, true);
248 $ignored = ((x($_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') {
277 $thisyear = DateTimeFormat::localNow('Y');
278 $thismonth = DateTimeFormat::localNow('m');
280 $y = intval($thisyear);
283 $m = intval($thismonth);
286 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
287 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
298 if ($nextmonth > 12) {
311 $dim = Temporal::getDaysInMonth($y, $m);
312 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
313 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
315 if ($a->argc > 1 && $a->argv[1] === 'json') {
316 if (x($_GET, 'start')) {
317 $start = $_GET['start'];
319 if (x($_GET, 'end')) {
320 $finish = $_GET['end'];
324 $start = DateTimeFormat::utc($start);
325 $finish = DateTimeFormat::utc($finish);
327 $adjust_start = DateTimeFormat::local($start);
328 $adjust_finish = DateTimeFormat::local($finish);
330 // put the event parametes in an array so we can better transmit them
332 'event_id' => intval(defaults($_GET, 'id', 0)),
335 'adjust_start' => $adjust_start,
336 'adjust_finish' => $adjust_finish,
337 'ignore' => $ignored,
340 // get events by id or by date
341 if ($event_params['event_id']) {
342 $r = Event::getListById(local_user(), $event_params['event_id']);
344 $r = Event::getListByDate(local_user(), $event_params);
349 if (DBM::is_result($r)) {
350 $r = Event::sortByDate($r);
351 foreach ($r as $rr) {
352 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
353 if (! x($links,$j)) {
354 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
361 // transform the event in a usable array
362 if (DBM::is_result($r)) {
363 $r = Event::sortByDate($r);
364 $events = Event::prepareListForTemplate($r);
367 if ($a->argc > 1 && $a->argv[1] === 'json'){
368 echo json_encode($events);
372 if (x($_GET, 'id')) {
373 $tpl = get_markup_template("event.tpl");
375 $tpl = get_markup_template("events_js.tpl");
378 // Get rid of dashes in key names, Smarty3 can't handle them
379 foreach ($events as $key => $event) {
381 foreach ($event['item'] as $k => $v) {
382 $k = str_replace('-' ,'_', $k);
383 $event_item[$k] = $v;
385 $events[$key]['item'] = $event_item;
388 $o = replace_macros($tpl, [
389 '$baseurl' => System::baseUrl(),
391 '$title' => L10n::t('Events'),
392 '$view' => L10n::t('View'),
393 '$new_event' => [System::baseUrl() . '/events/new', L10n::t('Create New Event'), '', ''],
394 '$previous' => [System::baseUrl() . '/events/$prevyear/$prevmonth', L10n::t('Previous'), '', ''],
395 '$next' => [System::baseUrl() . '/events/$nextyear/$nextmonth', L10n::t('Next'), '', ''],
396 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
398 '$events' => $events,
400 '$today' => L10n::t('today'),
401 '$month' => L10n::t('month'),
402 '$week' => L10n::t('week'),
403 '$day' => L10n::t('day'),
404 '$list' => L10n::t('list'),
407 if (x($_GET, 'id')) {
415 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
416 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
420 if (DBM::is_result($r)) {
425 // Passed parameters overrides anything found in the DB
426 if (in_array($mode, ['edit', 'new', 'copy'])) {
427 if (!x($orig_event)) {$orig_event = [];}
428 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
429 if (x($_REQUEST, 'nofinish')) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
430 if (x($_REQUEST, 'adjust')) {$orig_event['adjust'] = $_REQUEST['adjust'];}
431 if (x($_REQUEST, 'summary')) {$orig_event['summary'] = $_REQUEST['summary'];}
432 if (x($_REQUEST, 'description')) {$orig_event['description'] = $_REQUEST['description'];}
433 if (x($_REQUEST, 'location')) {$orig_event['location'] = $_REQUEST['location'];}
434 if (x($_REQUEST, 'start')) {$orig_event['start'] = $_REQUEST['start'];}
435 if (x($_REQUEST, 'finish')) {$orig_event['finish'] = $_REQUEST['finish'];}
436 if (x($_REQUEST,'finish')) $orig_event['finish'] = $_REQUEST['finish'];
438 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
439 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
441 $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
442 $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
443 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
444 $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
445 $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
446 $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
451 if (x($orig_event)) {
452 $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ');
455 if ($cid || $mode === 'edit') {
456 $sh_disabled = 'disabled="disabled"';
459 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
460 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
462 $tz = date_default_timezone_get();
463 if (x($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 = ((x($orig_event)) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'H') : '00');
472 $sminute = ((x($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 = ((x($orig_event)) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'H') : '00');
479 $fminute = ((x($orig_event)) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'i') : '00');
481 $perms = ACL::getDefaultUserPermissions($orig_event);
483 if ($mode === 'new' || $mode === 'copy') {
484 $acl = (($cid) ? '' : ACL::getFullSelectorHTML(((x($orig_event)) ? $orig_event : $a->user)));
487 // If we copy an old event, we need to remove the ID and URI
488 // from the original event.
489 if ($mode === 'copy') {
494 $tpl = get_markup_template('event_form.tpl');
496 $o .= replace_macros($tpl,[
497 '$post' => System::baseUrl() . '/events',
502 '$allow_cid' => json_encode($perms['allow_cid']),
503 '$allow_gid' => json_encode($perms['allow_gid']),
504 '$deny_cid' => json_encode($perms['deny_cid']),
505 '$deny_gid' => json_encode($perms['deny_gid']),
507 '$title' => L10n::t('Event details'),
508 '$desc' => L10n::t('Starting date and Title are required.'),
509 '$s_text' => L10n::t('Event Starts:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
510 '$s_dsel' => Temporal::getDateTimeField(new DateTime(), DateTime::createFromFormat('Y', $syear+5), DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"), L10n::t('Event Starts:'), 'start_text', true, true, '', '', true),
511 '$n_text' => L10n::t('Finish date/time is not known or not relevant'),
512 '$n_checked' => $n_checked,
513 '$f_text' => L10n::t('Event Finishes:'),
514 '$f_dsel' => Temporal::getDateTimeField(new DateTime(), DateTime::createFromFormat('Y', $fyear+5), DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"), L10n::t('Event Finishes:'), 'finish_text', true, true, 'start_text'),
515 '$a_text' => L10n::t('Adjust for viewer timezone'),
516 '$a_checked' => $a_checked,
517 '$d_text' => L10n::t('Description:'),
518 '$d_orig' => $d_orig,
519 '$l_text' => L10n::t('Location:'),
520 '$l_orig' => $l_orig,
521 '$t_text' => L10n::t('Title:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
522 '$t_orig' => $t_orig,
523 '$summary' => ['summary', L10n::t('Title:'), $t_orig, '', '*'],
524 '$sh_text' => L10n::t('Share this event'),
525 '$share' => ['share', L10n::t('Share this event'), $sh_checked, '', $sh_disabled],
526 '$sh_checked' => $sh_checked,
527 '$nofinish' => ['nofinish', L10n::t('Finish date/time is not known or not relevant'), $n_checked],
528 '$adjust' => ['adjust', L10n::t('Adjust for viewer timezone'), $a_checked],
529 '$preview' => L10n::t('Preview'),
531 '$submit' => L10n::t('Submit'),
532 '$basic' => L10n::t('Basic'),
533 '$advanced' => L10n::t('Advanced'),
534 '$permissions' => L10n::t('Permissions'),
541 // Remove an event from the calendar and its related items
542 if ($mode === 'drop' && $event_id) {
545 $ev = Event::getListById(local_user(), $event_id);
547 // Delete only real events (no birthdays)
548 if (DBM::is_result($ev) && $ev[0]['type'] == 'event') {
549 $del = Item::deleteForUser(['id' => $ev[0]['itemid']], local_user());
553 notice(L10n::t('Failed to remove event') . EOL);
555 info(L10n::t('Event removed') . EOL);
558 goaway(System::baseUrl() . '/events');