home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2004 April
/
CMCD0404.ISO
/
Software
/
Freeware
/
Programare
/
groupoffice-com-2.01
/
classes
/
holidays.class.inc
< prev
next >
Wrap
Text File
|
2004-03-08
|
6KB
|
255 lines
<?php
/*
Copyright Intermesh 2003
Author: Georg Lorenz <georg@lonux.de>
Version: 1.0 Release date: 08 July 2003
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.
*/
class holidays extends db
{
var $region;
var $holidays;
function holidays($GO_LANGUAGE, $region)
{
$this->db();
require($GO_LANGUAGE->language_path.'holidays.inc');
//if $region contains $HTTP_ACCEPT_LANGUAGE then try to extract the first region element
$region = explode(",", $region);
$search = strstr($region["0"], ";");
$this->region = str_replace($search, "", $region["0"]);
$this->holidays = array();
if(is_array($input_holidays))
{
foreach($input_holidays as $key => $sub_array)
{
if(array_key_exists($this->region, $sub_array))
{
if($sub_array[$this->region])
{
$this->holidays[$key] = $sub_array[$this->region];
}
}
}
}
}
function add_holiday($date, $name)
{
$holiday_id = $this->nextid("scHolidays");
if ($holiday_id > 0)
{
$name = addslashes($name);
$sql = "INSERT INTO scHolidays (id, region, date, name) VALUES ('$holiday_id', '$this->region', '$date', '$name')";
$this->query($sql);
if ($this->affected_rows() > 0)
{
return true;
}
else
{
return false;
}
}else
{
return false;
}
}
function add_holidays($year="")
{
$holidays = $this->generate_holidays($year);
if(is_array($holidays))
{
foreach($holidays as $date => $name)
{
if($this->get_holiday_by_date($date))
{
if($this->delete_holiday($this->f('id')))
{
if(!$this->add_holiday($date, $name))
return false;
}
}else
{
if(!$this->add_holiday($date, $name))
return false;
}
}
}else
{
return false;
}
return true;
}
function update_holiday($holiday_id, $date, $name)
{
$sql = "UPDATE scHolidays SET region='$this->region', date='$date', name='$name'";
$sql .= " WHERE id='$holiday_id'";
return ($this->query($sql));
}
function delete_holiday($holiday_id)
{
$sql = "DELETE FROM scHolidays WHERE id='$holiday_id'";
$this->query($sql);
if ($this->affected_rows() > 0)
return true;
else
return false;
}
function delete_holidays($year="")
{
$sql = "DELETE FROM scHolidays WHERE region='$this->region'";
if(!empty($year))
{
$date_from = mktime(0,0,0,12,31,$year-1);
$date_to = mktime(0,0,0,1,1,$year+1);
$sql .= " AND date>'$date_from' AND date<'$date_to'";
}
$this->query($sql);
if ($this->affected_rows() > 0)
return true;
else
return false;
}
function get_holiday($holiday_id)
{
$sql = "SELECT * FROM scHolidays WHERE id='$holiday_id'";
$this->query($sql);
if ($this->next_record())
return $this->Record;
else
return false;
}
function get_holiday_by_date($holiday_date)
{
$sql = "SELECT * FROM scHolidays WHERE date='$holiday_date' AND region='$this->region'";
$this->query($sql);
if ($this->next_record())
return true;
else
return false;
}
function get_holidays($year="")
{
$sql = "SELECT * FROM scHolidays WHERE region='$this->region'";
if(!empty($year))
{
$date_from = mktime(0,0,0,12,31,$year-1);
$date_to = mktime(0,0,0,1,1,$year+1);
$sql .= " AND date>'$date_from' AND date<'$date_to'";
}
$sql .= " ORDER BY date ASC";
$this->query($sql);
return $this->num_rows();
}
function generate_holidays($year="")
{
if(empty($year))
{
$current_date = getdate();
$year = $current_date["year"];
}
$holidays = array();
if(isset($this->holidays['fix']))
{
foreach($this->holidays['fix'] as $key => $value)
{
$month_day = explode("-", $key);
$date = mktime(0,0,0,$month_day[0],$month_day[1],$year);
$holidays[$date] = $value;
}
}
if(isset($this->holidays['var']))
{
$easter_day = $this->get_easter_day($year);
foreach($this->holidays['var'] as $key => $value)
{
$date = strtotime($key." days", $easter_day);
$holidays[$date] = $value;
}
}
if(isset($this->holidays['spc']))
{
$weekday = $this->get_weekday("24","12",$year);
foreach($this->holidays['spc'] as $key => $value)
{
$count = $key - $weekday;
$date = strtotime($count." days", mktime(0,0,0,"12","24",$year));
$holidays[$date] = $value;
}
}
if(empty($holidays))
{
return false;
}else
{
ksort($holidays, SORT_NUMERIC);
return $holidays;
}
}
function get_easter_day($year)
{
if($year < 2100)
$n = 5;
else
$n = 6;
$a = bcmod($year, 19);
$b = bcmod($year, 4);
$c = bcmod($year, 7);
$d = bcmod((19 * $a + 24), 30);
$e = bcmod((2 * $b + 4 * $c + 6 * $d + $n), 7);
if(($d + $e + 22) <= 31)
{
$day = $d + $e + 22;
$month = 3;
}else
{
$day = $d + $e - 9;
$month = 4;
}
if($day == 26 && $month == 4)
$day = 19;
if($day == 25 && $month == 4 && $a > 10 && $d == 28)
$day = 18;
return mktime(0,0,0,$month,$day,$year);
}
function get_weekday($day, $month, $year)
{
$date = getdate(mktime(0, 0, 0, $month, $day, $year));
return $date["wday"];
}
}