php7 fixes
[Pman.Builder] / FormToSQL.php
1 <?php
2
3 // quick way to build SQL based on a form design..
4 // might have uses later...
5
6 require_once 'Pman.php';
7
8 class Pman_Builder_FormToSQL extends Pman {
9     
10     function getAuth(){
11         if (!HTML_FlexyFramework::get()->cli) {
12             die("not cli");
13         }
14         return true;
15     }
16     
17     function get($base, $opts = array())
18     {
19         //print_R($_SERVER['argv']);exit;
20         $file  = $_SERVER['argv'][2];
21         if (!file_exists($file)) {
22             die("file $file does not exist");
23            }
24         $o = json_decode(file_get_contents($file));
25         $this->walk($o);
26         //print_R($this->cols);
27         
28         $b = explode('.', basename($file));
29         array_pop($b);
30         $tn = strtolower(preg_replace('/([A-Z])/','_$1', array_pop($b)));
31         $tn = preg_replace('/^_+/', '', $tn);
32         if (!empty($_SERVER['argv'][3])) {
33             $tn= $_SERVER['argv'][3];
34         }
35         
36         
37         $this->toSQL($tn);
38         $b= basename(dirname($file));
39         
40         $do = $this->toDO($b, $tn);
41         
42         $dofile = dirname($file).'/DataObjects/'. ucfirst($tn).'.php';
43         if (!file_exists($dofile)) {
44             echo "WRITING  $dofile\n";
45             file_put_contents($dofile, $do);
46         } else {
47             // should support AUTOCODE...
48             echo "DELETE $dofile IF YOU WANT TO RECREATED IT..\n";
49         }
50         
51         die("DONE");
52     }
53     
54     function walk($o) 
55     {
56         $this->flattenProps($o);
57         $this->parse($o);
58         
59         
60         foreach((array)$o as $k=>$v) {
61             if (is_array($v)) {
62                 foreach($v as $oo) {
63                     $this->walk($oo);
64                 }
65                 
66                 continue;
67             }
68             if (is_object($v)) {
69                 $this->walk($v);
70                 continue;
71             }
72         }
73         
74     }
75     
76     function flattenProps($o) {
77         if (empty($o->items)) {
78             return;
79         }
80         $items = array();
81         foreach($o->items as $oo) {
82             if (!isset($oo->{'*prop'})) {
83                 $items[] = $oo;
84                 continue;
85             }
86             $this->flattenProps($oo);
87             $o->{$oo->{'*prop'}} = $oo;
88             
89         }
90         $o->items = $items;
91     }
92     
93     function parse($o) 
94     {
95         require_once 'Services/JSON.php';
96         $s = new Services_JSON();
97         if (empty($o->xtype) || empty($o->{'|xns'})) {
98             return;
99         }
100         if ($o->{'|xns'} != 'Roo.form') {
101             return;
102         }
103         $f = new StdClass;
104         switch ($o->xtype) {
105             case 'TextField':
106                 $f->name = $o->name;
107                 $f->type = 'VARCHAR';
108                 $f->default = "''";
109                  $f->extra = "NOT NULL";
110                 $f->size = min(255,max(8, pow(2, strlen(decbin(($o->width/2)-1)))));
111                 $this->cols[] = $f;
112                 break;
113             
114             case 'ComboBox':
115               //  print_r($o);exit;
116                 if ($o->store->xtype == 'SimpleStore') {
117                     //print_R($o);exit;
118                     
119                     $data = $s->decode($o->store->{'|data'}); 
120                     
121                     $type = 'INT';
122                     $len = 0;
123                     foreach($data as $row) {
124                         if (is_numeric($row[0])) {
125                             continue;
126                         }
127                         $type = 'VARCHAR';
128                         $len = strlen($row[0]);
129                           
130                     }
131                     if ($type == 'VARCHAR') {
132                         $len = min(255,max(8, pow(2, strlen(decbin(($len))))));
133                         $f->default = "''";
134                     } else {
135                         $len = 11;
136                         $f->default = 0;
137                     }
138                     $f->name = $o->name; // hiddenname?
139                     $f->type = $type;
140                     $f->size = $len;
141                     $this->cols[] = $f;
142                     break;
143                 }
144                 // otherwise it's a datasource based one...
145                 // our 18N fields are a bit odd here...
146                 if (preg_match('/i18n/i', $o->store->proxy->{'|url'})) {
147                     $f->name = isset($o->hiddenName) ? $o->hiddenName : $o->name; 
148                     $f->type = 'VARCHAR';
149                     $f->size = 8;
150                     $f->default = "''";
151                     $this->cols[] = $f;
152                     break;
153                 }
154                 $f->name = isset($o->hiddenName) ? $o->hiddenName : $o->name; 
155                 $f->type = 'INT';
156                 $f->size = 11;
157                 $f->extra = "NOT NULL";
158                 $f->default = 0;
159                 $this->cols[] = $f;
160                 break;
161             
162             case 'TextArea':
163                 $f->name = $o->name;
164                 $f->type = 'TEXT';
165                 
166                 $this->cols[] = $f;
167                 break;
168             
169             case 'DateField':
170             case 'NumberField':
171                 echo 'FIXME';
172                 print_r($o);exit;
173                 
174             
175             case 'Hidden':
176                 $f->name = $o->name;
177                 $f->type = 'INT';
178                 $f->size = 11;
179                 if ($o->name == 'id') {
180                     $f->extra = "NOT NULL AUTO_INCREMENT ";
181                     $this->primary_key = $o->name;
182                 } else {
183                     $f->default = 0;
184                 }
185                 
186                 array_unshift($this->cols, $f); 
187                 break;
188             default:
189                 break;
190             
191         }
192          
193         
194     }
195     function toSQL($tn)
196     {
197         
198         $out = "CREATE TABLE  $tn (\n";
199         
200         foreach($this->cols as $i=> $f) {
201             $out .= $i ? ",\n"  : "";
202             
203             $row = '';
204             $sz = $f->type ;
205             if (!empty($f->size)) {
206                 $sz .= "(". $f->size.")";
207             }
208             $row .= "    ".str_pad($sz, 20);
209             if (!empty($f->extra)) {
210                 $row .= ' ' . $f->extra;
211             }
212             if (isset($f->default)) {
213                 $row .= " DEFAULT ". $f->default;
214             }
215             $this->cols[$i]->def = $row;
216             $out.=  "    ".str_pad($f->name, 30) . $row;
217         }
218         if ($this->primary_key) {
219             $out .= ",\n    PRIMARY KEY (". $this->primary_key . ")";
220         }
221         
222         $out .= "\n);";
223         echo $out;
224     }
225     
226     function toDO($b,$tn)
227     {
228         $utn = ucfirst($tn);
229         $out = '<?php
230 /**
231  * Table Definition for builder
232  */
233 require_once \'DB/DataObject.php\';
234
235 ';
236         $out.="class Pman_{$b}_DataObjects_$utn extends DB_DataObject 
237 {
238     ###START_AUTOCODE
239     /* the code below is auto generated do not remove the above tag */
240
241     public \$__table = '$tn';                         // table name
242 ";
243         foreach($this->cols as $f) {
244             $out .= '    public $' . str_pad($f->name.';',30 ). '// ' . $f->def . "\n";
245                 
246        }
247        $out.="
248     
249     /* the code above is auto generated do not remove the tag below */
250     ###END_AUTOCODE
251         
252         
253 }";
254         echo "\n\n";
255         echo $out;
256          echo "\n\n";
257          return $out;
258     }
259 }