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             $ret[] = $desc[$c];
97             
98             // colname_{remotename}_{col}
99             $kv = explode(':', $links[$c]);
100             $ar = $this->createRet($dos[$c], $c . '_' . $kv[1] . '_');
101             foreach($ar as $cn => $r) {
102                 $ret[] = $r;
103             }
104             
105             
106         }
107        // echo '<PRE>';print_R($ret);
108         
109         $this->jdata($ret); 
110         
111         
112         
113         
114     }
115     function createRet($do, $pref='')
116     {
117         static  $cache = array();
118         static  $types= array();
119         $tn = $do->tableName();
120
121         // get a description if available..
122         if (!isset($desc[$tn])) {
123             $desc[$tn] = array();
124             $dd = clone($do);
125             
126            // DB_DataObject::DebugLevel(1);
127             $dd->query("SELECT
128                     c.column_name as name,
129                     pgd.description as desc
130                 FROM pg_catalog.pg_statio_all_tables as st
131                     inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid)
132                     inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position and c.table_schema=st.schemaname and c.table_name=st.relname)
133                 WHERE
134                     c.table_schema = 'public' and c.table_name = '{$tn}'
135             ");
136             while($dd->fetch()) {
137                 $cache[$tn][$dd->name] = $dd->desc;
138             }
139             
140             $defs =  $dd->getDatabaseConnection()->tableInfo($tn);
141             $types[$tn] = array();
142             foreach($defs as $c) {
143                 $types[$tn][$c['name']] = $c['type'];
144             }
145             //echo '<PRE>';print_r($defs);
146             
147         }
148        
149        
150        
151         $ret = array();
152         foreach($do->table() as $k=>$ty) {
153             $ret[$k] = array(
154                 'table' => $tn,
155                 'column' => $pref . $k,
156                 'columnshort' => $k,
157                 'ctype' => $types[$tn][$k], // should always work!
158                 'desc' => isset($cache[$tn][$k]) ? $cache[$tn][$k] : '',
159             );
160         }
161         return $ret;
162         
163         
164         
165         
166         
167     }
168     
169     
170     
171     
172     
173 }