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