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