/
DatabaseColumns.php
86 lines (64 loc) · 2.35 KB
/
DatabaseColumns.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
require_once 'Pman.php';
class Pman_Core_DatabaseColumns extends Pman {
function getAuth()
{
parent::getAuth(); // load company!
$au = $this->getAuthUser();
if (!$au) {
$this->jerror("LOGIN-NOAUTH", "Not authenticated", array('authFailure' => true));
}
if (!$au->pid() ) { // not set up yet..
$this->jerror("LOGIN-NOAUTH", "Not authenticated", array('authFailure' => true));
}
$this->authUser = $au;
return true;
}
function get($table, $opts = Array()) {
$d = DB_DAtaObject::Factory($table);
if (method_exists($d, 'availableColumns')) {
$cols = $d->availableColumns();
} else {
$re = $d->autoJoin();
//echo '<PRE>';print_r($re);
$cols = $re['cols'] ;
$types = array();
$tables = array();
$schemas = array($table => $d->table());
foreach($cols as $name=>$table_col) {
list($tbl, $col) = explode('.', $table_col);
if (!isset($schemas[$tbl])) {
$schemas[$tbl] = DB_DataObject::Factory($tbl)->table();
}
$types[$name] = $schemas[$tbl][$col];
$tables[$name] = $tbl;
}
foreach($re['join_names'] as $c=>$f) {
$cols[$c] = $f;
}
}
foreach($cols as $c=>$f) {
$ret[] = array(
'name' => $c,
'val' => $f,
'type' => isset($types[$c]) ? $this->typeToName($types[$c]) : -1,
'table' => isset($tables[$c]) ? $tables[$c] : "",
);
}
$this->jdata($ret);
}
function typeToName($t)
{
switch(true) {
case ($t & 64): return 'text';
case ($t & 32): return 'text';
case ($t & 4 && $t & 8): return 'datetime';
case ($t & 4): return 'date';
case ($t & 8): return 'time';
case ($t & 16): return 'bool';
case ($t & 2): return 'varchar';
case ($t & 1): return 'number';
}
return '??';
}
}