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