Changed DataObjects/Core_holiday.phpImport/Core_holiday.phpsql/core_holiday.sql wip_alan_T8125_cny_holidays_import
authorAlan <alan@roojs.com>
Wed, 3 Apr 2024 06:50:09 +0000 (14:50 +0800)
committerAlan <alan@roojs.com>
Wed, 3 Apr 2024 06:50:09 +0000 (14:50 +0800)
DataObjects/Core_holiday.php
Import/Core_holiday.php
sql/core_holiday.sql

index 742a240..2e7bff1 100644 (file)
@@ -12,7 +12,8 @@ class Pman_Core_DataObjects_Core_holiday extends DB_DataObject
     public $__table = 'core_holiday';               // table name
     public $id;                              
     public $country;                            
-    public $holiday_date;                            
+    public $holiday_date;
+    public $name;
 
     
     /* the code above is auto generated do not remove the tag below */
@@ -25,7 +26,7 @@ class Pman_Core_DataObjects_Core_holiday extends DB_DataObject
     );
     
      
-    function updateHolidays($country)
+    function updateHolidays($country, $force = false)
     {
         
         if (!isset(self::$map[$country])) {
@@ -37,14 +38,14 @@ class Pman_Core_DataObjects_Core_holiday extends DB_DataObject
         $d->country = $country;
         $d->orderBy('holiday_date DESC');
         $d->limit(1);
-        if ($d->count() && $d->find(true) && strtotime($d->holiday_date) > strtotime('NOW + 6 MONTHS')) {
+        if (!$force && $d->count() && $d->find(true) && strtotime($d->holiday_date) > strtotime('NOW + 6 MONTHS')) {
             // no need to fetch..
             return;
         }
         
         
         
-        $data = file_get_contents("https://www.1823.gov.hk/common/ical/gc/en.ics", false,
+        $data = file_get_contents(self::$map[$country], false,
             stream_context_create(array(
                 "ssl"=>array(
                     "verify_peer"=>false,
@@ -61,7 +62,7 @@ class Pman_Core_DataObjects_Core_holiday extends DB_DataObject
             
             $start_dt = false;
             $end_dt = false;
-            
+            $name = '';
             foreach ($lines as $line){
                 
                 if(preg_match('/^DTSTART;VALUE=DATE:([0-9]+)/', $line, $matches)){
@@ -73,7 +74,9 @@ class Pman_Core_DataObjects_Core_holiday extends DB_DataObject
                     $fmt = substr($matches[1], 0, 4) . "-" . substr($matches[1], 4, 2) . "-" . substr($matches[1], 6, 2);
                     $end_dt = date('Y-m-d', strtotime($fmt));
                 }
-                
+                if(preg_match('/^SUMMARY[^:]*:(.*)/', $line, $matches)){
+                    $name = $matches[1];
+                }
             }
             
             if(empty($start_dt) || empty($end_dt)){
@@ -87,8 +90,13 @@ class Pman_Core_DataObjects_Core_holiday extends DB_DataObject
                 $d = DB_DataObject::Factory('core_holiday');
                 $d->country = $country;
                 $d->holiday_date = date('Y-m-d', $i);
+                
                 if (!$d->count()) {
+                    $d->name = $name;
                     $d->insert();
+                } else {
+                    $d->name = $name;
+                    $d->update();
                 }
                 
                 
index e01c11c..2fa4e59 100644 (file)
@@ -24,10 +24,10 @@ class Pman_Core_Import_Core_holiday extends Pman_Core_Cli
     
     function get($v, $opts=array())
     {   
-        DB_DAtaObject::debugLevel(1);
+        //DB_DAtaObject::debugLevel(1);
         $d = DB_DataObject::factory('core_holiday');
-        $d->updateHolidays('hk');
-        $d->updateHolidays('cny');
+        $d->updateHolidays('hk',true );
+        $d->updateHolidays('cny', true);
     }
     
     function log($str)
index 4880851..fe62b15 100644 (file)
@@ -5,6 +5,7 @@ CREATE TABLE core_holiday (
 
 ALTER TABLE core_holiday ADD COLUMN holiday_date DATE NOT NULL DEFAULT '0000-00-00';
 ALTER TABLE core_holiday ADD COLUMN country VARCHAR(4) NOT NULL DEFAULT '';
+ALTER TABLE core_holiday ADD COLUMN name VARCHAR(128) NOT NULL DEFAULT '';
 
 
 alter table core_holiday add index lookup (holiday_date, country);