DataObjects/core.sql
[Pman.Core] / UpdateDatabase.php
index 646c0f0..79de922 100644 (file)
@@ -54,6 +54,20 @@ class Pman_Core_UpdateDatabase extends Pman
             'max' => 1,
         ),
         
+        'json-company' => array(
+            'desc' => 'Company JSON file',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+            
+        ),
+        'json-person' => array(
+            'desc' => 'Person JSON file',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+            
+        ),
     );
     
     static function cli_opts()
@@ -82,7 +96,7 @@ class Pman_Core_UpdateDatabase extends Pman
         return $ret;
     }
     
-    var $cli_options = false;
+    var $opts = false;
     
     
     var $cli = false;
@@ -107,11 +121,11 @@ class Pman_Core_UpdateDatabase extends Pman
     function get($args, $opt)
     {
         
-        $this->cli_options = $opt;
+        $this->opts = $opt;
         
         // ask all the modules to verify the opts
         
-        $this->checkOpts();
+        $this->checkOpts($opt);
         
         
         
@@ -267,7 +281,7 @@ class Pman_Core_UpdateDatabase extends Pman
             
             // if init has been called
             // look in pgsql.ini
-            if (!empty($this->cli_options['init'])) {
+            if (!empty($this->opts['init'])) {
                 $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql.init");
                 
             }
@@ -287,7 +301,7 @@ class Pman_Core_UpdateDatabase extends Pman
             
             
             
-            if (!empty($this->cli_options['init']) && file_exists($this->rootDir. "/Pman/$m/pgsql.initdata")) {
+            if (!empty($this->opts['init']) && file_exists($this->rootDir. "/Pman/$m/pgsql.initdata")) {
                 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
                 
                 $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql.initdata");
@@ -458,8 +472,19 @@ class Pman_Core_UpdateDatabase extends Pman
     }
     
     
-    function checkOpts()
+    function checkOpts($opts)
     {
+        
+        
+        foreach($opts as $o=>$v) {
+            if (!preg_match('/^json-/', $o) || empty($v)) {
+                continue;
+            }
+            if (!file_exists($v)) {
+                die("File does not exist : OPTION --{$o} = {$v} \n");
+            }
+        }
+        
         $modules = array_reverse($this->modulesList());
         
         // move 'project' one to the end...
@@ -475,18 +500,38 @@ class Pman_Core_UpdateDatabase extends Pman
             if(!method_exists($x, 'checkOpts')){
                 continue;
             };
-            $x->checkOpts();
+            $x->checkOpts($opts);
         }
                 
     }
-    
+    static function jsonImportFromArray($opts)
+    {
+        foreach($opts as $o=>$v) {
+            if (!preg_match('/^json-/', $o) || empty($v)) {
+                continue;
+            }
+            $type = str_replace('_', '-', substr($o,6));
+            $data= json_decode(file_get_contents($file),true);
+            DB_DataObject::factory($type)->importFromArray($data);
+            
+        }
+        
+        
+        
+    }
     
     function runUpdateModulesData()
     {
+        
+        
         HTML_FlexyFramework::get()->generateDataobjectsCache(true);
-               
+        echo "Running jsonImportFromArray\n";
+        Pman_Core_UpdateDatabase::jsonImportFromArray($this->opts);
         
+        
+        echo "Running updateData on modules\n";
         // runs core...
+        echo "Core\n";
         $this->updateData(); 
         $modules = array_reverse($this->modulesList());
         
@@ -497,12 +542,14 @@ class Pman_Core_UpdateDatabase extends Pman
             if($module == 'Core' || !file_exists($file)){
                 continue;
             }
+            
             require_once $file;
             $class = "Pman_{$module}_UpdateDatabase";
             $x = new $class;
             if(!method_exists($x, 'updateData')){
                 continue;
             };
+            echo "$module\n";
             $x->updateData();
         }