3 require_once('include/bbcode.php');
4 require_once('include/datetime.php');
5 require_once('include/event.php');
6 require_once('include/items.php');
8 function events_post(&$a) {
10 logger('post: ' . print_r($_REQUEST,true));
15 $event_id = ((x($_POST,'event_id')) ? intval($_POST['event_id']) : 0);
16 $cid = ((x($_POST,'cid')) ? intval($_POST['cid']) : 0);
18 $startyear = intval($_POST['startyear']);
19 $startmonth = intval($_POST['startmonth']);
20 $startday = intval($_POST['startday']);
21 $starthour = intval($_POST['starthour']);
22 $startminute = intval($_POST['startminute']);
24 $finishyear = intval($_POST['finishyear']);
25 $finishmonth = intval($_POST['finishmonth']);
26 $finishday = intval($_POST['finishday']);
27 $finishhour = intval($_POST['finishhour']);
28 $finishminute = intval($_POST['finishminute']);
30 $adjust = intval($_POST['adjust']);
31 $nofinish = intval($_POST['nofinish']);
33 // The default setting for the `private` field in event_store() is false, so mirror that
34 $private_event = false;
37 $start = sprintf('%d-%d-%d %d:%d:0',$startyear,$startmonth,$startday,$starthour,$startminute);
39 $finish = '0000-00-00 00:00:00';
41 $finish = sprintf('%d-%d-%d %d:%d:0',$finishyear,$finishmonth,$finishday,$finishhour,$finishminute);
44 $start = datetime_convert(date_default_timezone_get(),'UTC',$start);
46 $finish = datetime_convert(date_default_timezone_get(),'UTC',$finish);
49 $start = datetime_convert('UTC','UTC',$start);
51 $finish = datetime_convert('UTC','UTC',$finish);
54 // Don't allow the event to finish before it begins.
55 // It won't hurt anything, but somebody will file a bug report
56 // and we'll waste a bunch of time responding to it. Time that
57 // could've been spent doing something else.
59 if(strcmp($finish,$start) < 0)
62 $summary = escape_tags(trim($_POST['summary']));
63 $desc = escape_tags(trim($_POST['desc']));
64 $location = escape_tags(trim($_POST['location']));
67 if((! $summary) || (! $start)) {
68 notice( t('Event title and start time are required.') . EOL);
69 if(intval($_REQUEST['preview'])) {
70 echo( t('Event title and start time are required.'));
73 goaway($a->get_baseurl() . '/events/new');
76 $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
78 $c = q("select id from contact where uid = %d and self = 1 limit 1",
88 $str_group_allow = perms2str($_POST['group_allow']);
89 $str_contact_allow = perms2str($_POST['contact_allow']);
90 $str_group_deny = perms2str($_POST['group_deny']);
91 $str_contact_deny = perms2str($_POST['contact_deny']);
93 // Undo the pseudo-contact of self, since there are real contacts now
94 if( strpos($str_contact_allow, '<' . $self . '>') !== false )
96 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
98 // Make sure to set the `private` field as true. This is necessary to
99 // have the posts show up correctly in Diaspora if an event is created
100 // as visible only to self at first, but then edited to display to others.
101 if( strlen($str_group_allow) or strlen($str_contact_allow) or strlen($str_group_deny) or strlen($str_contact_deny) )
103 $private_event = true;
107 // Note: do not set `private` field for self-only events. It will
108 // keep even you from seeing them!
109 $str_contact_allow = '<' . $self . '>';
110 $str_group_allow = $str_contact_deny = $str_group_deny = '';
115 $datarray['start'] = $start;
116 $datarray['finish'] = $finish;
117 $datarray['summary'] = $summary;
118 $datarray['desc'] = $desc;
119 $datarray['location'] = $location;
120 $datarray['type'] = $type;
121 $datarray['adjust'] = $adjust;
122 $datarray['nofinish'] = $nofinish;
123 $datarray['uid'] = $uid;
124 $datarray['cid'] = $cid;
125 $datarray['allow_cid'] = $str_contact_allow;
126 $datarray['allow_gid'] = $str_group_allow;
127 $datarray['deny_cid'] = $str_contact_deny;
128 $datarray['deny_gid'] = $str_group_deny;
129 $datarray['private'] = (($private_event) ? 1 : 0);
130 $datarray['id'] = $event_id;
131 $datarray['created'] = $created;
132 $datarray['edited'] = $edited;
134 if(intval($_REQUEST['preview'])) {
135 $html = format_event_html($datarray);
140 $item_id = event_store($datarray);
143 proc_run('php',"include/notifier.php","event","$item_id");
149 function events_content(&$a) {
152 notice( t('Permission denied.') . EOL);
156 if(($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
157 $r = q("update event set ignore = 1 where id = %d and uid = %d",
163 if(($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
164 $r = q("update event set ignore = 0 where id = %d and uid = %d",
171 $editselect = 'none';
172 if( feature_enabled(local_user(), 'richtext') )
173 $editselect = 'textareas';
175 $htpl = get_markup_template('event_head.tpl');
176 $a->page['htmlhead'] .= replace_macros($htpl,array(
177 '$baseurl' => $a->get_baseurl(),
178 '$editselect' => $editselect
181 $etpl = get_markup_template('event_end.tpl');
182 $a->page['end'] .= replace_macros($etpl,array(
183 '$baseurl' => $a->get_baseurl(),
184 '$editselect' => $editselect
189 $tabs = profile_tabs($a, True);
196 $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
199 if($a->argc > 2 && $a->argv[1] == 'event') {
201 $event_id = intval($a->argv[2]);
203 if($a->argv[1] === 'new') {
207 if($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
209 $y = intval($a->argv[1]);
210 $m = intval($a->argv[2]);
214 if($mode == 'view') {
217 $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
218 $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
220 $y = intval($thisyear);
222 $m = intval($thismonth);
224 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
225 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
234 if($nextmonth > 12) {
247 $dim = get_dim($y,$m);
248 $start = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
249 $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
252 if ($a->argv[1] === 'json'){
253 if (x($_GET,'start')) $start = date("Y-m-d h:i:s", $_GET['start']);
254 if (x($_GET,'end')) $finish = date("Y-m-d h:i:s", $_GET['end']);
257 $start = datetime_convert('UTC','UTC',$start);
258 $finish = datetime_convert('UTC','UTC',$finish);
260 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
261 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
265 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
266 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event` LEFT JOIN `item` ON `item`.`event-id` = `event`.`id`
267 WHERE `event`.`uid` = %d AND `event`.`id` = %d",
268 intval(local_user()),
272 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
273 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event` LEFT JOIN `item` ON `item`.`event-id` = `event`.`id`
274 WHERE `event`.`uid` = %d and event.ignore = %d
275 AND (( `adjust` = 0 AND ( `finish` >= '%s' OR ( nofinish AND start >= '%s' ) ) AND `start` <= '%s' )
276 OR ( `adjust` = 1 AND ( `finish` >= '%s' OR ( nofinish AND start >= '%s' ) ) AND `start` <= '%s' )) ",
277 intval(local_user()),
282 dbesc($adjust_start),
283 dbesc($adjust_start),
284 dbesc($adjust_finish)
291 $r = sort_by_date($r);
293 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
295 $links[$j] = $a->get_baseurl() . '/' . $a->cmd . '#link-' . $j;
306 $r = sort_by_date($r);
310 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
311 $d = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], $fmt) : datetime_convert('UTC','UTC',$rr['start'],$fmt));
312 $d = day_translate($d);
314 $start = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'c') : datetime_convert('UTC','UTC',$rr['start'],'c'));
315 if ($rr['nofinish']){
318 $end = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['finish'], 'c') : datetime_convert('UTC','UTC',$rr['finish'],'c'));
322 $is_first = ($d !== $last_date);
325 $edit = ((! $rr['cid']) ? array($a->get_baseurl().'/events/event/'.$rr['id'],t('Edit event'),'','') : null);
326 $title = strip_tags(html_entity_decode(bbcode($rr['summary']),ENT_QUOTES,'UTF-8'));
328 list($title, $_trash) = explode("<br",bbcode($rr['desc']),2);
329 $title = strip_tags(html_entity_decode($title,ENT_QUOTES,'UTF-8'));
331 $html = format_event_html($rr);
332 $rr['desc'] = bbcode($rr['desc']);
333 $rr['location'] = bbcode($rr['location']);
344 'is_first'=>$is_first,
347 'plink' => array($rr['plink'],t('link to source'),'',''),
354 if ($a->argv[1] === 'json'){
355 echo json_encode($events); killme();
358 // links: array('href', 'text', 'extra css classes', 'title')
360 $tpl = get_markup_template("event.tpl");
362 // if (get_config('experimentals','new_calendar')==1){
363 $tpl = get_markup_template("events-js.tpl");
365 // $tpl = get_markup_template("events.tpl");
369 // Get rid of dashes in key names, Smarty3 can't handle them
370 foreach($events as $key => $event) {
371 $event_item = array();
372 foreach($event['item'] as $k => $v) {
373 $k = str_replace('-','_',$k);
374 $event_item[$k] = $v;
376 $events[$key]['item'] = $event_item;
379 $o = replace_macros($tpl, array(
380 '$baseurl' => $a->get_baseurl(),
382 '$title' => t('Events'),
383 '$new_event'=> array($a->get_baseurl().'/events/new',t('Create New Event'),'',''),
384 '$previus' => array($a->get_baseurl()."/events/$prevyear/$prevmonth",t('Previous'),'',''),
385 '$next' => array($a->get_baseurl()."/events/$nextyear/$nextmonth",t('Next'),'',''),
386 '$calendar' => cal($y,$m,$links, ' eventcal'),
388 '$events' => $events,
393 if (x($_GET,'id')){ echo $o; killme(); }
399 if($mode === 'edit' && $event_id) {
400 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
408 if($mode === 'edit' || $mode === 'new') {
410 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
411 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
412 $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
413 $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
414 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
415 $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
416 $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
417 $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
423 $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ' );
426 $sh_checked .= ' disabled="disabled" ';
430 $tpl = get_markup_template('event_form.tpl');
432 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
433 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
435 $tz = date_default_timezone_get();
437 $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
439 $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
440 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
441 $sday = datetime_convert('UTC', $tz, $sdt, 'd');
443 $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
444 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
446 $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
447 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
448 $fday = datetime_convert('UTC', $tz, $fdt, 'd');
450 $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
451 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
453 $f = get_config('system','event_input_format');
457 $dateformat = datesel_format($f);
458 $timeformat = t('hour:minute');
460 require_once('include/acl_selectors.php');
462 $o .= replace_macros($tpl,array(
463 '$post' => $a->get_baseurl() . '/events',
468 '$title' => t('Event details'),
469 '$desc' => sprintf( t('Format is %s %s. Starting date and Title are required.'),$dateformat,$timeformat),
471 '$s_text' => t('Event Starts:') . ' <span class="required" title="' . t('Required') . '">*</span>',
472 '$s_dsel' => datesel($f,'start',$syear+5,$syear,false,$syear,$smonth,$sday),
473 '$s_tsel' => timesel('start',$shour,$sminute),
474 '$n_text' => t('Finish date/time is not known or not relevant'),
475 '$n_checked' => $n_checked,
476 '$f_text' => t('Event Finishes:'),
477 '$f_dsel' => datesel($f,'finish',$fyear+5,$fyear,false,$fyear,$fmonth,$fday),
478 '$f_tsel' => timesel('finish',$fhour,$fminute),
479 '$a_text' => t('Adjust for viewer timezone'),
480 '$a_checked' => $a_checked,
481 '$d_text' => t('Description:'),
482 '$d_orig' => $d_orig,
483 '$l_text' => t('Location:'),
484 '$l_orig' => $l_orig,
485 '$t_text' => t('Title:') . ' <span class="required" title="' . t('Required') . '">*</span>',
486 '$t_orig' => $t_orig,
487 '$sh_text' => t('Share this event'),
488 '$sh_checked' => $sh_checked,
489 '$preview' => t('Preview'),
490 '$acl' => (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user),false)),
491 '$submit' => t('Submit')