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         die("DONE");
28     }
29     
30     function walk($o) 
31     {
32         $this->flattenProps($o);
33         $this->parse($o);
34         
35         
36         foreach((array)$o as $k=>$v) {
37             if (is_array($v)) {
38                 foreach($v as $oo) {
39                     $this->walk($oo);
40                 }
41                 
42                 continue;
43             }
44             if (is_object($v)) {
45                 $this->walk($v);
46                 continue;
47             }
48         }
49         
50     }
51     
52     function flattenProps($o) {
53         if (empty($o->items)) {
54             return;
55         }
56         $items = array();
57         foreach($o->items as $oo) {
58             if (!isset($oo->{'*props'})) {
59                 $items[] = $oo;
60                 continue;
61             }
62             $o->{$oo->{'*props'}} = $oo;
63             
64         }
65         $o->items = $items;
66     }
67     
68     function parse($o) 
69     {
70         
71         if (empty($o->xtype) || empty($o->{'|xns'})) {
72             return;
73         }
74         if ($o->{'|xns'} != 'Roo.form') {
75             return;
76         }
77         $f = new StdClass;
78         switch ($o->xtype) {
79             case 'TextField':
80                 $f->name = $o->name;
81                 $f->type = 'VARCHAR';
82                 $f->default = "''";
83                 $f->size = min(255,max(8, pow(2, strlen(decbin(($o->width/2)-1)))));
84                 $this->cols[] = $f;
85                 break;
86             
87             case 'ComboBox':
88                 if ($o->store->xtype == 'SimpleStore') {
89                     $data = json_decode($o->store->{'|data'}); 
90                     $type = 'INT';
91                     $len = 0;
92                     foreach($data as $row) {
93                         if (is_numeric($row[0])) {
94                             continue;
95                         }
96                         $type = 'VARCHAR';
97                         $len = strlen($row[0]);
98                           
99                     }
100                     if ($type == 'VARCHAR') {
101                         $len = min(255,max(8, pow(2, strlen(decbin(($len))))));
102                     } else {
103                         $len = 11;
104                     }
105                     $f->name = $o->name; // hiddenname?
106                     $f->type = $type;
107                     $f->size = $len;
108                     
109                 }
110             
111             
112             
113             case 'DateField':
114             case 'NumberField':
115                 echo 'FIXME';
116                 print_r($o);exit;
117                 
118             
119             case 'Hidden':
120                 $f->name = $o->name;
121                 $f->type = 'INT';
122                 $f->size = 11;
123                 if ($o->name == 'id') {
124                     $f->extra = "AUTO_INCREMENT PRIMARY KEY";
125                 } else {
126                     $f->default = 0;
127                 }
128                 $this->cols[] = $f;
129                 break;
130             default:
131                 continue;
132             
133         }
134         
135         
136         
137         
138     }
139     
140 }