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