Generator.php
[Pman.Builder] / Generator.php
1 <?php
2  
3  
4 /**
5  * 
6  * Generate DataObjects... and readers in the right places..
7  * 
8  * note - we write to a temporary directory first...
9  * 
10  * 
11  */
12  
13 require_once 'DB/DataObject/Generator.php';
14
15
16 /** basic thing now works... 
17
18 * it needs a bit more intelligence to work out what to do...
19
20
21 * Basically we need to build up all the formats for each db column
22 * then 
23 *   - overlay any mapping stuff.
24 *   
25 *   - overlay user defined settings
26 *   = write it out to file..
27 *   
28 *  Strucutres:
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;
33
34 *   Readers
35 *       readersDef[table.col]
36
37
38
39 * **/
40
41
42
43 class Pman_Builder_Generator extends DB_DataObject_Generator
44 {
45     
46
47    
48     // inherrited..
49     // $tablekeys
50     // $tables
51     // $_definitions
52     /**
53      * def[order]  
54      *      [tablename] => array( list of columns ones with '-' indicate lookup
55      *    [readers]
56      *       [tablename][colname] -> reader foramt
57      *    [forms]
58      *        [tablename][colname] => xtype / name etc...
59      *    [readerArgs]
60      *        [tablename] => data for reader args (eg. id / total prop etc.)
61      *  readers =>
62      *         [tablename] => array of cols with types
63      *  forms =>
64      *        [tablename] -> array of cols
65      * 
66      */ 
67     var $def;
68     
69       
70     var $page = false; // page container when run from cli.
71     
72     // dont do usual stuff!!!
73     var $rootDir = '';
74     var $tablekeys = array();
75     
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!!!
81     
82     function start($cli=false, $mods='', $overwrite='')
83     {
84         
85         
86         $this->scanModules();
87         //echo '<PRE>'; print_r($this->modtables); exit;
88         
89         $options = &PEAR::getStaticProperty('DB_DataObject','options');
90         
91         
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);
97        
98         if (!file_exists($options['rootDir'])) {
99             mkdir($options['rootDir'], 0775, true);
100         }
101         
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;
108        
109        
110         $this->importSQL();
111        
112         $standard_database = $options['database'];
113        
114        
115        
116        
117        
118        
119        
120        
121        
122        
123         parent::start();
124         
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 ;
134                 
135                 parent::start();
136             }
137             
138             $options['database'] =  $standard_database ;
139             if (isset($options['database_'. $m])) {
140                 $options['database'] =  $options['database_'. $m];
141                 //var_dump($url);exit;
142                 
143                 // start again?
144                 parent::start();
145             }
146             
147             
148             if (!empty($options['mods'] ) && !in_array($m,  $options['mods'] )) {
149                 continue;
150             }
151             foreach(scandir($options['rootDir'].'/'.$m) as $f) {
152                 if (!strlen($f) || $f[0] == '.') {
153                     continue;
154                 }
155                 // does it exist!!!
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";
160                 }
161                 
162                 if (!file_exists($tg) || !filesize($tg) ) {
163                   
164                     if ($cli && in_array($f, $flist)) {
165                        echo "COPY $src $tg" . ($cli ? "\n" : "<BR>");
166                         copy($src, $tg);
167                         continue;
168                     }
169                     echo "!!!!MISSING!!! $tg" . ($cli ? "\n" : "<BR>");
170                     
171                     continue;
172                 }
173                 // always copy readers and ini file.=  nope - not on live..
174                 if ($cli && in_array($f, $flist)) {
175                     
176                    //|| $f=='pman.ini' || preg_match('/\.js$/', $f))) {
177                     echo "COPY $src $tg". ($cli ? "\n" : "<BR>");
178                     copy($src, $tg);
179                     continue;
180                 }
181                 
182                 // diff the two..
183                 $cmd = "$diff -u -w ". escapeshellarg($tg) . ' ' . escapeshellarg($src);
184                  
185                 $out = array(); $ret = 0;
186                 exec($cmd, $out, $ret);
187                 if ($ret ==0) { // files match..
188                     continue;
189                 }
190                 // var_dump($ret);
191                 echo "\n" .implode( "\n" , $out) . "\n";
192                
193                 
194             }
195             
196             
197         }
198         
199         
200         
201         
202     }
203     
204     
205     
206     function importSQL()
207     {
208         $options = &PEAR::getStaticProperty('DB_DataObject','options');
209         
210         $url = parse_url($options['database']);
211         // hide stuff for web..
212         $cli = $options['cli'];
213         if (!$cli) {
214             $url['pass'] = '*****';
215             $url['user'] = '*****';
216             $url['host'] = '*****';
217         }
218          
219         
220         
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)
226         {
227             if ($cli && isset($options['database_'. $m])) {
228                 $url =parse_url($options['database_'.$m]);
229             }
230             
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']);
236            
237             echo $mysql_cmd . "\n" ;
238             
239             if (!empty($options['mods'] ) && !in_array($m,  $options['mods'] )) {
240                 continue;
241             }
242             foreach($fl as $f) {
243                 $fn = $this->page->rootDir. "/Pman/$m/DataObjects/$f";
244                 $cmd = $cat . ' ' . escapeshellarg($fn) . " | $mysql_cmd -f ";
245                 echo $cmd. ($cli ? "\n" : "<BR>\n");
246                 if ($cli) {
247                     passthru($cmd);
248                 }
249                 
250             }
251         }
252         
253         
254         
255     }
256     /**
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.
260      * 
261      */
262     
263     
264     function scanModules()
265     {
266         
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'];
272             return;
273         }
274         
275         $ff = HTML_Flexyframework::get();
276         
277         $top = $ff->page->rootDir .'/Pman';
278         $this->modtables = array();
279         $this->modmap = array();
280         $this->modmapsql = array();
281         
282         foreach(scandir($top) as $m) {
283             
284             if (!strlen($m) || 
285                     $m[0] == '.' || 
286                     !is_dir($top .'/'.$m) || 
287                     !file_exists($top .'/'.$m.'/DataObjects')
288                 ) {
289                 continue;
290             }
291             $this->modtables[$m] = array();
292             $this->modsql[$m] = array();
293             foreach(scandir($top .'/'.$m.'/DataObjects') as $f) {
294                 if (!strlen($f) ||   $m[0] == '.') {
295                     continue;
296                 }
297                 if (preg_match('/\.sql$/', $f))  {
298                     $this->modsql[$m][] = $f;
299                 }
300                                 
301                 if (preg_match('/\.php$/', $f))  {
302                     $tn = strtolower(preg_replace('/\.php$/', '', $f));
303                     $this->modtables[$m][] = $tn;
304                     $this->modmap[$tn] = $m;
305                     continue;
306                 }
307             }
308         }
309         $options['modtables'] = $this->modtables;
310         $options['modmap'] = $this->modmap;
311         $options['modsql'] = $this->modsql;
312        // print_r($options);
313         
314     }
315     /**
316      * 
317      * this is run first, so picks up any missing dataobject files..
318      */
319     
320     function generateDefinitions()
321     {
322         if (!$this->tables) {
323             $this->debug("-- NO TABLES -- \n");
324             return;
325         }
326         if (!isset($this->modmap)) {
327             $this->scanModules();
328         }
329          $options = &PEAR::getStaticProperty('DB_DataObject','options');
330          $mods = $options['mods'];
331         $inis = array();
332         $this->_newConfig = '';
333         foreach($this->tables as $this->table) {
334             
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
340                     eg. 
341                     touch Pman/????/DataObjects/".ucfirst($this->table).".php
342                    \n");
343                     
344             }
345             $mod = $this->modmap[$tn];
346             $inis[$mod] = isset($inis[$mod]) ? $inis[$mod] : '';
347             
348             
349             $this->_newConfig = '';
350             $this->_generateDefinitionsTable();
351             
352             
353             $inis[$mod] .= $this->_newConfig;
354         }
355         
356         //echo '<PRE>';print_r($this->_inis); exit;
357         $options = PEAR::getStaticProperty('DB_DataObject','options');
358         
359         $rd = $options['rootDir'];
360         foreach($inis as $m=>$ini) {
361             if (!empty($mods) && !in_array($m, $mods)) {
362                 continue;
363             }
364             
365             if (!file_exists($rd.'/'.$m)) {
366                 mkdir($rd.'/'.$m, 0775, true);
367             }
368             $fname = '/pman.ini';
369             if (isset($options['database_'. $m])) {
370                 $url = parse_url($options['database_'.$m]);
371                 $fname = '/'. basename($url['path']).'.ini';
372             }
373
374             
375             file_put_contents($rd.'/'.$m.$fname, $ini);
376         }
377          
378          
379     }
380     
381     function generateClasses() 
382     {
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'];
391
392         foreach($this->tables as $this->table) {
393             $this->table        = trim($this->table);
394             $tn  = strtolower($this->table);
395             $mod = $this->modmap[$tn];
396             
397              if (!empty($mods) && !in_array($mod, $mods)) {
398                 continue;
399             }
400             
401             
402             $this->classname    = 'Pman_'.$mod . '_DataObjects_'. ucfirst($this->table); // replace odd chars?
403            
404            
405             $outfilename    = $rd.'/'.$mod.'/'. ucfirst($this->table).'.php';
406             $orig           = $this->page->rootDir .'/Pman/'.$mod.'/DataObjects/'. ucfirst($this->table).'.php';
407             
408            
409                 // file_get_contents???
410             $oldcontents = file_get_contents($orig);
411             
412              
413             echo "GENERATE: " .   $this->classname  . ($cli ? "\n" : "<BR>");
414             
415             $out = $this->_generateClassTable($oldcontents);
416             
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);
420               
421
422            // $this->debug( "writing $this->classname\n");
423             //$tmpname = tempnam(session_save_path(),'DataObject_');
424             file_put_contents($outfilename, $out);
425             
426         }
427     }
428     
429     
430         
431    // function generateDefinitions() { }
432     ////function generateForeignKeys() { }
433    // function generateClasses() { }
434    
435     var $jsHeader = "//<script type=\"text/javascript\">\n";
436         
437     function generateRoo()
438     {
439          
440         $options = &PEAR::getStaticProperty('DB_DataObject','options');
441          $mods = $options['mods'];
442         $this->rootDir = $options['rootDir'];
443         $this->overwrite = true;
444         //$this->parseOld();
445         
446         require_once 'Pman/Builder/Generator/JSON.php';
447         
448         $ret = '//<script type="text/javascript">'."\n";
449         
450       
451         foreach($this->tables as $this->table) {
452             $this->_generateData($this->table);
453         }
454         
455         
456        //echo '<PRE>';print_R($this->def);
457         $this->parseConfig();
458          
459         
460         
461          
462         $ds = $this->_database;
463         $ds = ucfirst($this->_database);
464        
465         // we always write these files....
466         foreach($this->modtables as $m=>$ts) {
467              if (!empty($mods) && !in_array($m, $mods)) {
468                 continue;
469             }
470              
471             file_put_contents($this->rootDir."/$m/$m.readers.js", $this->_generateReaders($m));
472            
473         }
474       
475     }
476     
477     
478     
479     
480      
481     
482     function _generateReaders($m)
483     {
484          
485         $udb = ucfirst($this->_database);
486         $ret = $this->jsHeader;
487         $j = new Pman_Builder_Generator_JSON();
488         $j->indent = 0;
489         
490         $j2 = new Pman_Builder_Generator_JSON(array('crlf' => '', 'tab' => ''));
491         
492         
493         foreach($this->tables as $this->table) {
494             
495             
496             if ($this->modmap[strtolower($this->table)] != $m) {
497                 continue;
498             }
499             
500             $utable = ucfirst($this->table);
501             
502             $r =  $this->def['readers'][$this->table];
503             foreach($r as $k=>$tab) {
504                 if ($tab['type'] == 'string') {
505                     $r[$k] = $tab['name'];
506                 }
507             }
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;
513             $ar = array();
514             foreach($r as $xr) {
515                 $ar[] = $j2->encodeUnsafe($xr);
516             }
517             $ret .= implode(",\n". $j->tab.$j->tab, $ar);
518             $ret .= "\n".  $j->tab . "]\n};\n";
519             
520         }
521         return $ret;
522    
523     }
524     /**
525      * tableToData:
526      * Generic covert databse data to readers etc..
527      * usage:
528      * $x = DB_DataObject::factory('anytable');
529      * $def = $x->getDatabaseConnection()->tableInfo($table);
530      * $mydata = Generator::tableToData($table, $def);
531      * 
532      * returns tablekey, args(for reader header), order, readers, colmodels, forms.
533      * 
534      * 
535      * 
536      */
537      
538     function tableToData($table, $def)
539     {
540         
541       
542         
543         $ret= array(
544             'tablekey' => '',   //$this->tablekeys[$this->table] 
545             'args' => array(    // was retiun..
546                 'root' => 'data',
547                 'totalProperty' => 'total',
548                 'id' => 'id', // primary key!!?
549             ),
550             'order' => array(),
551             'readers' => array(),
552             'colmodels' => array(),
553             'forms' => array()
554             
555         );
556          
557         $readers = array( );
558         $colmodels = array();
559         $form  = array();
560         $utable = ucfirst($table);
561         //$this->tablekeys[$this->table] = '';
562         
563         foreach($def as $t) {
564             if (is_array($t)) {
565                 $t = (object)$t;
566             }
567             $reader = array(
568                 'name' => $t->name,
569             );
570             
571             $colmodel = array(
572                 'id' => str_replace('_', '-', strtolower($utable.'-'.$t->name)),
573                 'header' => ucfirst($t->name), // get from somewhere?!!?!?
574                 'dataIndex' => $t->name,
575                 'sortable' => true,
576                 'width' => 150,
577                 //'editor' => 
578             );
579             $felement = array(
580                 'name' => $t->name,
581                 'fieldLabel' => str_replace(array('_id', '_'), array('', ' '), ucfirst($t->name)),
582                 'value' => '',
583                 'allowBlank' =>  preg_match('/not_null/i', $t->flags) ? false : true,
584                 'qtip' => 'Enter '. $t->name ,
585             );
586
587                 
588             switch (strtoupper($t->type)) {
589
590                 case 'INT':
591                 case 'INT2':    // postgres
592                 case 'INT4':    // postgres
593                 case 'INT8':    // postgres
594                 case 'SERIAL4': // postgres
595                 case 'SERIAL8': // postgres
596                 case 'INTEGER':
597                 case 'TINYINT':
598                 case 'SMALLINT':
599                 case 'MEDIUMINT':
600                 case 'BIGINT':
601                     $colmodel['width'] = 100;
602                     $reader['type'] = 'int';
603                     $felement['xtype'] = 'NumberField';
604                      $felement['allowDecimals'] = false;
605                     if ($t->len == 1) {
606                         $reader['type'] = 'boolean';
607                     }
608                     break;
609                
610                 case 'REAL':
611                 case 'DOUBLE':
612                 case 'DOUBLE PRECISION': // double precision (firebird)
613                 case 'FLOAT':
614                 case 'FLOAT4': // real (postgres)
615                 case 'FLOAT8': // double precision (postgres)
616                 case 'DECIMAL':
617                 case 'MONEY':  // mssql and maybe others
618                 case 'NUMERIC':
619                 case 'NUMBER': // oci8 
620                     $reader['type']  = 'float'; // should really by FLOAT!!! / MONEY...
621                     
622                     $colmodel['width'] = 100;
623                     $felement['xtype'] = 'NumberField';
624                     break;
625                     
626                 case 'YEAR':
627                     $reader['type']  = 'int'; 
628                     $felement['allowDecimals'] = false;
629                     $felement['xtype'] = 'NumberField';
630                     break;
631                     
632                 case 'BIT':
633                 case 'BOOL':   
634                 case 'BOOLEAN':   
635                     $colmodel['width'] = 50;
636                     $reader['type']  =  'boolean';
637                     $felement['xtype'] = 'CheckBox';
638                     // postgres needs to quote '0'
639                    
640                     break;
641                     
642                 case 'STRING':
643                 case 'CHAR':
644                 case 'VARCHAR':
645                 case 'VARCHAR2':
646                 case 'TINYTEXT':
647                 
648                 case 'ENUM':
649                 case 'SET':         // not really but oh well
650                 
651                 case 'POINT':       // mysql geometry stuff - not really string - but will do..
652                 
653                 case 'TIMESTAMPTZ': // postgres
654                 case 'BPCHAR':      // postgres
655                 case 'INTERVAL':    // postgres (eg. '12 days')
656                 
657                 case 'CIDR':        // postgres IP net spec
658                 case 'INET':        // postgres IP
659                 case 'MACADDR':     // postgress network Mac address.
660                 
661                 case 'INTEGER[]':   // postgres type
662                 case 'BOOLEAN[]':   // postgres type
663                   
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';
668                     break;
669                 
670                 case 'TEXT':
671                 case 'MEDIUMTEXT':
672                 case 'LONGTEXT':
673                     $colmodel['width'] =  300;
674                     $reader['type']  = 'string';
675                     $felement['xtype'] = 'TextArea'; // or HtmlEditor
676                     $felement['height'] = 100;
677                     break;
678                 
679                 
680                 case 'DATE':    
681                     $colmodel['width'] =  100;
682                     ///$colmodel['renderer'] =  
683                     
684                     $reader['type']  = 'date';
685                     $reader['dateFormat']   = 'Y-m-d';
686                     $felement['xtype'] = 'DateField'; 
687                     
688                     $felement['altFormats'] = 'Y-m-d|d/m/Y';
689                     $felement['format'] = 'd/m/Y';
690                     $felement['hiddenFormat'] = 'Y-m-d'; // not supported ATM
691         
692                     break;
693                     
694                 case 'TIME':    
695                     $colmodel['width'] = 100;
696                     ///$colmodel['renderer'] =  
697                     $reader['type']  = 'string';
698                     $felement['xtype'] = 'TextField';
699                     break;    
700                     
701                 
702                 case 'DATETIME': 
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';
709                      
710         
711                     
712                     break;    
713                     
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'; 
721                     break;    
722                     
723                 
724                 case 'BLOB':       /// these should really be ignored!!!???
725                 case 'TINYBLOB':
726                 case 'MEDIUMBLOB':
727                 case 'LONGBLOB':
728                 
729                 case 'CLOB': // oracle character lob support
730                 
731                 case 'BYTEA':   // postgres blob support..
732                     $colmodel['width'] =  300;
733                     $reader['type']  = 'string';
734                     $felement['xtype'] = 'TextArea'; // or HtmlEditor
735                     $felement['height'] = 100;
736                     break;
737                     
738                 default:     
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";
748                     $write_ini = false;
749                     break;
750             }
751             
752             if (preg_match('/(auto_increment|nextval\()/i',rawurldecode($t->flags)) 
753                 || (isset($t->autoincrement) && ($t->autoincrement === true))) {
754                     
755                 if (empty($ret['tablekeys'])) {
756                     $ret['tablekeys'] = $t->name;
757                 }
758                  
759             
760             }
761             
762             
763             
764              
765             // form inherits from users' colmodel width..
766             $felement['width'] = $colmodel['width'];
767             
768             //$readers[] = $reader;
769             //$colmodels[] = $colmodel;
770             if ($ret['tablekey'] == $t->name) {
771                 // do allow id to be 
772                 $felement['xtype'] = 'Hidden';
773                 unset($felement['allowBlank']);
774                 unset($felement['fieldLabel']);
775                 
776                 
777                 
778                 
779             } else {
780                 // only put id in the key col.
781                 unset($colmodel['id']);
782             }
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]);
788                     }
789                 }
790                  
791             }
792             
793            // $form[] = $felement;
794            
795             // store the globals
796             $ret['order'][] = $t->name;
797             $ret['readers'][$t->name] = $reader;
798             $ret['colmodels'][$t->name] = $colmodel;
799             $ret['forms'][$t->name] = $felement;
800             
801              
802         }
803         //$ret['args'] = $args;
804         
805         return $ret;
806         
807     }
808      
809     function _generateData($table)
810     {
811          
812         $args = array(
813             'root' => 'data',
814             'totalProperty' => 'total',
815             'id' => 'id', // primary key!!?
816         );
817          
818         $d = $this->tableToData($this->_definitions[$table]);
819         
820         $this->tablekeys[$this->table]  = $d['tablekey'];
821         
822             // $form[] = $felement;
823            
824             // store the globals
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'];
829         
830         $this->readersArgs[$table]   = $d['args'];
831         
832         
833         
834     }
835     
836      
837      
838    
839     function parseConfig()
840     {
841          $options = &PEAR::getStaticProperty('DB_DataObject','options');
842         
843         if (isset($options['modtables'])) {
844             $this->modtables = $options['modtables'];
845             $this->modmap = $options['modmap'];
846             $this->modsql = $options['modsql'];
847         }
848         
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';
853         }
854         $ini = array('database__render' => array());
855         foreach($dirs as $d) {
856             if (!file_exists($d.'/pman.links.ini')) {
857                 continue;
858             }
859             $in = parse_ini_file($d.'/pman.links.ini',true);
860             $r = array();
861             if (isset($in['database__render'])) {
862                 $r = $in['database__render'];
863                 unset($in['database__render']);
864             }
865             $ini = array_merge($ini, $in);
866             $ini['database__render'] = array_merge($ini['database__render'] , $r);
867         }
868          //echo '<PRE>';print_R($ini);//exit;
869         
870         
871         if (!isset($ini['database__render'])) {
872             die("database__render not available in links files.");
873             return;
874         }
875         $this->mapcols = array();
876         foreach($ini as $tab=>$conf) {
877             if ($tab == 'database__render') {
878                 continue;
879             }
880             $this->mergeConfig($tab,$conf,$ini['database__render']);
881              
882         }
883         $this->renderMap = $ini['database__render'];
884     }
885     function mergeConfig($table, $conf, $render)
886     {
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'];
893         }
894         
895         
896         foreach($conf as $ocol=>$info) {
897             // format col => showval..
898             //list($rtc, $rshow) = explode(':', $info);
899             list($tab,$col) = explode(':', $info);
900             //print_r($render);
901             $rshow = $render[$tab];
902             
903             $this->mapcols[$table][$ocol] = array('table'=>$tab, 'col' => $col);
904             
905             // for the grid...
906             
907             // reader:
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";
911                 continue;
912             }
913             
914             // for the readers.. we need to merge all the columns in the left to the right...
915             
916             // table => original
917             // ocol => column in table
918             // tab => remote table
919             // col => right col linked to...
920             
921             $rdef = $this->_definitions[$tab];
922             
923             
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;
928             }
929             
930             
931             
932             
933             
934             // remove the def column from the id one..
935             if (isset($this->def['colmodels'][$table][$ocol])) {
936                 unset($this->def['colmodels'][$table][$ocol]);
937             }
938             $this->def['colmodels'][$table][$ocol.'-'. $rshow] = 
939                     $this->def['colmodels'][$tab][$rshow];
940             
941             // change the header name (merge of two..)
942             list($colname,) = explode('_',$ocol,2);
943             
944             $this->def['colmodels'][$table][$ocol.'-'. $rshow]['dataIndex'] = $ocol.'_'. $rshow;
945             $this->def['colmodels'][$table][$ocol.'-'. $rshow]['id'] = $ocol.'-'. $rshow;
946             
947             $this->def['colmodels'][$table][$ocol.'-'. $rshow]['header'] = ucwords($colname . ' ' . 
948                 $this->def['colmodels'][$tab][$rshow]['header']);
949             
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;
954              
955             // --- now for forms!!!!
956             
957              
958         }
959         //var_dump($table);
960         //print_r( $this->def['readers'][$table]);
961        // print_r( $this->def['colmodels'][$table]);
962         //print_r($this->def['readers'][$table]); exit;
963         
964     }
965         
966        
967     function writeFileEx($n, $f, $str) 
968     {
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);
973                 return;
974             }
975         }
976         $this->writeFile($f,$str);
977         
978         
979     }
980     function writeFile($f, $str)
981     {
982         require_once 'System.php';
983         System::mkdir(array('-p', dirname($f)));
984         // overwrite???
985         echo "write: $f\n";
986         file_put_contents($f, $str);
987     } 
988    
989 }
990