]> git.mxchange.org Git - friendica.git/blob - mod/events.php
Hide the calculation for "previous" and "next" behind a setting
[friendica.git] / mod / events.php
1 <?php
2 /**
3  * @fiel mod/events.php
4  * @brief The events module
5  */
6 require_once('include/bbcode.php');
7 require_once('include/datetime.php');
8 require_once('include/event.php');
9 require_once('include/items.php');
10
11 function events_init(App $a) {
12         if (! local_user()) {
13                 return;
14         }
15
16         if ($a->argc == 1) {
17                 // if it's a json request abort here becaus we don't
18                 // need the widget data
19                 if ($a->argv[1] === 'json')
20                         return;
21
22                 $cal_widget = widget_events();
23
24                 if (! x($a->page,'aside')) {
25                         $a->page['aside'] = '';
26                 }
27
28                 $a->page['aside'] .= $cal_widget;
29         }
30
31         return;
32 }
33
34 function events_post(App $a) {
35
36         logger('post: ' . print_r($_REQUEST,true));
37
38         if (! local_user()) {
39                 return;
40         }
41
42         $event_id = ((x($_POST,'event_id')) ? intval($_POST['event_id']) : 0);
43         $cid = ((x($_POST,'cid')) ? intval($_POST['cid']) : 0);
44         $uid      = local_user();
45
46         $start_text = escape_tags($_REQUEST['start_text']);
47         $finish_text = escape_tags($_REQUEST['finish_text']);
48
49         $adjust   = intval($_POST['adjust']);
50         $nofinish = intval($_POST['nofinish']);
51
52         // The default setting for the `private` field in event_store() is false, so mirror that
53         $private_event = false;
54
55         if ($start_text) {
56                 $start = $start_text;
57         }
58         else {
59                 $start    = sprintf('%d-%d-%d %d:%d:0',$startyear,$startmonth,$startday,$starthour,$startminute);
60         }
61
62         if ($nofinish) {
63                 $finish = '0000-00-00 00:00:00';
64         }
65
66         if ($finish_text) {
67                 $finish = $finish_text;
68         }
69         else {
70                 $finish    = sprintf('%d-%d-%d %d:%d:0',$finishyear,$finishmonth,$finishday,$finishhour,$finishminute);
71         }
72
73         if ($adjust) {
74                 $start = datetime_convert(date_default_timezone_get(),'UTC',$start);
75                 if (! $nofinish) {
76                         $finish = datetime_convert(date_default_timezone_get(),'UTC',$finish);
77                 }
78         }
79         else {
80                 $start = datetime_convert('UTC','UTC',$start);
81                 if (! $nofinish) {
82                         $finish = datetime_convert('UTC','UTC',$finish);
83                 }
84         }
85
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.
90
91         $summary  = escape_tags(trim($_POST['summary']));
92         $desc     = escape_tags(trim($_POST['desc']));
93         $location = escape_tags(trim($_POST['location']));
94         $type     = 'event';
95
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";
98
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.'));
103                         killme();
104                 }
105                 goaway($onerror_url);
106         }
107
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.'));
112                         killme();
113                 }
114                 goaway($onerror_url);
115         }
116
117         $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
118
119         $c = q("select id from contact where uid = %d and self = 1 limit 1",
120                 intval(local_user())
121         );
122         if(count($c))
123                 $self = $c[0]['id'];
124         else
125                 $self = 0;
126
127
128         if($share) {
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']);
133
134                 // Undo the pseudo-contact of self, since there are real contacts now
135                 if( strpos($str_contact_allow, '<' . $self . '>') !== false )
136                 {
137                         $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
138                 }
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) )
143                 {
144                         $private_event = true;
145                 }
146         }
147         else {
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 = '';
152         }
153
154
155         $datarray = array();
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;
175
176         if(intval($_REQUEST['preview'])) {
177                 $html = format_event_html($datarray);
178                 echo $html;
179                         killme();
180         }
181
182         $item_id = event_store($datarray);
183
184         if(! $cid)
185                 proc_run(PRIORITY_HIGH, "include/notifier.php", "event", $item_id);
186
187         goaway($_SESSION['return_url']);
188 }
189
190
191
192 function events_content(App $a) {
193
194         if (! local_user()) {
195                 notice( t('Permission denied.') . EOL);
196                 return;
197         }
198
199         if ($a->argc == 1) {
200                 $_SESSION['return_url'] = App::get_baseurl() . '/' . $a->cmd;
201         }
202
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",
205                         intval($a->argv[2]),
206                         intval(local_user())
207                 );
208         }
209
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",
212                         intval($a->argv[2]),
213                         intval(local_user())
214                 );
215         }
216
217         if ($a->theme_events_in_profile) {
218                 nav_set_selected('home');
219         } else {
220                 nav_set_selected('events');
221         }
222
223         $editselect = 'none';
224         if ( feature_enabled(local_user(), 'richtext') ) {
225                 $editselect = 'textareas';
226         }
227
228         // get the translation strings for the callendar
229         $i18n = get_event_strings();
230
231         $htpl = get_markup_template('event_head.tpl');
232         $a->page['htmlhead'] .= replace_macros($htpl,array(
233                 '$baseurl' => App::get_baseurl(),
234                 '$module_url' => '/events',
235                 '$modparams' => 1,
236                 '$i18n' => $i18n,
237                 '$editselect' => $editselect
238         ));
239
240         $etpl = get_markup_template('event_end.tpl');
241         $a->page['end'] .= replace_macros($etpl,array(
242                 '$baseurl' => App::get_baseurl(),
243                 '$editselect' => $editselect
244         ));
245
246         $o ="";
247         // tabs
248         if ($a->theme_events_in_profile)
249                 $tabs = profile_tabs($a, True);
250
251
252
253         $mode = 'view';
254         $y = 0;
255         $m = 0;
256         $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
257
258         if($a->argc > 1) {
259                 if ($a->argc > 2 && $a->argv[1] == 'event') {
260                         $mode = 'edit';
261                         $event_id = intval($a->argv[2]);
262                 }
263                 if ($a->argv[1] === 'new') {
264                         $mode = 'new';
265                         $event_id = 0;
266                 }
267                 if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
268                         $mode = 'view';
269                         $y = intval($a->argv[1]);
270                         $m = intval($a->argv[2]);
271                 }
272         }
273
274         // The view mode part is similiar to /mod/cal.php
275         if ($mode == 'view') {
276
277
278                 $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
279                 $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
280                 if (! $y) {
281                         $y = intval($thisyear);
282                 }
283                 if (! $m) {
284                         $m = intval($thismonth);
285                 }
286
287                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
288                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
289
290                 if ($y < 1901) {
291                         $y = 1900;
292                 }
293                 if ($y > 2099) {
294                         $y = 2100;
295                 }
296
297                 $nextyear = $y;
298                 $nextmonth = $m + 1;
299                 if($nextmonth > 12) {
300                                 $nextmonth = 1;
301                         $nextyear ++;
302                 }
303
304                 $prevyear = $y;
305                 if($m > 1)
306                         $prevmonth = $m - 1;
307                 else {
308                         $prevmonth = 12;
309                         $prevyear --;
310                 }
311
312                 $dim    = get_dim($y,$m);
313                 $start  = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
314                 $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
315
316
317                 if ($a->argv[1] === 'json'){
318                         if (x($_GET,'start'))   $start = $_GET['start'];
319                         if (x($_GET,'end'))     $finish = $_GET['end'];
320                 }
321
322                 $start  = datetime_convert('UTC','UTC',$start);
323                 $finish = datetime_convert('UTC','UTC',$finish);
324
325                 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
326                 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
327
328                 // put the event parametes in an array so we can better transmit them
329                 $event_params = array(
330                         'event_id' => (x($_GET,'id') ? $_GET["id"] : 0),
331                         'start' => $start,
332                         'finish' => $finish,
333                         'adjust_start' => $adjust_start,
334                         'adjust_finish' => $adjust_finish,
335                         'ignored' => $ignored,
336                 );
337
338                 // get events by id or by date
339                 if (x($_GET,'id')){
340                         $r = event_by_id(local_user(), $event_params);
341                 } else {
342                         $r = events_by_date(local_user(), $event_params);
343                 }
344
345                 $links = array();
346
347                 if (dbm::is_result($r)) {
348                         $r = sort_by_date($r);
349                         foreach ($r as $rr) {
350                                 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
351                                 if (! x($links,$j)) {
352                                         $links[$j] = App::get_baseurl() . '/' . $a->cmd . '#link-' . $j;
353                                 }
354                         }
355                 }
356
357                 $events = array();
358
359                 // transform the event in a usable array
360                 if (dbm::is_result($r)) {
361                         $r = sort_by_date($r);
362                         $events = process_events($r);
363                 }
364
365                 if ($a->argv[1] === 'json'){
366                         echo json_encode($events); killme();
367                 }
368
369                 // links: array('href', 'text', 'extra css classes', 'title')
370                 if (x($_GET,'id')){
371                         $tpl =  get_markup_template("event.tpl");
372                 } else {
373 //                      if (get_config('experimentals','new_calendar')==1){
374                                 $tpl = get_markup_template("events_js.tpl");
375 //                      } else {
376 //                              $tpl = get_markup_template("events.tpl");
377 //                      }
378                 }
379
380                 // Get rid of dashes in key names, Smarty3 can't handle them
381                 foreach($events as $key => $event) {
382                         $event_item = array();
383                         foreach($event['item'] as $k => $v) {
384                                 $k = str_replace('-','_',$k);
385                                 $event_item[$k] = $v;
386                         }
387                         $events[$key]['item'] = $event_item;
388                 }
389
390                 $o = replace_macros($tpl, array(
391                         '$baseurl'      => App::get_baseurl(),
392                         '$tabs'         => $tabs,
393                         '$title'        => t('Events'),
394                         '$view'         => t('View'),
395                         '$new_event'    => array(App::get_baseurl().'/events/new',t('Create New Event'),'',''),
396                         '$previus'      => array(App::get_baseurl()."/events/$prevyear/$prevmonth",t('Previous'),'',''),
397                         '$next'         => array(App::get_baseurl()."/events/$nextyear/$nextmonth",t('Next'),'',''),
398                         '$calendar'     => cal($y,$m,$links, ' eventcal'),
399
400                         '$events'       => $events,
401
402                         "today" => t("today"),
403                         "month" => t("month"),
404                         "week" => t("week"),
405                         "day" => t("day"),
406                         "list" => t("list"),
407                 ));
408
409                 if (x($_GET,'id')){ echo $o; killme(); }
410
411                 return $o;
412
413         }
414
415         if($mode === 'edit' && $event_id) {
416                 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
417                         intval($event_id),
418                         intval(local_user())
419                 );
420                 if (dbm::is_result($r))
421                         $orig_event = $r[0];
422         }
423
424         // Passed parameters overrides anything found in the DB
425         if($mode === 'edit' || $mode === 'new') {
426                 if(!x($orig_event)) $orig_event = array();
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'];
435         }
436
437         if($mode === 'edit' || $mode === 'new') {
438
439                 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
440                 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
441                 $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
442                 $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
443                 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
444                 $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
445                 $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
446                 $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
447
448
449                 if(! x($orig_event))
450                         $sh_checked = '';
451                 else
452                         $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ' );
453
454                 if($cid OR ($mode !== 'new'))
455                         $sh_checked .= ' disabled="disabled" ';
456
457
458                 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
459                 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
460
461                 $tz = date_default_timezone_get();
462                 if(x($orig_event))
463                         $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
464
465                 $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
466                 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
467                 $sday = datetime_convert('UTC', $tz, $sdt, 'd');
468
469                 $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
470                 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
471
472                 $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
473                 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
474                 $fday = datetime_convert('UTC', $tz, $fdt, 'd');
475
476                 $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
477                 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
478
479                 $f = get_config('system','event_input_format');
480                 if(! $f)
481                         $f = 'ymd';
482
483                 require_once('include/acl_selectors.php');
484
485                 if ($mode === 'new')
486                         $acl = (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user)));
487
488                 $tpl = get_markup_template('event_form.tpl');
489
490                 $o .= replace_macros($tpl,array(
491                         '$post' => App::get_baseurl() . '/events',
492                         '$eid' => $eid,
493                         '$cid' => $cid,
494                         '$uri' => $uri,
495
496                         '$title' => t('Event details'),
497                         '$desc' => t('Starting date and Title are required.'),
498                         '$s_text' => t('Event Starts:') . ' <span class="required" title="' . t('Required') . '">*</span>',
499                         '$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),
500                         '$n_text' => t('Finish date/time is not known or not relevant'),
501                         '$n_checked' => $n_checked,
502                         '$f_text' => t('Event Finishes:'),
503                         '$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'),
504                         '$a_text' => t('Adjust for viewer timezone'),
505                         '$a_checked' => $a_checked,
506                         '$d_text' => t('Description:'),
507                         '$d_orig' => $d_orig,
508                         '$l_text' => t('Location:'),
509                         '$l_orig' => $l_orig,
510                         '$t_text' => t('Title:') . ' <span class="required" title="' . t('Required') . '">*</span>',
511                         '$t_orig' => $t_orig,
512                         '$summary' => array('summary', t('Title:'), $t_orig, '', '*'),
513                         '$sh_text' => t('Share this event'),
514                         '$share' => array('share', t('Share this event'), $sh_checked, ''),
515                         '$sh_checked' => $sh_checked,
516                         '$nofinish' => array('nofinish', t('Finish date/time is not known or not relevant'), $n_checked),
517                         '$adjust' => array('adjust', t('Adjust for viewer timezone'), $a_checked),
518                         '$preview' => t('Preview'),
519                         '$acl' => $acl,
520                         '$submit' => t('Submit'),
521                         '$basic' => t("Basic"),
522                         '$advanced' => t("Advanced"),
523                         '$permissions' => t('Permissions'),
524
525                 ));
526
527                 return $o;
528         }
529 }