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