diff --git a/calendar/inc/class.boholiday.inc.php b/calendar/inc/class.boholiday.inc.php new file mode 100755 index 0000000000..50f2c6d990 --- /dev/null +++ b/calendar/inc/class.boholiday.inc.php @@ -0,0 +1,450 @@ + * + * -------------------------------------------- * + * This program is free software; you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the * + * Free Software Foundation; either version 2 of the License, or (at your * + * option) any later version. * + \**************************************************************************/ + + /* $Id$ */ + + class boholiday + { + var $public_functions = Array( + 'add' => True, + 'delete_holiday' => True, + 'delete_locale' => True, + 'accept_holiday' => True, + + 'read_entries' => True, + 'read_entry' => True, + 'add_entry' => True, + 'update_entry' => True + ); + + var $debug = False; + var $base_url = '/index.php'; + + var $ui; + var $so; + var $owner; + var $year; + + var $id; + var $total; + var $start; + var $query; + var $sort; + + var $locales = Array(); + var $holidays; + var $cached_holidays; + + function boholiday() + { + $this->so = CreateObject('calendar.soholiday'); + + $this->start = (int)get_var('start',array('POST','GET')); + $this->query = get_var('query',array('POST','GET')); + $this->sort = get_var('sort',array('POST','GET')); + $this->order = get_var('order',array('POST','GET')); + $this->id = get_var('id',array('POST','GET')); + $this->year = get_var('year',array('POST','GET'),date('Y')); + $this->locale = get_var('locale',array('POST','GET')); + if ($this->locale) + { + $this->locales[] = $this->locale; + } + + if($this->debug) + { + echo '<-- Locale = '.$this->locales[0].' -->'."\n"; + } + + $this->total = $this->so->holiday_total($this->locales[0],$this->query,$this->year); + } + + /* Begin Calendar functions */ + function read_entry($id=0) + { + if($this->debug) + { + echo "BO : Reading Holiday ID : ".$id."
\n"; + } + + if(!$id) + { + if(!$this->id) + { + return Array(); + } + else + { + $id = $this->id; + } + } + + return $this->so->read_holiday($id); + } + + function delete_holiday($id=0) + { + if(!$id) + { + if($this->id) + { + $id = $this->id; + } + } + + $this->ui = CreateObject('calendar.uiholiday'); + if($id) + { + $this->so->delete_holiday($id); + $this->ui->edit_locale(); + } + else + { + $this->ui->admin(); + } + } + + function delete_locale($locale='') + { + if(!$locale) + { + if($this->locales[0]) + { + $locale = $this->locales[0]; + } + } + + if($locale) + { + $this->so->delete_locale($locale); + } + $this->ui = CreateObject('calendar.uiholiday'); + $this->ui->admin(); + } + + function accept_holiday() + { + $send_back_to = str_replace('submitlocale','holiday_admin',$_SERVER['HTTP_REFERER']); + if(!@$this->locales[0]) + { + Header('Location: '.$send_back_to); + } + + $send_back_to = str_replace('&locale='.$this->locales[0],'',$send_back_to); + $file = './holidays.'.$this->locales[0]; + if(!file_exists($file) && count($_POST['name'])) + { + $c_holidays = count($_POST['name']); + $fp = fopen($file,'w'); + for($i=0;$i<$c_holidays;$i++) + { + 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"); + } + fclose($fp); + } + Header('Location: '.$send_back_to); + } + + function get_holiday_list($locale='', $sort='', $order='', $query='', $total='', $year=0) + { + $locale = ($locale?$locale:$this->locales[0]); + $sort = ($sort?$sort:$this->sort); + $order = ($order?$order:$this->order); + $query = ($query?$query:$this->query); + $year = ($$year?$$year:$this->year); + return $this->so->read_holidays($locale,$query,$order,$year); + } + + function get_locale_list($sort='', $order='', $query='') + { + $sort = ($sort?$sort:$this->sort); + $order = ($order?$order:$this->order); + $query = ($query?$query:$this->query); + return $this->so->get_locale_list($sort,$order,$query); + } + + function prepare_read_holidays($year=0,$owner=0) + { + $this->year = (isset($year) && $year > 0?$year:$GLOBALS['phpgw']->common->show_date(time() - $GLOBALS['phpgw']->datetime->tz_offset,'Y')); + $this->owner = ($owner?$owner:$GLOBALS['phpgw_info']['user']['account_id']); + + if($this->debug) + { + echo 'Setting Year to : '.$this->year.'
'."\n"; + } + + if(@$GLOBALS['phpgw_info']['user']['preferences']['common']['country']) + { + $this->locales[] = $GLOBALS['phpgw_info']['user']['preferences']['common']['country']; + } + elseif(@$GLOBALS['phpgw_info']['user']['preferences']['calendar']['locale']) + { + $this->locales[] = $GLOBALS['phpgw_info']['user']['preferences']['calendar']['locale']; + } + else + { + $this->locales[] = 'US'; + } + + if($this->owner != $GLOBALS['phpgw_info']['user']['account_id']) + { + $owner_pref = CreateObject('phpgwapi.preferences',$owner); + $owner_prefs = $owner_pref->read_repository(); + if(@$owner_prefs['common']['country']) + { + $this->locales[] = $owner_prefs['common']['country']; + } + elseif(@$owner_prefs['calendar']['locale']) + { + $this->locales[] = $owner_prefs['calendar']['locale']; + } + unset($owner_pref); + } + + @reset($this->locales); + if($GLOBALS['phpgw_info']['server']['auto_load_holidays'] == True) + { + while(list($key,$value) = each($this->locales)) + { + $this->auto_load_holidays($value); + } + } + } + + function auto_load_holidays($locale) + { + if($this->so->holiday_total($locale) == 0) + { + @set_time_limit(0); + + /* get the file that contains the calendar events for your locale */ + /* "http://www.egroupware.org/cal/holidays.US.csv"; */ + $network = CreateObject('phpgwapi.network'); + if(isset($GLOBALS['phpgw_info']['server']['holidays_url_path']) && $GLOBALS['phpgw_info']['server']['holidays_url_path'] != 'localhost') + { + $load_from = $GLOBALS['phpgw_info']['server']['holidays_url_path']; + } + else + { + $pos = strpos(' '.$GLOBALS['phpgw_info']['server']['webserver_url'],$_SERVER['HTTP_HOST']); + if($pos == 0) + { + switch($_SERVER['SERVER_PORT']) + { + case 80: + $http_protocol = 'http://'; + break; + case 443: + $http_protocol = 'https://'; + break; + } + $server_host = $http_protocol.$_SERVER['HTTP_HOST'].$GLOBALS['phpgw_info']['server']['webserver_url']; + } + else + { + $server_host = $GLOBALS['phpgw_info']['server']['webserver_url']; + } + $load_from = $server_host.'/calendar/egroupware.org'; + } +// echo 'Loading from: '.$load_from.'/holidays.'.strtoupper($locale).'.csv'."
\n"; + $lines = $network->gethttpsocketfile($load_from.'/holidays.'.strtoupper($locale).'.csv'); + if (!$lines) + { + return false; + } + $c_lines = count($lines); + for($i=0;$i<$c_lines;$i++) + { +// echo 'Line #'.$i.' : '.$lines[$i]."
\n"; + $holiday = explode("\t",$lines[$i]); + if(count($holiday) == 7) + { + $holiday['locale'] = $holiday[0]; + $holiday['name'] = $GLOBALS['phpgw']->db->db_addslashes($holiday[1]); + $holiday['mday'] = (int)$holiday[2]; + $holiday['month_num'] = (int)$holiday[3]; + $holiday['occurence'] = (int)$holiday[4]; + $holiday['dow'] = (int)$holiday[5]; + $holiday['observance_rule'] = (int)$holiday[6]; + $holiday['hol_id'] = 0; + $this->so->save_holiday($holiday); + } + } + } + } + + function save_holiday($holiday) + { + $this->so->save_holiday($holiday); + } + + function add() + { + if(@$_POST['submit']) + { + $holiday = $_POST['holiday']; + + if(empty($holiday['mday'])) + { + $holiday['mday'] = 0; + } + if(!isset($this->bo->locales[0]) || $this->bo->locales[0]=='') + { + $this->bo->locales[0] = $holiday['locale']; + } + elseif(!isset($holiday['locale']) || $holiday['locale']=='') + { + $holiday['locale'] = $this->bo->locales[0]; + } + if(!isset($holiday['hol_id'])) + { + $holiday['hol_id'] = $this->bo->id; + } + + // some input validation + + if (!$holiday['mday'] == !$holiday['occurence']) + { + $errors[] = lang('You need to set either a day or a occurence !!!'); + } + if($holiday['year'] && $holiday['occurence']) + { + $errors[] = lang('You can only set a year or a occurence !!!'); + } + else + { + $holiday['occurence'] = (int)($holiday['occurence'] ? $holiday['occurence'] : $holiday['year']); + unset($holiday['year']); + } + + // Still need to put some validation in here..... + + $this->ui = CreateObject('calendar.uiholiday'); + + if (is_array($errors)) + { + $holiday['month'] = $holiday['month_num']; + $holiday['day'] = $holiday['mday']; + $this->ui->edit_holiday($errors,$holiday); + } + else + { + $this->so->save_holiday($holiday); + $this->ui->edit_locale($holiday['locale']); + } + } + } + + function sort_holidays_by_date($holidays) + { + $c_holidays = count($holidays); + for($outer_loop=0;$outer_loop<($c_holidays - 1);$outer_loop++) + { + for($inner_loop=$outer_loop;$inner_loop<$c_holidays;$inner_loop++) + { + if($holidays[$outer_loop]['date'] > $holidays[$inner_loop]['date']) + { + $temp = $holidays[$inner_loop]; + $holidays[$inner_loop] = $holidays[$outer_loop]; + $holidays[$outer_loop] = $temp; + } + } + } + return $holidays; + } + + function set_holidays_to_date($holidays) + { + $new_holidays = Array(); + for($i=0;$i\n"; + $new_holidays[date('Ymd',$holidays[$i]['date'])][] = $holidays[$i]; + } + return $new_holidays; + } + + function read_holiday() + { + if(isset($this->cached_holidays)) + { + return $this->cached_holidays; + } + $holidays = $this->so->read_holidays($this->locales,'','',$this->year); + + if(count($holidays) == 0) + { + return $holidays; + } + + $temp_locale = $GLOBALS['phpgw_info']['user']['preferences']['common']['country']; + foreach($holidays as $i => $holiday) + //for($i=0;$icalculate_date($holiday, $holidays, $this->year); + } + unset($holidaycalc); + $this->holidays = $this->sort_holidays_by_date($holidays); + $this->cached_holidays = $this->set_holidays_to_date($this->holidays); + $GLOBALS['phpgw_info']['user']['preferences']['common']['country'] = $temp_locale; + return $this->cached_holidays; + } + /* End Calendar functions */ + + function check_admin() + { + if(!@$GLOBALS['phpgw_info']['user']['apps']['admin']) + { + Header('Location: ' . $GLOBALS['phpgw']->link('/index.php')); + } + } + + function rule_string($holiday) + { + if (!is_array($holiday)) + { + return false; + } + $sbox = CreateObject('phpgwapi.sbox'); + $month = $holiday['month'] ? lang($sbox->monthnames[$holiday['month']]) : ''; + unset($sbox); + + if (!$holiday['day']) + { + $occ = $holiday['occurence'] == 99 ? lang('last') : $holiday['occurence'].'.'; + + $dow_str = Array(lang('Sun'),lang('Mon'),lang('Tue'),lang('Wed'),lang('Thu'),lang('Fri'),lang('Sat')); + $dow = $dow_str[$holiday['dow']]; + + $str = lang('%1 %2 in %3',$occ,$dow,$month); + } + else + { + $str = $GLOBALS['phpgw']->common->dateformatorder($holiday['occurence']>1900?$holiday['occurence']:'',$month,$holiday[day]); + } + if ($holiday['observance_rule']) + { + $str .= ' ('.lang('Observance Rule').')'; + } + return $str; + } + } +?> diff --git a/calendar/inc/class.holidaycalc_JP.inc.php b/calendar/inc/class.holidaycalc_JP.inc.php new file mode 100755 index 0000000000..07f4e7feb7 --- /dev/null +++ b/calendar/inc/class.holidaycalc_JP.inc.php @@ -0,0 +1,153 @@ + * + * http://www.itheart.com * + * -------------------------------------------- * + * This program is free software; you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the * + * Free Software Foundation; either version 2 of the License, or (at your * + * option) any later version. * + \**************************************************************************/ + + /* $Id$ */ + + class holidaycalc + { + function calculate_date($holiday, &$holidays, $year) + { + static $cached_month; + static $cached_day; + static $cached_observance_rule; + + if ($holiday['day'] == 0 && $holiday['dow'] != 0 && $holiday['occurence'] != 0) + { + $dow = $GLOBALS['phpgw']->datetime->day_of_week($year, $holiday['month'], 1); + $dayshift = (($holiday['dow'] + 7) - $dow) % 7; + $day = ($holiday['occurence'] - 1) * 7 + $dayshift + 1; + + // Happy monday law. + if ($holiday['month'] == 1) + { + if ($year < 2000) + { + $day = 15; + } + } + elseif ($holiday['month'] == 7) + { + if ($year < 2003) + { + $day = 20; + } + } + elseif ($holiday['month'] == 9) + { + if ($year < 2003) + { + $day = 15; + } + } + elseif ($holiday['month'] == 10) + { + if ($year < 2000) + { + $day = 10; + } + } + } + elseif ($holiday['day'] == 0 && $holiday['dow'] == 0 && $holiday['occurence'] == 0) + { + // For the next generation. + // over 2151, please set $factor... + if ($holiday['month'] == 3) + { + // for Vernal Equinox + if ($year >= 1980 && $year <= 2099) + { + $factor = 20.8431; + } + elseif ($year >= 2100 && $year <= 2150) + { + $factor = 21.851; + } + } + elseif ($holiday['month'] == 9) + { + // for Autumnal Equinox + if ($year >= 1980 && $year <= 2099) + { + $factor = 23.2488; + } + elseif ($year >= 2100 && $year <= 2150) + { + $factor = 24.2488; + } + } + + $day = (int)($factor + 0.242194 * ($year - 1980) + - (int)(($year - 1980) / 4)); + } + else + { + // normal holiday + $day = $holiday['day']; + } + + if ($year >= 1985 && $holiday['month'] == $cached_month && $day == $cached_day + 2 && $cached_observance_rule == True && $holiday['observance_rule'] == True) + { + $pdow = $GLOBALS['phpgw']->datetime->day_of_week($year,$holiday['month'],$day-1); + if ($pdow != 0) + { + $addcnt = count($holidays) + 1; + $holidays[$addcnt]['locale'] = $holiday['locale']; + if ($pdow == 1) + { + $holidays[$addcnt]['name'] = lang('overlap holiday'); + } + else + { + $holidays[$addcnt]['name'] = lang('people holiday'); + } + $holidays[$addcnt]['day'] = $day - 1; + $holidays[$addcnt]['month'] = $holiday['month']; + $holidays[$addcnt]['occurence'] = 0; + $holidays[$addcnt]['dow'] = 0; + $holidays[$addcnt]['date'] = mktime(0,0,0,$holiday['month'],$day-1,$year); + $holidays[$addcnt]['observance_rule'] = 0; + } + } + + $cached_month = $holiday['month']; + $cached_day = $day; + $cached_observance_rule = $holiday['observance_rule']; + + if ($year >= 1985 && $holiday['month'] == 5 && $day == 3) + { + ; + } + elseif ($holiday['observance_rule'] == True) + { + $dow = $GLOBALS['phpgw']->datetime->day_of_week($year,$holiday['month'],$day); + // This now calulates Observed holidays and creates a new entry for them. + if($dow == 0) + { + $addcnt = count($holidays) + 1; + $holidays[$addcnt]['locale'] = $holiday['locale']; + $holidays[$addcnt]['name'] = lang('overlap holiday'); + $holidays[$addcnt]['day'] = $day + 1; + $holidays[$addcnt]['month'] = $holiday['month']; + $holidays[$addcnt]['occurence'] = $holiday['occurence']; + $holidays[$addcnt]['dow'] = $holiday['dow']; + $holidays[$addcnt]['date'] = mktime(0,0,0,$holiday['month'],$day+1,$year); + $holidays[$addcnt]['observance_rule'] = 0; + } + } + + $date = mktime(0,0,0,$holiday['month'],$day,$year); + + return $date; + } + } +?> diff --git a/calendar/inc/class.holidaycalc_US.inc.php b/calendar/inc/class.holidaycalc_US.inc.php index 434b76e13f..4994a3dc6f 100755 --- a/calendar/inc/class.holidaycalc_US.inc.php +++ b/calendar/inc/class.holidaycalc_US.inc.php @@ -15,25 +15,25 @@ class holidaycalc { - function add($holiday,&$holidays,$year,&$i,$day_offset=0) + function add($holiday,&$holidays,$year,$day_offset=0) { - $i++; - $holidays[$i]= $holiday; if ($day_offset) { - $holidays[$i]['name'] .= ' (Observed)'; + $holiday['name'] .= ' (Observed)'; } - $holidays[$i]['date'] = mktime(0,0,0,$holiday['month'],$holiday['day']+$day_offset,$year); + $holiday['date'] = mktime(0,0,0,$holiday['month'],$holiday['day']+$day_offset,$year); foreach(array('day'=>'d','month'=>'m','occurence'=>'Y') as $key => $frmt) { - $holidays[$i][$key] = date($frmt,$holidays[$i]['date']); + $holiday[$key] = date($frmt,$holiday['date']); } - $holidays[$i]['obervance_rule'] = 0; + $holiday['obervance_rule'] = 0; - //echo "

holidaycalc::add(".print_r($holiday,True).",,$year,,$day_offset)=".print_r($holidays[$i],True)."

"; + $holidays[]= $holiday; + + //echo "

holidaycalc::add(,,$year,,$day_offset)=".print_r($holiday,True)."

"; } - function calculate_date($holiday, &$holidays, $year, &$i) + function calculate_date($holiday, &$holidays, $year) { //echo "

holidaycalc::calculate_date(".print_r($holiday,True).",,$year,)

"; @@ -71,7 +71,7 @@ // 0 = sundays are observed on monday (+1), 6 = saturdays are observed on fridays (-1) if($dow == 0 || $dow == 6) { - $this->add($holiday,$holidays,$year,$i,$dow == 0 ? 1 : -1); + $this->add($holiday,$holidays,$year,$dow == 0 ? 1 : -1); } if ($holiday['month'] == 1 && $day == 1) { @@ -79,10 +79,10 @@ // checking if next year's newyear might be observed in this year if ($dow == 6) { - $this->add($holiday,$holidays,$year+1,$i,-1); + $this->add($holiday,$holidays,$year+1,-1); } // add the next years newyear, to show it in a week- or month-view - $this->add($holiday,$holidays,$year+1,$i); + $this->add($holiday,$holidays,$year+1); } // checking if last year's new year's eve might be observed in this year if ($holiday['month'] == 12 && $day == 31) @@ -90,10 +90,10 @@ $dow = $GLOBALS['phpgw']->datetime->day_of_week($year-1,$holiday['month'],$day); if ($dow == 0) { - $this->add($holiday,$holidays,$year-1,$i,1); + $this->add($holiday,$holidays,$year-1,1); } // add the last years new year's eve, to show it in a week- or month-view - $this->add($holiday,$holidays,$year-1,$i); + $this->add($holiday,$holidays,$year-1); } } }