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