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