6 * Generate DataObjects... and readers in the right places..
8 * note - we write to a temporary directory first...
13 require_once 'DB/DataObject/Generator.php';
16 /** basic thing now works...
18 * it needs a bit more intelligence to work out what to do...
21 * Basically we need to build up all the formats for each db column
23 * - overlay any mapping stuff.
25 * - overlay user defined settings
26 * = write it out to file..
29 $this->def['order'][$table][] = $t->name;
30 $this->def['readers'][$table][$t->name] = $reader;
31 $this->def['colmodels'][$table][$t->name] = $colmodel;
32 $this->def['forms'][$table][$t->name] = $form;
35 * readersDef[table.col]
43 class Pman_Builder_Generator extends DB_DataObject_Generator
54 * [tablename] => array( list of columns ones with '-' indicate lookup
56 * [tablename][colname] -> reader foramt
58 * [tablename][colname] => xtype / name etc...
60 * [tablename] => data for reader args (eg. id / total prop etc.)
62 * [tablename] => array of cols with types
64 * [tablename] -> array of cols
70 var $page = false; // page container when run from cli.
72 // dont do usual stuff!!!
74 var $tablekeys = array();
76 var $overwrite = array(); // default dont overwrite any of the files..
77 // array('master', 'corejs', 'corephp', 'index', 'Roo')
78 // and ('js-?????' where ??? is the table name) <- for all the generated js classes.
79 // we always overwrite the definition!!!
80 // set to array('all') to overwrite everything!!!
82 function start($cli=false, $mods='', $overwrite='')
87 //echo '<PRE>'; print_r($this->modtables); exit;
89 $options = &PEAR::getStaticProperty('DB_DataObject','options');
92 $proj = 'pman'; //ucfirst(basename($options['database']));
93 // we are going to generate all of the code into a temporay foldler..
94 $options['rootDir'] = ini_get('session.save_path').'/temp_'. $proj;
95 $options['cli'] = $cli;
96 $options['mods'] = empty($mods) ? array() : explode('/',$mods);
98 if (!file_exists($options['rootDir'])) {
99 mkdir($options['rootDir'], 0775, true);
102 $this->rootDir = $options['rootDir'];
103 $options['schema_location'] = $this->rootDir .'/'.$proj.'/DataObjects';
104 $options['class_location'] = $this->rootDir .'/'.$proj.'/DataObjects';
105 $options['require_prefix'] = $proj . '/DataObjects/';
106 $options['class_prefix'] = $proj . '_DataObjects_';
107 // print_r($this);exit;
112 $standard_database = $options['database'];
125 $this->scanModules();
126 require_once 'System.php';
127 $diff = System::which('diff');
128 // now for each of the directories copy/show diffs..
129 echo $cli ? '' : '<PRE>';
130 $flist = explode(',', $overwrite);
131 foreach($this->modtables as $m=>$ar) {
132 if ($options['database'] != $standard_database) {
133 $options['database'] = $standard_database ;
138 $options['database'] = $standard_database ;
139 if (isset($options['database_'. $m])) {
140 $options['database'] = $options['database_'. $m];
141 //var_dump($url);exit;
148 if (!empty($options['mods'] ) && !in_array($m, $options['mods'] )) {
151 foreach(scandir($options['rootDir'].'/'.$m) as $f) {
152 if (!strlen($f) || $f[0] == '.') {
156 $src = $options['rootDir']."/$m/$f";
157 $tg = $this->page->rootDir."/Pman/$m/DataObjects/$f";
158 if (preg_match('/\.js$/', $f)) {
159 $tg = $this->page->rootDir."/Pman/$m/$f";
162 if (!file_exists($tg) || !filesize($tg) ) {
164 if ($cli && in_array($f, $flist)) {
165 echo "COPY $src $tg" . ($cli ? "\n" : "<BR>");
169 echo "!!!!MISSING!!! $tg" . ($cli ? "\n" : "<BR>");
173 // always copy readers and ini file.= nope - not on live..
174 if ($cli && in_array($f, $flist)) {
176 //|| $f=='pman.ini' || preg_match('/\.js$/', $f))) {
177 echo "COPY $src $tg". ($cli ? "\n" : "<BR>");
183 $cmd = "$diff -u -w ". escapeshellarg($tg) . ' ' . escapeshellarg($src);
185 $out = array(); $ret = 0;
186 exec($cmd, $out, $ret);
187 if ($ret ==0) { // files match..
191 echo "\n" .implode( "\n" , $out) . "\n";
208 $options = &PEAR::getStaticProperty('DB_DataObject','options');
210 $url = parse_url($options['database']);
211 // hide stuff for web..
212 $cli = $options['cli'];
214 $url['pass'] = '*****';
215 $url['user'] = '*****';
216 $url['host'] = '*****';
221 require_once 'System.php';
222 $cat = System::which('cat');
223 $mysql = System::which('mysql');
224 print_r($this->modsql);
225 foreach($this->modsql as $m => $fl)
227 if ($cli && isset($options['database_'. $m])) {
228 $url =parse_url($options['database_'.$m]);
231 $mysql_cmd = $mysql .
232 ' -h ' . $url['host'] .
233 ' -u' . escapeshellarg($url['user']) .
234 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
235 ' ' . basename($url['path']);
237 echo $mysql_cmd . "\n" ;
239 if (!empty($options['mods'] ) && !in_array($m, $options['mods'] )) {
243 $fn = $this->page->rootDir. "/Pman/$m/DataObjects/$f";
244 $cmd = $cat . ' ' . escapeshellarg($fn) . " | $mysql_cmd -f ";
245 echo $cmd. ($cli ? "\n" : "<BR>\n");
257 * Scan the folders for DataObjects
258 * - Use the list of php files in DataObjects folders
259 * to determine which module owns which database table.
264 function scanModules()
267 $options = &PEAR::getStaticProperty('DB_DataObject','options');
268 if (isset($options['modtables'])) {
269 $this->modtables = $options['modtables'];
270 $this->modmap = $options['modmap'];
271 $this->modsql = $options['modsql'];
275 $ff = HTML_Flexyframework::get();
277 $top = $ff->page->rootDir .'/Pman';
278 $this->modtables = array();
279 $this->modmap = array();
280 $this->modmapsql = array();
282 foreach(scandir($top) as $m) {
286 !is_dir($top .'/'.$m) ||
287 !file_exists($top .'/'.$m.'/DataObjects')
291 $this->modtables[$m] = array();
292 $this->modsql[$m] = array();
293 foreach(scandir($top .'/'.$m.'/DataObjects') as $f) {
294 if (!strlen($f) || $m[0] == '.') {
297 if (preg_match('/\.sql$/', $f)) {
298 $this->modsql[$m][] = $f;
301 if (preg_match('/\.php$/', $f)) {
302 $tn = strtolower(preg_replace('/\.php$/', '', $f));
303 $this->modtables[$m][] = $tn;
304 $this->modmap[$tn] = $m;
309 $options['modtables'] = $this->modtables;
310 $options['modmap'] = $this->modmap;
311 $options['modsql'] = $this->modsql;
312 // print_r($options);
317 * this is run first, so picks up any missing dataobject files..
320 function generateDefinitions()
322 if (!$this->tables) {
323 $this->debug("-- NO TABLES -- \n");
326 if (!isset($this->modmap)) {
327 $this->scanModules();
329 $options = &PEAR::getStaticProperty('DB_DataObject','options');
330 $mods = $options['mods'];
332 $this->_newConfig = '';
333 foreach($this->tables as $this->table) {
335 $tn = strtolower($this->table);
336 //print_r($this->modmap);//[$tn]);//
337 if (!isset($this->modmap[$tn])) {
338 die("No existing DataObject file found for table {$this->table} \n".
339 "- create an empty file in the related Module/DataObjects directory
341 touch Pman/????/DataObjects/".ucfirst($this->table).".php
345 $mod = $this->modmap[$tn];
346 $inis[$mod] = isset($inis[$mod]) ? $inis[$mod] : '';
349 $this->_newConfig = '';
350 $this->_generateDefinitionsTable();
353 $inis[$mod] .= $this->_newConfig;
356 //echo '<PRE>';print_r($this->_inis); exit;
357 $options = PEAR::getStaticProperty('DB_DataObject','options');
359 $rd = $options['rootDir'];
360 foreach($inis as $m=>$ini) {
361 if (!empty($mods) && !in_array($m, $mods)) {
365 if (!file_exists($rd.'/'.$m)) {
366 mkdir($rd.'/'.$m, 0775, true);
368 $fname = '/pman.ini';
369 if (isset($options['database_'. $m])) {
370 $url = parse_url($options['database_'.$m]);
371 $fname = '/'. basename($url['path']).'.ini';
375 file_put_contents($rd.'/'.$m.$fname, $ini);
381 function generateClasses()
383 // print_R($this->modmap);
384 // die("generateClasses");
385 $options = &PEAR::getStaticProperty('DB_DataObject','options');
386 $rd = $options['rootDir'];
387 $mods = $options['mods'];
388 $this->_extends = 'DB_DataObject';
389 $this->_extendsFile = 'DB/DataObject.php';
390 $cli = $options['cli'];
392 foreach($this->tables as $this->table) {
393 $this->table = trim($this->table);
394 $tn = strtolower($this->table);
395 $mod = $this->modmap[$tn];
397 if (!empty($mods) && !in_array($mod, $mods)) {
402 $this->classname = 'Pman_'.$mod . '_DataObjects_'. ucfirst($this->table); // replace odd chars?
405 $outfilename = $rd.'/'.$mod.'/'. ucfirst($this->table).'.php';
406 $orig = $this->page->rootDir .'/Pman/'.$mod.'/DataObjects/'. ucfirst($this->table).'.php';
409 // file_get_contents???
410 $oldcontents = file_get_contents($orig);
413 echo "GENERATE: " . $this->classname . ($cli ? "\n" : "<BR>");
415 $out = $this->_generateClassTable($oldcontents);
417 // get rid of static GET!!!
418 $out = preg_replace('/(\n|\r\n)\s*function staticGet[^\n]+(\n|\r\n)/s', '', $out);
419 $out = preg_replace('#/\* Static get \*/#s', '', $out);
422 // $this->debug( "writing $this->classname\n");
423 //$tmpname = tempnam(session_save_path(),'DataObject_');
424 file_put_contents($outfilename, $out);
431 // function generateDefinitions() { }
432 ////function generateForeignKeys() { }
433 // function generateClasses() { }
435 var $jsHeader = "//<script type=\"text/javascript\">\n";
437 function generateRoo()
440 $options = &PEAR::getStaticProperty('DB_DataObject','options');
441 $mods = $options['mods'];
442 $this->rootDir = $options['rootDir'];
443 $this->overwrite = true;
446 require_once 'Pman/Builder/Generator/JSON.php';
448 $ret = '//<script type="text/javascript">'."\n";
451 foreach($this->tables as $this->table) {
452 $this->_generateData($this->table);
456 //echo '<PRE>';print_R($this->def);
457 $this->parseConfig();
462 $ds = $this->_database;
463 $ds = ucfirst($this->_database);
465 // we always write these files....
466 foreach($this->modtables as $m=>$ts) {
467 if (!empty($mods) && !in_array($m, $mods)) {
471 file_put_contents($this->rootDir."/$m/$m.readers.js", $this->_generateReaders($m));
482 function _generateReaders($m)
485 $udb = ucfirst($this->_database);
486 $ret = $this->jsHeader;
487 $j = new Pman_Builder_Generator_JSON();
490 $j2 = new Pman_Builder_Generator_JSON(array('crlf' => '', 'tab' => ''));
493 foreach($this->tables as $this->table) {
496 if ($this->modmap[strtolower($this->table)] != $m) {
500 $utable = ucfirst($this->table);
502 $r = $this->def['readers'][$this->table];
503 foreach($r as $k=>$tab) {
504 if ($tab['type'] == 'string') {
505 $r[$k] = $tab['name'];
508 $this->readersArgs[$this->table]['xtype'] = 'JsonReader';
509 $ret.="\n$udb.Readers.$utable = ";
510 $x = $j->encodeUnsafe($this->readersArgs[$this->table]);
511 $ret .= trim(substr($x, 0, -1)) . ",\n"; // strip of trailing ;};
512 $ret .= $j->tab . "fields : [\n". $j->tab.$j->tab;
515 $ar[] = $j2->encodeUnsafe($xr);
517 $ret .= implode(",\n". $j->tab.$j->tab, $ar);
518 $ret .= "\n". $j->tab . "]\n};\n";
526 * Generic covert databse data to readers etc..
528 * $x = DB_DataObject::factory('anytable');
529 * $def = $x->getDatabaseConnection()->tableInfo($table);
530 * $mydata = Generator::tableToData($table, $def);
532 * returns tablekey, args(for reader header), order, readers, colmodels, forms.
538 function tableToData($table, $def)
544 'tablekey' => '', //$this->tablekeys[$this->table]
545 'args' => array( // was retiun..
547 'totalProperty' => 'total',
548 'id' => 'id', // primary key!!?
551 'readers' => array(),
552 'colmodels' => array(),
558 $colmodels = array();
560 $utable = ucfirst($table);
561 //$this->tablekeys[$this->table] = '';
563 foreach($def as $t) {
572 'id' => str_replace('_', '-', strtolower($utable.'-'.$t->name)),
573 'header' => ucfirst($t->name), // get from somewhere?!!?!?
574 'dataIndex' => $t->name,
581 'fieldLabel' => str_replace(array('_id', '_'), array('', ' '), ucfirst($t->name)),
583 'allowBlank' => preg_match('/not_null/i', $t->flags) ? false : true,
584 'qtip' => 'Enter '. $t->name ,
588 switch (strtoupper($t->type)) {
591 case 'INT2': // postgres
592 case 'INT4': // postgres
593 case 'INT8': // postgres
594 case 'SERIAL4': // postgres
595 case 'SERIAL8': // postgres
601 $colmodel['width'] = 100;
602 $reader['type'] = 'int';
603 $felement['xtype'] = 'NumberField';
604 $felement['allowDecimals'] = false;
606 $reader['type'] = 'boolean';
612 case 'DOUBLE PRECISION': // double precision (firebird)
614 case 'FLOAT4': // real (postgres)
615 case 'FLOAT8': // double precision (postgres)
617 case 'MONEY': // mssql and maybe others
619 case 'NUMBER': // oci8
620 $reader['type'] = 'float'; // should really by FLOAT!!! / MONEY...
622 $colmodel['width'] = 100;
623 $felement['xtype'] = 'NumberField';
627 $reader['type'] = 'int';
628 $felement['allowDecimals'] = false;
629 $felement['xtype'] = 'NumberField';
635 $colmodel['width'] = 50;
636 $reader['type'] = 'boolean';
637 $felement['xtype'] = 'CheckBox';
638 // postgres needs to quote '0'
649 case 'SET': // not really but oh well
651 case 'POINT': // mysql geometry stuff - not really string - but will do..
653 case 'TIMESTAMPTZ': // postgres
654 case 'BPCHAR': // postgres
655 case 'INTERVAL': // postgres (eg. '12 days')
657 case 'CIDR': // postgres IP net spec
658 case 'INET': // postgres IP
659 case 'MACADDR': // postgress network Mac address.
661 case 'INTEGER[]': // postgres type
662 case 'BOOLEAN[]': // postgres type
664 $colmodel['width'] = isset($t->len) ? max(50, min(floor($t->len * 10), 300)) : 10;
665 // editor - $colmodel['allowBlank'] = preg_match('/not_null/i', $t->flags) ? false, true;
666 $reader['type'] = 'string';
667 $felement['xtype'] = 'TextField';
673 $colmodel['width'] = 300;
674 $reader['type'] = 'string';
675 $felement['xtype'] = 'TextArea'; // or HtmlEditor
676 $felement['height'] = 100;
681 $colmodel['width'] = 100;
682 ///$colmodel['renderer'] =
684 $reader['type'] = 'date';
685 $reader['dateFormat'] = 'Y-m-d';
686 $felement['xtype'] = 'DateField';
688 $felement['altFormats'] = 'Y-m-d|d/m/Y';
689 $felement['format'] = 'd/m/Y';
690 $felement['hiddenFormat'] = 'Y-m-d'; // not supported ATM
695 $colmodel['width'] = 100;
696 ///$colmodel['renderer'] =
697 $reader['type'] = 'string';
698 $felement['xtype'] = 'TextField';
703 $colmodel['width'] = 100;
704 ///$colmodel['renderer'] =
705 $reader['type'] = 'date';
706 $reader['dateFormat'] = 'Y-m-d H:i:s';
707 $felement['xtype'] = 'TextField';
708 $felement['readOnly'] = 'true';
714 case 'TIMESTAMP': // do other databases use this???
715 $colmodel['width'] = 100;
716 ///$colmodel['renderer'] =
717 $reader['type'] = 'date';
718 $reader['dateFormat'] = 'YmdHis';
719 $felement['xtype'] = 'TextField';
720 $felement['readOnly'] = 'true';
724 case 'BLOB': /// these should really be ignored!!!???
729 case 'CLOB': // oracle character lob support
731 case 'BYTEA': // postgres blob support..
732 $colmodel['width'] = 300;
733 $reader['type'] = 'string';
734 $felement['xtype'] = 'TextArea'; // or HtmlEditor
735 $felement['height'] = 100;
739 echo "*****************************************************************\n".
740 "** WARNING UNKNOWN TYPE **\n".
741 "** Found column '{$t->name}', of type '{$t->type}' **\n".
742 "** Please submit a bug, describe what type you expect this **\n".
743 "** column to be **\n".
744 "** ---------POSSIBLE FIX / WORKAROUND -------------------------**\n".
745 "** Try using MDB2 as the backend - eg set the config option **\n".
746 "** db_driver = MDB2 **\n".
747 "*****************************************************************\n";
752 if (preg_match('/(auto_increment|nextval\()/i',rawurldecode($t->flags))
753 || (isset($t->autoincrement) && ($t->autoincrement === true))) {
755 if (empty($ret['tablekeys'])) {
756 $ret['tablekeys'] = $t->name;
765 // form inherits from users' colmodel width..
766 $felement['width'] = $colmodel['width'];
768 //$readers[] = $reader;
769 //$colmodels[] = $colmodel;
770 if ($ret['tablekey'] == $t->name) {
772 $felement['xtype'] = 'Hidden';
773 unset($felement['allowBlank']);
774 unset($felement['fieldLabel']);
780 // only put id in the key col.
781 unset($colmodel['id']);
783 // hidden elemetns do not need any display components..
784 if ($felement['xtype'] == 'Hidden') {
785 foreach($felement as $k=>$v) {
786 if (!in_array($k , array('name', 'xtype'))) {
787 unset($felement[$k]);
793 // $form[] = $felement;
796 $ret['order'][] = $t->name;
797 $ret['readers'][$t->name] = $reader;
798 $ret['colmodels'][$t->name] = $colmodel;
799 $ret['forms'][$t->name] = $felement;
803 //$ret['args'] = $args;
809 function _generateData($table)
814 'totalProperty' => 'total',
815 'id' => 'id', // primary key!!?
818 $d = $this->tableToData($this->_definitions[$table]);
820 $this->tablekeys[$this->table] = $d['tablekey'];
822 // $form[] = $felement;
825 $this->def['order'][$table] = $d['order'];
826 $this->def['readers'][$table]= $d['readers'];
827 $this->def['colmodels'][$table] = $d['colmodes'];
828 $this->def['forms'][$table] = $d['forms'];
830 $this->readersArgs[$table] = $d['args'];
839 function parseConfig()
841 $options = &PEAR::getStaticProperty('DB_DataObject','options');
843 if (isset($options['modtables'])) {
844 $this->modtables = $options['modtables'];
845 $this->modmap = $options['modmap'];
846 $this->modsql = $options['modsql'];
849 $ff = HTML_Flexyframework::get();
850 $dirs = array($ff->page->rootDir.'/Pman/DataObjects'); // not used anymore!
851 foreach($this->modtables as $m=>$ts) {
852 $dirs[] = $ff->page->rootDir.'/Pman/'.$m.'/DataObjects';
854 $ini = array('database__render' => array());
855 foreach($dirs as $d) {
856 if (!file_exists($d.'/pman.links.ini')) {
859 $in = parse_ini_file($d.'/pman.links.ini',true);
861 if (isset($in['database__render'])) {
862 $r = $in['database__render'];
863 unset($in['database__render']);
865 $ini = array_merge($ini, $in);
866 $ini['database__render'] = array_merge($ini['database__render'] , $r);
868 //echo '<PRE>';print_R($ini);//exit;
871 if (!isset($ini['database__render'])) {
872 die("database__render not available in links files.");
875 $this->mapcols = array();
876 foreach($ini as $tab=>$conf) {
877 if ($tab == 'database__render') {
880 $this->mergeConfig($tab,$conf,$ini['database__render']);
883 $this->renderMap = $ini['database__render'];
885 function mergeConfig($table, $conf, $render)
887 $this->mapcols[$table] = array();
888 $options = &PEAR::getStaticProperty('DB_DataObject','options');
889 if (isset($options['modtables'])) {
890 $this->modtables = $options['modtables'];
891 $this->modmap = $options['modmap'];
892 $this->modsql = $options['modsql'];
896 foreach($conf as $ocol=>$info) {
897 // format col => showval..
898 //list($rtc, $rshow) = explode(':', $info);
899 list($tab,$col) = explode(':', $info);
901 $rshow = $render[$tab];
903 $this->mapcols[$table][$ocol] = array('table'=>$tab, 'col' => $col);
908 //- just add an extra line..
909 if (!isset($this->def['readers'][$tab][$rshow])){
910 echo "WARNING in links.ini TABLE $tab does not have renderer $rshow <BR>\n";
914 // for the readers.. we need to merge all the columns in the left to the right...
917 // ocol => column in table
918 // tab => remote table
919 // col => right col linked to...
921 $rdef = $this->_definitions[$tab];
924 foreach($rdef as $t) {
925 //copy typedata from old coll
926 $this->def['readers'][$table][$ocol.'-'. $t->name] = $this->def['readers'][$tab][$t->name];
927 $this->def['readers'][$table][$ocol.'-'. $t->name]['name'] = $ocol.'_'. $t->name;
934 // remove the def column from the id one..
935 if (isset($this->def['colmodels'][$table][$ocol])) {
936 unset($this->def['colmodels'][$table][$ocol]);
938 $this->def['colmodels'][$table][$ocol.'-'. $rshow] =
939 $this->def['colmodels'][$tab][$rshow];
941 // change the header name (merge of two..)
942 list($colname,) = explode('_',$ocol,2);
944 $this->def['colmodels'][$table][$ocol.'-'. $rshow]['dataIndex'] = $ocol.'_'. $rshow;
945 $this->def['colmodels'][$table][$ocol.'-'. $rshow]['id'] = $ocol.'-'. $rshow;
947 $this->def['colmodels'][$table][$ocol.'-'. $rshow]['header'] = ucwords($colname . ' ' .
948 $this->def['colmodels'][$tab][$rshow]['header']);
950 // last of all add replace the old $col, with
951 $p = array_search($ocol, empty($this->def['order'][$table]) ? array() : $this->def['order'][$table]);
952 $this->def['order'][$table][$p] = $ocol.'-'. $rshow;
953 $this->def['order'][$table][] = $ocol;
955 // --- now for forms!!!!
960 //print_r( $this->def['readers'][$table]);
961 // print_r( $this->def['colmodels'][$table]);
962 //print_r($this->def['readers'][$table]); exit;
967 function writeFileEx($n, $f, $str)
969 if (file_exists($f)) {
970 // all - will not overwrite stuff.. (only being specific willl)
971 if (!in_array($n, $this->overwrite)) {
972 $this->writeFile($f.'.generated',$str);
976 $this->writeFile($f,$str);
980 function writeFile($f, $str)
982 require_once 'System.php';
983 System::mkdir(array('-p', dirname($f)));
986 file_put_contents($f, $str);