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