]> git.mxchange.org Git - friendica.git/blob - mod/events.php
731b824dec56b1411f8a89902b882ef21c5171d1
[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                                         $allday=true;
245                                 } else {
246                                         $end = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['finish'], 'c') : datetime_convert('UTC','UTC',$rr['finish'],'c'));
247                                         $allday=false;
248                                 }
249                                 
250                                 
251                                 $is_first = ($d !== $last_date);
252                                         
253                                 $last_date = $d;
254                                 $edit = ((! $rr['cid']) ? array($a->get_baseurl().'/events/event/'.$rr['id'],t('Edit event'),'','') : null);
255
256                                 $events[] = array(
257                                         'id'=>$rr['id'],
258                                         'start'=> $start,
259                                         'end' => $end,
260                                         'allDay' => $allday,
261                                         'title' => strip_tags(bbcode($rr['desc'])),
262                                         
263                                         'j' => $j,
264                                         'd' => $d,
265                                         'edit' => $edit,
266                                         'is_first'=>$is_first,
267                                         'item'=>$rr,
268                                         'html'=>format_event_html($rr),
269                                         'plink' => array($rr['plink'],t('link to source'),'',''),
270                                 );
271
272
273                         }
274                 }
275                 
276                 if ($a->argv[1] === 'json'){
277                         echo json_encode($events); killme();
278                 }
279                 
280                 // links: array('href', 'text', 'extra css classes', 'title')
281                 if (x($_GET,'id')){
282                         $tpl =  get_markup_template("event.tpl");
283                 } else {
284                         $tpl = get_markup_template("events.tpl");
285                 }
286                 $o = replace_macros($tpl, array(
287                         '$baseurl'      => $a->get_baseurl(),
288                         '$tabs'         => $tabs,
289                         '$title'        => t('Events'),
290                         '$new_event'=> array($a->get_baseurl().'/events/new',t('Create New Event'),'',''),
291                         '$previus'      => array($a->get_baseurl()."/events/$prevyear/$prevmonth",t('Previous'),'',''),
292                         '$next'         => array($a->get_baseurl()."/events/$nextyear/$nextmonth",t('Next'),'',''),
293                         '$calendar' => cal($y,$m,$links, ' eventcal'),
294                         
295                         '$events'       => $events,
296                         
297                         
298                 ));
299                 
300                 if (x($_GET,'id')){ echo $o; killme(); }
301                 
302                 return $o;
303                 
304         }
305
306         if($mode === 'edit' && $event_id) {
307                 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
308                         intval($event_id),
309                         intval(local_user())
310                 );
311                 if(count($r))
312                         $orig_event = $r[0];
313         }
314
315         if($mode === 'edit' || $mode === 'new') {
316
317                 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
318                 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
319                 $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
320                 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
321                 $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
322                 $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
323                 $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
324
325
326                 if(! x($orig_event))
327                         $sh_checked = '';
328                 else
329                         $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ' );
330
331                 if($cid)
332                         $sh_checked .= ' disabled="disabled" ';
333
334
335
336                 $tpl = get_markup_template('event_form.tpl');
337
338                 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
339                 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
340
341                 $tz = date_default_timezone_get();
342                 if(x($orig_event))
343                         $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
344
345                 $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
346                 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
347                 $sday = datetime_convert('UTC', $tz, $sdt, 'd');
348
349                 $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
350                 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
351
352                 $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
353                 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
354                 $fday = datetime_convert('UTC', $tz, $fdt, 'd');
355
356                 $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
357                 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
358
359                 $f = get_config('system','event_input_format');
360                 if(! $f)
361                         $f = 'ymd';
362
363                 $dateformat = datesel_format($f);
364                 $timeformat = t('hour:minute');
365
366                 require_once('include/acl_selectors.php');
367
368                 $o .= replace_macros($tpl,array(
369                         '$post' => $a->get_baseurl() . '/events',
370                         '$eid' => $eid, 
371                         '$cid' => $cid,
372                         '$uri' => $uri,
373                         '$title' => t('Event details'),
374                         '$desc' => sprintf( t('Format is %s %s. Starting date and Description are required.'),$dateformat,$timeformat),
375                         
376                         '$s_text' => t('Event Starts:') . ' <span class="required">*</span> ',
377                         '$s_dsel' => datesel($f,'start',$syear+5,$syear,false,$syear,$smonth,$sday),
378                         '$s_tsel' => timesel('start',$shour,$sminute),
379                         '$n_text' => t('Finish date/time is not known or not relevant'),
380                         '$n_checked' => $n_checked,
381                         '$f_text' => t('Event Finishes:'),
382                         '$f_dsel' => datesel($f,'finish',$fyear+5,$fyear,false,$fyear,$fmonth,$fday),
383                         '$f_tsel' => timesel('finish',$fhour,$fminute),
384                         '$a_text' => t('Adjust for viewer timezone'),
385                         '$a_checked' => $a_checked,
386                         '$d_text' => t('Description:') . ' <span class="required">*</span>',
387                         '$d_orig' => $d_orig,
388                         '$l_text' => t('Location:'),
389                         '$l_orig' => $l_orig,
390                         '$sh_text' => t('Share this event'),
391                         '$sh_checked' => $sh_checked,
392                         '$acl' => (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user),false)),
393                         '$submit' => t('Submit')
394
395                 ));
396
397                 return $o;
398         }
399 }