ERM.php
[Pman.Builder] / ERM.php
1 <?php
2
3 require_once 'Pman.php';
4
5
6 class Pman_Builder_ERM extends Pman
7 {
8     function getAuth()
9     {
10         parent::getAuth(); // load company!
11         $au = $this->getAuthUser();
12         if (!$au) {
13             $this->jerr("Not authenticated", array('authFailure' => true));
14         }
15         $this->authUser = $au;
16         return true;
17         
18        }
19     
20     function get($tbl)
21     {
22         
23         
24         //echo '<PRE>';
25         global $_DB_DATAOBJECT;
26         // DB_DataObject::debugLevel(1);
27         $tq = DB_DataObject::factory('Person');
28         $tq->table();
29         $tq->links();
30         $tables = $_DB_DATAOBJECT['INI'][$tq->_database];
31         
32         if (!isset($_GET['table'])) {
33             $ret = array();
34             $t = array_keys($tables);
35             sort($t);
36             
37             // for postgres we can get descriptions - this should just fail in Mysql..
38             $desc= array();
39            // DB_DataObjecT::DebugLevel(1);
40             $tq = DB_DataObject::factory('Person');
41             $tq->query( "
42                 select relname, obj_description( oid) as desc FROM pg_catalog.pg_class");
43             while ($tq->fetch()) {
44                 $desc[$tq->relname] = $tq->desc;
45             }
46
47             
48             
49             
50             
51             
52             foreach( $t as $k) {
53                 if (preg_match('/__keys$/', $k)) {
54                     continue;
55                 }
56                 $do = DB_DataObject::factory($k);
57                 if (!is_a($do,'DB_DataObject')) {
58                     continue;
59                 }
60                 $ret[] = array(
61                         'name' => $k,
62                         'desc' => isset($desc[$k]) ? $desc[$k] : ''
63                     );
64             }
65             $this->jdata($ret);
66         }
67         // this part is to find the table definition...
68         
69         $do = DB_DataObject::factory($_GET['table']);
70         $links = $do->links();
71         $cols = $do->table();
72         $dos = array();
73        //    echo '<PRE>';print_R($links);
74         foreach($links as $k=>$v) {
75             $kv = explode(':', $v);
76             //print_R($kv);
77             $dos[$k]= DB_DataObject::factory($kv[0]);
78             if (!is_a($dos[$k], 'DB_DataObject')) {
79                 echo '<PRE>'; print_r($dos[$k]);
80             }
81         }
82         
83       
84         $desc =   $this->createRet($do);
85        // echo '<PRE>';print_R($desc);
86         $ret = array();
87         foreach($cols as $c => $ty) {
88            
89             if (!isset($links[$c]))  {
90                  $ret[] = $desc[$c];
91                 continue;
92             }
93             // we need to add dependant information to column details so
94             // that combo box can determine how to use it..
95             
96             
97             
98             // colname_{remotename}_{col}
99             $kv = explode(':', $links[$c]);
100             $ar = $this->createRet($dos[$c], $c . '_' . $kv[1] . '_');
101             $desc[$c]['maps_to'] = $kv[1];
102             $desc[$c]['deps'] = array_values($ar);
103             
104             $ret[] = $desc[$c];
105             foreach($ar as $cn => $r) {
106                 $ret[] = $r;
107             }
108             
109             
110         }
111          echo '<PRE>';print_R($ret);
112         
113         $this->jdata($ret); 
114         
115         
116         
117         
118     }
119     function createRet($do, $pref='')
120     {
121         static  $cache = array();
122         static  $types= array();
123         $tn = $do->tableName();
124
125         // get a description if available..
126         if (!isset($desc[$tn])) {
127             $desc[$tn] = array();
128             $dd = clone($do);
129             
130            // DB_DataObject::DebugLevel(1);
131             $dd->query("SELECT
132                     c.column_name as name,
133                     pgd.description as desc
134                 FROM pg_catalog.pg_statio_all_tables as st
135                     inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid)
136                     inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position and c.table_schema=st.schemaname and c.table_name=st.relname)
137                 WHERE
138                     c.table_schema = 'public' and c.table_name = '{$tn}'
139             ");
140             while($dd->fetch()) {
141                 $cache[$tn][$dd->name] = $dd->desc;
142             }
143             
144             $defs =  $dd->getDatabaseConnection()->tableInfo($tn);
145             $types[$tn] = array();
146             foreach($defs as $c) {
147                 $types[$tn][$c['name']] = $c['type'];
148             }
149             //echo '<PRE>';print_r($defs);
150             
151         }
152        
153        
154        
155         $ret = array();
156         foreach($do->table() as $k=>$ty) {
157             $ret[$k] = array(
158                 'table' => $tn,
159                 'column' => $pref . $k,
160                 'columnshort' => $k,
161                 'ctype' => $types[$tn][$k], // should always work!
162                 'desc' => isset($cache[$tn][$k]) ? $cache[$tn][$k] : '',
163             );
164         }
165         return $ret;
166         
167         
168         
169         
170         
171     }
172     
173     
174     
175     
176     
177 }