]> git.mxchange.org Git - friendica.git/blob - mod/events.php
Merge branch 'master' of git://github.com/friendika/friendika
[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
49         $desc     = escape_tags(trim($_POST['desc']));
50         $location = escape_tags(trim($_POST['location']));
51         $type     = 'event';
52
53         if((! $desc) || (! $start)) {
54                 notice('Event description and start time are required.');
55                 goaway($a->get_baseurl() . '/events/new');
56         }
57
58         $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
59
60         if($share) {
61                 $str_group_allow   = perms2str($_POST['group_allow']);
62                 $str_contact_allow = perms2str($_POST['contact_allow']);
63                 $str_group_deny    = perms2str($_POST['group_deny']);
64                 $str_contact_deny  = perms2str($_POST['contact_deny']);
65         }
66         else {
67                 $str_contact_allow = '<' . local_user() . '>';
68                 $str_group_allow = $str_contact_deny = $str_group_deny = '';
69         }
70
71
72         $datarray = array();
73         $datarray['start'] = $start;
74         $datarray['finish'] = $finish;
75         $datarray['desc'] = $desc;
76         $datarray['location'] = $location;
77         $datarray['type'] = $type;
78         $datarray['adjust'] = $adjust;
79         $datarray['nofinish'] = $nofinish;
80         $datarray['uid'] = $uid;
81         $datarray['cid'] = $cid;
82         $datarray['allow_cid'] = $str_contact_allow;
83         $datarray['allow_gid'] = $str_group_allow;
84         $datarray['deny_cid'] = $str_contact_deny;
85         $datarray['deny_gid'] = $str_group_deny;
86         $datarray['id'] = $event_id;
87         $datarray['created'] = $created;
88         $datarray['edited'] = $edited;
89
90         $item_id = event_store($datarray);
91
92         if(! $cid)
93                 proc_run('php',"include/notifier.php","event","$item_id");
94
95 }
96
97
98
99 function events_content(&$a) {
100
101         if(! local_user()) {
102                 notice( t('Permission denied.') . EOL);
103                 return;
104         }
105
106         $o .= '<h2>' . t('Events') . '</h2>';
107
108         $mode = 'view';
109         $y = 0;
110         $m = 0;
111
112         if($a->argc > 1) {
113                 if($a->argc > 2 && $a->argv[1] == 'event') {
114                         $mode = 'edit';
115                         $event_id = intval($a->argv[2]);
116                 }
117                 if($a->argv[1] === 'new') {
118                         $mode = 'new';
119                         $event_id = 0;
120                 }
121                 if($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
122                         $mode = 'view';
123                         $y = intval($a->argv[1]);
124                         $m = intval($a->argv[2]);
125                 }
126         }
127
128         if($mode == 'view') {
129             $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
130         $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
131                 if(! $y)
132                         $y = intval($thisyear);
133                 if(! $m)
134                         $m = intval($thismonth);
135
136                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
137                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future. 
138
139                 if($y < 1901)
140                         $y = 1900;
141                 if($y > 2099)
142                         $y = 2100;
143
144                 $nextyear = $y;
145                 $nextmonth = $m + 1;
146                 if($nextmonth > 12) {
147                                 $nextmonth = 1;
148                         $nextyear ++;
149                 }
150
151                 $prevyear = $y;
152                 if($m > 1)
153                         $prevmonth = $m - 1;
154                 else {
155                         $prevmonth = 12;
156                         $prevyear --;
157                 }
158
159                         
160                 $dim    = get_dim($y,$m);
161                 $start  = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
162                 $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
163         
164                 $start  = datetime_convert('UTC','UTC',$start);
165                 $finish = datetime_convert('UTC','UTC',$finish);
166
167                 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
168                 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
169 dbg(1);
170
171                 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink` FROM `event` LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` 
172                         WHERE `event`.`uid` = %d
173                         AND (( `adjust` = 0 AND `start` >= '%s' AND `start` <= '%s' ) 
174                         OR  (  `adjust` = 1 AND `start` >= '%s' AND `start` <= '%s' )) ",
175                         intval(local_user()),
176                         dbesc($start),
177                         dbesc($finish),
178                         dbesc($adjust_start),
179                         dbesc($adjust_finish)
180                 );
181 dbg(0);
182                 $links = array();
183
184                 if(count($r)) {
185                         $r = sort_by_date($r);
186                         foreach($r as $rr) {
187                                 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
188                                 if(! x($links,$j)) 
189                                         $links[$j] = $a->get_baseurl() . '/' . $a->cmd . '#link-' . $j;
190                         }
191                 }
192
193
194                 $o .= '<div id="new-event-link"><a href="' . $a->get_baseurl() . '/events/new' . '" >' . t('Create New Event') . '</a></div>';
195                 $o .= '<div id="event-calendar-wrapper">';
196
197                 $o .= '<a href="' . $a->get_baseurl() . '/events/' . $prevyear . '/' . $prevmonth . '" class="prevcal"><div id="event-calendar-prev" class="icon prev" title="' . t('Previous') . '"></div></a>';
198                 $o .= cal($y,$m,$links, ' eventcal');
199
200                 $o .= '<a href="' . $a->get_baseurl() . '/events/' . $nextyear . '/' . $nextmonth . '" class="nextcal"><div id="event-calendar-next" class="icon next" title="' . t('Next') . '"></div></a>';
201                 $o .= '</div>';
202                 $o .= '<div class="event-calendar-end"></div>';
203
204
205
206
207
208
209
210                 $last_date = '';
211                 $fmt = t('l, F j');
212
213                 if(count($r)) {
214                         $r = sort_by_date($r);
215                         foreach($r as $rr) {
216                                 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
217                                 $d = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], $fmt) : datetime_convert('UTC','UTC',$rr['start'],$fmt));
218                                 $d = day_translate($d);
219                                 if($d !== $last_date)
220                                         $o .= '<hr /><a name="link-' . $j . '" ><div class="event-list-date">' . $d . '</div></a>';
221                                 $last_date = $d;
222                                 $o .= format_event_html($rr);
223                                 $o .= '<a href="' . $a->get_baseurl() . '/events/event/' . $rr['id'] . '" title="' . t('Edit event') . '" class="edit-event-link icon pencil"></a>';
224                                 if($rr['plink'])
225                                         $o .= '<a href="' . $rr['plink'] . '" title="' . t('link to source') . '" target="external-link" class="plink-event-link icon remote-link"></a></div>';
226
227                                 $o .= '<div class="clear"></div>';
228
229                         }
230                 }
231                 return $o;
232         }
233
234         if($mode === 'edit' && $event_id) {
235                 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
236                         intval($event_id),
237                         intval(local_user())
238                 );
239                 if(count($r))
240                         $orig_event = $r[0];
241         }
242
243         if($mode === 'edit' || $mode === 'new') {
244
245                 $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
246                 $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
247                 $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
248                 $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
249                 $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
250                 $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
251                 $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
252
253
254                 if(! x($orig_event))
255                         $sh_checked = '';
256                 else
257                         $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ' );
258
259                 if($cid)
260                         $sh_checked .= ' disabled="disabled" ';
261
262                 $htpl = get_markup_template('event_head.tpl');
263                 $a->page['htmlhead'] .= replace_macros($htpl,array('$baseurl' => $a->get_baseurl()));
264
265                 $tpl = get_markup_template('event_form.tpl');
266
267                 $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
268                 $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
269
270                 $tz = ((x($orig_event) && $orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
271  
272
273
274                 $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
275                 $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
276                 $sday = datetime_convert('UTC', $tz, $sdt, 'd');
277
278                 $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
279                 $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
280
281                 $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
282                 $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
283                 $fday = datetime_convert('UTC', $tz, $fdt, 'd');
284
285                 $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
286                 $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
287
288
289                 require_once('include/acl_selectors.php');
290
291                 $o .= replace_macros($tpl,array(
292                         '$post' => $a->get_baseurl() . '/events',
293                         '$eid' => $eid, 
294                         '$cid' => $cid,
295                         '$uri' => $uri,
296                         '$e_text' => t('Event details'),
297                         '$e_desc' => t('Format is year-month-day hour:minute. Starting date and Description are required.'),
298                         '$s_text' => t('Event Starts:') . ' <span class="required">*</span> ',
299                         '$s_dsel' => datesel('start',$syear+5,$syear,false,$syear,$smonth,$sday),
300                         '$s_tsel' => timesel('start',$shour,$sminute),
301                         '$n_text' => t('Finish date/time is not known or not relevant'),
302                         '$n_checked' => $n_checked,
303                         '$f_text' => t('Event Finishes:'),
304                         '$f_dsel' => datesel('finish',$fyear+5,$fyear,false,$fyear,$fmonth,$fday),
305                         '$f_tsel' => timesel('finish',$fhour,$fminute),
306                         '$a_text' => t('Adjust for viewer timezone'),
307                         '$a_checked' => $a_checked,
308                         '$d_text' => t('Description:') . ' <span class="required">*</span>',
309                         '$d_orig' => $d_orig,
310                         '$l_text' => t('Location:'),
311                         '$l_orig' => $l_orig,
312                         '$sh_text' => t('Share this event'),
313                         '$sh_checked' => $sh_checked,
314                         '$acl' => (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user),false)),
315                         '$submit' => t('Submit')
316
317                 ));
318
319                 return $o;
320         }
321 }