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