[3594] | 1 | <?php |
---|
| 2 | /**************************************************************************\ |
---|
| 3 | * eGroupWare - Holiday * |
---|
| 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 boholiday |
---|
| 15 | { |
---|
| 16 | var $public_functions = Array( |
---|
| 17 | 'add' => True, |
---|
| 18 | 'delete_holiday' => True, |
---|
| 19 | 'delete_locale' => True, |
---|
| 20 | 'accept_holiday' => True, |
---|
| 21 | |
---|
| 22 | 'read_entries' => True, |
---|
| 23 | 'read_entry' => True, |
---|
| 24 | 'add_entry' => True, |
---|
| 25 | 'update_entry' => True |
---|
| 26 | ); |
---|
| 27 | |
---|
| 28 | var $debug = False; |
---|
| 29 | var $base_url = '/index.php'; |
---|
| 30 | |
---|
| 31 | var $ui; |
---|
| 32 | var $so; |
---|
| 33 | var $owner; |
---|
| 34 | var $year; |
---|
| 35 | |
---|
| 36 | var $id; |
---|
| 37 | var $total; |
---|
| 38 | var $start; |
---|
| 39 | var $query; |
---|
| 40 | var $sort; |
---|
| 41 | |
---|
| 42 | var $locales = Array(); |
---|
| 43 | var $holidays; |
---|
| 44 | var $cached_holidays; |
---|
| 45 | |
---|
| 46 | function boholiday() |
---|
| 47 | { |
---|
| 48 | $this->so = CreateObject('calendar.soholiday'); |
---|
| 49 | |
---|
| 50 | $this->start = (int)get_var('start',array('POST','GET')); |
---|
| 51 | $this->query = get_var('query',array('POST','GET')); |
---|
| 52 | $this->sort = get_var('sort',array('POST','GET')); |
---|
| 53 | $this->order = get_var('order',array('POST','GET')); |
---|
| 54 | $this->id = get_var('id',array('POST','GET')); |
---|
| 55 | $this->year = get_var('year',array('POST','GET'),date('Y')); |
---|
| 56 | $this->locale = get_var('locale',array('POST','GET')); |
---|
| 57 | if ($this->locale) |
---|
| 58 | { |
---|
| 59 | $this->locales[] = $this->locale; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | if($this->debug) |
---|
| 63 | { |
---|
| 64 | echo '<-- Locale = '.$this->locales[0].' -->'."\n"; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | $this->total = $this->so->holiday_total($this->locales[0],$this->query,$this->year); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /* Begin Calendar functions */ |
---|
| 71 | function read_entry($id=0) |
---|
| 72 | { |
---|
| 73 | if($this->debug) |
---|
| 74 | { |
---|
| 75 | echo "BO : Reading Holiday ID : ".$id."<br>\n"; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | if(!$id) |
---|
| 79 | { |
---|
| 80 | if(!$this->id) |
---|
| 81 | { |
---|
| 82 | return Array(); |
---|
| 83 | } |
---|
| 84 | else |
---|
| 85 | { |
---|
| 86 | $id = $this->id; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | return $this->so->read_holiday($id); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | function delete_holiday($id=0) |
---|
| 94 | { |
---|
| 95 | if(!$id) |
---|
| 96 | { |
---|
| 97 | if($this->id) |
---|
| 98 | { |
---|
| 99 | $id = $this->id; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | $this->ui = CreateObject('calendar.uiholiday'); |
---|
| 104 | if($id) |
---|
| 105 | { |
---|
| 106 | $this->so->delete_holiday($id); |
---|
| 107 | $this->ui->edit_locale(); |
---|
| 108 | } |
---|
| 109 | else |
---|
| 110 | { |
---|
| 111 | $this->ui->admin(); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | function delete_locale($locale='') |
---|
| 116 | { |
---|
| 117 | if(!$locale) |
---|
| 118 | { |
---|
| 119 | if($this->locales[0]) |
---|
| 120 | { |
---|
| 121 | $locale = $this->locales[0]; |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | if($locale) |
---|
| 126 | { |
---|
| 127 | $this->so->delete_locale($locale); |
---|
| 128 | } |
---|
| 129 | $this->ui = CreateObject('calendar.uiholiday'); |
---|
| 130 | $this->ui->admin(); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | function accept_holiday() |
---|
| 134 | { |
---|
| 135 | $send_back_to = str_replace('submitlocale','holiday_admin',$_SERVER['HTTP_REFERER']); |
---|
| 136 | if(!@$this->locales[0]) |
---|
| 137 | { |
---|
| 138 | Header('Location: '.$send_back_to); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | $send_back_to = str_replace('&locale='.$this->locales[0],'',$send_back_to); |
---|
| 142 | $file = './holidays.'.$this->locales[0]; |
---|
| 143 | if(!file_exists($file) && count($_POST['name'])) |
---|
| 144 | { |
---|
| 145 | $c_holidays = count($_POST['name']); |
---|
| 146 | $fp = fopen($file,'w'); |
---|
| 147 | for($i=0;$i<$c_holidays;$i++) |
---|
| 148 | { |
---|
| 149 | fwrite($fp,$this->locales[0]."\t".$_POST['name'][$i]."\t".$_POST['day'][$i]."\t".$_POST['month'][$i]."\t".$_POST['occurence'][$i]."\t".$_POST['dow'][$i]."\t".$_POST['observance'][$i]."\n"); |
---|
| 150 | } |
---|
| 151 | fclose($fp); |
---|
| 152 | } |
---|
| 153 | Header('Location: '.$send_back_to); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | function get_holiday_list($locale='', $sort='', $order='', $query='', $total='', $year=0) |
---|
| 157 | { |
---|
| 158 | $locale = ($locale?$locale:$this->locales[0]); |
---|
| 159 | $sort = ($sort?$sort:$this->sort); |
---|
| 160 | $order = ($order?$order:$this->order); |
---|
| 161 | $query = ($query?$query:$this->query); |
---|
| 162 | $year = ($$year?$$year:$this->year); |
---|
| 163 | return $this->so->read_holidays($locale,$query,$order,$year); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | function get_locale_list($sort='', $order='', $query='') |
---|
| 167 | { |
---|
| 168 | $sort = ($sort?$sort:$this->sort); |
---|
| 169 | $order = ($order?$order:$this->order); |
---|
| 170 | $query = ($query?$query:$this->query); |
---|
| 171 | return $this->so->get_locale_list($sort,$order,$query); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | function prepare_read_holidays($year=0,$owner=0) |
---|
| 175 | { |
---|
| 176 | $this->year = (isset($year) && $year > 0?$year:$GLOBALS['phpgw']->common->show_date(time() - $GLOBALS['phpgw']->datetime->tz_offset,'Y')); |
---|
| 177 | $this->owner = ($owner?$owner:$GLOBALS['phpgw_info']['user']['account_id']); |
---|
| 178 | |
---|
| 179 | if($this->debug) |
---|
| 180 | { |
---|
| 181 | echo 'Setting Year to : '.$this->year.'<br>'."\n"; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | if(@$GLOBALS['phpgw_info']['user']['preferences']['common']['country']) |
---|
| 185 | { |
---|
| 186 | $this->locales[] = $GLOBALS['phpgw_info']['user']['preferences']['common']['country']; |
---|
| 187 | } |
---|
| 188 | elseif(@$GLOBALS['phpgw_info']['user']['preferences']['calendar']['locale']) |
---|
| 189 | { |
---|
| 190 | $this->locales[] = $GLOBALS['phpgw_info']['user']['preferences']['calendar']['locale']; |
---|
| 191 | } |
---|
| 192 | else |
---|
| 193 | { |
---|
| 194 | $this->locales[] = 'US'; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | if($this->owner != $GLOBALS['phpgw_info']['user']['account_id']) |
---|
| 198 | { |
---|
| 199 | $owner_pref = CreateObject('phpgwapi.preferences',$owner); |
---|
| 200 | $owner_prefs = $owner_pref->read_repository(); |
---|
| 201 | if(@$owner_prefs['common']['country']) |
---|
| 202 | { |
---|
| 203 | $this->locales[] = $owner_prefs['common']['country']; |
---|
| 204 | } |
---|
| 205 | elseif(@$owner_prefs['calendar']['locale']) |
---|
| 206 | { |
---|
| 207 | $this->locales[] = $owner_prefs['calendar']['locale']; |
---|
| 208 | } |
---|
| 209 | unset($owner_pref); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | @reset($this->locales); |
---|
| 213 | if($GLOBALS['phpgw_info']['server']['auto_load_holidays'] == True) |
---|
| 214 | { |
---|
| 215 | while(list($key,$value) = each($this->locales)) |
---|
| 216 | { |
---|
| 217 | $this->auto_load_holidays($value); |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | function auto_load_holidays($locale) |
---|
| 223 | { |
---|
| 224 | if($this->so->holiday_total($locale) == 0) |
---|
| 225 | { |
---|
| 226 | @set_time_limit(0); |
---|
| 227 | |
---|
| 228 | /* get the file that contains the calendar events for your locale */ |
---|
| 229 | /* "http://www.egroupware.org/cal/holidays.US.csv"; */ |
---|
| 230 | $network = CreateObject('phpgwapi.network'); |
---|
| 231 | if(isset($GLOBALS['phpgw_info']['server']['holidays_url_path']) && $GLOBALS['phpgw_info']['server']['holidays_url_path'] != 'localhost') |
---|
| 232 | { |
---|
| 233 | $load_from = $GLOBALS['phpgw_info']['server']['holidays_url_path']; |
---|
| 234 | } |
---|
| 235 | else |
---|
| 236 | { |
---|
| 237 | $pos = strpos(' '.$GLOBALS['phpgw_info']['server']['webserver_url'],$_SERVER['HTTP_HOST']); |
---|
| 238 | if($pos == 0) |
---|
| 239 | { |
---|
| 240 | switch($_SERVER['SERVER_PORT']) |
---|
| 241 | { |
---|
| 242 | case 80: |
---|
| 243 | $http_protocol = 'http://'; |
---|
| 244 | break; |
---|
| 245 | case 443: |
---|
| 246 | $http_protocol = 'https://'; |
---|
| 247 | break; |
---|
| 248 | } |
---|
| 249 | $server_host = $http_protocol.$_SERVER['HTTP_HOST'].$GLOBALS['phpgw_info']['server']['webserver_url']; |
---|
| 250 | } |
---|
| 251 | else |
---|
| 252 | { |
---|
| 253 | $server_host = $GLOBALS['phpgw_info']['server']['webserver_url']; |
---|
| 254 | } |
---|
| 255 | $load_from = $server_host.'/calendar/egroupware.org'; |
---|
| 256 | } |
---|
| 257 | // echo 'Loading from: '.$load_from.'/holidays.'.strtoupper($locale).'.csv'."<br>\n"; |
---|
| 258 | $lines = $network->gethttpsocketfile($load_from.'/holidays.'.strtoupper($locale).'.csv'); |
---|
| 259 | if (!$lines) |
---|
| 260 | { |
---|
| 261 | return false; |
---|
| 262 | } |
---|
| 263 | $c_lines = count($lines); |
---|
| 264 | for($i=0;$i<$c_lines;$i++) |
---|
| 265 | { |
---|
| 266 | // echo 'Line #'.$i.' : '.$lines[$i]."<br>\n"; |
---|
| 267 | $holiday = explode("\t",$lines[$i]); |
---|
| 268 | if(count($holiday) == 7) |
---|
| 269 | { |
---|
| 270 | $holiday['locale'] = $holiday[0]; |
---|
| 271 | $holiday['name'] = $GLOBALS['phpgw']->db->db_addslashes($holiday[1]); |
---|
| 272 | $holiday['mday'] = (int)$holiday[2]; |
---|
| 273 | $holiday['month_num'] = (int)$holiday[3]; |
---|
| 274 | $holiday['occurence'] = (int)$holiday[4]; |
---|
| 275 | $holiday['dow'] = (int)$holiday[5]; |
---|
| 276 | $holiday['observance_rule'] = (int)$holiday[6]; |
---|
| 277 | $holiday['hol_id'] = 0; |
---|
| 278 | $this->so->save_holiday($holiday); |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | function save_holiday($holiday) |
---|
| 285 | { |
---|
| 286 | $this->so->save_holiday($holiday); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | function add() |
---|
| 290 | { |
---|
| 291 | if(@$_POST['submit']) |
---|
| 292 | { |
---|
| 293 | $holiday = $_POST['holiday']; |
---|
| 294 | |
---|
| 295 | if(empty($holiday['mday'])) |
---|
| 296 | { |
---|
| 297 | $holiday['mday'] = 0; |
---|
| 298 | } |
---|
| 299 | if(!isset($this->bo->locales[0]) || $this->bo->locales[0]=='') |
---|
| 300 | { |
---|
| 301 | $this->bo->locales[0] = $holiday['locale']; |
---|
| 302 | } |
---|
| 303 | elseif(!isset($holiday['locale']) || $holiday['locale']=='') |
---|
| 304 | { |
---|
| 305 | $holiday['locale'] = $this->bo->locales[0]; |
---|
| 306 | } |
---|
| 307 | if(!isset($holiday['hol_id'])) |
---|
| 308 | { |
---|
| 309 | $holiday['hol_id'] = $this->bo->id; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | // some input validation |
---|
| 313 | |
---|
| 314 | if (!$holiday['mday'] == !$holiday['occurence']) |
---|
| 315 | { |
---|
| 316 | $errors[] = lang('You need to set either a day or a occurence !!!'); |
---|
| 317 | } |
---|
| 318 | if($holiday['year'] && $holiday['occurence']) |
---|
| 319 | { |
---|
| 320 | $errors[] = lang('You can only set a year or a occurence !!!'); |
---|
| 321 | } |
---|
| 322 | else |
---|
| 323 | { |
---|
| 324 | $holiday['occurence'] = (int)($holiday['occurence'] ? $holiday['occurence'] : $holiday['year']); |
---|
| 325 | unset($holiday['year']); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | // Still need to put some validation in here..... |
---|
| 329 | |
---|
| 330 | $this->ui = CreateObject('calendar.uiholiday'); |
---|
| 331 | |
---|
| 332 | if (is_array($errors)) |
---|
| 333 | { |
---|
| 334 | $holiday['month'] = $holiday['month_num']; |
---|
| 335 | $holiday['day'] = $holiday['mday']; |
---|
| 336 | $this->ui->edit_holiday($errors,$holiday); |
---|
| 337 | } |
---|
| 338 | else |
---|
| 339 | { |
---|
| 340 | $this->so->save_holiday($holiday); |
---|
| 341 | $this->ui->edit_locale($holiday['locale']); |
---|
| 342 | } |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | function sort_holidays_by_date($holidays) |
---|
| 347 | { |
---|
| 348 | $c_holidays = count($holidays); |
---|
| 349 | for($outer_loop=0;$outer_loop<($c_holidays - 1);$outer_loop++) |
---|
| 350 | { |
---|
| 351 | for($inner_loop=$outer_loop;$inner_loop<$c_holidays;$inner_loop++) |
---|
| 352 | { |
---|
| 353 | if($holidays[$outer_loop]['date'] > $holidays[$inner_loop]['date']) |
---|
| 354 | { |
---|
| 355 | $temp = $holidays[$inner_loop]; |
---|
| 356 | $holidays[$inner_loop] = $holidays[$outer_loop]; |
---|
| 357 | $holidays[$outer_loop] = $temp; |
---|
| 358 | } |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | return $holidays; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | function set_holidays_to_date($holidays) |
---|
| 365 | { |
---|
| 366 | $new_holidays = Array(); |
---|
| 367 | for($i=0;$i<count($holidays);$i++) |
---|
| 368 | { |
---|
| 369 | // echo "Setting Holidays Date : ".date('Ymd',$holidays[$i]['date'])."<br>\n"; |
---|
| 370 | $new_holidays[date('Ymd',$holidays[$i]['date'])][] = $holidays[$i]; |
---|
| 371 | } |
---|
| 372 | return $new_holidays; |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | function read_holiday() |
---|
| 376 | { |
---|
| 377 | if(isset($this->cached_holidays)) |
---|
| 378 | { |
---|
| 379 | return $this->cached_holidays; |
---|
| 380 | } |
---|
| 381 | $holidays = $this->so->read_holidays($this->locales,'','',$this->year); |
---|
| 382 | |
---|
| 383 | if(count($holidays) == 0) |
---|
| 384 | { |
---|
| 385 | return $holidays; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | $temp_locale = $GLOBALS['phpgw_info']['user']['preferences']['common']['country']; |
---|
| 389 | for($i=0;$i<count($holidays);$i++) |
---|
| 390 | { |
---|
| 391 | $c = $i; |
---|
| 392 | if($i == 0 || $holidays[$i]['locale'] != $holidays[$i - 1]['locale']) |
---|
| 393 | { |
---|
| 394 | if(is_object($holidaycalc)) |
---|
| 395 | { |
---|
| 396 | unset($holidaycalc); |
---|
| 397 | } |
---|
| 398 | $GLOBALS['phpgw_info']['user']['preferences']['common']['country'] = $holidays[$i]['locale']; |
---|
| 399 | $holidaycalc = CreateObject('calendar.holidaycalc'); |
---|
| 400 | } |
---|
| 401 | $holidays[$i]['date'] = $holidaycalc->calculate_date($holidays[$i], $holidays, $this->year, $c); |
---|
| 402 | if($c != $i) |
---|
| 403 | { |
---|
| 404 | $i = $c; |
---|
| 405 | } |
---|
| 406 | } |
---|
| 407 | unset($holidaycalc); |
---|
| 408 | $this->holidays = $this->sort_holidays_by_date($holidays); |
---|
| 409 | $this->cached_holidays = $this->set_holidays_to_date($this->holidays); |
---|
| 410 | $GLOBALS['phpgw_info']['user']['preferences']['common']['country'] = $temp_locale; |
---|
| 411 | return $this->cached_holidays; |
---|
| 412 | } |
---|
| 413 | /* End Calendar functions */ |
---|
| 414 | |
---|
| 415 | function check_admin() |
---|
| 416 | { |
---|
| 417 | if(!@$GLOBALS['phpgw_info']['user']['apps']['admin']) |
---|
| 418 | { |
---|
| 419 | Header('Location: ' . $GLOBALS['phpgw']->link('/index.php')); |
---|
| 420 | } |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | function rule_string($holiday) |
---|
| 424 | { |
---|
| 425 | if (!is_array($holiday)) |
---|
| 426 | { |
---|
| 427 | return false; |
---|
| 428 | } |
---|
| 429 | $sbox = CreateObject('phpgwapi.sbox'); |
---|
| 430 | $month = $holiday['month'] ? lang($sbox->monthnames[$holiday['month']]) : ''; |
---|
| 431 | unset($sbox); |
---|
| 432 | |
---|
| 433 | if (!$holiday['day']) |
---|
| 434 | { |
---|
| 435 | $occ = $holiday['occurence'] == 99 ? lang('last') : $holiday['occurence'].'.'; |
---|
| 436 | |
---|
| 437 | $dow_str = Array(lang('Sun'),lang('Mon'),lang('Tue'),lang('Wed'),lang('Thu'),lang('Fri'),lang('Sat')); |
---|
| 438 | $dow = $dow_str[$holiday['dow']]; |
---|
| 439 | |
---|
| 440 | $str = lang('%1 %2 in %3',$occ,$dow,$month); |
---|
| 441 | } |
---|
| 442 | else |
---|
| 443 | { |
---|
| 444 | $str = $GLOBALS['phpgw']->common->dateformatorder($holiday['occurence']>1900?$holiday['occurence']:'',$month,$holiday[day]); |
---|
| 445 | } |
---|
| 446 | if ($holiday['observance_rule']) |
---|
| 447 | { |
---|
| 448 | $str .= ' ('.lang('Observance Rule').')'; |
---|
| 449 | } |
---|
| 450 | return $str; |
---|
| 451 | } |
---|
| 452 | } |
---|
| 453 | ?> |
---|