1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare - Calendar Holidays * |
---|
4 | * http://www.egroupware.org * |
---|
5 | * Written by Mark Peters <skeeter@phpgroupware.org> * |
---|
6 | * -------------------------------------------- * |
---|
7 | * This program is free software; you can redistribute it and/or modify it * |
---|
8 | * under the terms of the GNU General Public License as published by the * |
---|
9 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
10 | * option) any later version. * |
---|
11 | \**************************************************************************/ |
---|
12 | |
---|
13 | |
---|
14 | class calendar_holiday |
---|
15 | { |
---|
16 | var $db; |
---|
17 | var $year; |
---|
18 | var $tz_offset; |
---|
19 | var $holidays = Array(); |
---|
20 | var $index = Array(); |
---|
21 | var $users = Array(); |
---|
22 | var $datetime; |
---|
23 | |
---|
24 | function calendar_holiday($owner='') |
---|
25 | { |
---|
26 | $this->datetime = CreateObject('phpgwapi.date_time'); |
---|
27 | $this->db = $GLOBALS['phpgw']->db; |
---|
28 | if(@$GLOBALS['phpgw_info']['user']['preferences']['common']['country']) |
---|
29 | { |
---|
30 | $this->users['user'] = $GLOBALS['phpgw_info']['user']['preferences']['common']['country']; |
---|
31 | } |
---|
32 | else |
---|
33 | { |
---|
34 | $this->users['user'] = 'US'; |
---|
35 | } |
---|
36 | $owner_id = get_account_id($owner); |
---|
37 | if($owner_id != $GLOBALS['phpgw_info']['user']['account_id']) |
---|
38 | { |
---|
39 | $owner_pref = CreateObject('phpgwapi.preferences',$owner_id); |
---|
40 | $owner_prefs = $owner_pref->read_repository(); |
---|
41 | if(isset($owner_prefs['calendar']['locale']) && $owner_prefs['common']['country']) |
---|
42 | { |
---|
43 | $this->users['owner'] = $owner_prefs['common']['country']; |
---|
44 | } |
---|
45 | else |
---|
46 | { |
---|
47 | $this->users['owner'] = 'US'; |
---|
48 | } |
---|
49 | } |
---|
50 | if($GLOBALS['phpgw_info']['server']['auto_load_holidays'] == True) |
---|
51 | { |
---|
52 | while(list($key,$value) = each($this->users)) |
---|
53 | { |
---|
54 | $this->is_network_load_needed($value); |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | function is_network_load_needed($locale) |
---|
60 | { |
---|
61 | $sql = "SELECT count(*) FROM phpgw_cal_holidays WHERE locale='".$locale."'"; |
---|
62 | $this->db->query($sql,__LINE__,__FILE__); |
---|
63 | $this->db->next_record(); |
---|
64 | $rows = $this->db->f(0); |
---|
65 | if($rows==0) |
---|
66 | { |
---|
67 | $this->load_from_network($locale); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | function save_holiday($holiday) |
---|
72 | { |
---|
73 | if(isset($holiday['hol_id']) && $holiday['hol_id']) |
---|
74 | { |
---|
75 | // echo "Updating LOCALE='".$holiday['locale']."' NAME='".$holiday['name']."' extra=(".$holiday['mday'].'/'.$holiday['month_num'].'/'.$holiday['occurence'].'/'.$holiday['dow'].'/'.$holiday['observance_rule'].")<br>\n"; |
---|
76 | $sql = "UPDATE phpgw_cal_holidays SET name='".$holiday['name']."', mday=".$holiday['mday'].', month_num='.$holiday['month_num'].', occurence='.$holiday['occurence'].', dow='.$holiday['dow'].', observance_rule='.(int)$holiday['observance_rule'].' WHERE hol_id='.$holiday['hol_id']; |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | // echo "Inserting LOCALE='".$holiday['locale']."' NAME='".$holiday['name']."' extra=(".$holiday['mday'].'/'.$holiday['month_num'].'/'.$holiday['occurence'].'/'.$holiday['dow'].'/'.$holiday['observance_rule'].")<br>\n"; |
---|
81 | $sql = 'INSERT INTO phpgw_cal_holidays(locale,name,mday,month_num,occurence,dow,observance_rule) ' |
---|
82 | . "VALUES('".strtoupper($holiday['locale'])."','".$holiday['name']."',".$holiday['mday'].','.$holiday['month_num'].','.$holiday['occurence'].','.$holiday['dow'].','.(int)$holiday['observance_rule'].")"; |
---|
83 | } |
---|
84 | $this->db->query($sql,__LINE__,__FILE__); |
---|
85 | } |
---|
86 | |
---|
87 | function delete_holiday($id) |
---|
88 | { |
---|
89 | $sql = 'DELETE FROM phpgw_cal_holidays WHERE hol_id='.$id; |
---|
90 | $this->db->query($sql,__LINE__,__FILE__); |
---|
91 | } |
---|
92 | |
---|
93 | function delete_locale($locale) |
---|
94 | { |
---|
95 | $sql = "DELETE FROM phpgw_cal_holidays WHERE locale='".$locale."'"; |
---|
96 | $this->db->query($sql,__LINE__,__FILE__); |
---|
97 | } |
---|
98 | |
---|
99 | function load_from_network($locale) |
---|
100 | { |
---|
101 | @set_time_limit(0); |
---|
102 | |
---|
103 | // get the file that contains the calendar events for your locale |
---|
104 | // "http://www.phpgroupware.org/headlines.rdf"; |
---|
105 | $network = CreateObject('phpgwapi.network'); |
---|
106 | if(isset($GLOBALS['phpgw_info']['server']['holidays_url_path']) && $GLOBALS['phpgw_info']['server']['holidays_url_path'] != 'localhost') |
---|
107 | { |
---|
108 | $load_from = $GLOBALS['phpgw_info']['server']['holidays_url_path']; |
---|
109 | } |
---|
110 | else |
---|
111 | { |
---|
112 | $pos = strpos(' '.$GLOBALS['phpgw_info']['server']['webserver_url'],$_SERVER['HTTP_HOST']); |
---|
113 | if($pos == 0) |
---|
114 | { |
---|
115 | switch($_SERVER['SERVER_PORT']) |
---|
116 | { |
---|
117 | case 80: |
---|
118 | $http_protocol = 'http://'; |
---|
119 | break; |
---|
120 | case 443: |
---|
121 | $http_protocol = 'https://'; |
---|
122 | break; |
---|
123 | } |
---|
124 | $server_host = $http_protocol.$_SERVER['HTTP_HOST'].$GLOBALS['phpgw_info']['server']['webserver_url']; |
---|
125 | } |
---|
126 | else |
---|
127 | { |
---|
128 | $server_host = $GLOBALS['phpgw_info']['server']['webserver_url']; |
---|
129 | } |
---|
130 | $load_from = $server_host.'/calendar/setup'; |
---|
131 | } |
---|
132 | // echo 'Loading from: '.$load_from.'/holidays.'.strtoupper($locale)."<br>\n"; |
---|
133 | $lines = $network->gethttpsocketfile($load_from.'/holidays.'.strtoupper($locale)); |
---|
134 | if(!$lines) |
---|
135 | { |
---|
136 | return False; |
---|
137 | } |
---|
138 | $c_lines = count($lines); |
---|
139 | for($i=0;$i<$c_lines;++$i) |
---|
140 | { |
---|
141 | // echo 'Line #'.$i.' : '.$lines[$i]."<br>\n"; |
---|
142 | $holiday = explode("\t",$lines[$i]); |
---|
143 | if(count($holiday) == 7) |
---|
144 | { |
---|
145 | $holiday['locale'] = $holiday[0]; |
---|
146 | $holiday['name'] = addslashes($holiday[1]); |
---|
147 | $holiday['mday'] = (int)$holiday[2]; |
---|
148 | $holiday['month_num'] = (int)$holiday[3]; |
---|
149 | $holiday['occurence'] = (int)$holiday[4]; |
---|
150 | $holiday['dow'] = (int)$holiday[5]; |
---|
151 | $holiday['observance_rule'] = (int)$holiday[6]; |
---|
152 | $holiday['hol_id'] = 0; |
---|
153 | $this->save_holiday($holiday); |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | function read_holiday() |
---|
159 | { |
---|
160 | $this->year = (int)$GLOBALS['phpgw']->calendar->tempyear; |
---|
161 | |
---|
162 | $sql = $this->build_holiday_query(); |
---|
163 | if($sql == False) |
---|
164 | { |
---|
165 | return False; |
---|
166 | } |
---|
167 | $this->holidays = Null; |
---|
168 | $this->db->query($sql,__LINE__,__FILE__); |
---|
169 | |
---|
170 | $i = 0; |
---|
171 | $temp_locale = $GLOBALS['phpgw_info']['user']['preferences']['common']['country']; |
---|
172 | while($this->db->next_record()) |
---|
173 | { |
---|
174 | $this->index[$this->db->f('hol_id')] = $i; |
---|
175 | $this->holidays[$i]['locale'] = $this->db->f('locale'); |
---|
176 | $this->holidays[$i]['name'] = $GLOBALS['phpgw']->strip_html($this->db->f('name')); |
---|
177 | $this->holidays[$i]['day'] = (int)$this->db->f('mday'); |
---|
178 | $this->holidays[$i]['month'] = (int)$this->db->f('month_num'); |
---|
179 | $this->holidays[$i]['occurence'] = (int)$this->db->f('occurence'); |
---|
180 | $this->holidays[$i]['dow'] = (int)$this->db->f('dow'); |
---|
181 | $this->holidays[$i]['observance_rule'] = $this->db->f('observance_rule'); |
---|
182 | if(count($this->users) == 2 && $this->users[0] != $this->users[1]) |
---|
183 | { |
---|
184 | if($this->holidays[$i]['locale'] == $this->users[1]) |
---|
185 | { |
---|
186 | $this->holidays[$i]['owner'] = 'user'; |
---|
187 | } |
---|
188 | else |
---|
189 | { |
---|
190 | $this->holidays[$i]['owner'] = 'owner'; |
---|
191 | } |
---|
192 | } |
---|
193 | else |
---|
194 | { |
---|
195 | $this->holidays[$i]['owner'] = 'user'; |
---|
196 | } |
---|
197 | $c = $i; |
---|
198 | $GLOBALS['phpgw_info']['user']['preferences']['common']['country'] = $this->holidays[$i]['locale']; |
---|
199 | $holidaycalc = CreateObject('calendar.holidaycalc'); |
---|
200 | $this->holidays[$i]['date'] = $holidaycalc->calculate_date($this->holidays[$i], $this->holidays, $this->year, $this->datetime, $c); |
---|
201 | unset($holidaycalc); |
---|
202 | if($c != $i) |
---|
203 | { |
---|
204 | $i = $c; |
---|
205 | } |
---|
206 | ++$i; |
---|
207 | } |
---|
208 | $this->holidays = $this->sort_by_date($this->holidays); |
---|
209 | $GLOBALS['phpgw_info']['user']['preferences']['common']['country'] = $temp_locale; |
---|
210 | return $this->holidays; |
---|
211 | } |
---|
212 | |
---|
213 | function build_list_for_submission($locale) |
---|
214 | { |
---|
215 | $i = -1; |
---|
216 | $this->db->query("SELECT * FROM phpgw_cal_holidays WHERE locale='".$locale."'"); |
---|
217 | while($this->db->next_record()) |
---|
218 | { |
---|
219 | ++$i; |
---|
220 | $holidays[$i]['locale'] = $this->db->f('locale'); |
---|
221 | $holidays[$i]['name'] = $GLOBALS['phpgw']->strip_html($this->db->f('name')); |
---|
222 | $holidays[$i]['day'] = (int)$this->db->f('mday'); |
---|
223 | $holidays[$i]['month'] = (int)$this->db->f('month_num'); |
---|
224 | $holidays[$i]['occurence'] = (int)$this->db->f('occurence'); |
---|
225 | $holidays[$i]['dow'] = (int)$this->db->f('dow'); |
---|
226 | $holidays[$i]['observance_rule'] = $this->db->f('observance_rule'); |
---|
227 | } |
---|
228 | return $holidays; |
---|
229 | } |
---|
230 | |
---|
231 | function build_holiday_query() |
---|
232 | { |
---|
233 | if(!isset($this->users) || count($this->users) == 0) |
---|
234 | { |
---|
235 | return False; |
---|
236 | } |
---|
237 | $sql = 'SELECT * FROM phpgw_cal_holidays WHERE locale in ('; |
---|
238 | $find_it = ''; |
---|
239 | reset($this->users); |
---|
240 | while(list($key,$value) = each($this->users)) |
---|
241 | { |
---|
242 | if($find_it) |
---|
243 | { |
---|
244 | $find_it .= ','; |
---|
245 | } |
---|
246 | $find_it .= "'".$value."'"; |
---|
247 | } |
---|
248 | $sql .= $find_it.')'; |
---|
249 | |
---|
250 | return $sql; |
---|
251 | } |
---|
252 | |
---|
253 | function sort_by_date($holidays) |
---|
254 | { |
---|
255 | $c_holidays = count($holidays); |
---|
256 | for($outer_loop=0;$outer_loop<($c_holidays - 1);++$outer_loop) |
---|
257 | { |
---|
258 | $outer_date = $holidays[$outer_loop]['date']; |
---|
259 | for($inner_loop=$outer_loop;$inner_loop<$c_holidays;++$inner_loop) |
---|
260 | { |
---|
261 | $inner_date = $holidays[$inner_loop]['date']; |
---|
262 | if($outer_date > $inner_date) |
---|
263 | { |
---|
264 | $temp = $holidays[$inner_loop]; |
---|
265 | $holidays[$inner_loop] = $holidays[$outer_loop]; |
---|
266 | $holidays[$outer_loop] = $temp; |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|
270 | return $holidays; |
---|
271 | } |
---|
272 | |
---|
273 | function find_date($date) |
---|
274 | { |
---|
275 | if($this->holidays == Null) |
---|
276 | { |
---|
277 | return False; |
---|
278 | } |
---|
279 | |
---|
280 | $c_holidays = count($this->holidays); |
---|
281 | for($i=0;$i<$c_holidays;++$i) |
---|
282 | { |
---|
283 | if($this->holidays[$i]['date'] > $date) |
---|
284 | { |
---|
285 | $i = $c_holidays + 1; |
---|
286 | } |
---|
287 | elseif($this->holidays[$i]['date'] == $date) |
---|
288 | { |
---|
289 | $return_value[] = $i; |
---|
290 | } |
---|
291 | } |
---|
292 | // echo 'Searching for '.$GLOBALS['phpgw']->common->show_date($date).' Found = '.count($return_value)."<br>\n"; |
---|
293 | if(isset($return_value)) |
---|
294 | { |
---|
295 | return $return_value; |
---|
296 | } |
---|
297 | else |
---|
298 | { |
---|
299 | return False; |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | function get_holiday($index) |
---|
304 | { |
---|
305 | return $this->holidays[$index]; |
---|
306 | } |
---|
307 | |
---|
308 | function get_name($id) |
---|
309 | { |
---|
310 | return $this->holidays[$id]['name']; |
---|
311 | } |
---|
312 | } |
---|
313 | ?> |
---|