3 // quick way to build SQL based on a form design..
4 // might have uses later...
6 require_once 'Pman.php';
8 class Pman_Builder_FormToSQL extends Pman {
11 if (!HTML_FlexyFramework::get()->cli) {
19 //print_R($_SERVER['argv']);exit;
20 $file = $_SERVER['argv'][2];
21 if (!file_exists($file)) {
22 die("file $file does not exist");
24 $o = json_decode(file_get_contents($file));
26 //print_R($this->cols);
28 $b = explode('.', basename($file));
30 $tn = strtolower(preg_replace('/([A-Z])/','$1_', array_pop($b)));
31 $tn = preg_replace('/^_+/', '', $tn);
39 $this->flattenProps($o);
43 foreach((array)$o as $k=>$v) {
59 function flattenProps($o) {
60 if (empty($o->items)) {
64 foreach($o->items as $oo) {
65 if (!isset($oo->{'*prop'})) {
69 $this->flattenProps($oo);
70 $o->{$oo->{'*prop'}} = $oo;
78 require_once 'Services/JSON.php';
79 $s = new Services_JSON();
80 if (empty($o->xtype) || empty($o->{'|xns'})) {
83 if ($o->{'|xns'} != 'Roo.form') {
92 $f->size = min(255,max(8, pow(2, strlen(decbin(($o->width/2)-1)))));
98 if ($o->store->xtype == 'SimpleStore') {
101 $data = $s->decode($o->store->{'|data'});
105 foreach($data as $row) {
106 if (is_numeric($row[0])) {
110 $len = strlen($row[0]);
113 if ($type == 'VARCHAR') {
114 $len = min(255,max(8, pow(2, strlen(decbin(($len))))));
120 $f->name = $o->name; // hiddenname?
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';
136 $f->name = isset($o->hiddenName) ? $o->hiddenName : $o->name;
160 if ($o->name == 'id') {
161 $f->extra = "AUTO_INCREMENT PRIMARY KEY";
165 array_unshift($this->cols, $f);
177 $out = "CREATE TABLE $tn (\n";
179 foreach($this->cols as $i=> $f) {
180 $out .= $i ? ",\n" : "";
182 $out .= " {$f->name} {$f->type}";
183 if (!empty($f->size)) {
184 $out .= "(". $f->size.")";
186 if (!empty($f->extra)) {
187 $out .= ' ' . $f->extra;
189 if (isset($f->default)) {
190 $out .= " DEFAULT ". $f->default;