DataObjects/Core_holiday.php
[Pman.Core] / DataObjects / Core_holiday.php
1 <?php
2 /**
3  * Table Definition for core company
4  */
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_holiday extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'core_holiday';               // table name
13     public $id;                              
14     public $country;                            
15     public $holiday_date;                            
16
17     
18     /* the code above is auto generated do not remove the tag below */
19     ###END_AUTOCODE
20     
21     
22     static $map = array(
23         'hk' => 'http://www.1823.gov.hk/common/ical/gc/en.ics'
24     );
25     
26      
27     function updateHolidays($country)
28     {
29         
30         if (!isset(self::$map[$country])) {
31             die("invalid country");
32         }
33         
34         // do we alredy have the data for this year.
35         $d = DB_DAtaObject::Factory('core_holiday');
36         $d->orderBy('holiday_date DESC');
37         $d->limit(1);
38         if ($d->count() && $d->find(true) && strtotime($d->holiday_date) > strtotime('NOW + 6 MONTHS')) {
39             // no need to fetch..
40             return;
41         }
42         
43         
44         
45          $data = file_get_contents("http://www.1823.gov.hk/common/ical/gc/en.ics"));
46         }
47         
48         $vevents = explode('BEGIN:VEVENT', file_get_contents($ics));
49         
50         foreach ($vevents as $vevent){
51             
52             $lines = explode("\n", $vevent);
53             
54             $start_dt = false;
55             $end_dt = false;
56             
57             foreach ($lines as $line){
58                 
59                 if(preg_match('/^DTSTART;VALUE=DATE:([0-9]+)/', $line, $matches)){
60                     $fmt = substr($matches[1], 0, 4) . "-" . substr($matches[1], 4, 2) . "-" . substr($matches[1], 6, 2);
61                     $start_dt = date('Y-m-d', strtotime($fmt));
62                 }
63                 
64                 if(preg_match('/^DTEND;VALUE=DATE:([0-9]+)/', $line, $matches)){
65                     $fmt = substr($matches[1], 0, 4) . "-" . substr($matches[1], 4, 2) . "-" . substr($matches[1], 6, 2);
66                     $end_dt = date('Y-m-d', strtotime($fmt));
67                 }
68                 
69             }
70             
71             if(empty($start_dt) || empty($end_dt)){
72                 continue;
73             }
74             
75             for ($i = strtotime($start_dt); $i < strtotime($end_dt) ; $i += (60 * 60 * 24)) {
76                 $this->holidays[] = date('Y-m-d', $i);
77             }
78             
79         }
80         
81         $this->holidays = array_unique(array_filter($this->holidays));
82         
83         return $this->holidays;
84     }
85     
86 }