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