1<?php
2  /**************************************************************************\
3  * phpGroupWare - Calendar                                                  *
4  * http://www.phpgroupware.org                                              *
5  * Based on Webcalendar by Craig Knudsen <cknudsen@radix.net>               *
6  *          http://www.radix.net/~cknudsen                                  *
7  * Modified by Mark Peters <skeeter@phpgroupware.org>                       *
8  * --------------------------------------------                             *
9  *  This program is free software; you can redistribute it and/or modify it *
10  *  under the terms of the GNU General Public License as published by the   *
11  *  Free Software Foundation; either version 2 of the License, or (at your  *
12  *  option) any later version.                                              *
13  \**************************************************************************/
14
15  /* $Id: class.socalendar.inc.php 21060 2010-03-25 22:45:41Z Caeies $ */
16
17	class socalendar
18	{
19//		var $debug = True;
20		var $debug = False;
21		var $cal;
22		var $db;
23		var $owner;
24		var $g_owner;
25		var $is_group = False;
26		var $datetime;
27		var $filter;
28		var $cat_id;
29
30		function socalendar($param)
31		{
32			$this->db = $GLOBALS['phpgw']->db;
33			if(!is_object($GLOBALS['phpgw']->datetime))
34			{
35				$GLOBALS['phpgw']->datetime = createobject('phpgwapi.datetime');
36			}
37
38			$this->owner = (!isset($param['owner']) || $param['owner'] == 0?$GLOBALS['phpgw_info']['user']['account_id']:$param['owner']);
39			$this->filter = (isset($param['filter']) && $param['filter'] != ''?$param['filter']:$this->filter);
40			$this->cat_id = (isset($param['category']) && $param['category'] != ''?$param['category']:$this->cat_id);
41			if(isset($param['g_owner']) && is_array($param['g_owner']))
42			{
43				$this->is_group = True;
44				$this->g_owner = $param['g_owner'];
45			}
46			if($this->debug)
47			{
48				echo '<!-- SO Filter : '.$this->filter.' -->'."\n";
49				echo '<!-- SO cat_id : '.$this->cat_id.' -->'."\n";
50			}
51			$this->cal = CreateObject('calendar.socalendar_');
52			$this->open_box($this->owner);
53		}
54
55		function open_box($owner)
56		{
57			$this->cal->open('INBOX',intval($owner));
58		}
59
60		function maketime($time)
61		{
62			return mktime($time['hour'],$time['min'],$time['sec'],$time['month'],$time['mday'],$time['year']);
63		}
64
65		function read_entry($id)
66		{
67			return $this->cal->fetch_event($id);
68		}
69
70		function list_events($startYear,$startMonth,$startDay,$endYear=0,$endMonth=0,$endDay=0,$owner_id=0)
71		{
72			$extra = '';
73			$extra .= (strpos($this->filter,'private')?'AND phpgw_cal.is_public=0 ':'');
74			$extra .= ($this->cat_id?"AND phpgw_cal.category like '%".$this->cat_id."%' ":'');
75			if($owner_id)
76			{
77				return $this->cal->list_events($startYear,$startMonth,$startDay,$endYear,$endMonth,$endDay,$extra,$GLOBALS['phpgw']->datetime->tz_offset,$owner_id);
78			}
79			else
80			{
81				return $this->cal->list_events($startYear,$startMonth,$startDay,$endYear,$endMonth,$endDay,$extra,$GLOBALS['phpgw']->datetime->tz_offset);
82			}
83		}
84
85		function list_repeated_events($syear,$smonth,$sday,$eyear,$emonth,$eday,$owner_id=0)
86		{
87			if($GLOBALS['phpgw_info']['server']['calendar_type'] != 'sql')
88			{
89				return Array();
90			}
91
92			$starttime = mktime(0,0,0,$smonth,$sday,$syear) - $GLOBALS['phpgw']->datetime->tz_offset;
93			$endtime = mktime(23,59,59,$emonth,$eday,$eyear) - $GLOBALS['phpgw']->datetime->tz_offset;
94//			$starttime = mktime(0,0,0,$smonth,$sday,$syear);
95//			$endtime = mktime(23,59,59,$emonth,$eday,$eyear);
96			$sql = "AND (phpgw_cal.cal_type='M') "
97				. 'AND (phpgw_cal_user.cal_login IN (';
98			if($owner_id)
99			{
100				if(is_array($owner_id))
101				{
102					$ids = $owner_id;
103				}
104				else
105				{
106					$ids[] = $owner_id;
107				}
108			}
109			else
110			{
111				$ids =  (!$this->is_group ? array($this->owner) : $this->g_owner);
112			}
113
114			$sql .= (is_array($ids) && count($ids) ? implode(',', $ids) : 0);
115
116//			$member_groups = $GLOBALS['phpgw']->accounts->membership($this->user);
117//			@reset($member_groups);
118//			while(list($key,$group_info) = each($member_groups))
119//			{
120//				$member[] = $group_info['account_id'];
121//			}
122//			@reset($member);
123//			$sql .= ','.implode(',',$member).') ';
124//			$sql .= 'AND (phpgw_cal.datetime <= '.$starttime.') ';
125//			$sql .= 'AND (((phpgw_cal_repeats.recur_enddate >= '.$starttime.') AND (phpgw_cal_repeats.recur_enddate <= '.$endtime.')) OR (phpgw_cal_repeats.recur_enddate=0))) '
126			$sql .= ') AND ((phpgw_cal_repeats.recur_enddate >= '.$starttime.') OR (phpgw_cal_repeats.recur_enddate=0))) '
127				. (strpos($this->filter,'private')?'AND phpgw_cal.is_public=0 ':'')
128				. ($this->cat_id?"AND phpgw_cal.category like '%".$this->cat_id."%' ":'')
129				. 'ORDER BY phpgw_cal.datetime ASC, phpgw_cal.edatetime ASC, phpgw_cal.priority ASC';
130
131			if($this->debug)
132			{
133				echo '<!-- SO list_repeated_events : SQL : '.$sql.' -->'."\n";
134			}
135
136			return $this->get_event_ids(True,$sql);
137		}
138
139		function list_events_keyword($keywords,$members='')
140		{
141			if (!$members)
142			{
143				$members[] = $this->owner;
144			}
145			$sql = 'AND (phpgw_cal_user.cal_login IN ('.implode(',',$members).')) AND '.
146				'(phpgw_cal_user.cal_login='.intval($this->owner).' OR phpgw_cal.is_public=1) AND (';
147
148			$words = explode(' ', $keywords);
149			foreach($words as $i => $word)
150			{
151				$word = $this->db->db_addslashes(trim($word));
152				$sql .= $i > 0 ? ' OR ' : '';
153				$sql .= "(UPPER(phpgw_cal.title) LIKE UPPER('%$word%') OR "
154						. "UPPER(phpgw_cal.description) LIKE UPPER('%$word%') OR "
155						. "UPPER(phpgw_cal.location) LIKE UPPER('%$word%') OR "
156						. "UPPER(phpgw_cal_extra.cal_extra_value) LIKE UPPER('%$word%'))";
157			}
158			$sql .= ') ';
159
160			$sql .= (strpos($this->filter,'private')?'AND phpgw_cal.is_public=0 ':'');
161			$sql .= ($this->cat_id? "AND (phpgw_cal.category='$this->cat_id' OR phpgw_cal.category like '%,".$this->cat_id.",%') ":'');
162			$sql .= 'ORDER BY phpgw_cal.datetime DESC, phpgw_cal.edatetime DESC, phpgw_cal.priority ASC';
163
164			return $this->get_event_ids(false, $sql, true);
165		}
166
167		function read_from_store($startYear,$startMonth,$startDay,$endYear='',$endMonth='',$endDay='')
168		{
169			$events = $this->list_events($startYear,$startMonth,$startDay,$endYear,$endMonth,$endDay);
170			$events_cached = Array();
171			for($i=0;$i<count($events);$i++)
172			{
173				$events_cached[] = $this->read_entry($events[$i]);
174			}
175			return $events_cached;
176		}
177
178		function get_event_ids($search_repeats=False, $sql='',$search_extra=False)
179		{
180			return $this->cal->get_event_ids($search_repeats,$sql,$search_extra);
181		}
182
183		function find_uid($uid)
184		{
185			$sql = " AND (phpgw_cal.uid = '".$uid."') ";
186
187			$found = $this->cal->get_event_ids(False,$sql);
188			if(!$found)
189			{
190				$found = $this->cal->get_event_ids(True,$sql);
191			}
192			if(is_array($found))
193			{
194				return $found[0];
195			}
196			else
197			{
198				return False;
199			}
200		}
201
202		function add_entry(&$event)
203		{
204			$this->cal->store_event($event);
205		}
206
207		function save_alarm($cal_id,$alarm,$id=0)
208		{
209			$this->cal->save_alarm($cal_id,$alarm,$id);
210		}
211
212		function delete_alarm($id)
213		{
214			$this->cal->delete_alarm($id);
215		}
216
217		function delete_entry($id)
218		{
219			$this->cal->delete_event($id);
220		}
221
222		function expunge()
223		{
224			$this->cal->expunge();
225		}
226
227		function delete_calendar($owner)
228		{
229			$this->cal->delete_calendar($owner);
230		}
231
232		function change_owner($account_id, $new_owner)
233		{
234			$account_id = (int)$account_id;
235			$new_owner = (int)$new_owner;
236
237			if($GLOBALS['phpgw_info']['server']['calendar_type'] == 'sql')
238			{
239				$db2 = $this->cal->stream;
240				$this->cal->stream->query('SELECT cal_id FROM phpgw_cal_user WHERE cal_login='.$account_id,__LINE__,__FILE__);
241				while($this->cal->stream->next_record())
242				{
243					$id = $this->cal->stream->f('cal_id');
244					$db2->query('SELECT count(*) FROM phpgw_cal_user WHERE cal_id='.$id.' AND cal_login='.$new_owner,__LINE__,__FILE__);
245					$db2->next_record();
246					if($db2->f(0) == 0)
247					{
248						$db2->query('UPDATE phpgw_cal_user SET cal_login='.$new_owner.' WHERE cal_id='.$id.' AND cal_login='.$account_id,__LINE__,__FILE__);
249					}
250					else
251					{
252						$db2->query('DELETE FROM phpgw_cal_user WHERE cal_id='.$id.' AND cal_login='.$account_id,__LINE__,__FILE__);
253					}
254				}
255				$this->cal->stream->query('UPDATE phpgw_cal SET owner='.$new_owner.' WHERE owner='.$account_id,__LINE__,__FILE__);
256			}
257		}
258
259		function set_status($id,$status)
260		{
261			$this->cal->set_status($id,$this->owner,$status);
262		}
263
264		function get_alarm($cal_id)
265		{
266			if (!method_exists($this->cal,'get_alarm'))
267			{
268				return False;
269			}
270			return $this->cal->get_alarm($cal_id);
271		}
272
273		function read_alarm($id)
274		{
275			if (!method_exists($this->cal,'read_alarm'))
276			{
277				return False;
278			}
279			return $this->cal->read_alarm($id);
280		}
281
282		function read_alarms($cal_id)
283		{
284			if (!method_exists($this->cal,'read_alarms'))
285			{
286				return False;
287			}
288			return $this->cal->read_alarms($cal_id);
289		}
290
291		function find_recur_exceptions($event_id)
292		{
293			$event_id = (int)$event_id;
294			if($GLOBALS['phpgw_info']['server']['calendar_type'] == 'sql')
295			{
296				$arr = Array();
297				$this->cal->query('SELECT datetime FROM phpgw_cal WHERE reference='.$event_id,__LINE__,__FILE__);
298				if($this->cal->num_rows())
299				{
300					while($this->cal->next_record())
301					{
302						$arr[] = intval($this->cal->f('datetime'));
303					}
304				}
305				if(count($arr) == 0)
306				{
307					return False;
308				}
309				else
310				{
311					return $arr;
312				}
313			}
314			else
315			{
316				return False;
317			}
318		}
319
320		/* Begin mcal equiv functions */
321		function get_cached_event()
322		{
323			return $this->cal->event;
324		}
325
326		function add_attribute($var,$value,$element='**(**')
327		{
328			$this->cal->add_attribute($var,$value,$element);
329		}
330
331		function event_init()
332		{
333			$this->cal->event_init();
334		}
335
336		function set_date($element,$year,$month,$day=0,$hour=0,$min=0,$sec=0)
337		{
338			$this->cal->set_date($element,$year,$month,$day,$hour,$min,$sec);
339		}
340
341		function set_start($year,$month,$day=0,$hour=0,$min=0,$sec=0)
342		{
343			$this->cal->set_start($year,$month,$day,$hour,$min,$sec);
344		}
345
346		function set_end($year,$month,$day=0,$hour=0,$min=0,$sec=0)
347		{
348			$this->cal->set_end($year,$month,$day,$hour,$min,$sec);
349		}
350
351		function set_title($title='')
352		{
353			$this->cal->set_title($title);
354		}
355
356		function set_description($description='')
357		{
358			$this->cal->set_description($description);
359		}
360
361		function set_class($class)
362		{
363			$this->cal->set_class($class);
364		}
365
366		function set_category($category='')
367		{
368			$this->cal->set_category($category);
369		}
370
371		function set_alarm($alarm)
372		{
373			$this->cal->set_alarm($alarm);
374		}
375
376		function set_recur_none()
377		{
378			$this->cal->set_recur_none();
379		}
380
381		function set_recur_daily($year,$month,$day,$interval)
382		{
383			$this->cal->set_recur_daily($year,$month,$day,$interval);
384		}
385
386		function set_recur_weekly($year,$month,$day,$interval,$weekdays)
387		{
388			$this->cal->set_recur_weekly($year,$month,$day,$interval,$weekdays);
389		}
390
391		function set_recur_monthly_mday($year,$month,$day,$interval)
392		{
393			$this->cal->set_recur_monthly_mday($year,$month,$day,$interval);
394		}
395
396		function set_recur_monthly_wday($year,$month,$day,$interval)
397		{
398			$this->cal->set_recur_monthly_wday($year,$month,$day,$interval);
399		}
400
401		function set_recur_yearly($year,$month,$day,$interval)
402		{
403			$this->cal->set_recur_yearly($year,$month,$day,$interval);
404		}
405
406		/* End mcal equiv functions */
407	}
408?>
409