php7 fixes
[Pman.Builder] / FormToSQL.php
index 731bbb9..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");
     }
     
@@ -82,6 +106,7 @@ 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;
@@ -114,7 +139,7 @@ class Pman_Builder_FormToSQL extends Pman {
                     $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...
@@ -124,17 +149,22 @@ class Pman_Builder_FormToSQL extends Pman {
                     $f->size = 8;
                     $f->default = "''";
                     $this->cols[] = $f;
-                    continue;
+                    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;
-                continue;
-            
-            
+                break;
             
+            case 'TextArea':
+                $f->name = $o->name;
+                $f->type = 'TEXT';
+                
+                $this->cols[] = $f;
+                break;
             
             case 'DateField':
             case 'NumberField':
@@ -147,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