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