4 * @brief The events module
7 use Friendica\Content\Nav;
8 use Friendica\Core\Config;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBM;
12 use Friendica\Model\Profile;
14 require_once 'include/bbcode.php';
15 require_once 'include/datetime.php';
16 require_once 'include/event.php';
17 require_once 'include/items.php';
19 function events_init(App $a) {
25 // If it's a json request abort here because we don't
26 // need the widget data
27 if ($a->argv[1] === 'json') {
31 $cal_widget = widget_events();
33 if (! x($a->page,'aside')) {
34 $a->page['aside'] = '';
37 $a->page['aside'] .= $cal_widget;
43 function events_post(App $a) {
45 logger('post: ' . print_r($_REQUEST, true), LOGGER_DATA);
51 $event_id = ((x($_POST, 'event_id')) ? intval($_POST['event_id']) : 0);
52 $cid = ((x($_POST, 'cid')) ? intval($_POST['cid']) : 0);
55 $start_text = escape_tags($_REQUEST['start_text']);
56 $finish_text = escape_tags($_REQUEST['finish_text']);
58 $adjust = intval($_POST['adjust']);
59 $nofinish = intval($_POST['nofinish']);
61 // The default setting for the `private` field in event_store() is false, so mirror that
62 $private_event = false;
72 $finish = $finish_text;
76 $start = datetime_convert(date_default_timezone_get(), 'UTC', $start);
78 $finish = datetime_convert(date_default_timezone_get(), 'UTC', $finish);
81 $start = datetime_convert('UTC', 'UTC', $start);
83 $finish = datetime_convert('UTC', 'UTC', $finish);
87 // Don't allow the event to finish before it begins.
88 // It won't hurt anything, but somebody will file a bug report
89 // and we'll waste a bunch of time responding to it. Time that
90 // could've been spent doing something else.
92 $summary = escape_tags(trim($_POST['summary']));
93 $desc = escape_tags(trim($_POST['desc']));
94 $location = escape_tags(trim($_POST['location']));
97 $action = ($event_id == '') ? 'new' : "event/" . $event_id;
98 $onerror_url = System::baseUrl() . "/events/" . $action . "?summary=$summary&description=$desc&location=$location&start=$start_text&finish=$finish_text&adjust=$adjust&nofinish=$nofinish";
100 if (strcmp($finish, $start) < 0 && !$nofinish) {
101 notice(t('Event can not end before it has started.') . EOL);
102 if (intval($_REQUEST['preview'])) {
103 echo t('Event can not end before it has started.');
106 goaway($onerror_url);
109 if ((! $summary) || ($start === NULL_DATE)) {
110 notice(t('Event title and start time are required.') . EOL);
111 if (intval($_REQUEST['preview'])) {
112 echo t('Event title and start time are required.');
115 goaway($onerror_url);
118 $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
120 $c = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
131 $str_group_allow = perms2str($_POST['group_allow']);
132 $str_contact_allow = perms2str($_POST['contact_allow']);
133 $str_group_deny = perms2str($_POST['group_deny']);
134 $str_contact_deny = perms2str($_POST['contact_deny']);
136 // Undo the pseudo-contact of self, since there are real contacts now
137 if (strpos($str_contact_allow, '<' . $self . '>') !== false ) {
138 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
140 // Make sure to set the `private` field as true. This is necessary to
141 // have the posts show up correctly in Diaspora if an event is created
142 // as visible only to self at first, but then edited to display to others.
143 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
144 $private_event = true;
147 // Note: do not set `private` field for self-only events. It will
148 // keep even you from seeing them!
149 $str_contact_allow = '<' . $self . '>';
150 $str_group_allow = $str_contact_deny = $str_group_deny = '';
155 $datarray['guid'] = get_guid(32);
156 $datarray['start'] = $start;
157 $datarray['finish'] = $finish;
158 $datarray['summary'] = $summary;
159 $datarray['desc'] = $desc;
160 $datarray['location'] = $location;
161 $datarray['type'] = $type;
162 $datarray['adjust'] = $adjust;
163 $datarray['nofinish'] = $nofinish;
164 $datarray['uid'] = $uid;
165 $datarray['cid'] = $cid;
166 $datarray['allow_cid'] = $str_contact_allow;
167 $datarray['allow_gid'] = $str_group_allow;
168 $datarray['deny_cid'] = $str_contact_deny;
169 $datarray['deny_gid'] = $str_group_deny;
170 $datarray['private'] = (($private_event) ? 1 : 0);
171 $datarray['id'] = $event_id;
172 $datarray['created'] = $created;
173 $datarray['edited'] = $edited;
175 if (intval($_REQUEST['preview'])) {
176 $html = format_event_html($datarray);
181 $item_id = event_store($datarray);
184 Worker::add(PRIORITY_HIGH, "Notifier", "event", $item_id);
187 goaway($_SESSION['return_url']);
190 function events_content(App $a) {
192 if (! local_user()) {
193 notice(t('Permission denied.') . EOL);
198 $_SESSION['return_url'] = System::baseUrl() . '/' . $a->cmd;
201 if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
202 $r = q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
208 if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
209 $r = q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
215 if ($a->theme_events_in_profile) {
216 Nav::setSelected('home');
218 Nav::setSelected('events');
221 // get the translation strings for the callendar
222 $i18n = get_event_strings();
224 $htpl = get_markup_template('event_head.tpl');
225 $a->page['htmlhead'] .= replace_macros($htpl, [
226 '$baseurl' => System::baseUrl(),
227 '$module_url' => '/events',
232 $etpl = get_markup_template('event_end.tpl');
233 $a->page['end'] .= replace_macros($etpl, [
234 '$baseurl' => System::baseUrl(),
240 if ($a->theme_events_in_profile) {
241 $tabs = Profile::getTabs($a, true);
247 $ignored = ((x($_REQUEST, 'ignored')) ? intval($_REQUEST['ignored']) : 0);
250 if ($a->argc > 2 && $a->argv[1] == 'event') {
252 $event_id = intval($a->argv[2]);
254 if ($a->argc > 2 && $a->argv[1] == 'drop') {
256 $event_id = intval($a->argv[2]);
258 if ($a->argc > 2 && $a->argv[1] == 'copy') {
260 $event_id = intval($a->argv[2]);
262 if ($a->argv[1] === 'new') {
266 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
268 $y = intval($a->argv[1]);
269 $m = intval($a->argv[2]);
273 // The view mode part is similiar to /mod/cal.php
274 if ($mode == 'view') {
276 $thisyear = datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y');
277 $thismonth = datetime_convert('UTC', date_default_timezone_get(), 'now', 'm');
279 $y = intval($thisyear);
282 $m = intval($thismonth);
285 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
286 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
297 if ($nextmonth > 12) {
310 $dim = get_dim($y, $m);
311 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
312 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
314 if ($a->argc > 1 && $a->argv[1] === 'json') {
315 if (x($_GET, 'start')) {
316 $start = $_GET['start'];
318 if (x($_GET, 'end')) {
319 $finish = $_GET['end'];
323 $start = datetime_convert('UTC', 'UTC', $start);
324 $finish = datetime_convert('UTC', 'UTC', $finish);
326 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
327 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
329 // put the event parametes in an array so we can better transmit them
331 'event_id' => (x($_GET, 'id') ? $_GET['id'] : 0),
334 'adjust_start' => $adjust_start,
335 'adjust_finish' => $adjust_finish,
336 'ignored' => $ignored,
339 // get events by id or by date
340 if (x($_GET, 'id')) {
341 $r = event_by_id(local_user(), $event_params);
343 $r = events_by_date(local_user(), $event_params);
348 if (DBM::is_result($r)) {
349 $r = sort_by_date($r);
350 foreach ($r as $rr) {
351 $j = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'j') : datetime_convert('UTC', 'UTC', $rr['start'], 'j'));
352 if (! x($links,$j)) {
353 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
360 // transform the event in a usable array
361 if (DBM::is_result($r)) {
362 $r = sort_by_date($r);
363 $events = process_events($r);
366 if ($a->argc > 1 && $a->argv[1] === 'json'){
367 echo json_encode($events);
371 if (x($_GET, 'id')) {
372 $tpl = get_markup_template("event.tpl");
374 $tpl = get_markup_template("events_js.tpl");
377 // Get rid of dashes in key names, Smarty3 can't handle them
378 foreach ($events as $key => $event) {
380 foreach ($event['item'] as $k => $v) {
381 $k = str_replace('-' ,'_', $k);
382 $event_item[$k] = $v;
384 $events[$key]['item'] = $event_item;
387 $o = replace_macros($tpl, [
388 '$baseurl' => System::baseUrl(),
390 '$title' => t('Events'),
391 '$view' => t('View'),
392 '$new_event' => [System::baseUrl() . '/events/new', t('Create New Event'), '', ''],
393 '$previous' => [System::baseUrl() . '/events/$prevyear/$prevmonth', t('Previous'), '', ''],
394 '$next' => [System::baseUrl() . '/events/$nextyear/$nextmonth', t('Next'), '', ''],
395 '$calendar' => cal($y, $m, $links, ' eventcal'),
397 '$events' => $events,
399 '$today' => t('today'),
400 '$month' => t('month'),
401 '$week' => t('week'),
403 '$list' => t('list'),
406 if (x($_GET, 'id')) {
414 if (($mode === 'edit' || $mode === 'copy') && $event_id) {
415 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
419 if (DBM::is_result($r)) {
424 // Passed parameters overrides anything found in the DB
425 if (in_array($mode, ['edit', 'new', 'copy'])) {
426 if (!x($orig_event)) {$orig_event = [];}
427 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
428 if (x($_REQUEST, 'nofinish')) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
429 if (x($_REQUEST, 'adjust')) {$orig_event['adjust'] = $_REQUEST['adjust'];}
430 if (x($_REQUEST, 'summary')) {$orig_event['summary'] = $_REQUEST['summary'];}
431 if (x($_REQUEST, 'description')) {$orig_event['description'] = $_REQUEST['description'];}
432 if (x($_REQUEST, 'location')) {$orig_event['location'] = $_REQUEST['location'];}
433 if (x($_REQUEST, 'start')) {$orig_event['start'] = $_REQUEST['start'];}
434 if (x($_REQUEST, 'finish')) {$orig_event['finish'] = $_REQUEST['finish'];}
436 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
437 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
439 $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
440 $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
441 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
442 $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
443 $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
444 $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
449 if (x($orig_event)) {
450 $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ');
453 if ($cid || $mode === 'edit') {
454 $sh_disabled = 'disabled="disabled"';
457 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
458 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
460 $tz = date_default_timezone_get();
461 if (x($orig_event)) {
462 $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
465 $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
466 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
467 $sday = datetime_convert('UTC', $tz, $sdt, 'd');
469 $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
470 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
472 $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
473 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
474 $fday = datetime_convert('UTC', $tz, $fdt, 'd');
476 $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
477 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
479 require_once 'include/acl_selectors.php' ;
481 $perms = get_acl_permissions($orig_event);
483 if ($mode === 'new' || $mode === 'copy') {
484 $acl = (($cid) ? '' : populate_acl(((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' => t('Event details'),
508 '$desc' => t('Starting date and Title are required.'),
509 '$s_text' => t('Event Starts:') . ' <span class="required" title="' . t('Required') . '">*</span>',
510 '$s_dsel' => datetimesel(new DateTime(), DateTime::createFromFormat('Y', $syear+5), DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"), t('Event Starts:'), 'start_text', true, true, '', '', true),
511 '$n_text' => t('Finish date/time is not known or not relevant'),
512 '$n_checked' => $n_checked,
513 '$f_text' => t('Event Finishes:'),
514 '$f_dsel' => datetimesel(new DateTime(), DateTime::createFromFormat('Y', $fyear+5), DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"), t('Event Finishes:'), 'finish_text', true, true, 'start_text'),
515 '$a_text' => t('Adjust for viewer timezone'),
516 '$a_checked' => $a_checked,
517 '$d_text' => t('Description:'),
518 '$d_orig' => $d_orig,
519 '$l_text' => t('Location:'),
520 '$l_orig' => $l_orig,
521 '$t_text' => t('Title:') . ' <span class="required" title="' . t('Required') . '">*</span>',
522 '$t_orig' => $t_orig,
523 '$summary' => ['summary', t('Title:'), $t_orig, '', '*'],
524 '$sh_text' => t('Share this event'),
525 '$share' => ['share', t('Share this event'), $sh_checked, '', $sh_disabled],
526 '$sh_checked' => $sh_checked,
527 '$nofinish' => ['nofinish', t('Finish date/time is not known or not relevant'), $n_checked],
528 '$adjust' => ['adjust', t('Adjust for viewer timezone'), $a_checked],
529 '$preview' => t('Preview'),
531 '$submit' => t('Submit'),
532 '$basic' => t('Basic'),
533 '$advanced' => t('Advanced'),
534 '$permissions' => t('Permissions'),
541 // Remove an event from the calendar and its related items
542 if ($mode === 'drop' && $event_id) {
545 $params = ['event_id' => ($event_id)];
546 $ev = event_by_id(local_user(), $params);
548 // Delete only real events (no birthdays)
549 if (DBM::is_result($ev) && $ev[0]['type'] == 'event') {
550 $del = drop_item($ev[0]['itemid'], false);
554 notice(t('Failed to remove event' ) . EOL);
556 info(t('Event removed') . EOL);
559 goaway(System::baseUrl() . '/events');