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