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