1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare - Calendar * |
---|
4 | * http://www.eGroupWare.org * |
---|
5 | * Maintained and further developed by RalfBecker@outdoor-training.de * |
---|
6 | * Based on Webcalendar by Craig Knudsen <cknudsen@radix.net> * |
---|
7 | * http://www.radix.net/~cknudsen * |
---|
8 | * Originaly modified by Mark Peters <skeeter@phpgroupware.org> * |
---|
9 | * -------------------------------------------- * |
---|
10 | * This program is free software; you can redistribute it and/or modify it * |
---|
11 | * under the terms of the GNU General Public License as published by the * |
---|
12 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
13 | * option) any later version. * |
---|
14 | \**************************************************************************/ |
---|
15 | |
---|
16 | |
---|
17 | if (@$GLOBALS['phpgw_info']['flags']['included_classes']['socalendar_']) |
---|
18 | { |
---|
19 | return; |
---|
20 | } |
---|
21 | |
---|
22 | $GLOBALS['phpgw_info']['flags']['included_classes']['socalendar_'] = True; |
---|
23 | |
---|
24 | class socalendar_ extends socalendar__ |
---|
25 | { |
---|
26 | var $deleted_events = Array(); |
---|
27 | |
---|
28 | var $cal_event; |
---|
29 | var $today = Array('raw','day','month','year','full','dow','dm','bd'); |
---|
30 | |
---|
31 | function socalendar_() |
---|
32 | { |
---|
33 | $this->socalendar__(); |
---|
34 | |
---|
35 | if (!is_object($GLOBALS['phpgw']->asyncservice)) |
---|
36 | { |
---|
37 | $GLOBALS['phpgw']->asyncservice = CreateObject('phpgwapi.asyncservice'); |
---|
38 | } |
---|
39 | $this->async = &$GLOBALS['phpgw']->asyncservice; |
---|
40 | } |
---|
41 | |
---|
42 | function open($calendar='',$user='',$passwd='',$options='') |
---|
43 | { |
---|
44 | if($user=='') |
---|
45 | { |
---|
46 | // settype($user,'integer'); |
---|
47 | $this->user = $GLOBALS['phpgw_info']['user']['account_id']; |
---|
48 | } |
---|
49 | elseif(is_int($user)) |
---|
50 | { |
---|
51 | $this->user = $user; |
---|
52 | } |
---|
53 | elseif(is_string($user)) |
---|
54 | { |
---|
55 | $this->user = $GLOBALS['phpgw']->accounts->name2id($user); |
---|
56 | } |
---|
57 | |
---|
58 | $this->stream = $GLOBALS['phpgw']->db; |
---|
59 | return $this->stream; |
---|
60 | } |
---|
61 | |
---|
62 | function popen($calendar='',$user='',$passwd='',$options='') |
---|
63 | { |
---|
64 | return $this->open($calendar,$user,$passwd,$options); |
---|
65 | } |
---|
66 | |
---|
67 | function reopen($calendar,$options='') |
---|
68 | { |
---|
69 | return $this->stream; |
---|
70 | } |
---|
71 | |
---|
72 | function close($options='') |
---|
73 | { |
---|
74 | return True; |
---|
75 | } |
---|
76 | |
---|
77 | function create_calendar($calendar='') |
---|
78 | { |
---|
79 | return $calendar; |
---|
80 | } |
---|
81 | |
---|
82 | function rename_calendar($old_name='',$new_name='') |
---|
83 | { |
---|
84 | return $new_name; |
---|
85 | } |
---|
86 | |
---|
87 | function delete_calendar($calendar='') |
---|
88 | { |
---|
89 | $this->stream->query('SELECT cal_id FROM phpgw_cal WHERE owner='.(int)$calendar,__LINE__,__FILE__); |
---|
90 | if($this->stream->num_rows()) |
---|
91 | { |
---|
92 | while($this->stream->next_record()) |
---|
93 | { |
---|
94 | $this->delete_event((int)$this->stream->f('cal_id')); |
---|
95 | } |
---|
96 | $this->expunge(); |
---|
97 | } |
---|
98 | $this->stream->lock(array('phpgw_cal_user')); |
---|
99 | $this->stream->query('DELETE FROM phpgw_cal_user WHERE cal_login='.(int)$calendar,__LINE__,__FILE__); |
---|
100 | $this->stream->unlock(); |
---|
101 | |
---|
102 | return $calendar; |
---|
103 | } |
---|
104 | |
---|
105 | /*! |
---|
106 | @function read_alarms |
---|
107 | @abstract read the alarms of a calendar-event specified by $cal_id |
---|
108 | @returns array of alarms with alarm-id as key |
---|
109 | @note the alarm-id is a string of 'cal:'.$cal_id.':'.$alarm_nr, it is used as the job-id too |
---|
110 | */ |
---|
111 | function read_alarms($cal_id) |
---|
112 | { |
---|
113 | $alarms = array(); |
---|
114 | |
---|
115 | if ($jobs = $this->async->read('cal:'.(int)$cal_id.':%')) |
---|
116 | { |
---|
117 | foreach($jobs as $id => $job) |
---|
118 | { |
---|
119 | $alarm = $job['data']; // text, enabled |
---|
120 | $alarm['id'] = $id; |
---|
121 | $alarm['time'] = $job['next']; |
---|
122 | |
---|
123 | $alarms[$id] = $alarm; |
---|
124 | } |
---|
125 | } |
---|
126 | return $alarms; |
---|
127 | } |
---|
128 | |
---|
129 | /*! |
---|
130 | @function read_alarm |
---|
131 | @abstract read a single alarm specified by it's $id |
---|
132 | @returns array with data of the alarm |
---|
133 | @note the alarm-id is a string of 'cal:'.$cal_id.':'.$alarm_nr, it is used as the job-id too |
---|
134 | */ |
---|
135 | function read_alarm($id) |
---|
136 | { |
---|
137 | if (!($jobs = $this->async->read($id))) |
---|
138 | { |
---|
139 | return False; |
---|
140 | } |
---|
141 | list($id,$job) = each($jobs); |
---|
142 | $alarm = $job['data']; // text, enabled |
---|
143 | $alarm['id'] = $id; |
---|
144 | $alarm['time'] = $job['next']; |
---|
145 | |
---|
146 | //echo "<p>read_alarm('$id')="; print_r($alarm); echo "</p>\n"; |
---|
147 | return $alarm; |
---|
148 | } |
---|
149 | |
---|
150 | /*! |
---|
151 | @function save_alarm |
---|
152 | @abstract saves a new or updated alarm |
---|
153 | @syntax save_alarm($cal_id,$alarm,$id=False) |
---|
154 | @param $cal_id Id of the calendar-entry |
---|
155 | @param $alarm array with fields: text, owner, enabled, .. |
---|
156 | @returns the id of the alarm |
---|
157 | */ |
---|
158 | function save_alarm($cal_id,$alarm) |
---|
159 | { |
---|
160 | //echo "<p>save_alarm(cal_id=$cal_id, alarm="; print_r($alarm); echo ")</p>\n"; |
---|
161 | if (!($id = $alarm['id'])) |
---|
162 | { |
---|
163 | $alarms = $this->read_alarms($cal_id); // find a free alarm# |
---|
164 | $n = count($alarms); |
---|
165 | do |
---|
166 | { |
---|
167 | $id = 'cal:'.(int)$cal_id.':'.$n; |
---|
168 | ++$n; |
---|
169 | } |
---|
170 | while (@isset($alarms[$id])); |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | $this->async->cancel_timer($id); |
---|
175 | } |
---|
176 | $alarm['cal_id'] = $cal_id; // we need the back-reference |
---|
177 | |
---|
178 | //$alarm['time'] -= $GLOBALS['phpgw']->datetime->tz_offset; // time should be stored in server timezone |
---|
179 | if (!$this->async->set_timer($alarm['time'],$id,'calendar.bocalendar.send_alarm',$alarm)) |
---|
180 | { |
---|
181 | return False; |
---|
182 | } |
---|
183 | return $id; |
---|
184 | } |
---|
185 | |
---|
186 | /*! |
---|
187 | @function delete_alarms($cal_id) |
---|
188 | @abstract delete all alarms of a calendar-entry |
---|
189 | @returns the number of alarms deleted |
---|
190 | */ |
---|
191 | function delete_alarms($cal_id) |
---|
192 | { |
---|
193 | $alarms = $this->read_alarms($cal_id); |
---|
194 | |
---|
195 | foreach($alarms as $id => $alarm) |
---|
196 | { |
---|
197 | $this->async->cancel_timer($id); |
---|
198 | } |
---|
199 | return count($alarms); |
---|
200 | } |
---|
201 | |
---|
202 | /*! |
---|
203 | @function delete_alarm($id) |
---|
204 | @abstract delete one alarms identified by its id |
---|
205 | @returns the number of alarms deleted |
---|
206 | */ |
---|
207 | function delete_alarm($id) |
---|
208 | { |
---|
209 | return $this->async->cancel_timer($id); |
---|
210 | } |
---|
211 | |
---|
212 | function fetch_event($event_id,$options='') |
---|
213 | { |
---|
214 | if(!isset($this->stream)) |
---|
215 | { |
---|
216 | return False; |
---|
217 | } |
---|
218 | |
---|
219 | $event_id = (int)$event_id; |
---|
220 | |
---|
221 | $this->stream->lock(array('phpgw_cal','phpgw_cal_user','phpgw_cal_repeats','phpgw_cal_extra'/* OLD-ALARM,'phpgw_cal_alarm'*/)); |
---|
222 | |
---|
223 | $this->stream->query('SELECT * FROM phpgw_cal WHERE cal_id='.$event_id,__LINE__,__FILE__); |
---|
224 | |
---|
225 | if($this->stream->num_rows() > 0) |
---|
226 | { |
---|
227 | $this->event_init(); |
---|
228 | |
---|
229 | $this->stream->next_record(); |
---|
230 | // Load the calendar event data from the db into $event structure |
---|
231 | // Use http://www.php.net/manual/en/function.mcal-fetch-event.php as the reference |
---|
232 | $this->add_attribute('owner',(int)$this->stream->f('owner')); |
---|
233 | $this->add_attribute('id',(int)$this->stream->f('cal_id')); |
---|
234 | $this->set_class((int)$this->stream->f('is_public')); |
---|
235 | $this->set_category($this->stream->f('category')); |
---|
236 | $this->set_title(stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('title')))); |
---|
237 | $this->set_description(stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('description')))); |
---|
238 | $this->set_ex_participants(stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('ex_participants')))); |
---|
239 | $this->add_attribute('uid',$GLOBALS['phpgw']->strip_html($this->stream->f('uid'))); |
---|
240 | $this->add_attribute('location',stripslashes($GLOBALS['phpgw']->strip_html($this->stream->f('location')))); |
---|
241 | $this->add_attribute('reference',(int)$this->stream->f('reference')); |
---|
242 | |
---|
243 | // This is the preferred method once everything is normalized... |
---|
244 | //$this->event->alarm = (int)$this->stream->f('alarm'); |
---|
245 | // But until then, do it this way... |
---|
246 | //Legacy Support (New) |
---|
247 | |
---|
248 | $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('datetime')); |
---|
249 | $this->set_start($datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']); |
---|
250 | |
---|
251 | $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('mdatetime')); |
---|
252 | $this->set_date('modtime',$datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']); |
---|
253 | |
---|
254 | $datetime = $GLOBALS['phpgw']->datetime->localdates($this->stream->f('edatetime')); |
---|
255 | $this->set_end($datetime['year'],$datetime['month'],$datetime['day'],$datetime['hour'],$datetime['minute'],$datetime['second']); |
---|
256 | |
---|
257 | //Legacy Support |
---|
258 | $this->add_attribute('priority',(int)$this->stream->f('priority')); |
---|
259 | if($this->stream->f('cal_group') || $this->stream->f('groups') != 'NULL') |
---|
260 | { |
---|
261 | $groups = explode(',',$this->stream->f('groups')); |
---|
262 | for($j=1;$j<count($groups) - 1;$j++) |
---|
263 | { |
---|
264 | $this->add_attribute('groups',$groups[$j],$j-1); |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | $this->stream->query('SELECT * FROM phpgw_cal_repeats WHERE cal_id='.$event_id,__LINE__,__FILE__); |
---|
269 | if($this->stream->num_rows()) |
---|
270 | { |
---|
271 | $this->stream->next_record(); |
---|
272 | |
---|
273 | $this->add_attribute('recur_type',(int)$this->stream->f('recur_type')); |
---|
274 | $this->add_attribute('recur_interval',(int)$this->stream->f('recur_interval')); |
---|
275 | $enddate = $this->stream->f('recur_enddate'); |
---|
276 | if($enddate != 0 && $enddate != Null) |
---|
277 | { |
---|
278 | $datetime = $GLOBALS['phpgw']->datetime->localdates($enddate); |
---|
279 | $this->add_attribute('recur_enddate',$datetime['year'],'year'); |
---|
280 | $this->add_attribute('recur_enddate',$datetime['month'],'month'); |
---|
281 | $this->add_attribute('recur_enddate',$datetime['day'],'mday'); |
---|
282 | $this->add_attribute('recur_enddate',$datetime['hour'],'hour'); |
---|
283 | $this->add_attribute('recur_enddate',$datetime['minute'],'min'); |
---|
284 | $this->add_attribute('recur_enddate',$datetime['second'],'sec'); |
---|
285 | } |
---|
286 | else |
---|
287 | { |
---|
288 | $this->add_attribute('recur_enddate',0,'year'); |
---|
289 | $this->add_attribute('recur_enddate',0,'month'); |
---|
290 | $this->add_attribute('recur_enddate',0,'mday'); |
---|
291 | $this->add_attribute('recur_enddate',0,'hour'); |
---|
292 | $this->add_attribute('recur_enddate',0,'min'); |
---|
293 | $this->add_attribute('recur_enddate',0,'sec'); |
---|
294 | } |
---|
295 | $this->add_attribute('recur_enddate',0,'alarm'); |
---|
296 | if($this->debug) |
---|
297 | { |
---|
298 | echo 'Event ID#'.$this->event['id'].' : Enddate = '.$enddate."<br>\n"; |
---|
299 | } |
---|
300 | $this->add_attribute('recur_data',$this->stream->f('recur_data')); |
---|
301 | |
---|
302 | $exception_list = $this->stream->f('recur_exception'); |
---|
303 | $exceptions = Array(); |
---|
304 | if(strpos(' '.$exception_list,',')) |
---|
305 | { |
---|
306 | $exceptions = explode(',',$exception_list); |
---|
307 | } |
---|
308 | elseif($exception_list != '') |
---|
309 | { |
---|
310 | $exceptions[]= $exception_list; |
---|
311 | } |
---|
312 | $this->add_attribute('recur_exception',$exceptions); |
---|
313 | } |
---|
314 | |
---|
315 | //Legacy Support |
---|
316 | $this->stream->query('SELECT * FROM phpgw_cal_user WHERE cal_id='.$event_id,__LINE__,__FILE__); |
---|
317 | if($this->stream->num_rows()) |
---|
318 | { |
---|
319 | while($this->stream->next_record()) |
---|
320 | { |
---|
321 | if((int)$this->stream->f('cal_login') == (int)$this->user) |
---|
322 | { |
---|
323 | $this->add_attribute('users_status',$this->stream->f('cal_status')); |
---|
324 | } |
---|
325 | $this->add_attribute('participants',$this->stream->f('cal_status'),(int)$this->stream->f('cal_login')); |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | // Custom fields |
---|
330 | $this->stream->query('SELECT * FROM phpgw_cal_extra WHERE cal_id='.$event_id,__LINE__,__FILE__); |
---|
331 | if($this->stream->num_rows()) |
---|
332 | { |
---|
333 | while($this->stream->next_record()) |
---|
334 | { |
---|
335 | $this->add_attribute('#'.$this->stream->f('cal_extra_name'),$this->stream->f('cal_extra_value')); |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | /* OLD-ALARM |
---|
340 | if($this->event['reference']) |
---|
341 | { |
---|
342 | // What is event['reference']??? |
---|
343 | $alarm_cal_id = $event_id.','.$this->event['reference']; |
---|
344 | } |
---|
345 | else |
---|
346 | { |
---|
347 | $alarm_cal_id = $event_id; |
---|
348 | } |
---|
349 | |
---|
350 | //echo '<!-- cal_id='.$alarm_cal_id.' -->'."\n"; |
---|
351 | //$this->stream->query('SELECT * FROM phpgw_cal_alarm WHERE cal_id in ('.$alarm_cal_id.') AND cal_owner='.$this->user,__LINE__,__FILE__); |
---|
352 | $this->stream->query('SELECT * FROM phpgw_cal_alarm WHERE cal_id='.$event_id.' AND cal_owner='.$this->user,__LINE__,__FILE__); |
---|
353 | if($this->stream->num_rows()) |
---|
354 | { |
---|
355 | while($this->stream->next_record()) |
---|
356 | { |
---|
357 | $this->event['alarm'][] = Array( |
---|
358 | 'id' => (int)$this->stream->f('alarm_id'), |
---|
359 | 'time' => (int)$this->stream->f('cal_time'), |
---|
360 | 'text' => $this->stream->f('cal_text'), |
---|
361 | 'enabled' => (int)$this->stream->f('alarm_enabled') |
---|
362 | ); |
---|
363 | } |
---|
364 | } |
---|
365 | */ |
---|
366 | } |
---|
367 | else |
---|
368 | { |
---|
369 | $this->event = False; |
---|
370 | } |
---|
371 | |
---|
372 | $this->stream->unlock(); |
---|
373 | |
---|
374 | if ($this->event) |
---|
375 | { |
---|
376 | $this->event['alarm'] = $this->read_alarms($event_id); |
---|
377 | |
---|
378 | if($this->event['reference']) |
---|
379 | { |
---|
380 | $this->event['alarm'] += $this->read_alarms($event_id); |
---|
381 | } |
---|
382 | } |
---|
383 | return $this->event; |
---|
384 | } |
---|
385 | |
---|
386 | function list_events($startYear,$startMonth,$startDay,$endYear=0,$endMonth=0,$endDay=0,$extra='',$tz_offset=0,$owner_id=0) |
---|
387 | { |
---|
388 | if(!isset($this->stream)) |
---|
389 | { |
---|
390 | return False; |
---|
391 | } |
---|
392 | |
---|
393 | $datetime = mktime(0,0,0,$startMonth,$startDay,$startYear) - $tz_offset; |
---|
394 | |
---|
395 | $user_where = ' AND (phpgw_cal_user.cal_login in ('; |
---|
396 | if(is_array($owner_id) && count($owner_id)) |
---|
397 | { |
---|
398 | $user_where .= implode(',',$owner_id); |
---|
399 | } |
---|
400 | else |
---|
401 | { |
---|
402 | $user_where .= $this->user; |
---|
403 | } |
---|
404 | $member_groups = $GLOBALS['phpgw']->accounts->membership($this->user); |
---|
405 | @reset($member_groups); |
---|
406 | while($member_groups != False && list($key,$group_info) = each($member_groups)) |
---|
407 | { |
---|
408 | $member[] = $group_info['account_id']; |
---|
409 | } |
---|
410 | @reset($member); |
---|
411 | // $user_where .= ','.implode(',',$member); |
---|
412 | $user_where .= ')) '; |
---|
413 | |
---|
414 | if($this->debug) |
---|
415 | { |
---|
416 | echo '<!-- '.$user_where.' -->'."\n"; |
---|
417 | } |
---|
418 | |
---|
419 | $startDate = 'AND ( ( (phpgw_cal.datetime >= '.$datetime.') '; |
---|
420 | |
---|
421 | $enddate = ''; |
---|
422 | if($endYear != 0 && $endMonth != 0 && $endDay != 0) |
---|
423 | { |
---|
424 | $edatetime = mktime(23,59,59,(int)$endMonth,(int)$endDay,(int)$endYear) - $tz_offset; |
---|
425 | $endDate .= 'AND (phpgw_cal.edatetime <= '.$edatetime.') ) ' |
---|
426 | . 'OR ( (phpgw_cal.datetime <= '.$datetime.') ' |
---|
427 | . 'AND (phpgw_cal.edatetime >= '.$edatetime.') ) ' |
---|
428 | . 'OR ( (phpgw_cal.datetime >= '.$datetime.') ' |
---|
429 | . 'AND (phpgw_cal.datetime <= '.$edatetime.') ' |
---|
430 | . 'AND (phpgw_cal.edatetime >= '.$edatetime.') ) ' |
---|
431 | . 'OR ( (phpgw_cal.datetime <= '.$datetime.') ' |
---|
432 | . 'AND (phpgw_cal.edatetime >= '.$datetime.') ' |
---|
433 | . 'AND (phpgw_cal.edatetime <= '.$edatetime.') '; |
---|
434 | } |
---|
435 | $endDate .= ') ) '; |
---|
436 | |
---|
437 | $order_by = 'ORDER BY phpgw_cal.datetime ASC, phpgw_cal.edatetime ASC, phpgw_cal.priority ASC'; |
---|
438 | if($this->debug) |
---|
439 | { |
---|
440 | echo "SQL : ".$user_where.$startDate.$endDate.$extra."<br>\n"; |
---|
441 | } |
---|
442 | return $this->get_event_ids(False,$user_where.$startDate.$endDate.$extra.$order_by); |
---|
443 | } |
---|
444 | |
---|
445 | function append_event() |
---|
446 | { |
---|
447 | $this->save_event($this->event); |
---|
448 | $this->send_update(MSG_ADDED,$this->event->participants,'',$this->event); |
---|
449 | return $this->event['id']; |
---|
450 | } |
---|
451 | |
---|
452 | function store_event() |
---|
453 | { |
---|
454 | return $this->save_event(&$this->event); |
---|
455 | } |
---|
456 | |
---|
457 | function delete_event($event_id) |
---|
458 | { |
---|
459 | $this->deleted_events[] = $event_id; |
---|
460 | } |
---|
461 | |
---|
462 | function snooze($event_id) |
---|
463 | { |
---|
464 | //Turn off an alarm for an event |
---|
465 | //Returns true. |
---|
466 | } |
---|
467 | |
---|
468 | function list_alarms($begin_year='',$begin_month='',$begin_day='',$end_year='',$end_month='',$end_day='') |
---|
469 | { |
---|
470 | //Return a list of events that has an alarm triggered at the given datetime |
---|
471 | //Returns an array of event ID's |
---|
472 | } |
---|
473 | |
---|
474 | // The function definition doesn't look correct... |
---|
475 | // Need more information for this function |
---|
476 | function next_recurrence($weekstart,$next) |
---|
477 | { |
---|
478 | // return next_recurrence (int stream, int weekstart, array next); |
---|
479 | } |
---|
480 | |
---|
481 | function expunge() |
---|
482 | { |
---|
483 | if(count($this->deleted_events) <= 0) |
---|
484 | { |
---|
485 | return 1; |
---|
486 | } |
---|
487 | $this_event = $this->event; |
---|
488 | $locks = Array( |
---|
489 | 'phpgw_cal', |
---|
490 | 'phpgw_cal_user', |
---|
491 | 'phpgw_cal_repeats', |
---|
492 | 'phpgw_cal_extra' |
---|
493 | // OLD-ALARM 'phpgw_cal_alarm' |
---|
494 | ); |
---|
495 | $this->stream->lock($locks); |
---|
496 | foreach($this->deleted_events as $cal_id) |
---|
497 | { |
---|
498 | foreach ($locks as $table) |
---|
499 | { |
---|
500 | $this->stream->query('DELETE FROM '.$table.' WHERE cal_id='.$cal_id,__LINE__,__FILE__); |
---|
501 | } |
---|
502 | } |
---|
503 | $this->stream->unlock(); |
---|
504 | |
---|
505 | foreach($this->deleted_events as $cal_id) |
---|
506 | { |
---|
507 | $this->delete_alarms($cal_id); |
---|
508 | } |
---|
509 | $this->deleted_events = array(); |
---|
510 | |
---|
511 | $this->event = $this_event; |
---|
512 | return 1; |
---|
513 | } |
---|
514 | |
---|
515 | /***************** Local functions for SQL based Calendar *****************/ |
---|
516 | |
---|
517 | function get_event_ids($search_repeats=False,$extra='',$search_extra=False) |
---|
518 | { |
---|
519 | $from = $where = ' '; |
---|
520 | if($search_repeats) |
---|
521 | { |
---|
522 | $from = ', phpgw_cal_repeats '; |
---|
523 | $where = 'AND (phpgw_cal_repeats.cal_id = phpgw_cal.cal_id) '; |
---|
524 | } |
---|
525 | if($search_extra) |
---|
526 | { |
---|
527 | $from .= 'LEFT JOIN phpgw_cal_extra ON phpgw_cal_extra.cal_id = phpgw_cal.cal_id '; |
---|
528 | } |
---|
529 | |
---|
530 | $sql = 'SELECT DISTINCT phpgw_cal.cal_id,' |
---|
531 | . 'phpgw_cal.datetime,phpgw_cal.edatetime,' |
---|
532 | . 'phpgw_cal.priority ' |
---|
533 | . 'FROM phpgw_cal_user, phpgw_cal' |
---|
534 | . $from |
---|
535 | . 'WHERE (phpgw_cal_user.cal_id = phpgw_cal.cal_id) ' |
---|
536 | . $where . $extra; |
---|
537 | |
---|
538 | if($this->debug) |
---|
539 | { |
---|
540 | echo "FULL SQL : ".$sql."<br>\n"; |
---|
541 | } |
---|
542 | |
---|
543 | $this->stream->query($sql,__LINE__,__FILE__); |
---|
544 | |
---|
545 | $retval = Array(); |
---|
546 | if($this->stream->num_rows() == 0) |
---|
547 | { |
---|
548 | if($this->debug) |
---|
549 | { |
---|
550 | echo "No records found!<br>\n"; |
---|
551 | } |
---|
552 | return $retval; |
---|
553 | } |
---|
554 | |
---|
555 | while($this->stream->next_record()) |
---|
556 | { |
---|
557 | $retval[] = (int)$this->stream->f('cal_id'); |
---|
558 | } |
---|
559 | if($this->debug) |
---|
560 | { |
---|
561 | echo "Records found!<br>\n"; |
---|
562 | } |
---|
563 | return $retval; |
---|
564 | } |
---|
565 | |
---|
566 | function save_event(&$event) |
---|
567 | { |
---|
568 | $locks = Array( |
---|
569 | 'phpgw_cal', |
---|
570 | 'phpgw_cal_user', |
---|
571 | 'phpgw_cal_repeats', |
---|
572 | 'phpgw_cal_extra' |
---|
573 | // OLD-ALARM 'phpgw_cal_alarm' |
---|
574 | ); |
---|
575 | $this->stream->lock($locks); |
---|
576 | if($event['id'] == 0) |
---|
577 | { |
---|
578 | if(!$event['uid']) |
---|
579 | { |
---|
580 | if ($GLOBALS['phpgw_info']['server']['hostname'] != '') |
---|
581 | { |
---|
582 | $id_suffix = $GLOBALS['phpgw_info']['server']['hostname']; |
---|
583 | } |
---|
584 | else |
---|
585 | { |
---|
586 | $id_suffix = $GLOBALS['phpgw']->common->randomstring(3).'local'; |
---|
587 | } |
---|
588 | $parts = Array( |
---|
589 | 0 => 'title', |
---|
590 | 1 => 'description', |
---|
591 | 2 => 'ex_participants' |
---|
592 | ); |
---|
593 | @reset($parts); |
---|
594 | while(list($key,$field) = each($parts)) |
---|
595 | { |
---|
596 | $part[$key] = substr($GLOBALS['phpgw']->crypto->encrypt($event[$field]),0,20); |
---|
597 | if(!$GLOBALS['phpgw']->crypto->enabled) |
---|
598 | { |
---|
599 | $part[$key] = bin2hex(unserialize($part[$key])); |
---|
600 | } |
---|
601 | } |
---|
602 | $event['uid'] = $part[0].'-'.$part[1].'@'.$id_suffix; |
---|
603 | } |
---|
604 | $this->stream->query('INSERT INTO phpgw_cal(uid,title,owner,priority,is_public,category) ' |
---|
605 | . "values('".$event['uid']."','".$this->stream->db_addslashes($event['title']) |
---|
606 | . "',".(int)$event['owner'].','.(int)$event['priority'].','.(int)$event['public'].",'" |
---|
607 | . $event['category']."')",__LINE__,__FILE__); |
---|
608 | $event['id'] = $this->stream->get_last_insert_id('phpgw_cal','cal_id'); |
---|
609 | } |
---|
610 | |
---|
611 | $date = $this->maketime($event['start']) - $GLOBALS['phpgw']->datetime->tz_offset; |
---|
612 | $enddate = $this->maketime($event['end']) - $GLOBALS['phpgw']->datetime->tz_offset; |
---|
613 | $today = time() - $GLOBALS['phpgw']->datetime->tz_offset; |
---|
614 | |
---|
615 | if($event['recur_type'] != MCAL_RECUR_NONE) |
---|
616 | { |
---|
617 | $type = 'M'; |
---|
618 | } |
---|
619 | else |
---|
620 | { |
---|
621 | $type = 'E'; |
---|
622 | } |
---|
623 | |
---|
624 | $sql = 'UPDATE phpgw_cal SET ' |
---|
625 | . 'owner='.(int)$event['owner'].', ' |
---|
626 | . 'datetime='.(int)$date.', ' |
---|
627 | . 'mdatetime='.(int)$today.', ' |
---|
628 | . 'edatetime='.(int)$enddate.', ' |
---|
629 | . 'priority='.(int)$event['priority'].', ' |
---|
630 | . "category='".$this->stream->db_addslashes($event['category'])."', " |
---|
631 | . "cal_type='".$this->stream->db_addslashes($type)."', " |
---|
632 | . 'is_public='.(int)$event['public'].', ' |
---|
633 | . "title='".$this->stream->db_addslashes($event['title'])."', " |
---|
634 | . "description='".$this->stream->db_addslashes($event['description'])."', " |
---|
635 | . "ex_participants='".$this->stream->db_addslashes($event['ex_participants'])."', " |
---|
636 | . "location='".$this->stream->db_addslashes($event['location'])."', " |
---|
637 | . ($event['groups']?"groups='".(count($event['groups'])>1?implode(',',$event['groups']):','.$event['groups'][0].',')."', ":'') |
---|
638 | . 'reference='.(int)$event['reference'].' ' |
---|
639 | . 'WHERE cal_id='.(int)$event['id']; |
---|
640 | |
---|
641 | $this->stream->query($sql,__LINE__,__FILE__); |
---|
642 | |
---|
643 | $this->stream->query('DELETE FROM phpgw_cal_user WHERE cal_id='.(int)$event['id'],__LINE__,__FILE__); |
---|
644 | |
---|
645 | @reset($event['participants']); |
---|
646 | while (list($key,$value) = @each($event['participants'])) |
---|
647 | { |
---|
648 | if((int)$key == $event['owner']) |
---|
649 | { |
---|
650 | $value = 'A'; |
---|
651 | } |
---|
652 | $this->stream->query('INSERT INTO phpgw_cal_user(cal_id,cal_login,cal_status) ' |
---|
653 | . 'VALUES('.(int)$event['id'].','.(int)$key.",'".$this->stream->db_addslashes($value)."')",__LINE__,__FILE__); |
---|
654 | } |
---|
655 | |
---|
656 | if($event['recur_type'] != MCAL_RECUR_NONE) |
---|
657 | { |
---|
658 | if($event['recur_enddate']['month'] != 0 && $event['recur_enddate']['mday'] != 0 && $event['recur_enddate']['year'] != 0) |
---|
659 | { |
---|
660 | $end = $this->maketime($event['recur_enddate']) - $GLOBALS['phpgw']->datetime->tz_offset; |
---|
661 | } |
---|
662 | else |
---|
663 | { |
---|
664 | $end = 0; |
---|
665 | } |
---|
666 | |
---|
667 | $this->stream->query('SELECT count(cal_id) FROM phpgw_cal_repeats WHERE cal_id='.(int)$event['id'],__LINE__,__FILE__); |
---|
668 | $this->stream->next_record(); |
---|
669 | $num_rows = $this->stream->f(0); |
---|
670 | if($num_rows == 0) |
---|
671 | { |
---|
672 | $this->stream->query('INSERT INTO phpgw_cal_repeats(cal_id,recur_type,recur_enddate,recur_data,recur_interval) ' |
---|
673 | .'VALUES('.(int)$event['id'].','.$event['recur_type'].','.(int)$end.','.$event['recur_data'].','.$event['recur_interval'].')',__LINE__,__FILE__); |
---|
674 | } |
---|
675 | else |
---|
676 | { |
---|
677 | $this->stream->query('UPDATE phpgw_cal_repeats ' |
---|
678 | . 'SET recur_type='.$event['recur_type'].', ' |
---|
679 | . 'recur_enddate='.(int)$end.', ' |
---|
680 | . 'recur_data='.$event['recur_data'].', ' |
---|
681 | . 'recur_interval='.$event['recur_interval'].', ' |
---|
682 | . "recur_exception='".(count($event['recur_exception'])>1?implode(',',$event['recur_exception']):(count($event['recur_exception'])==1?$event['recur_exception'][0]:''))."' " |
---|
683 | . 'WHERE cal_id='.$event['id'],__LINE__,__FILE__); |
---|
684 | } |
---|
685 | } |
---|
686 | else |
---|
687 | { |
---|
688 | $this->stream->query('DELETE FROM phpgw_cal_repeats WHERE cal_id='.$event['id'],__LINE__,__FILE__); |
---|
689 | } |
---|
690 | // Custom fields |
---|
691 | $this->stream->query('DELETE FROM phpgw_cal_extra WHERE cal_id='.$event['id'],__LINE__,__FILE__); |
---|
692 | |
---|
693 | foreach($event as $name => $value) |
---|
694 | { |
---|
695 | if ($name[0] == '#' && strlen($value)) |
---|
696 | { |
---|
697 | $this->stream->query('INSERT INTO phpgw_cal_extra (cal_id,cal_extra_name,cal_extra_value) ' |
---|
698 | . 'VALUES('.$event['id'].",'".addslashes(substr($name,1))."','".addslashes($value)."')",__LINE__,__FILE__); |
---|
699 | } |
---|
700 | } |
---|
701 | /* |
---|
702 | $alarmcount = count($event['alarm']); |
---|
703 | if ($alarmcount > 1) |
---|
704 | { |
---|
705 | // this should never happen, $event['alarm'] should only be set |
---|
706 | // if creating a new event and uicalendar only sets up 1 alarm |
---|
707 | // the user must use "Alarm Management" to create/establish multiple |
---|
708 | // alarms or to edit/change an alarm |
---|
709 | echo '<!-- how did this happen, too many alarms -->'."\n"; |
---|
710 | $this->stream->unlock(); |
---|
711 | return True; |
---|
712 | } |
---|
713 | |
---|
714 | if ($alarmcount == 1) |
---|
715 | { |
---|
716 | |
---|
717 | list($key,$alarm) = @each($event['alarm']); |
---|
718 | |
---|
719 | $this->stream->query('INSERT INTO phpgw_cal_alarm(cal_id,cal_owner,cal_time,cal_text,alarm_enabled) VALUES('.$event['id'].','.$event['owner'].','.$alarm['time'].",'".$alarm['text']."',".$alarm['enabled'].')',__LINE__,__FILE__); |
---|
720 | $this->stream->query('SELECT LAST_INSERT_ID()'); |
---|
721 | $this->stream->next_record(); |
---|
722 | $alarm['id'] = $this->stream->f(0); |
---|
723 | } |
---|
724 | */ |
---|
725 | print_debug('Event Saved: ID #',$event['id']); |
---|
726 | |
---|
727 | $this->stream->unlock(); |
---|
728 | |
---|
729 | if (is_array($event['alarm'])) |
---|
730 | { |
---|
731 | foreach ($event['alarm'] as $alarm) // this are all new alarms |
---|
732 | { |
---|
733 | $this->save_alarm($event['id'],$alarm); |
---|
734 | } |
---|
735 | } |
---|
736 | $GLOBALS['phpgw_info']['cal_new_event_id'] = $event['id']; |
---|
737 | $this->event = $event; |
---|
738 | return True; |
---|
739 | } |
---|
740 | |
---|
741 | function get_alarm($cal_id) |
---|
742 | { |
---|
743 | /* OLD-ALARM |
---|
744 | $this->stream->query('SELECT cal_time, cal_text FROM phpgw_cal_alarm WHERE cal_id='.$id.' AND cal_owner='.$this->user,__LINE__,__FILE__); |
---|
745 | if($this->stream->num_rows()) |
---|
746 | { |
---|
747 | while($this->stream->next_record()) |
---|
748 | { |
---|
749 | $alarm[$this->stream->f('cal_time')] = $this->stream->f('cal_text'); |
---|
750 | } |
---|
751 | @reset($alarm); |
---|
752 | return $alarm; |
---|
753 | } |
---|
754 | else |
---|
755 | { |
---|
756 | return False; |
---|
757 | } |
---|
758 | */ |
---|
759 | $alarms = $this->read_alarms($cal_id); |
---|
760 | $ret = False; |
---|
761 | |
---|
762 | foreach($alarms as $alarm) |
---|
763 | { |
---|
764 | if ($alarm['owner'] == $this->user || !$alarm['owner']) |
---|
765 | { |
---|
766 | $ret[$alarm['time']] = $alarm['text']; |
---|
767 | } |
---|
768 | } |
---|
769 | return $ret; |
---|
770 | } |
---|
771 | |
---|
772 | function set_status($id,$owner,$status) |
---|
773 | { |
---|
774 | $status_code_short = Array( |
---|
775 | REJECTED => 'R', |
---|
776 | NO_RESPONSE => 'U', |
---|
777 | TENTATIVE => 'T', |
---|
778 | ACCEPTED => 'A' |
---|
779 | ); |
---|
780 | |
---|
781 | $this->stream->query("UPDATE phpgw_cal_user SET cal_status='".$status_code_short[$status]."' WHERE cal_id=".$id." AND cal_login=".$owner,__LINE__,__FILE__); |
---|
782 | /* OLD-ALARM |
---|
783 | if ($status == 'R') |
---|
784 | { |
---|
785 | $this->stream->query('UPDATE phpgw_cal_alarm set alarm_enabled=0 where cal_id='.$id.' and cal_owner='.$owner,__LINE__,__FILE__); |
---|
786 | } |
---|
787 | */ |
---|
788 | return True; |
---|
789 | } |
---|
790 | |
---|
791 | // End of ICal style support....... |
---|
792 | |
---|
793 | function group_search($owner=0) |
---|
794 | { |
---|
795 | $owner = ($owner==$GLOBALS['phpgw_info']['user']['account_id']?0:$owner); |
---|
796 | $groups = substr($GLOBALS['phpgw']->common->sql_search('phpgw_cal.groups',(int)$owner),4); |
---|
797 | if (!$groups) |
---|
798 | { |
---|
799 | return ''; |
---|
800 | } |
---|
801 | else |
---|
802 | { |
---|
803 | return "(phpgw_cal.is_public=2 AND (". $groups .')) '; |
---|
804 | } |
---|
805 | } |
---|
806 | |
---|
807 | function splittime_($time) |
---|
808 | { |
---|
809 | $temp = array('hour','minute','second','ampm'); |
---|
810 | $time = strrev($time); |
---|
811 | $second = (int)strrev(substr($time,0,2)); |
---|
812 | $minute = (int)strrev(substr($time,2,2)); |
---|
813 | $hour = (int)strrev(substr($time,4)); |
---|
814 | $temp['second'] = (int)$second; |
---|
815 | $temp['minute'] = (int)$minute; |
---|
816 | $temp['hour'] = (int)$hour; |
---|
817 | $temp['ampm'] = ' '; |
---|
818 | |
---|
819 | return $temp; |
---|
820 | } |
---|
821 | |
---|
822 | function date_to_epoch($d) |
---|
823 | { |
---|
824 | return $this->localdates(mktime(0,0,0,(int)(substr($d,4,2)),(int)(substr($d,6,2)),(int)(substr($d,0,4)))); |
---|
825 | } |
---|
826 | |
---|
827 | function list_dirty_events($lastmod=-1,$repeats=false) |
---|
828 | { |
---|
829 | if(!isset($this->stream)) |
---|
830 | { |
---|
831 | return False; |
---|
832 | } |
---|
833 | $lastmod = (int) $lastmod; |
---|
834 | $repeats = (bool) $repeats; |
---|
835 | |
---|
836 | $user_where = " AND phpgw_cal_user.cal_login = $this->user"; |
---|
837 | |
---|
838 | $member_groups = $GLOBALS['phpgw']->accounts->membership($this->user); |
---|
839 | @reset($member_groups); |
---|
840 | while($member_groups != False && list($key,$group_info) = each($member_groups)) |
---|
841 | { |
---|
842 | $member[] = $group_info['account_id']; |
---|
843 | } |
---|
844 | @reset($member); |
---|
845 | // $user_where .= ','.implode(',',$member); |
---|
846 | //$user_where .= ')) '; |
---|
847 | |
---|
848 | if($this->debug) |
---|
849 | { |
---|
850 | echo '<!-- '.$user_where.' -->'."\n"; |
---|
851 | } |
---|
852 | |
---|
853 | if($lastmod > 0) |
---|
854 | { |
---|
855 | $wheremod = "AND mdatetime = $lastmod"; |
---|
856 | } |
---|
857 | |
---|
858 | $order_by = ' ORDER BY phpgw_cal.cal_id ASC'; |
---|
859 | if($this->debug) |
---|
860 | { |
---|
861 | echo "SQL : ".$user_where.$wheremod.$extra."<br>\n"; |
---|
862 | } |
---|
863 | return $this->get_event_ids($repeats,$user_where.$wheremod.$extra.$order_by); |
---|
864 | } |
---|
865 | |
---|
866 | /* OLD-ALARM |
---|
867 | function add_alarm($eventid,$alarm,$owner) |
---|
868 | { |
---|
869 | $this->stream->query('INSERT INTO phpgw_cal_alarm(cal_id,cal_owner,cal_time,cal_text,alarm_enabled) VALUES('.$eventid.','.$owner.','.$alarm['time'].",'".$alarm['text']."',1)",__LINE__,__FILE__); |
---|
870 | $this->stream->query('SELECT LAST_INSERT_ID()'); |
---|
871 | $this->stream->next_record(); |
---|
872 | return($this->stream->f(0)); |
---|
873 | } |
---|
874 | function delete_alarm($alarmid) |
---|
875 | { |
---|
876 | $this->stream->query('DELETE FROM phpgw_cal_alarm WHERE alarm_id='.$alarmid,__LINE__,__FILE__); |
---|
877 | } |
---|
878 | */ |
---|
879 | } |
---|