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