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