5 * Supply the palete of elements..
7 * Things it can supply:
8 * -> Basic Element information? -
9 * -- eg. our standard widget set (with all the options?) - grabed from our doc-code.?
11 * -> Database Tables / Fields
12 * == start off with just the table..
18 require_once 'Pman.php';
20 class Pman_Builder_Elements extends Pman
23 parent::getAuth(); // load company!
24 $au = $this->getAuthUser();
26 $this->jerr("Not authenticated", array('authFailure' => true));
28 $this->authUser = $au;
32 function get($args, $opts = array())
34 if (empty($_REQUEST['table'])) {
35 $this->getTableList();
37 $tb = $_REQUEST['table'];
39 if ($tb == '*grids') {
40 $this->getGridElements();
43 $this->readElement(substr($tb,1));
45 $this->getTableFormElements($tb);
49 function getTableList() // fro pulldown list
51 $d = DB_DataObject::factory('Person');
52 $db = $d->getDatabaseConnection();
55 $tables = $db->getListOf('tables');
57 array('name' => 'Layout Elements', 'table' => '*Layout'),
58 array('name' => 'Form Elements', 'table' => '*Form'),
59 array('name' => 'Grid Elements', 'table' => '*Grid'),
60 array('name' => 'Grids (from database)', 'table' => '*grids')
62 foreach($tables as $t) {
63 $ret[] = array('name' => 'Table: ' . $t, 'table' => $t);
65 //echo'<PRE>';print_r($tables);
68 //$__DB->tableInfo($quotedTable);
71 function getTableFormElements($tb)
73 $d = DB_DataObject::factory('Person');
74 $db = $d->getDatabaseConnection();
76 $def= $db->tableInfo($tb);
77 if (is_object($def)) {
78 $this->jerr($def->toString());
84 'name' => 'Form: '. $tb,
85 'description' => "A Form for $tb",
89 'style' => 'margin: 5px',
90 '|url' => "baseURL + '/Roo/" . ucfirst($tb) . ".php'",
93 '|actionfailed' => 'function (_self, action)
95 _this.dialog.el.unmask();
96 Pman.standardActionFailed(_self, action);
99 '|actioncomplete' => "function (_self, action)
102 if (action.type =='submit') {
104 _this.dialog.el.unmask();
107 if (_this.callback) {
108 _this.callback.call(_this, _this.form.getValues());
115 '|rendered' => 'function (_self)
127 foreach($def as $r) {
128 $el = $this->rowToFormElement((object)$r,$tb);
132 // next.. cros table detection!?!?!?
133 if ( $el['xtype'] == 'NumberField') {
134 $el = $this->linkedToFormElement($r['name'], $tb, $el);
138 ['Forms','Row', 'A Row of form elements',
145 $fn = $el['*fullname'];
147 unset($el['*fullname']);
148 $title = isset($el['fieldLabel']) ?$el['fieldLabel'] : $fn;
149 if (isset($el['*title'])) {
150 $title = $el['*title'];
151 unset($el['*title']);
156 'description' => $fn,
160 $ret[0]['cfg']['items'][] = $el;
168 function rowToFormElement($t,$tab)
171 $el['name'] = $t->name;
172 $el['*fullname'] = $tab.'.' . $t->name; /// ???
173 $fl = preg_replace('/_/', ' ', $t->name);
174 $fl = preg_replace('/ id$/', ' ', $fl);
175 $el['fieldLabel'] = ucfirst($fl);
179 switch (strtoupper($t->type)) {
182 case 'INT2': // postgres
183 case 'INT4': // postgres
184 case 'INT8': // postgres
185 case 'SERIAL4': // postgres
186 case 'SERIAL8': // postgres
192 if ($t->name == 'id') {
193 $el['*title'] = 'Hidden: id';
194 $el['xtype'] = 'Hidden';
195 unset($el['fieldLabel']);
199 $el['xtype'] = 'NumberField';
200 $el['allowDecimals'] = false;
207 case 'DOUBLE PRECISION': // double precision (firebird)
209 case 'FLOAT4': // real (postgres)
210 case 'FLOAT8': // double precision (postgres)
212 case 'MONEY': // mssql and maybe others
214 case 'NUMBER': // oci8
215 $el['xtype'] = 'NumberField';
220 $el['allowDecimals'] = false;
221 $el['xtype'] = 'NumberField'; // YeaR?
228 $el['xtype'] = 'CheckBox'; // fix - I think I've updated the api..
240 case 'SET': // not really but oh well
242 case 'POINT': // mysql geometry stuff - not really string - but will do..
244 case 'TIMESTAMPTZ': // postgres
245 case 'BPCHAR': // postgres
246 case 'INTERVAL': // postgres (eg. '12 days')
248 case 'CIDR': // postgres IP net spec
249 case 'INET': // postgres IP
250 case 'MACADDR': // postgress network Mac address.
252 case 'INTEGER[]': // postgres type
253 case 'BOOLEAN[]': // postgres type
255 $el['width'] = isset($t->len) ? max(50, min(floor($t->len * 10), 200)) : 200;
256 $el['xtype'] = 'TextField';
264 $el['xtype'] = 'TextArea'; // or HtmlEditor
271 $el['xtype'] = 'DateField';
272 $el['altFormats'] = 'Y-m-d|d/m/Y';
273 $el['format'] = 'd/m/Y';
274 $el['hiddenFormat'] = 'Y-m-d'; // not supported ATM
279 $el['xtype'] = 'TextField'; // time field.?
284 $el['xtype'] = 'TextField';
285 $el['readOnly'] = 'true';
288 case 'TIMESTAMP': // do other databases use this???
289 $el['xtype'] = 'TextField';
290 $el['readOnly'] = 'true';
294 case 'BLOB': /// these should really be ignored!!!???
299 case 'CLOB': // oracle character lob support
301 case 'BYTEA': // postgres blob support..
303 $el['xtype'] = 'TextArea'; // or HtmlEditor
316 function readElement($fn)
319 $fn = preg_replace('/[^a-z]+i/', '',$fn);
320 $f = dirname(__FILE__)."/Elements/$fn.js";
321 if (!file_exists($f)) {
322 $this->jerr("no such file");
328 for ($i = 0; $i< count($lines); $i++) {
330 if ($atStart && trim($l) != '[') {
335 if (!preg_match('/function/', $l)) {
339 // got a function def...
341 $pad = substr($l,0, strpos($l, $lt)) .'}';
342 list($k, $func) = explode(':', $l);
344 while ($i < count($lines)) {
348 if ($pad == substr($l, 0, strlen($pad))) {
350 $rem = substr($l, strlen($pad)); //left over..
356 $ret[] = $k .' : ' . json_encode($func."\n") . $rem;
361 require_once 'Services/JSON.php';
362 $j = new Services_JSON();
365 $json= $j->decode(implode("\n", $ret));
367 // echo '<PRE>';print_r($json);exit;
380 static $conf = false;
384 $ff = HTML_FlexyFramework::get();
385 $top = $ff->rootDir.'/Pman/';
388 $conf = array('database__render'=>array());
389 foreach(scandir($top) as $m) {
390 $vf = $top .'/'.$m.'/DataObjects/pman.links.ini';
391 if (!strlen($m) || $m[0] == '.' || !file_exists($vf) ) {
394 $c = parse_ini_file($vf,true);
395 if (isset($c['database__render'])) {
396 $conf['database__render'] = array_merge($conf['database__render'], $c['database__render']);
397 unset($c['database__render']);
399 $conf = array_merge($conf , $c);
405 function linkedToFormElement($r, $tb, $el) // creates combos for for elements.
408 $conf = $this->links();
410 if (!is_array($conf) || !isset($conf[$tb]) || !isset($conf[$tb][$r])) {
413 list($mtb, $mr) = explode(':', $conf[$tb][$r]);
414 // work out the display field.
415 if (!isset($conf['database__render'][$mtb])) {
418 $df = $conf['database__render'][$mtb];
422 '*title'=> 'Combo: '. $el['fieldLabel'],
423 '*fullname' => $el['*fullname'],
424 'fieldLabel' => $el['fieldLabel'],
425 'name' => $el['name'] . '_' . $df,
426 'hiddenName' => $el['name'],
428 'qtip' => 'Select ' . $el['fieldLabel'],
429 'emptyText' => 'Select ' . $el['fieldLabel'],
431 'xtype' => 'ComboBox',
432 'selectOnFocus' => true,
434 'allowBlank' => true,
442 'xtype'=> 'HttpProxy',
443 '|url'=> "baseURL + '/Roo/" . $mtb.".php'",
446 //'listeners' =>storeListeners,
447 '|reader'=> 'Pman.Readers.'.$mtb,
450 'displayField'=> $df,
454 'forceSelection'=> true,
456 'triggerAction'=> 'all',
458 'tpl'=> '<div class="x-grid-cell-text x-btn button">'.
463 'queryParam'=> 'query['.$df.']',
464 'loadingText'=> "Searching...",
474 'items' => array( array(
477 'xtype'=> 'HttpProxy',
478 '|url'=> "baseURL + '/Roo/" . ucfirst($mtb).".php'",
482 //'listeners' =>storeListeners,
483 '|reader'=> 'Pman.Readers.'.ucfirst($mtb),
490 // 'select' => function(cb, rec, ix) {
491 // cb.lastData = rec.data;
507 function getGridElements()
509 $d = DB_DataObject::factory('Person');
510 $db = $d->getDatabaseConnection();
513 $tables = $db->getListOf('tables');
514 foreach($tables as $t) {
515 $ret[] = $this->tableToGrid($t);
523 function tableToGrid($t)
525 $d = DB_DataObject::factory('Person');
526 $this->db = $d->getDatabaseConnection();
528 $colmodel = $this->colModel($t);
529 $aec = isset($colmodel[0]['dataIndex']) ? $colmodel[0]['dataIndex'] : '';
530 $rawReader = $this->readerRaw($t); // usefull if no reader will be generated.
536 'description' => "Grid for table . $t",
538 'xtype'=>'GridPanel',
540 'fitToframe' => true,
541 'fitContainer' => true,
543 'background' => true,
544 'listeners' => array(
545 '|activate' => "function() {
548 _this.grid.footer.onClick('first');
556 'autoExpandColumn' => $aec,
558 'listeners' => array(
559 '|render' => 'function() {
561 //_this.dialog = Pman.Dialog.FILL_IN
562 if (_this.panel.active) {
563 this.footer.onClick(\'first\');
569 '*prop' => 'dataSource',
571 'items' => array( array(
574 'xtype'=> 'HttpProxy',
576 '|url'=> "baseURL + '/Roo/$t.php'"
578 '|reader'=> "Pman.Readers." . ucfirst($t),
579 '|readerRaw'=> $rawReader,
582 '*prop' => 'colModel',
584 'items' => $colmodel,
591 'xtype' => 'PagingToolbar',
593 'displayInfo' => true,
594 'displayMsg' => "Displaying $t {0} - {1} of {2}",
595 'emptyMsg' => "No $t found"
599 '*prop' => 'toolbar',
600 'xtype' => 'Toolbar',
605 'cls' => 'x-btn-text-icon',
606 '|icon' => "Roo.rootURL + 'images/default/dd/drop-add.gif'",
607 'listeners' => array(
608 '|click' => 'function()
610 _this.dialog.show( { id : 0 }, function() {
611 _this.grid.footer.onClick(\'first\');
622 'cls' => 'x-btn-text-icon',
623 '|icon' => "Roo.rootURL + 'images/default/tree/leaf.gif'",
625 'listeners' => array(
626 '|click' => 'function()
628 var s = _this.grid.getSelectionModel().getSelections();
629 if (!s.length || (s.length > 1)) {
630 Roo.MessageBox.alert("Error", s.length ? "Select only one Row" : "Select a Row");
634 _this.dialog.show(s[0].data, function() {
635 _this.grid.footer.onClick(\'first\');
644 'cls' => 'x-btn-text-icon',
645 '|icon' => "rootURL + '/Pman/templates/images/trash.gif'",
647 'listeners' => array(
648 '|click' => 'function()
650 Pman.genericDelete(_this, _this.grid.tableName);
668 function colModel($tb)
670 // just ouput stuff...
672 $def= $this->db->tableInfo($tb);
674 $conf = $this->links();
675 foreach($def as $r) {
677 if ($r['name'] == 'id') {
680 $fl = preg_replace('/_/', ' ', $r['name']);
681 $fl = preg_replace('/ id$/', ' ', $fl);
684 'xtype'=> '*ColumnModel',
685 'header'=> ucfirst($fl),
688 '|renderer' => "function(v) { return String.format('{0}', v); }",
693 if (!is_array($conf) || !isset($conf[$tb]) || !isset($conf[$tb][$rn])) {
697 list($mtb, $mr) = explode(':', $conf[$tb][$rn]);
698 // work out the display field.
699 if (!isset($conf['database__render'][$mtb])) {
705 $add['dataIndex'] = $rn.'_'. $conf['database__render'][$mtb];
711 function readerRaw($tb)
713 // just ouput stuff...
714 require_once 'Pman/Builder/Generator.php';
715 require_once 'Pman/Builder/Generator/JSON.php';
717 $x = DB_DataObject::factory('Builder');
718 $basedef = $x->getDatabaseConnection()->tableInfo($tb);
719 $def = Pman_Builder_Generator::tableToData($tb, $basedef);
720 require_once 'Pman/Builder/Generator/JSON.php';
721 $j = new Pman_Builder_Generator_JSON(array('crlf' => "\n", 'tab' => ' '));
722 $j2 = new Pman_Builder_Generator_JSON(array('crlf' => '', 'tab' => ''));
724 $x = 'new Roo.data.JsonReader(' . $j->encodeUnsafe($def['args']);
725 $ret = trim(substr($x, 0, -1)) . ",\n"; // strip of trailing ;};
726 $ret .= $j->tab . "fields : [\n". $j->tab.$j->tab;
728 foreach($def['readers'] as $xr) {
729 $ar[] = $j2->encodeUnsafe($xr);
731 $ret .= implode(",\n". $j->tab.$j->tab, $ar);
732 $ret .= "\n". $j->tab . "]\n})\n";