php7 fixes
[Pman.Builder] / FormToSQL.php
index 6c74cb2..835ee30 100644 (file)
@@ -14,7 +14,7 @@ class Pman_Builder_FormToSQL extends Pman {
         return true;
     }
     
-    function get()
+    function get($base, $opts = array())
     {
         //print_R($_SERVER['argv']);exit;
         $file  = $_SERVER['argv'][2];
@@ -23,7 +23,31 @@ class Pman_Builder_FormToSQL extends Pman {
            }
         $o = json_decode(file_get_contents($file));
         $this->walk($o);
-        print_R($this->cols);
+        //print_R($this->cols);
+        
+        $b = explode('.', basename($file));
+        array_pop($b);
+        $tn = strtolower(preg_replace('/([A-Z])/','_$1', array_pop($b)));
+        $tn = preg_replace('/^_+/', '', $tn);
+        if (!empty($_SERVER['argv'][3])) {
+            $tn= $_SERVER['argv'][3];
+        }
+        
+        
+        $this->toSQL($tn);
+        $b= basename(dirname($file));
+        
+        $do = $this->toDO($b, $tn);
+        
+        $dofile = dirname($file).'/DataObjects/'. ucfirst($tn).'.php';
+        if (!file_exists($dofile)) {
+            echo "WRITING  $dofile\n";
+            file_put_contents($dofile, $do);
+        } else {
+            // should support AUTOCODE...
+            echo "DELETE $dofile IF YOU WANT TO RECREATED IT..\n";
+        }
+        
         die("DONE");
     }
     
@@ -55,11 +79,12 @@ class Pman_Builder_FormToSQL extends Pman {
         }
         $items = array();
         foreach($o->items as $oo) {
-            if (!isset($oo->{'*props'})) {
+            if (!isset($oo->{'*prop'})) {
                 $items[] = $oo;
                 continue;
             }
-            $o->{$oo->{'*props'}} = $oo;
+            $this->flattenProps($oo);
+            $o->{$oo->{'*prop'}} = $oo;
             
         }
         $o->items = $items;
@@ -67,7 +92,8 @@ class Pman_Builder_FormToSQL extends Pman {
     
     function parse($o) 
     {
-        
+        require_once 'Services/JSON.php';
+        $s = new Services_JSON();
         if (empty($o->xtype) || empty($o->{'|xns'})) {
             return;
         }
@@ -80,13 +106,18 @@ class Pman_Builder_FormToSQL extends Pman {
                 $f->name = $o->name;
                 $f->type = 'VARCHAR';
                 $f->default = "''";
+                 $f->extra = "NOT NULL";
                 $f->size = min(255,max(8, pow(2, strlen(decbin(($o->width/2)-1)))));
                 $this->cols[] = $f;
                 break;
             
             case 'ComboBox':
+              //  print_r($o);exit;
                 if ($o->store->xtype == 'SimpleStore') {
-                    $data = json_decode($o->store->{'|data'}); 
+                    //print_R($o);exit;
+                    
+                    $data = $s->decode($o->store->{'|data'}); 
+                    
                     $type = 'INT';
                     $len = 0;
                     foreach($data as $row) {
@@ -99,22 +130,41 @@ class Pman_Builder_FormToSQL extends Pman {
                     }
                     if ($type == 'VARCHAR') {
                         $len = min(255,max(8, pow(2, strlen(decbin(($len))))));
+                        $f->default = "''";
                     } else {
                         $len = 11;
+                        $f->default = 0;
                     }
                     $f->name = $o->name; // hiddenname?
                     $f->type = $type;
                     $f->size = $len;
                     $this->cols[] = $f;
-                    continue;
+                    break;
                 }
                 // otherwise it's a datasource based one...
-                
-            
-            
-            
-            
+                // our 18N fields are a bit odd here...
+                if (preg_match('/i18n/i', $o->store->proxy->{'|url'})) {
+                    $f->name = isset($o->hiddenName) ? $o->hiddenName : $o->name; 
+                    $f->type = 'VARCHAR';
+                    $f->size = 8;
+                    $f->default = "''";
+                    $this->cols[] = $f;
+                    break;
+                }
+                $f->name = isset($o->hiddenName) ? $o->hiddenName : $o->name; 
+                $f->type = 'INT';
+                $f->size = 11;
+                $f->extra = "NOT NULL";
+                $f->default = 0;
+                $this->cols[] = $f;
+                break;
             
+            case 'TextArea':
+                $f->name = $o->name;
+                $f->type = 'TEXT';
+                
+                $this->cols[] = $f;
+                break;
             
             case 'DateField':
             case 'NumberField':
@@ -127,20 +177,83 @@ class Pman_Builder_FormToSQL extends Pman {
                 $f->type = 'INT';
                 $f->size = 11;
                 if ($o->name == 'id') {
-                    $f->extra = "AUTO_INCREMENT PRIMARY KEY";
+                    $f->extra = "NOT NULL AUTO_INCREMENT ";
+                    $this->primary_key = $o->name;
                 } else {
                     $f->default = 0;
                 }
-                $this->cols[] = $f;
+                
+                array_unshift($this->cols, $f); 
                 break;
             default:
-                continue;
+                break;
             
         }
+         
         
+    }
+    function toSQL($tn)
+    {
         
+        $out = "CREATE TABLE  $tn (\n";
         
+        foreach($this->cols as $i=> $f) {
+            $out .= $i ? ",\n"  : "";
+            
+            $row = '';
+            $sz = $f->type ;
+            if (!empty($f->size)) {
+                $sz .= "(". $f->size.")";
+            }
+            $row .= "    ".str_pad($sz, 20);
+            if (!empty($f->extra)) {
+                $row .= ' ' . $f->extra;
+            }
+            if (isset($f->default)) {
+                $row .= " DEFAULT ". $f->default;
+            }
+            $this->cols[$i]->def = $row;
+            $out.=  "    ".str_pad($f->name, 30) . $row;
+        }
+        if ($this->primary_key) {
+            $out .= ",\n    PRIMARY KEY (". $this->primary_key . ")";
+        }
         
+        $out .= "\n);";
+        echo $out;
     }
     
+    function toDO($b,$tn)
+    {
+        $utn = ucfirst($tn);
+        $out = '<?php
+/**
+ * Table Definition for builder
+ */
+require_once \'DB/DataObject.php\';
+
+';
+        $out.="class Pman_{$b}_DataObjects_$utn extends DB_DataObject 
+{
+    ###START_AUTOCODE
+    /* the code below is auto generated do not remove the above tag */
+
+    public \$__table = '$tn';                         // table name
+";
+        foreach($this->cols as $f) {
+            $out .= '    public $' . str_pad($f->name.';',30 ). '// ' . $f->def . "\n";
+                
+       }
+       $out.="
+    
+    /* the code above is auto generated do not remove the tag below */
+    ###END_AUTOCODE
+        
+        
+}";
+        echo "\n\n";
+        echo $out;
+         echo "\n\n";
+         return $out;
+    }
 }
\ No newline at end of file