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) {
17 function get($base, $opts = array())
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);
32 if (!empty($_SERVER['argv'][3])) {
33 $tn= $_SERVER['argv'][3];
38 $b= basename(dirname($file));
40 $do = $this->toDO($b, $tn);
42 $dofile = dirname($file).'/DataObjects/'. ucfirst($tn).'.php';
43 if (!file_exists($dofile)) {
44 echo "WRITING $dofile\n";
45 file_put_contents($dofile, $do);
47 // should support AUTOCODE...
48 echo "DELETE $dofile IF YOU WANT TO RECREATED IT..\n";
56 $this->flattenProps($o);
60 foreach((array)$o as $k=>$v) {
76 function flattenProps($o) {
77 if (empty($o->items)) {
81 foreach($o->items as $oo) {
82 if (!isset($oo->{'*prop'})) {
86 $this->flattenProps($oo);
87 $o->{$oo->{'*prop'}} = $oo;
95 require_once 'Services/JSON.php';
96 $s = new Services_JSON();
97 if (empty($o->xtype) || empty($o->{'|xns'})) {
100 if ($o->{'|xns'} != 'Roo.form') {
107 $f->type = 'VARCHAR';
109 $f->extra = "NOT NULL";
110 $f->size = min(255,max(8, pow(2, strlen(decbin(($o->width/2)-1)))));
116 if ($o->store->xtype == 'SimpleStore') {
119 $data = $s->decode($o->store->{'|data'});
123 foreach($data as $row) {
124 if (is_numeric($row[0])) {
128 $len = strlen($row[0]);
131 if ($type == 'VARCHAR') {
132 $len = min(255,max(8, pow(2, strlen(decbin(($len))))));
138 $f->name = $o->name; // hiddenname?
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';
154 $f->name = isset($o->hiddenName) ? $o->hiddenName : $o->name;
157 $f->extra = "NOT NULL";
179 if ($o->name == 'id') {
180 $f->extra = "NOT NULL AUTO_INCREMENT ";
181 $this->primary_key = $o->name;
186 array_unshift($this->cols, $f);
198 $out = "CREATE TABLE $tn (\n";
200 foreach($this->cols as $i=> $f) {
201 $out .= $i ? ",\n" : "";
205 if (!empty($f->size)) {
206 $sz .= "(". $f->size.")";
208 $row .= " ".str_pad($sz, 20);
209 if (!empty($f->extra)) {
210 $row .= ' ' . $f->extra;
212 if (isset($f->default)) {
213 $row .= " DEFAULT ". $f->default;
215 $this->cols[$i]->def = $row;
216 $out.= " ".str_pad($f->name, 30) . $row;
218 if ($this->primary_key) {
219 $out .= ",\n PRIMARY KEY (". $this->primary_key . ")";
226 function toDO($b,$tn)
231 * Table Definition for builder
233 require_once \'DB/DataObject.php\';
236 $out.="class Pman_{$b}_DataObjects_$utn extends DB_DataObject
239 /* the code below is auto generated do not remove the above tag */
241 public \$__table = '$tn'; // table name
243 foreach($this->cols as $f) {
244 $out .= ' public $' . str_pad($f->name.';',30 ). '// ' . $f->def . "\n";
249 /* the code above is auto generated do not remove the tag below */