]> git.mxchange.org Git - friendica.git/blob - mod/events.php
Events: Now with guid.
[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['guid'] = get_guid(32);
128         $datarray['start'] = $start;
129         $datarray['finish'] = $finish;
130         $datarray['summary'] = $summary;
131         $datarray['desc'] = $desc;
132         $datarray['location'] = $location;
133         $datarray['type'] = $type;
134         $datarray['adjust'] = $adjust;
135         $datarray['nofinish'] = $nofinish;
136         $datarray['uid'] = $uid;
137         $datarray['cid'] = $cid;
138         $datarray['allow_cid'] = $str_contact_allow;
139         $datarray['allow_gid'] = $str_group_allow;
140         $datarray['deny_cid'] = $str_contact_deny;
141         $datarray['deny_gid'] = $str_group_deny;
142         $datarray['private'] = (($private_event) ? 1 : 0);
143         $datarray['id'] = $event_id;
144         $datarray['created'] = $created;
145         $datarray['edited'] = $edited;
146
147         if(intval($_REQUEST['preview'])) {
148                 $html = format_event_html($datarray);
149                 echo $html;
150                         killme();
151         }
152
153         $item_id = event_store($datarray);
154
155         if(! $cid)
156                 proc_run('php',"include/notifier.php","event","$item_id");
157
158         goaway($_SESSION['return_url']);
159 }
160
161
162
163 function events_content(&$a) {
164
165         if(! local_user()) {
166                 notice( t('Permission denied.') . EOL);
167                 return;
168         }
169
170         if($a->argc == 1)
171                 $_SESSION['return_url'] = $a->get_baseurl() . '/' . $a->cmd;
172
173         if(($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
174                 $r = q("update event set ignore = 1 where id = %d and uid = %d",
175                         intval($a->argv[2]),
176                         intval(local_user())
177                 );
178         }
179
180         if(($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
181                 $r = q("update event set ignore = 0 where id = %d and uid = %d",
182                         intval($a->argv[2]),
183                         intval(local_user())
184                 );
185         }
186
187         if ($a->theme_events_in_profile)
188                 nav_set_selected('home');
189         else
190                 nav_set_selected('events');
191
192         $editselect = 'none';
193         if( feature_enabled(local_user(), 'richtext') )
194                 $editselect = 'textareas';
195
196         // First day of the week (0 = Sunday)
197         $firstDay = get_pconfig(local_user(),'system','first_day_of_week');
198         if ($firstDay === false) $firstDay=0;
199
200         $i18n = array(
201                         "firstDay" => $firstDay,
202                         "Sun" => t("Sun"),
203                         "Mon" => t("Mon"),
204                         "Tue" => t("Tue"),
205                         "Wed" => t("Wed"),
206                         "Thu" => t("Thu"),
207                         "Fri" => t("Fri"),
208                         "Sat" => t("Sat"),
209                         "Sunday" => t("Sunday"),
210                         "Monday" => t("Monday"),
211                         "Tuesday" => t("Tuesday"),
212                         "Wednesday" => t("Wednesday"),
213                         "Thursday" => t("Thursday"),
214                         "Friday" => t("Friday"),
215                         "Saturday" => t("Saturday"),
216                         "Jan" => t("Jan"),
217                         "Feb" => t("Feb"),
218                         "Mar" => t("Mar"),
219                         "Apr" => t("Apr"),
220                         "May" => t("May"),
221                         "Jun" => t("Jun"),
222                         "Jul" => t("Jul"),
223                         "Aug" => t("Aug"),
224                         "Sep" => t("Sept"),
225                         "Oct" => t("Oct"),
226                         "Nov" => t("Nov"),
227                         "Dec" => t("Dec"),
228                         "January" => t("January"),
229                         "February" => t("February"),
230                         "March" => t("March"),
231                         "April" => t("April"),
232                         "May" => t("May"),
233                         "June" => t("June"),
234                         "July" => t("July"),
235                         "August" => t("August"),
236                         "September" => t("September"),
237                         "October" => t("October"),
238                         "November" => t("November"),
239                         "December" => t("December"),
240                         "today" => t("today"),
241                         "month" => t("month"),
242                         "week" => t("week"),
243                         "day" => t("day"),
244                 );
245
246         $htpl = get_markup_template('event_head.tpl');
247         $a->page['htmlhead'] .= replace_macros($htpl,array(
248                 '$baseurl' => $a->get_baseurl(),
249                 '$i18n' => $i18n,
250                 '$editselect' => $editselect
251         ));
252
253         $etpl = get_markup_template('event_end.tpl');
254         $a->page['end'] .= replace_macros($etpl,array(
255                 '$baseurl' => $a->get_baseurl(),
256                 '$editselect' => $editselect
257         ));
258
259         $o ="";
260         // tabs
261         if ($a->theme_events_in_profile)
262                 $tabs = profile_tabs($a, True);
263
264
265
266         $mode = 'view';
267         $y = 0;
268         $m = 0;
269         $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
270
271         if($a->argc > 1) {
272                 if($a->argc > 2 && $a->argv[1] == 'event') {
273                         $mode = 'edit';
274                         $event_id = intval($a->argv[2]);
275                 }
276                 if($a->argv[1] === 'new') {
277                         $mode = 'new';
278                         $event_id = 0;
279                 }
280                 if($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
281                         $mode = 'view';
282                         $y = intval($a->argv[1]);
283                         $m = intval($a->argv[2]);
284                 }
285         }
286
287         if($mode == 'view') {
288
289
290                 $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
291                 $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
292                 if(! $y)
293                         $y = intval($thisyear);
294                 if(! $m)
295                         $m = intval($thismonth);
296
297                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
298                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
299
300                 if($y < 1901)
301                         $y = 1900;
302                 if($y > 2099)
303                         $y = 2100;
304
305                 $nextyear = $y;
306                 $nextmonth = $m + 1;
307                 if($nextmonth > 12) {
308                                 $nextmonth = 1;
309                         $nextyear ++;
310                 }
311
312                 $prevyear = $y;
313                 if($m > 1)
314                         $prevmonth = $m - 1;
315                 else {
316                         $prevmonth = 12;
317                         $prevyear --;
318                 }
319
320                 $dim    = get_dim($y,$m);
321                 $start  = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
322                 $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
323
324
325                 if ($a->argv[1] === 'json'){
326                         if (x($_GET,'start'))   $start = date("Y-m-d h:i:s", $_GET['start']);
327                         if (x($_GET,'end'))     $finish = date("Y-m-d h:i:s", $_GET['end']);
328                 }
329
330                 $start  = datetime_convert('UTC','UTC',$start);
331                 $finish = datetime_convert('UTC','UTC',$finish);
332
333                 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
334                 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
335
336
337                 if (x($_GET,'id')){
338                         $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
339                                 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
340                                 LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
341                                 WHERE `event`.`uid` = %d AND `event`.`id` = %d",
342                                 intval(local_user()),
343                                 intval($_GET['id'])
344                         );
345                 } else {
346                         $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
347                                 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
348                                 LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
349                                 WHERE `event`.`uid` = %d and event.ignore = %d
350                                 AND ((`adjust` = 0 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s')
351                                 OR  (`adjust` = 1 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s')) ",
352                                 intval(local_user()),
353                                 intval($ignored),
354                                 dbesc($start),
355                                 dbesc($start),
356                                 dbesc($finish),
357                                 dbesc($adjust_start),
358                                 dbesc($adjust_start),
359                                 dbesc($adjust_finish)
360                         );
361                 }
362
363                 $links = array();
364
365                 if(count($r)) {
366                         $r = sort_by_date($r);
367                         foreach($r as $rr) {
368                                 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
369                                 if(! x($links,$j))
370                                         $links[$j] = $a->get_baseurl() . '/' . $a->cmd . '#link-' . $j;
371                         }
372                 }
373
374
375                 $events=array();
376
377                 $last_date = '';
378                 $fmt = t('l, F j');
379
380                 if(count($r)) {
381                         $r = sort_by_date($r);
382                         foreach($r as $rr) {
383
384
385                                 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
386                                 $d = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], $fmt) : datetime_convert('UTC','UTC',$rr['start'],$fmt));
387                                 $d = day_translate($d);
388
389                                 $start = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'c') : datetime_convert('UTC','UTC',$rr['start'],'c'));
390                                 if ($rr['nofinish']){
391                                         $end = null;
392                                 } else {
393                                         $end = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['finish'], 'c') : datetime_convert('UTC','UTC',$rr['finish'],'c'));
394                                 }
395
396
397                                 $is_first = ($d !== $last_date);
398
399                                 $last_date = $d;
400                                 $edit = ((! $rr['cid']) ? array($a->get_baseurl().'/events/event/'.$rr['id'],t('Edit event'),'','') : null);
401                                 $title = strip_tags(html_entity_decode(bbcode($rr['summary']),ENT_QUOTES,'UTF-8'));
402                                 if(! $title) {
403                                         list($title, $_trash) = explode("<br",bbcode($rr['desc']),2);
404                                         $title = strip_tags(html_entity_decode($title,ENT_QUOTES,'UTF-8'));
405                                 }
406                                 $html = format_event_html($rr);
407                                 $rr['desc'] = bbcode($rr['desc']);
408                                 $rr['location'] = bbcode($rr['location']);
409                                 $events[] = array(
410                                         'id'=>$rr['id'],
411                                         'start'=> $start,
412                                         'end' => $end,
413                                         'allDay' => false,
414                                         'title' => $title,
415
416                                         'j' => $j,
417                                         'd' => $d,
418                                         'edit' => $edit,
419                                         'is_first'=>$is_first,
420                                         'item'=>$rr,
421                                         'html'=>$html,
422                                         'plink' => array($rr['plink'],t('link to source'),'',''),
423                                 );
424
425
426                         }
427                 }
428
429                 if ($a->argv[1] === 'json'){
430                         echo json_encode($events); killme();
431                 }
432
433                 // links: array('href', 'text', 'extra css classes', 'title')
434                 if (x($_GET,'id')){
435                         $tpl =  get_markup_template("event.tpl");
436                 } else {
437 //                      if (get_config('experimentals','new_calendar')==1){
438                                 $tpl = get_markup_template("events-js.tpl");
439 //                      } else {
440 //                              $tpl = get_markup_template("events.tpl");
441 //                      }
442                 }
443
444                 // Get rid of dashes in key names, Smarty3 can't handle them
445                 foreach($events as $key => $event) {
446                         $event_item = array();
447                         foreach($event['item'] as $k => $v) {
448                                 $k = str_replace('-','_',$k);
449                                 $event_item[$k] = $v;
450                         }
451                         $events[$key]['item'] = $event_item;
452                 }
453
454                 $o = replace_macros($tpl, array(
455                         '$baseurl'      => $a->get_baseurl(),
456                         '$tabs'         => $tabs,
457                         '$title'        => t('Events'),
458                         '$new_event'=> array($a->get_baseurl().'/events/new',t('Create New Event'),'',''),
459                         '$previus'      => array($a->get_baseurl()."/events/$prevyear/$prevmonth",t('Previous'),'',''),
460                         '$next'         => array($a->get_baseurl()."/events/$nextyear/$nextmonth",t('Next'),'',''),
461                         '$calendar' => cal($y,$m,$links, ' eventcal'),
462
463                         '$events'       => $events,
464
465
466                 ));
467
468                 if (x($_GET,'id')){ echo $o; killme(); }
469
470                 return $o;
471
472         }
473
474         if($mode === 'edit' && $event_id) {
475                 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
476                         intval($event_id),
477                         intval(local_user())
478                 );
479                 if(count($r))
480                         $orig_event = $r[0];
481         }
482
483         // Passed parameters overrides anything found in the DB
484         if($mode === 'edit' || $mode === 'new') {
485                 if(!x($orig_event)) $orig_event = array();
486                 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
487                 if(x($_REQUEST,'nofinish')) $orig_event['nofinish'] = $_REQUEST['nofinish'];
488                 if(x($_REQUEST,'adjust')) $orig_event['adjust'] = $_REQUEST['adjust'];
489                 if(x($_REQUEST,'summary')) $orig_event['summary'] = $_REQUEST['summary'];
490                 if(x($_REQUEST,'description')) $orig_event['description'] = $_REQUEST['description'];
491                 if(x($_REQUEST,'location')) $orig_event['location'] = $_REQUEST['location'];
492                 if(x($_REQUEST,'start')) $orig_event['start'] = $_REQUEST['start'];
493                 if(x($_REQUEST,'finish')) $orig_event['finish'] = $_REQUEST['finish'];
494         }
495
496         if($mode === 'edit' || $mode === 'new') {
497
498                 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
499                 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
500                 $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
501                 $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
502                 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
503                 $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
504                 $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
505                 $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
506
507
508                 if(! x($orig_event))
509                         $sh_checked = '';
510                 else
511                         $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ' );
512
513                 if($cid OR ($mode !== 'new'))
514                         $sh_checked .= ' disabled="disabled" ';
515
516
517                 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
518                 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
519
520                 $tz = date_default_timezone_get();
521                 if(x($orig_event))
522                         $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
523
524                 $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
525                 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
526                 $sday = datetime_convert('UTC', $tz, $sdt, 'd');
527
528                 $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
529                 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
530
531                 $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
532                 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
533                 $fday = datetime_convert('UTC', $tz, $fdt, 'd');
534
535                 $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
536                 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
537
538                 $f = get_config('system','event_input_format');
539                 if(! $f)
540                         $f = 'ymd';
541
542                 require_once('include/acl_selectors.php');
543
544                 if ($mode === 'new')
545                         $acl = (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user)));
546
547                 $tpl = get_markup_template('event_form.tpl');
548
549                 $o .= replace_macros($tpl,array(
550                         '$post' => $a->get_baseurl() . '/events',
551                         '$eid' => $eid,
552                         '$cid' => $cid,
553                         '$uri' => $uri,
554
555                         '$title' => t('Event details'),
556                         '$desc' => t('Starting date and Title are required.'),
557                         '$s_text' => t('Event Starts:') . ' <span class="required" title="' . t('Required') . '">*</span>',
558                         '$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),
559                         '$n_text' => t('Finish date/time is not known or not relevant'),
560                         '$n_checked' => $n_checked,
561                         '$f_text' => t('Event Finishes:'),
562                         '$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'),
563                         '$a_text' => t('Adjust for viewer timezone'),
564                         '$a_checked' => $a_checked,
565                         '$d_text' => t('Description:'),
566                         '$d_orig' => $d_orig,
567                         '$l_text' => t('Location:'),
568                         '$l_orig' => $l_orig,
569                         '$t_text' => t('Title:') . ' <span class="required" title="' . t('Required') . '">*</span>',
570                         '$t_orig' => $t_orig,
571                         '$sh_text' => t('Share this event'),
572                         '$sh_checked' => $sh_checked,
573                         '$preview' => t('Preview'),
574                         '$acl' => $acl,
575                         '$submit' => t('Submit')
576
577                 ));
578
579                 return $o;
580         }
581 }