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