4 * @brief The events module
6 require_once('include/bbcode.php');
7 require_once('include/datetime.php');
8 require_once('include/event.php');
9 require_once('include/items.php');
11 function events_init(App $a) {
17 // if it's a json request abort here becaus we don't
18 // need the widget data
19 if ($a->argv[1] === 'json')
22 $cal_widget = widget_events();
24 if (! x($a->page,'aside')) {
25 $a->page['aside'] = '';
28 $a->page['aside'] .= $cal_widget;
34 function events_post(App $a) {
36 logger('post: ' . print_r($_REQUEST,true));
42 $event_id = ((x($_POST,'event_id')) ? intval($_POST['event_id']) : 0);
43 $cid = ((x($_POST,'cid')) ? intval($_POST['cid']) : 0);
46 $start_text = escape_tags($_REQUEST['start_text']);
47 $finish_text = escape_tags($_REQUEST['finish_text']);
49 $adjust = intval($_POST['adjust']);
50 $nofinish = intval($_POST['nofinish']);
52 // The default setting for the `private` field in event_store() is false, so mirror that
53 $private_event = false;
59 $start = sprintf('%d-%d-%d %d:%d:0',$startyear,$startmonth,$startday,$starthour,$startminute);
63 $finish = '0000-00-00 00:00:00';
67 $finish = $finish_text;
70 $finish = sprintf('%d-%d-%d %d:%d:0',$finishyear,$finishmonth,$finishday,$finishhour,$finishminute);
74 $start = datetime_convert(date_default_timezone_get(),'UTC',$start);
76 $finish = datetime_convert(date_default_timezone_get(),'UTC',$finish);
80 $start = datetime_convert('UTC','UTC',$start);
82 $finish = datetime_convert('UTC','UTC',$finish);
86 // Don't allow the event to finish before it begins.
87 // It won't hurt anything, but somebody will file a bug report
88 // and we'll waste a bunch of time responding to it. Time that
89 // could've been spent doing something else.
91 $summary = escape_tags(trim($_POST['summary']));
92 $desc = escape_tags(trim($_POST['desc']));
93 $location = escape_tags(trim($_POST['location']));
96 $action = ($event_id == '') ? 'new' : "event/" . $event_id;
97 $onerror_url = App::get_baseurl() . "/events/" . $action . "?summary=$summary&description=$desc&location=$location&start=$start_text&finish=$finish_text&adjust=$adjust&nofinish=$nofinish";
99 if (strcmp($finish,$start) < 0 && !$nofinish) {
100 notice( t('Event can not end before it has started.') . EOL);
101 if (intval($_REQUEST['preview'])) {
102 echo( t('Event can not end before it has started.'));
105 goaway($onerror_url);
108 if((! $summary) || (! $start)) {
109 notice( t('Event title and start time are required.') . EOL);
110 if(intval($_REQUEST['preview'])) {
111 echo( t('Event title and start time are required.'));
114 goaway($onerror_url);
117 $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
119 $c = q("select id from contact where uid = %d and self = 1 limit 1",
129 $str_group_allow = perms2str($_POST['group_allow']);
130 $str_contact_allow = perms2str($_POST['contact_allow']);
131 $str_group_deny = perms2str($_POST['group_deny']);
132 $str_contact_deny = perms2str($_POST['contact_deny']);
134 // Undo the pseudo-contact of self, since there are real contacts now
135 if( strpos($str_contact_allow, '<' . $self . '>') !== false )
137 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
139 // Make sure to set the `private` field as true. This is necessary to
140 // have the posts show up correctly in Diaspora if an event is created
141 // as visible only to self at first, but then edited to display to others.
142 if( strlen($str_group_allow) or strlen($str_contact_allow) or strlen($str_group_deny) or strlen($str_contact_deny) )
144 $private_event = true;
148 // Note: do not set `private` field for self-only events. It will
149 // keep even you from seeing them!
150 $str_contact_allow = '<' . $self . '>';
151 $str_group_allow = $str_contact_deny = $str_group_deny = '';
156 $datarray['guid'] = get_guid(32);
157 $datarray['start'] = $start;
158 $datarray['finish'] = $finish;
159 $datarray['summary'] = $summary;
160 $datarray['desc'] = $desc;
161 $datarray['location'] = $location;
162 $datarray['type'] = $type;
163 $datarray['adjust'] = $adjust;
164 $datarray['nofinish'] = $nofinish;
165 $datarray['uid'] = $uid;
166 $datarray['cid'] = $cid;
167 $datarray['allow_cid'] = $str_contact_allow;
168 $datarray['allow_gid'] = $str_group_allow;
169 $datarray['deny_cid'] = $str_contact_deny;
170 $datarray['deny_gid'] = $str_group_deny;
171 $datarray['private'] = (($private_event) ? 1 : 0);
172 $datarray['id'] = $event_id;
173 $datarray['created'] = $created;
174 $datarray['edited'] = $edited;
176 if(intval($_REQUEST['preview'])) {
177 $html = format_event_html($datarray);
182 $item_id = event_store($datarray);
185 proc_run(PRIORITY_HIGH, "include/notifier.php", "event", $item_id);
187 goaway($_SESSION['return_url']);
192 function events_content(App $a) {
194 if (! local_user()) {
195 notice( t('Permission denied.') . EOL);
200 $_SESSION['return_url'] = App::get_baseurl() . '/' . $a->cmd;
203 if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
204 $r = q("update event set ignore = 1 where id = %d and uid = %d",
210 if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
211 $r = q("update event set ignore = 0 where id = %d and uid = %d",
217 if ($a->theme_events_in_profile) {
218 nav_set_selected('home');
220 nav_set_selected('events');
223 // get the translation strings for the callendar
224 $i18n = get_event_strings();
226 $htpl = get_markup_template('event_head.tpl');
227 $a->page['htmlhead'] .= replace_macros($htpl,array(
228 '$baseurl' => App::get_baseurl(),
229 '$module_url' => '/events',
234 $etpl = get_markup_template('event_end.tpl');
235 $a->page['end'] .= replace_macros($etpl,array(
236 '$baseurl' => App::get_baseurl(),
241 if ($a->theme_events_in_profile)
242 $tabs = profile_tabs($a, True);
249 $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
252 if ($a->argc > 2 && $a->argv[1] == 'event') {
254 $event_id = intval($a->argv[2]);
256 if ($a->argv[1] === 'new') {
260 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
262 $y = intval($a->argv[1]);
263 $m = intval($a->argv[2]);
267 // The view mode part is similiar to /mod/cal.php
268 if ($mode == 'view') {
271 $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
272 $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
274 $y = intval($thisyear);
277 $m = intval($thismonth);
280 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
281 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
292 if($nextmonth > 12) {
305 $dim = get_dim($y,$m);
306 $start = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
307 $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
310 if ($a->argv[1] === 'json'){
311 if (x($_GET,'start')) $start = $_GET['start'];
312 if (x($_GET,'end')) $finish = $_GET['end'];
315 $start = datetime_convert('UTC','UTC',$start);
316 $finish = datetime_convert('UTC','UTC',$finish);
318 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
319 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
321 // put the event parametes in an array so we can better transmit them
322 $event_params = array(
323 'event_id' => (x($_GET,'id') ? $_GET["id"] : 0),
326 'adjust_start' => $adjust_start,
327 'adjust_finish' => $adjust_finish,
328 'ignored' => $ignored,
331 // get events by id or by date
333 $r = event_by_id(local_user(), $event_params);
335 $r = events_by_date(local_user(), $event_params);
340 if (dbm::is_result($r)) {
341 $r = sort_by_date($r);
342 foreach ($r as $rr) {
343 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
344 if (! x($links,$j)) {
345 $links[$j] = App::get_baseurl() . '/' . $a->cmd . '#link-' . $j;
352 // transform the event in a usable array
353 if (dbm::is_result($r)) {
354 $r = sort_by_date($r);
355 $events = process_events($r);
358 if ($a->argv[1] === 'json'){
359 echo json_encode($events); killme();
362 // links: array('href', 'text', 'extra css classes', 'title')
364 $tpl = get_markup_template("event.tpl");
366 // if (get_config('experimentals','new_calendar')==1){
367 $tpl = get_markup_template("events_js.tpl");
369 // $tpl = get_markup_template("events.tpl");
373 // Get rid of dashes in key names, Smarty3 can't handle them
374 foreach($events as $key => $event) {
375 $event_item = array();
376 foreach($event['item'] as $k => $v) {
377 $k = str_replace('-','_',$k);
378 $event_item[$k] = $v;
380 $events[$key]['item'] = $event_item;
383 $o = replace_macros($tpl, array(
384 '$baseurl' => App::get_baseurl(),
386 '$title' => t('Events'),
387 '$view' => t('View'),
388 '$new_event' => array(App::get_baseurl().'/events/new',t('Create New Event'),'',''),
389 '$previous' => array(App::get_baseurl()."/events/$prevyear/$prevmonth",t('Previous'),'',''),
390 '$next' => array(App::get_baseurl()."/events/$nextyear/$nextmonth",t('Next'),'',''),
391 '$calendar' => cal($y,$m,$links, ' eventcal'),
393 '$events' => $events,
395 "today" => t("today"),
396 "month" => t("month"),
402 if (x($_GET,'id')){ echo $o; killme(); }
408 if($mode === 'edit' && $event_id) {
409 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
413 if (dbm::is_result($r))
417 // Passed parameters overrides anything found in the DB
418 if($mode === 'edit' || $mode === 'new') {
419 if(!x($orig_event)) $orig_event = array();
420 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
421 if(x($_REQUEST,'nofinish')) $orig_event['nofinish'] = $_REQUEST['nofinish'];
422 if(x($_REQUEST,'adjust')) $orig_event['adjust'] = $_REQUEST['adjust'];
423 if(x($_REQUEST,'summary')) $orig_event['summary'] = $_REQUEST['summary'];
424 if(x($_REQUEST,'description')) $orig_event['description'] = $_REQUEST['description'];
425 if(x($_REQUEST,'location')) $orig_event['location'] = $_REQUEST['location'];
426 if(x($_REQUEST,'start')) $orig_event['start'] = $_REQUEST['start'];
427 if(x($_REQUEST,'finish')) $orig_event['finish'] = $_REQUEST['finish'];
430 if($mode === 'edit' || $mode === 'new') {
432 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
433 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
434 $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
435 $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
436 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
437 $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
438 $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
439 $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
445 $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ' );
447 if($cid OR ($mode !== 'new'))
448 $sh_checked .= ' disabled="disabled" ';
451 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
452 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
454 $tz = date_default_timezone_get();
456 $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
458 $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
459 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
460 $sday = datetime_convert('UTC', $tz, $sdt, 'd');
462 $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
463 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
465 $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
466 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
467 $fday = datetime_convert('UTC', $tz, $fdt, 'd');
469 $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
470 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
472 $f = get_config('system','event_input_format');
476 require_once('include/acl_selectors.php');
479 $acl = (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user)));
481 $tpl = get_markup_template('event_form.tpl');
483 $o .= replace_macros($tpl,array(
484 '$post' => App::get_baseurl() . '/events',
489 '$title' => t('Event details'),
490 '$desc' => t('Starting date and Title are required.'),
491 '$s_text' => t('Event Starts:') . ' <span class="required" title="' . t('Required') . '">*</span>',
492 '$s_dsel' => datetimesel($f,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),
493 '$n_text' => t('Finish date/time is not known or not relevant'),
494 '$n_checked' => $n_checked,
495 '$f_text' => t('Event Finishes:'),
496 '$f_dsel' => datetimesel($f,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'),
497 '$a_text' => t('Adjust for viewer timezone'),
498 '$a_checked' => $a_checked,
499 '$d_text' => t('Description:'),
500 '$d_orig' => $d_orig,
501 '$l_text' => t('Location:'),
502 '$l_orig' => $l_orig,
503 '$t_text' => t('Title:') . ' <span class="required" title="' . t('Required') . '">*</span>',
504 '$t_orig' => $t_orig,
505 '$summary' => array('summary', t('Title:'), $t_orig, '', '*'),
506 '$sh_text' => t('Share this event'),
507 '$share' => array('share', t('Share this event'), $sh_checked, ''),
508 '$sh_checked' => $sh_checked,
509 '$nofinish' => array('nofinish', t('Finish date/time is not known or not relevant'), $n_checked),
510 '$adjust' => array('adjust', t('Adjust for viewer timezone'), $a_checked),
511 '$preview' => t('Preview'),
513 '$submit' => t('Submit'),
514 '$basic' => t("Basic"),
515 '$advanced' => t("Advanced"),
516 '$permissions' => t('Permissions'),