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_post(&$a) {
13 logger('post: ' . print_r($_REQUEST,true));
18 $event_id = ((x($_POST,'event_id')) ? intval($_POST['event_id']) : 0);
19 $cid = ((x($_POST,'cid')) ? intval($_POST['cid']) : 0);
22 $start_text = escape_tags($_REQUEST['start_text']);
23 $finish_text = escape_tags($_REQUEST['finish_text']);
25 $adjust = intval($_POST['adjust']);
26 $nofinish = intval($_POST['nofinish']);
28 // The default setting for the `private` field in event_store() is false, so mirror that
29 $private_event = false;
35 $start = sprintf('%d-%d-%d %d:%d:0',$startyear,$startmonth,$startday,$starthour,$startminute);
39 $finish = '0000-00-00 00:00:00';
43 $finish = $finish_text;
46 $finish = sprintf('%d-%d-%d %d:%d:0',$finishyear,$finishmonth,$finishday,$finishhour,$finishminute);
50 $start = datetime_convert(date_default_timezone_get(),'UTC',$start);
52 $finish = datetime_convert(date_default_timezone_get(),'UTC',$finish);
55 $start = datetime_convert('UTC','UTC',$start);
57 $finish = datetime_convert('UTC','UTC',$finish);
60 // Don't allow the event to finish before it begins.
61 // It won't hurt anything, but somebody will file a bug report
62 // and we'll waste a bunch of time responding to it. Time that
63 // could've been spent doing something else.
65 $summary = escape_tags(trim($_POST['summary']));
66 $desc = escape_tags(trim($_POST['desc']));
67 $location = escape_tags(trim($_POST['location']));
70 $action = ($event_id == '') ? 'new' : "event/" . $event_id;
71 $onerror_url = $a->get_baseurl() . "/events/" . $action . "?summary=$summary&description=$desc&location=$location&start=$start_text&finish=$finish_text&adjust=$adjust&nofinish=$nofinish";
73 if(strcmp($finish,$start) < 0 && !$nofinish) {
74 notice( t('Event can not end before it has started.') . EOL);
75 if(intval($_REQUEST['preview'])) {
76 echo( t('Event can not end before it has started.'));
82 if((! $summary) || (! $start)) {
83 notice( t('Event title and start time are required.') . EOL);
84 if(intval($_REQUEST['preview'])) {
85 echo( t('Event title and start time are required.'));
91 $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
93 $c = q("select id from contact where uid = %d and self = 1 limit 1",
103 $str_group_allow = perms2str($_POST['group_allow']);
104 $str_contact_allow = perms2str($_POST['contact_allow']);
105 $str_group_deny = perms2str($_POST['group_deny']);
106 $str_contact_deny = perms2str($_POST['contact_deny']);
108 // Undo the pseudo-contact of self, since there are real contacts now
109 if( strpos($str_contact_allow, '<' . $self . '>') !== false )
111 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
113 // Make sure to set the `private` field as true. This is necessary to
114 // have the posts show up correctly in Diaspora if an event is created
115 // as visible only to self at first, but then edited to display to others.
116 if( strlen($str_group_allow) or strlen($str_contact_allow) or strlen($str_group_deny) or strlen($str_contact_deny) )
118 $private_event = true;
122 // Note: do not set `private` field for self-only events. It will
123 // keep even you from seeing them!
124 $str_contact_allow = '<' . $self . '>';
125 $str_group_allow = $str_contact_deny = $str_group_deny = '';
130 $datarray['guid'] = get_guid(32);
131 $datarray['start'] = $start;
132 $datarray['finish'] = $finish;
133 $datarray['summary'] = $summary;
134 $datarray['desc'] = $desc;
135 $datarray['location'] = $location;
136 $datarray['type'] = $type;
137 $datarray['adjust'] = $adjust;
138 $datarray['nofinish'] = $nofinish;
139 $datarray['uid'] = $uid;
140 $datarray['cid'] = $cid;
141 $datarray['allow_cid'] = $str_contact_allow;
142 $datarray['allow_gid'] = $str_group_allow;
143 $datarray['deny_cid'] = $str_contact_deny;
144 $datarray['deny_gid'] = $str_group_deny;
145 $datarray['private'] = (($private_event) ? 1 : 0);
146 $datarray['id'] = $event_id;
147 $datarray['created'] = $created;
148 $datarray['edited'] = $edited;
150 if(intval($_REQUEST['preview'])) {
151 $html = format_event_html($datarray);
156 $item_id = event_store($datarray);
159 proc_run('php',"include/notifier.php","event","$item_id");
161 goaway($_SESSION['return_url']);
166 function events_content(&$a) {
169 notice( t('Permission denied.') . EOL);
174 $_SESSION['return_url'] = $a->get_baseurl() . '/' . $a->cmd;
176 if(($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
177 $r = q("update event set ignore = 1 where id = %d and uid = %d",
183 if(($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
184 $r = q("update event set ignore = 0 where id = %d and uid = %d",
190 if ($a->theme_events_in_profile)
191 nav_set_selected('home');
193 nav_set_selected('events');
195 $editselect = 'none';
196 if( feature_enabled(local_user(), 'richtext') )
197 $editselect = 'textareas';
199 // get the translation strings for the callendar
200 $i18n = get_event_strings();
202 $htpl = get_markup_template('event_head.tpl');
203 $a->page['htmlhead'] .= replace_macros($htpl,array(
204 '$baseurl' => $a->get_baseurl(),
205 '$module_url' => '/events',
208 '$editselect' => $editselect
211 $etpl = get_markup_template('event_end.tpl');
212 $a->page['end'] .= replace_macros($etpl,array(
213 '$baseurl' => $a->get_baseurl(),
214 '$editselect' => $editselect
219 if ($a->theme_events_in_profile)
220 $tabs = profile_tabs($a, True);
227 $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
230 if($a->argc > 2 && $a->argv[1] == 'event') {
232 $event_id = intval($a->argv[2]);
234 if($a->argv[1] === 'new') {
238 if($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
240 $y = intval($a->argv[1]);
241 $m = intval($a->argv[2]);
245 // The view mode part is similiar to /mod/cal.php
246 if($mode == 'view') {
249 $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
250 $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
252 $y = intval($thisyear);
254 $m = intval($thismonth);
256 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
257 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
266 if($nextmonth > 12) {
279 $dim = get_dim($y,$m);
280 $start = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
281 $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
284 if ($a->argv[1] === 'json'){
285 if (x($_GET,'start')) $start = date("Y-m-d h:i:s", $_GET['start']);
286 if (x($_GET,'end')) $finish = date("Y-m-d h:i:s", $_GET['end']);
289 $start = datetime_convert('UTC','UTC',$start);
290 $finish = datetime_convert('UTC','UTC',$finish);
292 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
293 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
295 // put the event parametes in an array so we can better transmit them
296 $event_params = array(
297 'event_id' => (x($_GET,'id') ? $_GET["id"] : 0),
300 'adjust_start' => $adjust_start,
301 'adjust_finish' => $adjust_finish,
302 'ignored' => $ignored,
305 // get events by id or by date
307 $r = event_by_id(local_user(), $event_params);
309 $r = events_by_date(local_user(), $event_params);
315 $r = sort_by_date($r);
317 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
319 $links[$j] = $a->get_baseurl() . '/' . $a->cmd . '#link-' . $j;
325 // transform the event in a usable array
327 $r = sort_by_date($r);
328 $events = process_events($r);
330 if ($a->argv[1] === 'json'){
331 echo json_encode($events); killme();
334 // links: array('href', 'text', 'extra css classes', 'title')
336 $tpl = get_markup_template("event.tpl");
338 // if (get_config('experimentals','new_calendar')==1){
339 $tpl = get_markup_template("events-js.tpl");
341 // $tpl = get_markup_template("events.tpl");
345 // Get rid of dashes in key names, Smarty3 can't handle them
346 foreach($events as $key => $event) {
347 $event_item = array();
348 foreach($event['item'] as $k => $v) {
349 $k = str_replace('-','_',$k);
350 $event_item[$k] = $v;
352 $events[$key]['item'] = $event_item;
355 $o = replace_macros($tpl, array(
356 '$baseurl' => $a->get_baseurl(),
358 '$title' => t('Events'),
359 '$view' => t('View'),
360 '$new_event'=> array($a->get_baseurl().'/events/new',t('Create New Event'),'',''),
361 '$previus' => array($a->get_baseurl()."/events/$prevyear/$prevmonth",t('Previous'),'',''),
362 '$next' => array($a->get_baseurl()."/events/$nextyear/$nextmonth",t('Next'),'',''),
363 '$calendar' => cal($y,$m,$links, ' eventcal'),
365 '$events' => $events,
367 "today" => t("today"),
368 "month" => t("month"),
375 if (x($_GET,'id')){ echo $o; killme(); }
381 if($mode === 'edit' && $event_id) {
382 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
390 // Passed parameters overrides anything found in the DB
391 if($mode === 'edit' || $mode === 'new') {
392 if(!x($orig_event)) $orig_event = array();
393 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
394 if(x($_REQUEST,'nofinish')) $orig_event['nofinish'] = $_REQUEST['nofinish'];
395 if(x($_REQUEST,'adjust')) $orig_event['adjust'] = $_REQUEST['adjust'];
396 if(x($_REQUEST,'summary')) $orig_event['summary'] = $_REQUEST['summary'];
397 if(x($_REQUEST,'description')) $orig_event['description'] = $_REQUEST['description'];
398 if(x($_REQUEST,'location')) $orig_event['location'] = $_REQUEST['location'];
399 if(x($_REQUEST,'start')) $orig_event['start'] = $_REQUEST['start'];
400 if(x($_REQUEST,'finish')) $orig_event['finish'] = $_REQUEST['finish'];
403 if($mode === 'edit' || $mode === 'new') {
405 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
406 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
407 $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
408 $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
409 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
410 $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
411 $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
412 $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
418 $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ' );
420 if($cid OR ($mode !== 'new'))
421 $sh_checked .= ' disabled="disabled" ';
424 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
425 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
427 $tz = date_default_timezone_get();
429 $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
431 $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
432 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
433 $sday = datetime_convert('UTC', $tz, $sdt, 'd');
435 $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
436 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
438 $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
439 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
440 $fday = datetime_convert('UTC', $tz, $fdt, 'd');
442 $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
443 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
445 $f = get_config('system','event_input_format');
449 require_once('include/acl_selectors.php');
452 $acl = (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user)));
454 $tpl = get_markup_template('event_form.tpl');
456 $o .= replace_macros($tpl,array(
457 '$post' => $a->get_baseurl() . '/events',
462 '$title' => t('Event details'),
463 '$desc' => t('Starting date and Title are required.'),
464 '$s_text' => t('Event Starts:') . ' <span class="required" title="' . t('Required') . '">*</span>',
465 '$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),
466 '$n_text' => t('Finish date/time is not known or not relevant'),
467 '$n_checked' => $n_checked,
468 '$f_text' => t('Event Finishes:'),
469 '$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'),
470 '$a_text' => t('Adjust for viewer timezone'),
471 '$a_checked' => $a_checked,
472 '$d_text' => t('Description:'),
473 '$d_orig' => $d_orig,
474 '$l_text' => t('Location:'),
475 '$l_orig' => $l_orig,
476 '$t_text' => t('Title:') . ' <span class="required" title="' . t('Required') . '">*</span>',
477 '$t_orig' => $t_orig,
478 '$summary' => array('summary', t('Title:'), $t_orig, '', '*'),
479 '$sh_text' => t('Share this event'),
480 '$share' => array('share', t('Share this event'), $sh_checked, ''),
481 '$sh_checked' => $sh_checked,
482 '$nofinish' => array('nofinish', t('Finish date/time is not known or not relevant'), $n_checked),
483 '$adjust' => array('adjust', t('Adjust for viewer timezone'), $a_checked),
484 '$preview' => t('Preview'),
486 '$submit' => t('Submit'),
487 '$basic' => t("Basic"),
488 '$advanced' => t("Advanced")