53 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
class Camp {
|
|
|
4 |
protected static $dbStructure;
|
|
|
5 |
public static $myName;
|
|
|
6 |
protected static $viewName = 'view_device_location_owner_type';
|
|
|
7 |
|
59 |
rodolico |
8 |
public function __construct( $id ) {
|
|
|
9 |
global $dbConnection;
|
|
|
10 |
|
|
|
11 |
$this->data = array();
|
|
|
12 |
$fields = array();
|
|
|
13 |
foreach ( static::$dbStructure['table']['fields'] as $key => $data ) {
|
|
|
14 |
if ( $data['type'] != 'calculated' ) { // we have some which are calculated
|
|
|
15 |
$fields[] = $data['fieldname'];
|
|
|
16 |
}
|
|
|
17 |
}
|
|
|
18 |
$query = sprintf( "select %s from %s where %s = %s",
|
|
|
19 |
implode( ',', $fields ),
|
|
|
20 |
static::$dbStructure['table']['tableName'],
|
|
|
21 |
static::$dbStructure['table']['primaryKey'],
|
|
|
22 |
$id
|
|
|
23 |
);
|
|
|
24 |
//print "<pre> Constructor using query\n$query</pre>";
|
|
|
25 |
//print "<pre>Constructor finding dbstructure of\n" . print_r( static::$dbStructure, true) . '</pre>';
|
|
|
26 |
$this->data = $dbConnection->getOneRow( $query );
|
|
|
27 |
|
|
|
28 |
// figure out which fields have calculated set so we can
|
|
|
29 |
// do them next
|
|
|
30 |
$calculatedFields = array();
|
|
|
31 |
foreach ( static::$dbStructure['table']['fields'] as $field => $record ) {
|
|
|
32 |
if ( $record['type'] == 'calculated' || $record['type'] == 'link') {
|
|
|
33 |
$calculatedFields[] = $field;
|
|
|
34 |
}
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
//print "<pre>" . print_r( $calculatedFields, true ) . "</pre>"; die;
|
|
|
38 |
//print "<pre>" . print_r( $this->data, true ) . "</pre>"; die;
|
|
|
39 |
|
|
|
40 |
$query = sprintf( 'select distinct %s from %s %s',
|
|
|
41 |
implode( ',', $calculatedFields ),
|
|
|
42 |
static::$dbStructure['view']['viewName'],
|
|
|
43 |
static::makeWhereClause( array(
|
|
|
44 |
static::$dbStructure['table']['fields']['id']['fieldname'] .
|
|
|
45 |
' = ' .
|
|
|
46 |
$this->data[static::$dbStructure['table']['primaryKey']] )
|
|
|
47 |
)
|
|
|
48 |
);
|
|
|
49 |
//print "<pre>$query</pre>"; die;
|
|
|
50 |
$result = $dbConnection->getOneRow( $query );
|
|
|
51 |
foreach ( $calculatedFields as $field ) {
|
|
|
52 |
$this->data[$field] = empty( $result[$field] ) ? '' : $result[$field];
|
|
|
53 |
} // foreach
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public function __get( $fieldname ) {
|
|
|
57 |
return
|
|
|
58 |
isset( $this->data[static::$dbStructure['table']['fields'][$fieldname]['fieldname']] ) ?
|
|
|
59 |
$this->data[static::$dbStructure['table']['fields'][$fieldname]['fieldname']] :
|
|
|
60 |
null;
|
|
|
61 |
} // __get
|
|
|
62 |
|
|
|
63 |
|
55 |
rodolico |
64 |
/**
|
|
|
65 |
* Gets stats for an extended class.
|
|
|
66 |
*
|
|
|
67 |
* Gets number of active and inactive rows for a class, using the
|
|
|
68 |
* view ($viewName)
|
|
|
69 |
*
|
|
|
70 |
* @return int[] array of integers which is a count
|
|
|
71 |
*/
|
53 |
rodolico |
72 |
public static function getStats () {
|
|
|
73 |
global $dbConnection;
|
57 |
rodolico |
74 |
global $activeOnly;
|
53 |
rodolico |
75 |
|
|
|
76 |
$return = array();
|
|
|
77 |
$restrictions = array();
|
56 |
rodolico |
78 |
$trueClass = get_called_class();
|
57 |
rodolico |
79 |
$saveActive = $activeOnly;
|
|
|
80 |
$activeOnly = true;
|
54 |
rodolico |
81 |
|
|
|
82 |
$active = sprintf(
|
57 |
rodolico |
83 |
'select count(distinct %s) from %s %s',
|
54 |
rodolico |
84 |
static::$dbStructure['table']['primaryKey'],
|
|
|
85 |
self::$viewName,
|
57 |
rodolico |
86 |
self::makeWhereClause()
|
|
|
87 |
);
|
|
|
88 |
|
|
|
89 |
$activeOnly = false;
|
54 |
rodolico |
90 |
$inactive = sprintf(
|
57 |
rodolico |
91 |
'select count(distinct %s) from %s %s',
|
54 |
rodolico |
92 |
static::$dbStructure['table']['primaryKey'],
|
|
|
93 |
self::$viewName,
|
57 |
rodolico |
94 |
self::makeWhereClause( array( strtolower( $trueClass ) . "_removed is not null" ) )
|
|
|
95 |
);
|
|
|
96 |
$activeOnly = $saveActive;
|
|
|
97 |
|
|
|
98 |
//print "<pre>Active\n$active</pre><pre>Inactive\n$inactive</pre>"; die;
|
|
|
99 |
|
54 |
rodolico |
100 |
$return['active'] = $dbConnection->getOneDBValue( $active );
|
|
|
101 |
$return['inactive'] = $dbConnection->getOneDBValue( $inactive );
|
53 |
rodolico |
102 |
return $return;
|
|
|
103 |
}
|
|
|
104 |
|
57 |
rodolico |
105 |
protected static function makeWhereClause( $restrictions = array() ) {
|
|
|
106 |
global $activeOnly;
|
|
|
107 |
|
|
|
108 |
//print "<pre>" . print_r( $restrictions, true ) . "</pre>";
|
|
|
109 |
|
|
|
110 |
$trueClass = get_called_class();
|
|
|
111 |
foreach ( $_SESSION['restrictions'] as $class => $restriction ) {
|
|
|
112 |
$restrictions[] = $_SESSION['restrictions'][$class];
|
|
|
113 |
}
|
|
|
114 |
if ( $activeOnly ) {
|
|
|
115 |
$restrictions[] = strtolower( $trueClass ) . '_removed is null';
|
|
|
116 |
}
|
|
|
117 |
return count( $restrictions ) ? 'where ' . implode( ' and ', $restrictions ) : '';
|
|
|
118 |
} //
|
|
|
119 |
|
56 |
rodolico |
120 |
public static function showSelectionList( $filter = array(), $list = array() ) {
|
53 |
rodolico |
121 |
global $url;
|
59 |
rodolico |
122 |
$return = array();
|
56 |
rodolico |
123 |
if ( empty( $list ) ) {
|
|
|
124 |
$list = self::getAll( $filter );
|
|
|
125 |
}
|
|
|
126 |
$module = get_called_class();
|
53 |
rodolico |
127 |
foreach ( $list as $id => $name ) {
|
56 |
rodolico |
128 |
$return[] = "<a href='$url?module=$module&id=$id'>$name</a>";
|
53 |
rodolico |
129 |
}
|
59 |
rodolico |
130 |
return "<ul>\n<li>" . implode( "</li>\n<li>", $return ) . "</li>\n</ul>";
|
53 |
rodolico |
131 |
}
|
|
|
132 |
|
56 |
rodolico |
133 |
public static function getAll( $filter = array() ) {
|
53 |
rodolico |
134 |
global $activeOnly;
|
|
|
135 |
global $dbConnection;
|
|
|
136 |
|
59 |
rodolico |
137 |
//print "<pre>" . print_r( $filter, true ) . "</pre>";
|
56 |
rodolico |
138 |
if ( isset( $_REQUEST['to_find'] ) ) {
|
|
|
139 |
$filter[] = sprintf( " %s like '%%%s%%'", static::$dbStructure['view']['selectionDisplay'], $_REQUEST['to_find']) ;
|
|
|
140 |
}
|
|
|
141 |
|
57 |
rodolico |
142 |
|
53 |
rodolico |
143 |
$return = array();
|
57 |
rodolico |
144 |
$query = sprintf( 'select distinct %s id, %s name from %s %s',
|
56 |
rodolico |
145 |
static::$dbStructure['view']['primaryKey'],
|
|
|
146 |
static::$dbStructure['view']['selectionDisplay'],
|
|
|
147 |
static::$dbStructure['view']['viewName'],
|
57 |
rodolico |
148 |
self::makeWhereClause( $filter )
|
53 |
rodolico |
149 |
);
|
56 |
rodolico |
150 |
$query .= " order by " . static::$dbStructure['view']['selectionDisplay'];
|
59 |
rodolico |
151 |
//print "<pre>$query</pre>\n";
|
53 |
rodolico |
152 |
$result = $dbConnection->doSQL( $query );
|
|
|
153 |
foreach ( $result['returnData'] as $row ) {
|
|
|
154 |
$return[$row['id']] = $row['name'];
|
|
|
155 |
}
|
|
|
156 |
return $return;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
public function __toString() {
|
|
|
160 |
return $this->name;
|
|
|
161 |
}
|
|
|
162 |
|
56 |
rodolico |
163 |
public function display() {
|
59 |
rodolico |
164 |
global $url;
|
56 |
rodolico |
165 |
$return = array();
|
59 |
rodolico |
166 |
$save = '';
|
|
|
167 |
|
|
|
168 |
/* get the records for this, including from other tables */
|
56 |
rodolico |
169 |
foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
|
59 |
rodolico |
170 |
if ( isset( $record['display'] ) && $record['display'] === false ) // we don't try to show links
|
|
|
171 |
continue;
|
|
|
172 |
if ( isset( $record['link'] ) ) { // this one is supposed to be set up as a link
|
|
|
173 |
$return[] = sprintf(
|
|
|
174 |
"<td>%s</td><td><a href='$url?module=%s&id=%s'>%s</a></td>",
|
|
|
175 |
$record['displayName'],
|
|
|
176 |
static::$dbStructure['table']['fields'][$record['link']]['class'],
|
|
|
177 |
$this->data[static::$dbStructure['table']['fields'][$record['link']]['fieldname']],
|
|
|
178 |
$this->data[$record['fieldname']]
|
|
|
179 |
);
|
|
|
180 |
} else { // just pulling data from the table itself
|
|
|
181 |
$return[] = '<td>' . $record['displayName'] . "</td>\n<td>" . $this->data[$record['fieldname']] . '</td>';
|
|
|
182 |
}
|
56 |
rodolico |
183 |
}
|
59 |
rodolico |
184 |
$return = "<div class='show_record'>\n<table class='show_record'>\n<tr>" . implode("</tr>\n<tr>", $return ) . "</tr>\n</table>\n</div>";
|
|
|
185 |
|
|
|
186 |
/* Now, get the children records */
|
|
|
187 |
if ( isset( $_REQUEST['to_find'] ) ) {
|
|
|
188 |
$save = $_REQUEST['to_find'];
|
|
|
189 |
unset( $_REQUEST['to_find'] );
|
|
|
190 |
}
|
|
|
191 |
foreach ( static::$dbStructure['children'] as $class ) {
|
|
|
192 |
$return .= "<div class='show_record'>\n<h3>$class</h3>\n" . $class::showSelectionList( array( static::$dbStructure['table']['primaryKey'] . ' = ' . $this->data[static::$dbStructure['table']['primaryKey']] ) ) . '</div>';
|
|
|
193 |
}
|
|
|
194 |
if ( $save !== null ) {
|
|
|
195 |
$_REQUEST['to_find'] = $save;
|
|
|
196 |
}
|
|
|
197 |
return $return;
|
56 |
rodolico |
198 |
}
|
|
|
199 |
|
|
|
200 |
public function run() {
|
|
|
201 |
return $this->display();
|
|
|
202 |
}
|
53 |
rodolico |
203 |
} // abstract class Camp
|
|
|
204 |
|
|
|
205 |
?>
|