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);
19 $start_text = escape_tags($_REQUEST['start_text']);
20 $finish_text = escape_tags($_REQUEST['finish_text']);
22 $adjust = intval($_POST['adjust']);
23 $nofinish = intval($_POST['nofinish']);
25 // The default setting for the `private` field in event_store() is false, so mirror that
26 $private_event = false;
32 $start = sprintf('%d-%d-%d %d:%d:0',$startyear,$startmonth,$startday,$starthour,$startminute);
36 $finish = '0000-00-00 00:00:00';
40 $finish = $finish_text;
43 $finish = sprintf('%d-%d-%d %d:%d:0',$finishyear,$finishmonth,$finishday,$finishhour,$finishminute);
47 $start = datetime_convert(date_default_timezone_get(),'UTC',$start);
49 $finish = datetime_convert(date_default_timezone_get(),'UTC',$finish);
52 $start = datetime_convert('UTC','UTC',$start);
54 $finish = datetime_convert('UTC','UTC',$finish);
57 // Don't allow the event to finish before it begins.
58 // It won't hurt anything, but somebody will file a bug report
59 // and we'll waste a bunch of time responding to it. Time that
60 // could've been spent doing something else.
62 $summary = escape_tags(trim($_POST['summary']));
63 $desc = escape_tags(trim($_POST['desc']));
64 $location = escape_tags(trim($_POST['location']));
67 $action = ($event_id == '') ? 'new' : "event/" . $event_id;
68 $onerror_url = $a->get_baseurl() . "/events/" . $action . "?summary=$summary&description=$desc&location=$location&start=$start_text&finish=$finish_text&adjust=$adjust&nofinish=$nofinish";
70 if(strcmp($finish,$start) < 0 && !$nofinish) {
71 notice( t('Event can not end before it has started.') . EOL);
72 if(intval($_REQUEST['preview'])) {
73 echo( t('Event can not end before it has started.'));
79 if((! $summary) || (! $start)) {
80 notice( t('Event title and start time are required.') . EOL);
81 if(intval($_REQUEST['preview'])) {
82 echo( t('Event title and start time are required.'));
88 $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
90 $c = q("select id from contact where uid = %d and self = 1 limit 1",
100 $str_group_allow = perms2str($_POST['group_allow']);
101 $str_contact_allow = perms2str($_POST['contact_allow']);
102 $str_group_deny = perms2str($_POST['group_deny']);
103 $str_contact_deny = perms2str($_POST['contact_deny']);
105 // Undo the pseudo-contact of self, since there are real contacts now
106 if( strpos($str_contact_allow, '<' . $self . '>') !== false )
108 $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
110 // Make sure to set the `private` field as true. This is necessary to
111 // have the posts show up correctly in Diaspora if an event is created
112 // as visible only to self at first, but then edited to display to others.
113 if( strlen($str_group_allow) or strlen($str_contact_allow) or strlen($str_group_deny) or strlen($str_contact_deny) )
115 $private_event = true;
119 // Note: do not set `private` field for self-only events. It will
120 // keep even you from seeing them!
121 $str_contact_allow = '<' . $self . '>';
122 $str_group_allow = $str_contact_deny = $str_group_deny = '';
127 $datarray['start'] = $start;
128 $datarray['finish'] = $finish;
129 $datarray['summary'] = $summary;
130 $datarray['desc'] = $desc;
131 $datarray['location'] = $location;
132 $datarray['type'] = $type;
133 $datarray['adjust'] = $adjust;
134 $datarray['nofinish'] = $nofinish;
135 $datarray['uid'] = $uid;
136 $datarray['cid'] = $cid;
137 $datarray['allow_cid'] = $str_contact_allow;
138 $datarray['allow_gid'] = $str_group_allow;
139 $datarray['deny_cid'] = $str_contact_deny;
140 $datarray['deny_gid'] = $str_group_deny;
141 $datarray['private'] = (($private_event) ? 1 : 0);
142 $datarray['id'] = $event_id;
143 $datarray['created'] = $created;
144 $datarray['edited'] = $edited;
146 if(intval($_REQUEST['preview'])) {
147 $html = format_event_html($datarray);
152 $item_id = event_store($datarray);
155 proc_run('php',"include/notifier.php","event","$item_id");
161 function events_content(&$a) {
164 notice( t('Permission denied.') . EOL);
168 if(($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
169 $r = q("update event set ignore = 1 where id = %d and uid = %d",
175 if(($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
176 $r = q("update event set ignore = 0 where id = %d and uid = %d",
182 if ($a->theme_events_in_profile)
183 nav_set_selected('home');
185 nav_set_selected('events');
187 $editselect = 'none';
188 if( feature_enabled(local_user(), 'richtext') )
189 $editselect = 'textareas';
191 // First day of the week (0 = Sunday)
192 $firstDay = get_pconfig(local_user(),'system','first_day_of_week');
193 if ($firstDay === false) $firstDay=0;
196 "firstDay" => $firstDay,
204 "Sunday" => t("Sunday"),
205 "Monday" => t("Monday"),
206 "Tuesday" => t("Tuesday"),
207 "Wednesday" => t("Wednesday"),
208 "Thursday" => t("Thursday"),
209 "Friday" => t("Friday"),
210 "Saturday" => t("Saturday"),
223 "January" => t("January"),
224 "February" => t("February"),
225 "March" => t("March"),
226 "April" => t("April"),
230 "August" => t("August"),
231 "September" => t("September"),
232 "October" => t("October"),
233 "November" => t("November"),
234 "December" => t("December"),
235 "today" => t("today"),
236 "month" => t("month"),
241 $htpl = get_markup_template('event_head.tpl');
242 $a->page['htmlhead'] .= replace_macros($htpl,array(
243 '$baseurl' => $a->get_baseurl(),
245 '$editselect' => $editselect
248 $etpl = get_markup_template('event_end.tpl');
249 $a->page['end'] .= replace_macros($etpl,array(
250 '$baseurl' => $a->get_baseurl(),
251 '$editselect' => $editselect
256 if ($a->theme_events_in_profile)
257 $tabs = profile_tabs($a, True);
264 $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
267 if($a->argc > 2 && $a->argv[1] == 'event') {
269 $event_id = intval($a->argv[2]);
271 if($a->argv[1] === 'new') {
275 if($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
277 $y = intval($a->argv[1]);
278 $m = intval($a->argv[2]);
282 if($mode == 'view') {
285 $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
286 $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
288 $y = intval($thisyear);
290 $m = intval($thismonth);
292 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
293 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
302 if($nextmonth > 12) {
315 $dim = get_dim($y,$m);
316 $start = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
317 $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
320 if ($a->argv[1] === 'json'){
321 if (x($_GET,'start')) $start = date("Y-m-d h:i:s", $_GET['start']);
322 if (x($_GET,'end')) $finish = date("Y-m-d h:i:s", $_GET['end']);
325 $start = datetime_convert('UTC','UTC',$start);
326 $finish = datetime_convert('UTC','UTC',$finish);
328 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
329 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
333 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
334 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
335 LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
336 WHERE `event`.`uid` = %d AND `event`.`id` = %d",
337 intval(local_user()),
341 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
342 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
343 LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
344 WHERE `event`.`uid` = %d and event.ignore = %d
345 AND ((`adjust` = 0 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s')
346 OR (`adjust` = 1 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s')) ",
347 intval(local_user()),
352 dbesc($adjust_start),
353 dbesc($adjust_start),
354 dbesc($adjust_finish)
361 $r = sort_by_date($r);
363 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
365 $links[$j] = $a->get_baseurl() . '/' . $a->cmd . '#link-' . $j;
376 $r = sort_by_date($r);
380 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
381 $d = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], $fmt) : datetime_convert('UTC','UTC',$rr['start'],$fmt));
382 $d = day_translate($d);
384 $start = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'c') : datetime_convert('UTC','UTC',$rr['start'],'c'));
385 if ($rr['nofinish']){
388 $end = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['finish'], 'c') : datetime_convert('UTC','UTC',$rr['finish'],'c'));
392 $is_first = ($d !== $last_date);
395 $edit = ((! $rr['cid']) ? array($a->get_baseurl().'/events/event/'.$rr['id'],t('Edit event'),'','') : null);
396 $title = strip_tags(html_entity_decode(bbcode($rr['summary']),ENT_QUOTES,'UTF-8'));
398 list($title, $_trash) = explode("<br",bbcode($rr['desc']),2);
399 $title = strip_tags(html_entity_decode($title,ENT_QUOTES,'UTF-8'));
401 $html = format_event_html($rr);
402 $rr['desc'] = bbcode($rr['desc']);
403 $rr['location'] = bbcode($rr['location']);
414 'is_first'=>$is_first,
417 'plink' => array($rr['plink'],t('link to source'),'',''),
424 if ($a->argv[1] === 'json'){
425 echo json_encode($events); killme();
428 // links: array('href', 'text', 'extra css classes', 'title')
430 $tpl = get_markup_template("event.tpl");
432 // if (get_config('experimentals','new_calendar')==1){
433 $tpl = get_markup_template("events-js.tpl");
435 // $tpl = get_markup_template("events.tpl");
439 // Get rid of dashes in key names, Smarty3 can't handle them
440 foreach($events as $key => $event) {
441 $event_item = array();
442 foreach($event['item'] as $k => $v) {
443 $k = str_replace('-','_',$k);
444 $event_item[$k] = $v;
446 $events[$key]['item'] = $event_item;
449 $o = replace_macros($tpl, array(
450 '$baseurl' => $a->get_baseurl(),
452 '$title' => t('Events'),
453 '$new_event'=> array($a->get_baseurl().'/events/new',t('Create New Event'),'',''),
454 '$previus' => array($a->get_baseurl()."/events/$prevyear/$prevmonth",t('Previous'),'',''),
455 '$next' => array($a->get_baseurl()."/events/$nextyear/$nextmonth",t('Next'),'',''),
456 '$calendar' => cal($y,$m,$links, ' eventcal'),
458 '$events' => $events,
463 if (x($_GET,'id')){ echo $o; killme(); }
469 if($mode === 'edit' && $event_id) {
470 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
478 // Passed parameters overrides anything found in the DB
479 if($mode === 'edit' || $mode === 'new') {
480 if(!x($orig_event)) $orig_event = array();
481 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
482 if(x($_REQUEST,'nofinish')) $orig_event['nofinish'] = $_REQUEST['nofinish'];
483 if(x($_REQUEST,'adjust')) $orig_event['adjust'] = $_REQUEST['adjust'];
484 if(x($_REQUEST,'summary')) $orig_event['summary'] = $_REQUEST['summary'];
485 if(x($_REQUEST,'description')) $orig_event['description'] = $_REQUEST['description'];
486 if(x($_REQUEST,'location')) $orig_event['location'] = $_REQUEST['location'];
487 if(x($_REQUEST,'start')) $orig_event['start'] = $_REQUEST['start'];
488 if(x($_REQUEST,'finish')) $orig_event['finish'] = $_REQUEST['finish'];
491 if($mode === 'edit' || $mode === 'new') {
493 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
494 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
495 $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
496 $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
497 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
498 $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
499 $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
500 $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
506 $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ' );
509 $sh_checked .= ' disabled="disabled" ';
512 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
513 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
515 $tz = date_default_timezone_get();
517 $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
519 $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
520 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
521 $sday = datetime_convert('UTC', $tz, $sdt, 'd');
523 $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
524 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
526 $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
527 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
528 $fday = datetime_convert('UTC', $tz, $fdt, 'd');
530 $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
531 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
533 $f = get_config('system','event_input_format');
537 require_once('include/acl_selectors.php');
539 $tpl = get_markup_template('event_form.tpl');
541 $o .= replace_macros($tpl,array(
542 '$post' => $a->get_baseurl() . '/events',
547 '$title' => t('Event details'),
548 '$desc' => t('Starting date and Title are required.'),
549 '$s_text' => t('Event Starts:') . ' <span class="required" title="' . t('Required') . '">*</span>',
550 '$s_dsel' => datetimesel($f,new DateTime(),DateTime::createFromFormat('Y',$syear+5),DateTime::createFromFormat('Y-m-d H:i',"$syear-$smonth-$sday $shour:$sminute"),'start_text',true,true,'','',true),
551 '$n_text' => t('Finish date/time is not known or not relevant'),
552 '$n_checked' => $n_checked,
553 '$f_text' => t('Event Finishes:'),
554 '$f_dsel' => datetimesel($f,new DateTime(),DateTime::createFromFormat('Y',$fyear+5),DateTime::createFromFormat('Y-m-d H:i',"$fyear-$fmonth-$fday $fhour:$fminute"),'finish_text',true,true,'start_text'),
555 '$a_text' => t('Adjust for viewer timezone'),
556 '$a_checked' => $a_checked,
557 '$d_text' => t('Description:'),
558 '$d_orig' => $d_orig,
559 '$l_text' => t('Location:'),
560 '$l_orig' => $l_orig,
561 '$t_text' => t('Title:') . ' <span class="required" title="' . t('Required') . '">*</span>',
562 '$t_orig' => $t_orig,
563 '$sh_text' => t('Share this event'),
564 '$sh_checked' => $sh_checked,
565 '$preview' => t('Preview'),
566 '$acl' => (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user))),
567 '$submit' => t('Submit')