FormToSQL.php
[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()
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         $this->toSQL($tn);
33         
34         die("DONE");
35     }
36     
37     function walk($o) 
38     {
39         $this->flattenProps($o);
40         $this->parse($o);
41         
42         
43         foreach((array)$o as $k=>$v) {
44             if (is_array($v)) {
45                 foreach($v as $oo) {
46                     $this->walk($oo);
47                 }
48                 
49                 continue;
50             }
51             if (is_object($v)) {
52                 $this->walk($v);
53                 continue;
54             }
55         }
56         
57     }
58     
59     function flattenProps($o) {
60         if (empty($o->items)) {
61             return;
62         }
63         $items = array();
64         foreach($o->items as $oo) {
65             if (!isset($oo->{'*prop'})) {
66                 $items[] = $oo;
67                 continue;
68             }
69             $this->flattenProps($oo);
70             $o->{$oo->{'*prop'}} = $oo;
71             
72         }
73         $o->items = $items;
74     }
75     
76     function parse($o) 
77     {
78         require_once 'Services/JSON.php';
79         $s = new Services_JSON();
80         if (empty($o->xtype) || empty($o->{'|xns'})) {
81             return;
82         }
83         if ($o->{'|xns'} != 'Roo.form') {
84             return;
85         }
86         $f = new StdClass;
87         switch ($o->xtype) {
88             case 'TextField':
89                 $f->name = $o->name;
90                 $f->type = 'VARCHAR';
91                 $f->default = "''";
92                 $f->size = min(255,max(8, pow(2, strlen(decbin(($o->width/2)-1)))));
93                 $this->cols[] = $f;
94                 break;
95             
96             case 'ComboBox':
97               //  print_r($o);exit;
98                 if ($o->store->xtype == 'SimpleStore') {
99                     //print_R($o);exit;
100                     
101                     $data = $s->decode($o->store->{'|data'}); 
102                     
103                     $type = 'INT';
104                     $len = 0;
105                     foreach($data as $row) {
106                         if (is_numeric($row[0])) {
107                             continue;
108                         }
109                         $type = 'VARCHAR';
110                         $len = strlen($row[0]);
111                           
112                     }
113                     if ($type == 'VARCHAR') {
114                         $len = min(255,max(8, pow(2, strlen(decbin(($len))))));
115                         $f->default = "''";
116                     } else {
117                         $len = 11;
118                         $f->default = 0;
119                     }
120                     $f->name = $o->name; // hiddenname?
121                     $f->type = $type;
122                     $f->size = $len;
123                     $this->cols[] = $f;
124                     continue;
125                 }
126                 // otherwise it's a datasource based one...
127                 // our 18N fields are a bit odd here...
128                 if (preg_match('/i18n/i', $o->store->proxy->{'|url'})) {
129                     $f->name = isset($o->hiddenName) ? $o->hiddenName : $o->name; 
130                     $f->type = 'VARCHAR';
131                     $f->size = 8;
132                     $f->default = "''";
133                     $this->cols[] = $f;
134                     continue;
135                 }
136                 $f->name = isset($o->hiddenName) ? $o->hiddenName : $o->name; 
137                 $f->type = 'INT';
138                 $f->size = 11;
139                 $f->extra = "NOT NULL";
140                 $f->default = 0;
141                 $this->cols[] = $f;
142                 continue;
143             
144             case 'TextArea':
145                 $f->name = $o->name;
146                 $f->type = 'TEXT';
147                 
148                 $this->cols[] = $f;
149                 continue;
150             
151             case 'DateField':
152             case 'NumberField':
153                 echo 'FIXME';
154                 print_r($o);exit;
155                 
156             
157             case 'Hidden':
158                 $f->name = $o->name;
159                 $f->type = 'INT';
160                 $f->size = 11;
161                 if ($o->name == 'id') {
162                     $f->extra = "NOT NULL AUTO_INCREMENT ";
163                     $this->primary_key = $o->name;
164                 } else {
165                     $f->default = 0;
166                 }
167                 
168                 array_unshift($this->cols, $f); 
169                 break;
170             default:
171                 continue;
172             
173         }
174          
175         
176     }
177     function toSQL($tn)
178     {
179         
180         $out = "CREATE TABLE  $tn (\n";
181         
182         foreach($this->cols as $i=> $f) {
183             $out .= $i ? ",\n"  : "";
184             
185             $out .= "    {$f->name} {$f->type}";
186             if (!empty($f->size)) {
187                 $out .= "(". $f->size.")";
188             }
189             if (!empty($f->extra)) {
190                 $out .= ' ' . $f->extra;
191             }
192             if (isset($f->default)) {
193                 $out .= " DEFAULT ". $f->default;
194             }
195             
196         }
197         $out .= "\n);";
198         echo $out;
199     }
200     
201     
202 }