php7 fixes
[Pman.Builder] / FormToSQL.php
index c888ac9..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,13 +23,37 @@ 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");
     }
     
     function walk($o) 
     {
-        
+        $this->flattenProps($o);
         $this->parse($o);
         
         
@@ -48,9 +72,28 @@ class Pman_Builder_FormToSQL extends Pman {
         }
         
     }
+    
+    function flattenProps($o) {
+        if (empty($o->items)) {
+            return;
+        }
+        $items = array();
+        foreach($o->items as $oo) {
+            if (!isset($oo->{'*prop'})) {
+                $items[] = $oo;
+                continue;
+            }
+            $this->flattenProps($oo);
+            $o->{$oo->{'*prop'}} = $oo;
+            
+        }
+        $o->items = $items;
+    }
+    
     function parse($o) 
     {
-        
+        require_once 'Services/JSON.php';
+        $s = new Services_JSON();
         if (empty($o->xtype) || empty($o->{'|xns'})) {
             return;
         }
@@ -62,19 +105,155 @@ class Pman_Builder_FormToSQL extends Pman {
             case 'TextField':
                 $f->name = $o->name;
                 $f->type = 'VARCHAR';
-                var_dump(pow(2, strlen(decbin($o->width-1))));
+                $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') {
+                    //print_R($o);exit;
+                    
+                    $data = $s->decode($o->store->{'|data'}); 
+                    
+                    $type = 'INT';
+                    $len = 0;
+                    foreach($data as $row) {
+                        if (is_numeric($row[0])) {
+                            continue;
+                        }
+                        $type = 'VARCHAR';
+                        $len = strlen($row[0]);
+                          
+                    }
+                    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;
+                    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':
+                echo 'FIXME';
+                print_r($o);exit;
+                
+            
+            case 'Hidden':
+                $f->name = $o->name;
+                $f->type = 'INT';
+                $f->size = 11;
+                if ($o->name == 'id') {
+                    $f->extra = "NOT NULL AUTO_INCREMENT ";
+                    $this->primary_key = $o->name;
+                } else {
+                    $f->default = 0;
+                }
+                
+                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