fix holiday
[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         'cny' => 'https://raw.githubusercontent.com/andrewlkho/cny.ics/master/cny.ics', // CNY
25     );
26     
27      
28     function updateHolidays($country)
29     {
30         
31         if (!isset(self::$map[$country])) {
32             die("invalid country");
33         }
34         
35         // do we alredy have the data for this year.
36         $d = DB_DataObject::Factory('core_holiday');
37         $d->country = $country;
38         $d->orderBy('holiday_date DESC');
39         $d->limit(1);
40         if ($d->count() && $d->find(true) && strtotime($d->holiday_date) > strtotime('NOW + 6 MONTHS')) {
41             // no need to fetch..
42             return;
43         }
44         
45         
46         
47         $data = file_get_contents("https://www.1823.gov.hk/common/ical/gc/en.ics", false,
48             stream_context_create(array(
49                 "ssl"=>array(
50                     "verify_peer"=>false,
51                     "verify_peer_name"=>false,
52                 ),
53             ))
54         );
55         
56         $vevents = explode('BEGIN:VEVENT', $data);
57         
58         foreach ($vevents as $vevent){
59             
60             $lines = explode("\n", $vevent);
61             
62             $start_dt = false;
63             $end_dt = false;
64             
65             foreach ($lines as $line){
66                 
67                 if(preg_match('/^DTSTART;VALUE=DATE:([0-9]+)/', $line, $matches)){
68                     $fmt = substr($matches[1], 0, 4) . "-" . substr($matches[1], 4, 2) . "-" . substr($matches[1], 6, 2);
69                     $start_dt = date('Y-m-d', strtotime($fmt));
70                 }
71                 
72                 if(preg_match('/^DTEND;VALUE=DATE:([0-9]+)/', $line, $matches)){
73                     $fmt = substr($matches[1], 0, 4) . "-" . substr($matches[1], 4, 2) . "-" . substr($matches[1], 6, 2);
74                     $end_dt = date('Y-m-d', strtotime($fmt));
75                 }
76                 if(preg_match('/^SUMMARY[^:]*:(.*)/', $line, $matches)){
77                     $name = trim($matches[1]);
78                 }
79             }
80             
81             if(empty($start_dt) || empty($end_dt)){
82                 continue;
83             }
84             //DB_DataObject::DebugLevel(1);
85             //var_dump($start_dt); var_dump($end_dt); exit;
86             
87             for ($i = strtotime($start_dt); $i < strtotime($end_dt) ; $i += (60 * 60 * 24)) {
88                 
89                 $d = DB_DataObject::Factory('core_holiday');
90                 $d->country = $country;
91                 $d->holiday_date = date('Y-m-d', $i);
92                 if (!$d->count()) {
93                     $d->insert();
94  
95                 } else {
96                     $d->find(true);
97                     $dd = clone($d);
98                     $d->name = $name;
99                     $d->update($dd);
100                  }
101                 
102                 
103             }
104             
105         }
106
107     }
108     function isHoliday($country, $date)
109     {
110         $d = DB_DataObject::Factory('core_holiday');
111         $d->country = $country;
112         $d->holiday_date = $date;
113         return $d->count();
114     }
115     
116 }