53 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
class Camp {
|
|
|
4 |
protected static $dbStructure;
|
61 |
rodolico |
5 |
//public static $myName;
|
|
|
6 |
//protected static $viewName = 'view_device_location_owner_type';
|
|
|
7 |
|
|
|
8 |
/**
|
|
|
9 |
* Builds an instance and loads data from database
|
|
|
10 |
*
|
|
|
11 |
* #parameter int $id The id of the record to load
|
|
|
12 |
*/
|
59 |
rodolico |
13 |
public function __construct( $id ) {
|
|
|
14 |
global $dbConnection;
|
|
|
15 |
|
|
|
16 |
$this->data = array();
|
|
|
17 |
$fields = array();
|
|
|
18 |
foreach ( static::$dbStructure['table']['fields'] as $key => $data ) {
|
|
|
19 |
if ( $data['type'] != 'calculated' ) { // we have some which are calculated
|
|
|
20 |
$fields[] = $data['fieldname'];
|
|
|
21 |
}
|
|
|
22 |
}
|
|
|
23 |
$query = sprintf( "select %s from %s where %s = %s",
|
|
|
24 |
implode( ',', $fields ),
|
|
|
25 |
static::$dbStructure['table']['tableName'],
|
|
|
26 |
static::$dbStructure['table']['primaryKey'],
|
|
|
27 |
$id
|
|
|
28 |
);
|
|
|
29 |
//print "<pre> Constructor using query\n$query</pre>";
|
|
|
30 |
//print "<pre>Constructor finding dbstructure of\n" . print_r( static::$dbStructure, true) . '</pre>';
|
61 |
rodolico |
31 |
if ( ( $this->data = $dbConnection->getOneRow( $query ) ) === false )
|
|
|
32 |
print DBQuery::error2String();
|
59 |
rodolico |
33 |
|
|
|
34 |
// figure out which fields have calculated set so we can
|
|
|
35 |
// do them next
|
|
|
36 |
$calculatedFields = array();
|
|
|
37 |
foreach ( static::$dbStructure['table']['fields'] as $field => $record ) {
|
|
|
38 |
if ( $record['type'] == 'calculated' || $record['type'] == 'link') {
|
|
|
39 |
$calculatedFields[] = $field;
|
|
|
40 |
}
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
//print "<pre>" . print_r( $calculatedFields, true ) . "</pre>"; die;
|
|
|
44 |
//print "<pre>" . print_r( $this->data, true ) . "</pre>"; die;
|
61 |
rodolico |
45 |
if ( count( $calculatedFields ) ) {
|
|
|
46 |
$query = sprintf( 'select distinct %s from %s %s',
|
|
|
47 |
implode( ',', $calculatedFields ),
|
|
|
48 |
static::$dbStructure['view']['viewName'],
|
|
|
49 |
static::makeWhereClause( array(
|
|
|
50 |
static::$dbStructure['table']['fields']['id']['fieldname'] .
|
|
|
51 |
' = ' .
|
|
|
52 |
$this->data[static::$dbStructure['table']['primaryKey']] )
|
|
|
53 |
)
|
|
|
54 |
);
|
|
|
55 |
//print "<pre>$query</pre>"; die;
|
|
|
56 |
$result = $dbConnection->getOneRow( $query );
|
|
|
57 |
foreach ( $calculatedFields as $field ) {
|
|
|
58 |
$this->data[$field] = empty( $result[$field] ) ? '' : $result[$field];
|
|
|
59 |
} // foreach
|
|
|
60 |
}
|
59 |
rodolico |
61 |
}
|
|
|
62 |
|
61 |
rodolico |
63 |
/**
|
|
|
64 |
* gets the contents of $fieldname
|
|
|
65 |
*
|
|
|
66 |
* @parameter string $fieldname name of the database field to retrieve
|
|
|
67 |
*
|
|
|
68 |
* @return string The value of $fieldname stored in structure
|
|
|
69 |
*/
|
59 |
rodolico |
70 |
public function __get( $fieldname ) {
|
|
|
71 |
return
|
|
|
72 |
isset( $this->data[static::$dbStructure['table']['fields'][$fieldname]['fieldname']] ) ?
|
|
|
73 |
$this->data[static::$dbStructure['table']['fields'][$fieldname]['fieldname']] :
|
|
|
74 |
null;
|
|
|
75 |
} // __get
|
|
|
76 |
|
|
|
77 |
|
55 |
rodolico |
78 |
/**
|
|
|
79 |
* Gets stats for an extended class.
|
|
|
80 |
*
|
|
|
81 |
* Gets number of active and inactive rows for a class, using the
|
|
|
82 |
* view ($viewName)
|
|
|
83 |
*
|
|
|
84 |
* @return int[] array of integers which is a count
|
|
|
85 |
*/
|
53 |
rodolico |
86 |
public static function getStats () {
|
|
|
87 |
global $dbConnection;
|
57 |
rodolico |
88 |
global $activeOnly;
|
53 |
rodolico |
89 |
|
|
|
90 |
$return = array();
|
|
|
91 |
$restrictions = array();
|
56 |
rodolico |
92 |
$trueClass = get_called_class();
|
57 |
rodolico |
93 |
$saveActive = $activeOnly;
|
|
|
94 |
$activeOnly = true;
|
54 |
rodolico |
95 |
|
|
|
96 |
$active = sprintf(
|
57 |
rodolico |
97 |
'select count(distinct %s) from %s %s',
|
54 |
rodolico |
98 |
static::$dbStructure['table']['primaryKey'],
|
61 |
rodolico |
99 |
static::$dbStructure['view']['viewName'],
|
57 |
rodolico |
100 |
self::makeWhereClause()
|
|
|
101 |
);
|
|
|
102 |
|
|
|
103 |
$activeOnly = false;
|
54 |
rodolico |
104 |
$inactive = sprintf(
|
57 |
rodolico |
105 |
'select count(distinct %s) from %s %s',
|
54 |
rodolico |
106 |
static::$dbStructure['table']['primaryKey'],
|
61 |
rodolico |
107 |
static::$dbStructure['view']['viewName'],
|
57 |
rodolico |
108 |
self::makeWhereClause( array( strtolower( $trueClass ) . "_removed is not null" ) )
|
|
|
109 |
);
|
|
|
110 |
$activeOnly = $saveActive;
|
|
|
111 |
|
|
|
112 |
//print "<pre>Active\n$active</pre><pre>Inactive\n$inactive</pre>"; die;
|
|
|
113 |
|
54 |
rodolico |
114 |
$return['active'] = $dbConnection->getOneDBValue( $active );
|
|
|
115 |
$return['inactive'] = $dbConnection->getOneDBValue( $inactive );
|
53 |
rodolico |
116 |
return $return;
|
|
|
117 |
}
|
|
|
118 |
|
61 |
rodolico |
119 |
/**
|
|
|
120 |
* Creates a where clause
|
|
|
121 |
*
|
|
|
122 |
* Merges $restrictions with any restrictions which may be in place
|
|
|
123 |
* for the current user. Also sets 'class_removed is null' if
|
|
|
124 |
* $activeOnly (global) is set
|
|
|
125 |
*
|
|
|
126 |
* @parameter string[] $restrictions array of conditionals
|
|
|
127 |
*
|
|
|
128 |
* @return string all restricions ANDED together
|
|
|
129 |
*/
|
57 |
rodolico |
130 |
protected static function makeWhereClause( $restrictions = array() ) {
|
|
|
131 |
global $activeOnly;
|
|
|
132 |
|
|
|
133 |
//print "<pre>" . print_r( $restrictions, true ) . "</pre>";
|
|
|
134 |
|
|
|
135 |
$trueClass = get_called_class();
|
|
|
136 |
foreach ( $_SESSION['restrictions'] as $class => $restriction ) {
|
|
|
137 |
$restrictions[] = $_SESSION['restrictions'][$class];
|
|
|
138 |
}
|
|
|
139 |
if ( $activeOnly ) {
|
|
|
140 |
$restrictions[] = strtolower( $trueClass ) . '_removed is null';
|
|
|
141 |
}
|
|
|
142 |
return count( $restrictions ) ? 'where ' . implode( ' and ', $restrictions ) : '';
|
|
|
143 |
} //
|
|
|
144 |
|
61 |
rodolico |
145 |
/**
|
|
|
146 |
* Creates a list of all records for a particular class and returns
|
|
|
147 |
* a UL with all LI's set as links (A's) to displaying a record.
|
|
|
148 |
*
|
|
|
149 |
* @parameter string[] $filter array of conditionals to limit if $list not set
|
|
|
150 |
* @parameter string[] $list An optional list of data to be displayed
|
|
|
151 |
*
|
|
|
152 |
* @return string A UL with each LI containing a link to an entry
|
|
|
153 |
*/
|
56 |
rodolico |
154 |
public static function showSelectionList( $filter = array(), $list = array() ) {
|
53 |
rodolico |
155 |
global $url;
|
59 |
rodolico |
156 |
$return = array();
|
56 |
rodolico |
157 |
if ( empty( $list ) ) {
|
|
|
158 |
$list = self::getAll( $filter );
|
|
|
159 |
}
|
|
|
160 |
$module = get_called_class();
|
53 |
rodolico |
161 |
foreach ( $list as $id => $name ) {
|
56 |
rodolico |
162 |
$return[] = "<a href='$url?module=$module&id=$id'>$name</a>";
|
53 |
rodolico |
163 |
}
|
59 |
rodolico |
164 |
return "<ul>\n<li>" . implode( "</li>\n<li>", $return ) . "</li>\n</ul>";
|
53 |
rodolico |
165 |
}
|
|
|
166 |
|
61 |
rodolico |
167 |
/**
|
|
|
168 |
* Gets all matching rows
|
|
|
169 |
*
|
|
|
170 |
* Does an SQL call to retrieve ID and Name for all rows matching
|
|
|
171 |
* $filter and $_REQUEST['to_find'].
|
|
|
172 |
*
|
|
|
173 |
* @parameter string[] $filter Optional list of conditionals for where clause
|
|
|
174 |
*
|
|
|
175 |
* @return array[] An array of indexes and names matching query
|
|
|
176 |
*/
|
56 |
rodolico |
177 |
public static function getAll( $filter = array() ) {
|
53 |
rodolico |
178 |
global $activeOnly;
|
|
|
179 |
global $dbConnection;
|
|
|
180 |
|
59 |
rodolico |
181 |
//print "<pre>" . print_r( $filter, true ) . "</pre>";
|
56 |
rodolico |
182 |
if ( isset( $_REQUEST['to_find'] ) ) {
|
|
|
183 |
$filter[] = sprintf( " %s like '%%%s%%'", static::$dbStructure['view']['selectionDisplay'], $_REQUEST['to_find']) ;
|
|
|
184 |
}
|
|
|
185 |
|
57 |
rodolico |
186 |
|
53 |
rodolico |
187 |
$return = array();
|
57 |
rodolico |
188 |
$query = sprintf( 'select distinct %s id, %s name from %s %s',
|
56 |
rodolico |
189 |
static::$dbStructure['view']['primaryKey'],
|
|
|
190 |
static::$dbStructure['view']['selectionDisplay'],
|
|
|
191 |
static::$dbStructure['view']['viewName'],
|
57 |
rodolico |
192 |
self::makeWhereClause( $filter )
|
53 |
rodolico |
193 |
);
|
56 |
rodolico |
194 |
$query .= " order by " . static::$dbStructure['view']['selectionDisplay'];
|
59 |
rodolico |
195 |
//print "<pre>$query</pre>\n";
|
53 |
rodolico |
196 |
$result = $dbConnection->doSQL( $query );
|
|
|
197 |
foreach ( $result['returnData'] as $row ) {
|
|
|
198 |
$return[$row['id']] = $row['name'];
|
|
|
199 |
}
|
|
|
200 |
return $return;
|
|
|
201 |
}
|
61 |
rodolico |
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Returns the "name" of an instance
|
|
|
205 |
*
|
|
|
206 |
* Normally, this is $this->name, but some classes may want to put
|
|
|
207 |
* in additional data
|
|
|
208 |
*
|
|
|
209 |
* @return string Display name of this instance
|
|
|
210 |
*/
|
53 |
rodolico |
211 |
public function __toString() {
|
|
|
212 |
return $this->name;
|
|
|
213 |
}
|
|
|
214 |
|
61 |
rodolico |
215 |
/**
|
|
|
216 |
* Returns an HTML structure which can display a record
|
|
|
217 |
*
|
|
|
218 |
* Note that if the instance has children, they will be displayed
|
|
|
219 |
* also. Additionally, if it has fields marked as type 'calculated'
|
|
|
220 |
* it will perform the limited calculations.
|
|
|
221 |
*
|
|
|
222 |
* @return string HTML to display record
|
|
|
223 |
*/
|
56 |
rodolico |
224 |
public function display() {
|
59 |
rodolico |
225 |
global $url;
|
56 |
rodolico |
226 |
$return = array();
|
59 |
rodolico |
227 |
$save = '';
|
|
|
228 |
|
|
|
229 |
/* get the records for this, including from other tables */
|
56 |
rodolico |
230 |
foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
|
59 |
rodolico |
231 |
if ( isset( $record['display'] ) && $record['display'] === false ) // we don't try to show links
|
|
|
232 |
continue;
|
|
|
233 |
if ( isset( $record['link'] ) ) { // this one is supposed to be set up as a link
|
|
|
234 |
$return[] = sprintf(
|
|
|
235 |
"<td>%s</td><td><a href='$url?module=%s&id=%s'>%s</a></td>",
|
|
|
236 |
$record['displayName'],
|
|
|
237 |
static::$dbStructure['table']['fields'][$record['link']]['class'],
|
|
|
238 |
$this->data[static::$dbStructure['table']['fields'][$record['link']]['fieldname']],
|
|
|
239 |
$this->data[$record['fieldname']]
|
|
|
240 |
);
|
|
|
241 |
} else { // just pulling data from the table itself
|
|
|
242 |
$return[] = '<td>' . $record['displayName'] . "</td>\n<td>" . $this->data[$record['fieldname']] . '</td>';
|
|
|
243 |
}
|
56 |
rodolico |
244 |
}
|
61 |
rodolico |
245 |
$return[] = sprintf( "<td colspan='2'><a href='$url?module=%s&id=%s&action=edit'>Edit</a></td>",
|
|
|
246 |
get_called_class(),
|
|
|
247 |
$this->data[static::$dbStructure['table']['primaryKey']]
|
|
|
248 |
);
|
|
|
249 |
|
59 |
rodolico |
250 |
$return = "<div class='show_record'>\n<table class='show_record'>\n<tr>" . implode("</tr>\n<tr>", $return ) . "</tr>\n</table>\n</div>";
|
|
|
251 |
|
|
|
252 |
/* Now, get the children records */
|
|
|
253 |
if ( isset( $_REQUEST['to_find'] ) ) {
|
|
|
254 |
$save = $_REQUEST['to_find'];
|
|
|
255 |
unset( $_REQUEST['to_find'] );
|
|
|
256 |
}
|
|
|
257 |
foreach ( static::$dbStructure['children'] as $class ) {
|
|
|
258 |
$return .= "<div class='show_record'>\n<h3>$class</h3>\n" . $class::showSelectionList( array( static::$dbStructure['table']['primaryKey'] . ' = ' . $this->data[static::$dbStructure['table']['primaryKey']] ) ) . '</div>';
|
|
|
259 |
}
|
|
|
260 |
if ( $save !== null ) {
|
|
|
261 |
$_REQUEST['to_find'] = $save;
|
|
|
262 |
}
|
|
|
263 |
return $return;
|
56 |
rodolico |
264 |
}
|
|
|
265 |
|
61 |
rodolico |
266 |
/**
|
|
|
267 |
* Simple controller. Will determine what action is going on, then
|
|
|
268 |
* call the correct method, returning the HTML required
|
|
|
269 |
*/
|
56 |
rodolico |
270 |
public function run() {
|
|
|
271 |
return $this->display();
|
|
|
272 |
}
|
53 |
rodolico |
273 |
} // abstract class Camp
|
|
|
274 |
|
|
|
275 |
?>
|