Fix #6466 - hourly car park cost in mysql
authorAlan Knowles <alan@roojs.com>
Tue, 10 Nov 2020 07:50:00 +0000 (15:50 +0800)
committerAlan Knowles <alan@roojs.com>
Tue, 10 Nov 2020 07:50:00 +0000 (15:50 +0800)
DataObjects/Core_holiday.php [new file with mode: 0644]
Import/Core_holiday.php [new file with mode: 0644]
sql/core_holiday.sql [new file with mode: 0644]

diff --git a/DataObjects/Core_holiday.php b/DataObjects/Core_holiday.php
new file mode 100644 (file)
index 0000000..45ac129
--- /dev/null
@@ -0,0 +1,93 @@
+<?php
+/**
+ * Table Definition for core company
+ */
+class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
+
+class Pman_Core_DataObjects_Core_holiday extends DB_DataObject 
+{
+    ###START_AUTOCODE
+    /* the code below is auto generated do not remove the above tag */
+
+    public $__table = 'core_holiday';               // table name
+    public $id;                              
+    public $country;                            
+    public $holiday_date;                            
+
+    
+    /* the code above is auto generated do not remove the tag below */
+    ###END_AUTOCODE
+    
+    
+    static $map = array(
+        'hk' => 'http://www.1823.gov.hk/common/ical/gc/en.ics'
+    );
+    
+     
+    function updateHolidays($country)
+    {
+        
+        if (!isset(self::$map[$country])) {
+            die("invalid country");
+        }
+        
+        // do we alredy have the data for this year.
+        $d = DB_DataObject::Factory('core_holiday');
+        $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')) {
+            // no need to fetch..
+            return;
+        }
+        
+        
+        
+        $data = file_get_contents("http://www.1823.gov.hk/common/ical/gc/en.ics");
+        
+        $vevents = explode('BEGIN:VEVENT', $data);
+        
+        foreach ($vevents as $vevent){
+            
+            $lines = explode("\n", $vevent);
+            
+            $start_dt = false;
+            $end_dt = false;
+            
+            foreach ($lines as $line){
+                
+                if(preg_match('/^DTSTART;VALUE=DATE:([0-9]+)/', $line, $matches)){
+                    $fmt = substr($matches[1], 0, 4) . "-" . substr($matches[1], 4, 2) . "-" . substr($matches[1], 6, 2);
+                    $start_dt = date('Y-m-d', strtotime($fmt));
+                }
+                
+                if(preg_match('/^DTEND;VALUE=DATE:([0-9]+)/', $line, $matches)){
+                    $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(empty($start_dt) || empty($end_dt)){
+                continue;
+            }
+            
+            //var_dump($start_dt); var_dump($end_dt); exit;
+            
+            for ($i = strtotime($start_dt); $i < strtotime($end_dt) ; $i += (60 * 60 * 24)) {
+                
+                $d = DB_DataObject::Factory('core_holiday');
+                $d->country = $country;
+                $d->holiday_date = date('Y-m-d', $i);
+                if (!$d->count()) {
+                    $d->insert();
+                }
+                
+                
+            }
+            
+        }
+
+    }
+    
+}
diff --git a/Import/Core_holiday.php b/Import/Core_holiday.php
new file mode 100644 (file)
index 0000000..71b6aad
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+
+require_once 'Pman/Roo.php';
+
+class Pman_Core_Import_Core_holiday extends Pman_Roo 
+{
+    static $cli_desc = "Update the holiday database (HK Only at present)"; 
+    
+    static $cli_opts = array(
+        
+    );
+    
+    function getAuth()
+    {
+        if (!HTML_FlexyFramework::get()->cli) {
+            return false;
+        }
+        
+        return true;
+        
+    }
+
+    var $defaults = array();
+    
+    function get($v, $opts=array())
+    {   
+        DB_DAtaObject::debugLevel(1);
+        $d = DB_DataObject::factory('core_holiday');
+        $d->updateHolidays('hk');
+        
+    }
+    
+    function log($str)
+    {
+        echo "$str \n";
+    }
+}
\ No newline at end of file
diff --git a/sql/core_holiday.sql b/sql/core_holiday.sql
new file mode 100644 (file)
index 0000000..f0449bb
--- /dev/null
@@ -0,0 +1,9 @@
+CREATE TABLE core_holiday (
+  id int(11)  NOT NULL auto_increment,
+  PRIMARY KEY   (id)
+);
+
+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 '';
+
+