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