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