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 >
Text File  |  2004-03-08  |  6KB  |  255 lines

  1. <?php
  2. /*
  3. Copyright Intermesh 2003
  4. Author: Georg Lorenz <georg@lonux.de>
  5. Version: 1.0 Release date: 08 July 2003
  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. class holidays extends db
  14. {
  15.     var $region;
  16.     var $holidays;
  17.  
  18.     function holidays($GO_LANGUAGE, $region)
  19.     {
  20.         $this->db();
  21.  
  22.         require($GO_LANGUAGE->language_path.'holidays.inc');
  23.  
  24.         //if $region contains $HTTP_ACCEPT_LANGUAGE then try to extract the first region element
  25.         $region = explode(",", $region);
  26.         $search = strstr($region["0"], ";");
  27.         $this->region = str_replace($search, "", $region["0"]);
  28.  
  29.         $this->holidays = array();
  30.         if(is_array($input_holidays))
  31.         {
  32.             foreach($input_holidays as $key => $sub_array)
  33.             {
  34.                 if(array_key_exists($this->region, $sub_array))
  35.                 {
  36.                     if($sub_array[$this->region])
  37.                     {
  38.                         $this->holidays[$key] = $sub_array[$this->region];
  39.                     }
  40.                 }
  41.             }
  42.         }
  43.     }
  44.  
  45.     function add_holiday($date, $name)
  46.     {
  47.         $holiday_id = $this->nextid("scHolidays");
  48.         if ($holiday_id > 0)
  49.         {
  50.             $name = addslashes($name);
  51.             $sql = "INSERT INTO scHolidays (id, region, date, name) VALUES ('$holiday_id', '$this->region', '$date', '$name')";
  52.                $this->query($sql);
  53.             if ($this->affected_rows() > 0)
  54.             {
  55.                 return true;
  56.             }
  57.             else
  58.             {
  59.                 return false;
  60.             }
  61.         }else
  62.         {
  63.             return false;
  64.         }
  65.     }
  66.  
  67.     function add_holidays($year="")
  68.     {
  69.         $holidays = $this->generate_holidays($year);
  70.  
  71.         if(is_array($holidays))
  72.         {
  73.             foreach($holidays as $date => $name)
  74.             {
  75.                 if($this->get_holiday_by_date($date))
  76.                 {
  77.                     if($this->delete_holiday($this->f('id')))
  78.                     {
  79.                         if(!$this->add_holiday($date, $name))
  80.                             return false;
  81.                     }
  82.                 }else
  83.                 {
  84.                 if(!$this->add_holiday($date, $name))
  85.                         return false;
  86.                 }
  87.             }
  88.         }else
  89.         {
  90.             return false;
  91.         }
  92.         return true;
  93.     }
  94.  
  95.     function update_holiday($holiday_id, $date, $name)
  96.     {
  97.         $sql = "UPDATE scHolidays SET region='$this->region', date='$date', name='$name'";
  98.         $sql .= " WHERE id='$holiday_id'";
  99.         return ($this->query($sql));
  100.     }
  101.  
  102.     function delete_holiday($holiday_id)
  103.     {
  104.         $sql = "DELETE FROM scHolidays WHERE id='$holiday_id'";
  105.         $this->query($sql);
  106.         if ($this->affected_rows() > 0)
  107.             return true;
  108.         else
  109.             return false;
  110.     }
  111.  
  112.     function delete_holidays($year="")
  113.     {
  114.         $sql = "DELETE FROM scHolidays WHERE region='$this->region'";
  115.  
  116.         if(!empty($year))
  117.         {
  118.             $date_from = mktime(0,0,0,12,31,$year-1);
  119.             $date_to = mktime(0,0,0,1,1,$year+1);
  120.             $sql .= " AND date>'$date_from' AND date<'$date_to'";
  121.         }
  122.  
  123.         $this->query($sql);
  124.         if ($this->affected_rows() > 0)
  125.             return true;
  126.         else
  127.             return false;
  128.     }
  129.  
  130.     function get_holiday($holiday_id)
  131.     {
  132.         $sql = "SELECT * FROM scHolidays WHERE id='$holiday_id'";
  133.         $this->query($sql);
  134.         if ($this->next_record())
  135.             return $this->Record;
  136.         else
  137.             return false;
  138.     }
  139.  
  140.     function get_holiday_by_date($holiday_date)
  141.     {
  142.         $sql = "SELECT * FROM scHolidays WHERE date='$holiday_date' AND region='$this->region'";
  143.         $this->query($sql);
  144.         if ($this->next_record())
  145.             return true;
  146.         else
  147.             return false;
  148.     }
  149.  
  150.     function get_holidays($year="")
  151.     {
  152.         $sql = "SELECT * FROM scHolidays WHERE region='$this->region'";
  153.  
  154.         if(!empty($year))
  155.         {
  156.             $date_from = mktime(0,0,0,12,31,$year-1);
  157.             $date_to = mktime(0,0,0,1,1,$year+1);
  158.             $sql .= " AND date>'$date_from' AND date<'$date_to'";
  159.         }
  160.         $sql .= " ORDER BY date ASC";
  161.  
  162.         $this->query($sql);
  163.         return $this->num_rows();
  164.     }
  165.  
  166.     function generate_holidays($year="")
  167.     {
  168.         if(empty($year))
  169.         {
  170.             $current_date = getdate();
  171.             $year = $current_date["year"];
  172.         }
  173.  
  174.             $holidays = array();
  175.         if(isset($this->holidays['fix']))
  176.         {
  177.                 foreach($this->holidays['fix'] as $key => $value)
  178.                 {
  179.                 $month_day = explode("-", $key);
  180.                 $date = mktime(0,0,0,$month_day[0],$month_day[1],$year);
  181.                 $holidays[$date] = $value;
  182.                 }
  183.             }
  184.  
  185.         if(isset($this->holidays['var']))
  186.         {
  187.                 $easter_day = $this->get_easter_day($year);
  188.                 foreach($this->holidays['var'] as $key => $value)
  189.                 {
  190.                     $date = strtotime($key." days", $easter_day);
  191.                 $holidays[$date] = $value;
  192.                 }
  193.             }
  194.  
  195.         if(isset($this->holidays['spc']))
  196.         {
  197.                 $weekday = $this->get_weekday("24","12",$year);
  198.                 foreach($this->holidays['spc'] as $key => $value)
  199.                 {
  200.                 $count = $key - $weekday;
  201.                     $date = strtotime($count." days", mktime(0,0,0,"12","24",$year));
  202.                 $holidays[$date] = $value;
  203.                 }
  204.             }
  205.  
  206.         if(empty($holidays))
  207.         {
  208.             return false;
  209.         }else
  210.         {
  211.                 ksort($holidays, SORT_NUMERIC);
  212.                 return $holidays;
  213.         }
  214.     }
  215.  
  216.     function get_easter_day($year)
  217.     {
  218.             if($year < 2100)
  219.             $n = 5;
  220.             else
  221.             $n = 6;
  222.  
  223.             $a = bcmod($year, 19);
  224.             $b = bcmod($year, 4);
  225.             $c = bcmod($year, 7);
  226.             $d = bcmod((19 * $a + 24), 30);
  227.             $e = bcmod((2 * $b + 4 * $c + 6 * $d + $n), 7);
  228.  
  229.             if(($d + $e + 22) <= 31)
  230.             {
  231.             $day = $d + $e + 22;
  232.             $month = 3;
  233.             }else
  234.             {
  235.             $day = $d + $e - 9;
  236.             $month = 4;
  237.             }
  238.  
  239.             if($day == 26 && $month == 4)
  240.             $day = 19;
  241.  
  242.             if($day == 25 && $month == 4 && $a > 10 && $d == 28)
  243.             $day = 18;
  244.  
  245.             return mktime(0,0,0,$month,$day,$year);
  246.     }
  247.  
  248.     function get_weekday($day, $month, $year)
  249.     {
  250.             $date = getdate(mktime(0, 0, 0, $month, $day, $year));
  251.             return $date["wday"];
  252.     }
  253.  
  254. }
  255.