53 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
class Camp {
|
|
|
4 |
protected static $dbStructure;
|
61 |
rodolico |
5 |
|
|
|
6 |
/**
|
|
|
7 |
* Builds an instance and loads data from database
|
|
|
8 |
*
|
|
|
9 |
* #parameter int $id The id of the record to load
|
|
|
10 |
*/
|
63 |
rodolico |
11 |
public function __construct( $id = 0 ) {
|
66 |
rodolico |
12 |
if ( $id ) {
|
63 |
rodolico |
13 |
$this->loadFromDB( $id );
|
66 |
rodolico |
14 |
$_SESSION['workingon'][get_called_class()] = $id;
|
|
|
15 |
}
|
63 |
rodolico |
16 |
} // __construct
|
|
|
17 |
|
|
|
18 |
public function loadFromDB( $id ) {
|
69 |
rodolico |
19 |
//print "<pre>Loading from DB\nClass = " . get_called_class() . "\nID = $id\n</pre>";
|
59 |
rodolico |
20 |
global $dbConnection;
|
|
|
21 |
|
|
|
22 |
$this->data = array();
|
|
|
23 |
$fields = array();
|
64 |
rodolico |
24 |
$calculatedFields = array();
|
59 |
rodolico |
25 |
foreach ( static::$dbStructure['table']['fields'] as $key => $data ) {
|
64 |
rodolico |
26 |
switch ( $data['type'] ) {
|
|
|
27 |
case '1:1H' : // one to one with history
|
69 |
rodolico |
28 |
$calculatedFields[$key] = $data;
|
64 |
rodolico |
29 |
break;
|
|
|
30 |
case '1:M' : // one to many
|
69 |
rodolico |
31 |
$calculatedFields[$key] = $data;
|
64 |
rodolico |
32 |
break;
|
|
|
33 |
default: // standard
|
|
|
34 |
$fields[] = $data['fieldname'];
|
59 |
rodolico |
35 |
}
|
|
|
36 |
}
|
|
|
37 |
$query = sprintf( "select %s from %s where %s = %s",
|
|
|
38 |
implode( ',', $fields ),
|
|
|
39 |
static::$dbStructure['table']['tableName'],
|
|
|
40 |
static::$dbStructure['table']['primaryKey'],
|
|
|
41 |
$id
|
|
|
42 |
);
|
|
|
43 |
//print "<pre> Constructor using query\n$query</pre>";
|
|
|
44 |
//print "<pre>Constructor finding dbstructure of\n" . print_r( static::$dbStructure, true) . '</pre>';
|
61 |
rodolico |
45 |
if ( ( $this->data = $dbConnection->getOneRow( $query ) ) === false )
|
|
|
46 |
print DBQuery::error2String();
|
59 |
rodolico |
47 |
|
69 |
rodolico |
48 |
// note, we had to wait to do the calculated fields since they needed the primary key from this record
|
|
|
49 |
foreach ( $calculatedFields as $key => $data ) {
|
|
|
50 |
switch ( $data['type'] ) {
|
|
|
51 |
case '1:1H' : // one to one with history
|
|
|
52 |
$this->data[$key] = $this->oneToOneHistoryLoad( $data );
|
|
|
53 |
break;
|
|
|
54 |
case '1:M' : // one to many
|
|
|
55 |
$this->data[$key] = $this->oneToManyLoad( $data );
|
|
|
56 |
break;
|
|
|
57 |
default:
|
|
|
58 |
print "<pre>Error, I do not know how to load calculated field type $data[type]</pre>";
|
|
|
59 |
} // switch
|
|
|
60 |
} // foreach
|
64 |
rodolico |
61 |
//print "<pre>Data after loadFromDB\n" . print_r($this->data, true) . "</pre>"; die;
|
63 |
rodolico |
62 |
} // loadFromDB
|
59 |
rodolico |
63 |
|
61 |
rodolico |
64 |
/**
|
|
|
65 |
* gets the contents of $fieldname
|
|
|
66 |
*
|
|
|
67 |
* @parameter string $fieldname name of the database field to retrieve
|
|
|
68 |
*
|
|
|
69 |
* @return string The value of $fieldname stored in structure
|
|
|
70 |
*/
|
59 |
rodolico |
71 |
public function __get( $fieldname ) {
|
66 |
rodolico |
72 |
if ( empty( $this->data ) )
|
|
|
73 |
$this->data = array();
|
59 |
rodolico |
74 |
return
|
|
|
75 |
isset( $this->data[static::$dbStructure['table']['fields'][$fieldname]['fieldname']] ) ?
|
|
|
76 |
$this->data[static::$dbStructure['table']['fields'][$fieldname]['fieldname']] :
|
|
|
77 |
null;
|
|
|
78 |
} // __get
|
|
|
79 |
|
|
|
80 |
|
55 |
rodolico |
81 |
/**
|
|
|
82 |
* Gets stats for an extended class.
|
|
|
83 |
*
|
|
|
84 |
* Gets number of active and inactive rows for a class, using the
|
|
|
85 |
* view ($viewName)
|
|
|
86 |
*
|
|
|
87 |
* @return int[] array of integers which is a count
|
|
|
88 |
*/
|
53 |
rodolico |
89 |
public static function getStats () {
|
|
|
90 |
global $dbConnection;
|
57 |
rodolico |
91 |
global $activeOnly;
|
53 |
rodolico |
92 |
|
|
|
93 |
$return = array();
|
|
|
94 |
$restrictions = array();
|
56 |
rodolico |
95 |
$trueClass = get_called_class();
|
57 |
rodolico |
96 |
$saveActive = $activeOnly;
|
66 |
rodolico |
97 |
$activeOnly = false;
|
|
|
98 |
/*
|
|
|
99 |
$query = sprintf(
|
|
|
100 |
'select count(*) num,isnull(removed) active from %s %s group by isnull(removed) order by isnull(removed) desc',
|
|
|
101 |
static::$dbStructure['table']['tableName'],
|
|
|
102 |
self::makeWhereClause()
|
|
|
103 |
);
|
|
|
104 |
$result = $dbConnection->doSQL( $query );
|
|
|
105 |
foreach ( $result['returnData'] as $row ) {
|
|
|
106 |
if ( $row['active'] == 1 ) {
|
|
|
107 |
$return['active'] = $row['num'];
|
|
|
108 |
} elseif ( $row['active'] == 0 ) {
|
|
|
109 |
$return['inactive'] = $row['num'];
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
*/
|
69 |
rodolico |
113 |
if ( isset( static::$dbStructure['table']['inactiveField'] ) ) {
|
|
|
114 |
$query = sprintf(
|
|
|
115 |
'select count(*) num, isnull(%s) active from %s %s group by isnull(%s) order by isnull(%s) desc',
|
|
|
116 |
static::$dbStructure['table']['inactiveField'],
|
|
|
117 |
static::$dbStructure['table']['tableName'],
|
|
|
118 |
self::makeWhereClause(),
|
|
|
119 |
static::$dbStructure['table']['inactiveField'],
|
|
|
120 |
static::$dbStructure['table']['inactiveField']
|
|
|
121 |
);
|
|
|
122 |
$activeOnly = $saveActive;
|
|
|
123 |
$result = $dbConnection->doSQL( $query );
|
|
|
124 |
foreach ( $result['returnData'] as $row ) {
|
|
|
125 |
if ( $row['active'] == 1 ) {
|
|
|
126 |
$return['active'] = $row['num'];
|
|
|
127 |
} elseif ( $row['active'] == 0 ) {
|
|
|
128 |
$return['inactive'] = $row['num'];
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
} else {
|
|
|
132 |
$query = sprintf(
|
|
|
133 |
'select count(*) num from %s %s',
|
|
|
134 |
static::$dbStructure['table']['tableName'],
|
|
|
135 |
self::makeWhereClause()
|
|
|
136 |
);
|
|
|
137 |
$return['total'] = $dbConnection->getOneDBValue( $query );
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
|
|
|
141 |
/*
|
54 |
rodolico |
142 |
$active = sprintf(
|
69 |
rodolico |
143 |
'select count(%s) from %s %s',
|
54 |
rodolico |
144 |
static::$dbStructure['table']['primaryKey'],
|
69 |
rodolico |
145 |
static::$dbStructure['table']['tableName'],
|
57 |
rodolico |
146 |
self::makeWhereClause()
|
|
|
147 |
);
|
|
|
148 |
|
|
|
149 |
$activeOnly = false;
|
54 |
rodolico |
150 |
$inactive = sprintf(
|
69 |
rodolico |
151 |
'select count(%s) from %s %s',
|
54 |
rodolico |
152 |
static::$dbStructure['table']['primaryKey'],
|
69 |
rodolico |
153 |
static::$dbStructure['table']['tableName'],
|
|
|
154 |
self::makeWhereClause( "removed is not null" ) )
|
57 |
rodolico |
155 |
);
|
|
|
156 |
|
|
|
157 |
//print "<pre>Active\n$active</pre><pre>Inactive\n$inactive</pre>"; die;
|
|
|
158 |
|
54 |
rodolico |
159 |
$return['active'] = $dbConnection->getOneDBValue( $active );
|
|
|
160 |
$return['inactive'] = $dbConnection->getOneDBValue( $inactive );
|
69 |
rodolico |
161 |
*/
|
66 |
rodolico |
162 |
|
|
|
163 |
$activeOnly = $saveActive;
|
53 |
rodolico |
164 |
return $return;
|
|
|
165 |
}
|
|
|
166 |
|
61 |
rodolico |
167 |
/**
|
|
|
168 |
* Creates a where clause
|
|
|
169 |
*
|
|
|
170 |
* Merges $restrictions with any restrictions which may be in place
|
|
|
171 |
* for the current user. Also sets 'class_removed is null' if
|
|
|
172 |
* $activeOnly (global) is set
|
|
|
173 |
*
|
|
|
174 |
* @parameter string[] $restrictions array of conditionals
|
|
|
175 |
*
|
|
|
176 |
* @return string all restricions ANDED together
|
|
|
177 |
*/
|
68 |
rodolico |
178 |
public static function makeWhereClause( $restrictions = array(), $restrictGlobals = array(), $tablename = null ) {
|
57 |
rodolico |
179 |
global $activeOnly;
|
|
|
180 |
|
|
|
181 |
//print "<pre>" . print_r( $restrictions, true ) . "</pre>";
|
|
|
182 |
|
|
|
183 |
$trueClass = get_called_class();
|
|
|
184 |
foreach ( $_SESSION['restrictions'] as $class => $restriction ) {
|
|
|
185 |
$restrictions[] = $_SESSION['restrictions'][$class];
|
|
|
186 |
}
|
69 |
rodolico |
187 |
if ( $activeOnly && isset( $trueClass::$dbStructure['table']['inactiveField'] ) ) {
|
|
|
188 |
$restrictions[] = $trueClass::$dbStructure['table']['inactiveField'] . ' is null';
|
57 |
rodolico |
189 |
}
|
66 |
rodolico |
190 |
/*
|
|
|
191 |
if ( isset( $restrictGlobals['workingon'] ) && isset( $_SESSION['workingon'] ) ) {
|
|
|
192 |
foreach ( $_SESSION['workingon'] as $class => $id ) {
|
|
|
193 |
$restrictions[] = strtolower( $class ) . "_id = $id";
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
*/
|
57 |
rodolico |
197 |
return count( $restrictions ) ? 'where ' . implode( ' and ', $restrictions ) : '';
|
|
|
198 |
} //
|
|
|
199 |
|
61 |
rodolico |
200 |
/**
|
|
|
201 |
* Creates a list of all records for a particular class and returns
|
|
|
202 |
* a UL with all LI's set as links (A's) to displaying a record.
|
|
|
203 |
*
|
|
|
204 |
* @parameter string[] $filter array of conditionals to limit if $list not set
|
|
|
205 |
* @parameter string[] $list An optional list of data to be displayed
|
|
|
206 |
*
|
|
|
207 |
* @return string A UL with each LI containing a link to an entry
|
|
|
208 |
*/
|
66 |
rodolico |
209 |
public static function showSelectionList( $filter = array(), $list = array(), $noAdd = false ) {
|
53 |
rodolico |
210 |
global $url;
|
59 |
rodolico |
211 |
$return = array();
|
66 |
rodolico |
212 |
$module = get_called_class();
|
|
|
213 |
unset ( $_SESSION['workingon'][get_called_class()] );
|
56 |
rodolico |
214 |
if ( empty( $list ) ) {
|
|
|
215 |
$list = self::getAll( $filter );
|
|
|
216 |
}
|
64 |
rodolico |
217 |
//print "<pre>showSelectionList\n" . print_r( $list, true ) . "</pre>"; die;
|
53 |
rodolico |
218 |
foreach ( $list as $id => $name ) {
|
64 |
rodolico |
219 |
if ( $id )
|
|
|
220 |
$return[] = "<a href='$url?module=$module&id=$id'>$name</a>";
|
53 |
rodolico |
221 |
}
|
66 |
rodolico |
222 |
//print "<pre>noadd is $noAdd</pre>"; die;
|
|
|
223 |
if ( ! $noAdd && $_SESSION['user']->isAuthorized( strtolower($module) . '_add') )
|
|
|
224 |
$return[] = "<br /><a href='$url?module=$module&action=add'>Add New</a>";
|
64 |
rodolico |
225 |
return count($return) ? "<ul>\n<li>" . implode( "</li>\n<li>", $return ) . "</li>\n</ul>" : '';
|
53 |
rodolico |
226 |
}
|
|
|
227 |
|
61 |
rodolico |
228 |
/**
|
|
|
229 |
* Gets all matching rows
|
|
|
230 |
*
|
|
|
231 |
* Does an SQL call to retrieve ID and Name for all rows matching
|
|
|
232 |
* $filter and $_REQUEST['to_find'].
|
|
|
233 |
*
|
|
|
234 |
* @parameter string[] $filter Optional list of conditionals for where clause
|
|
|
235 |
*
|
|
|
236 |
* @return array[] An array of indexes and names matching query
|
|
|
237 |
*/
|
66 |
rodolico |
238 |
public static function getAll( $filter = array(), $restrictGlobals = array() ) {
|
53 |
rodolico |
239 |
global $activeOnly;
|
|
|
240 |
global $dbConnection;
|
|
|
241 |
|
59 |
rodolico |
242 |
//print "<pre>" . print_r( $filter, true ) . "</pre>";
|
56 |
rodolico |
243 |
if ( isset( $_REQUEST['to_find'] ) ) {
|
69 |
rodolico |
244 |
$filter[] = sprintf( " %s like '%%%s%%'", static::$dbStructure['table']['selectionDisplay'], $_REQUEST['to_find']) ;
|
56 |
rodolico |
245 |
}
|
53 |
rodolico |
246 |
$return = array();
|
69 |
rodolico |
247 |
$query = sprintf( 'select %s id, %s name from %s %s',
|
|
|
248 |
static::$dbStructure['table']['primaryKey'],
|
|
|
249 |
static::$dbStructure['table']['selectionDisplay'],
|
|
|
250 |
static::$dbStructure['table']['tableName'],
|
66 |
rodolico |
251 |
self::makeWhereClause( $filter, $restrictGlobals )
|
53 |
rodolico |
252 |
);
|
69 |
rodolico |
253 |
$query .= " order by " . static::$dbStructure['table']['selectionDisplay'];
|
59 |
rodolico |
254 |
//print "<pre>$query</pre>\n";
|
53 |
rodolico |
255 |
$result = $dbConnection->doSQL( $query );
|
64 |
rodolico |
256 |
$result = $result['returnData'];
|
|
|
257 |
foreach ( $result as $row ) {
|
53 |
rodolico |
258 |
$return[$row['id']] = $row['name'];
|
|
|
259 |
}
|
|
|
260 |
return $return;
|
|
|
261 |
}
|
61 |
rodolico |
262 |
|
|
|
263 |
/**
|
|
|
264 |
* Returns the "name" of an instance
|
|
|
265 |
*
|
|
|
266 |
* Normally, this is $this->name, but some classes may want to put
|
|
|
267 |
* in additional data
|
|
|
268 |
*
|
|
|
269 |
* @return string Display name of this instance
|
|
|
270 |
*/
|
53 |
rodolico |
271 |
public function __toString() {
|
|
|
272 |
return $this->name;
|
|
|
273 |
}
|
|
|
274 |
|
61 |
rodolico |
275 |
/**
|
|
|
276 |
* Returns an HTML structure which can display a record
|
|
|
277 |
*
|
|
|
278 |
* Note that if the instance has children, they will be displayed
|
|
|
279 |
* also. Additionally, if it has fields marked as type 'calculated'
|
|
|
280 |
* it will perform the limited calculations.
|
|
|
281 |
*
|
|
|
282 |
* @return string HTML to display record
|
|
|
283 |
*/
|
56 |
rodolico |
284 |
public function display() {
|
69 |
rodolico |
285 |
//print "<pre>In Display\n" . print_r( $this->data, true) . '</pre>'; die;
|
59 |
rodolico |
286 |
global $url;
|
56 |
rodolico |
287 |
$return = array();
|
59 |
rodolico |
288 |
$save = '';
|
|
|
289 |
|
|
|
290 |
/* get the records for this, including from other tables */
|
56 |
rodolico |
291 |
foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
|
59 |
rodolico |
292 |
if ( isset( $record['display'] ) && $record['display'] === false ) // we don't try to show links
|
|
|
293 |
continue;
|
64 |
rodolico |
294 |
if ( $record['type'] == '1:1H' ) { // this one is supposed to be set up as a link
|
59 |
rodolico |
295 |
$return[] = sprintf(
|
|
|
296 |
"<td>%s</td><td><a href='$url?module=%s&id=%s'>%s</a></td>",
|
|
|
297 |
$record['displayName'],
|
64 |
rodolico |
298 |
$record['class'],
|
69 |
rodolico |
299 |
$this->data[$key]['id'],
|
|
|
300 |
$this->data[$key]['display']
|
59 |
rodolico |
301 |
);
|
69 |
rodolico |
302 |
} elseif ( $record['type'] == '1:M' ) { // this is an array in data, so we just grab the values
|
|
|
303 |
$return[] = '<td>' . $record['displayName'] . "</td>\n<td>" . implode( ',', array_values( $this->data[$key] ) ) . '</td>';
|
59 |
rodolico |
304 |
} else { // just pulling data from the table itself
|
|
|
305 |
$return[] = '<td>' . $record['displayName'] . "</td>\n<td>" . $this->data[$record['fieldname']] . '</td>';
|
|
|
306 |
}
|
56 |
rodolico |
307 |
}
|
66 |
rodolico |
308 |
if ( $_SESSION['user']->isAuthorized( strtolower(get_called_class()) . '_edit') )
|
|
|
309 |
$return[] = sprintf( "<td colspan='2'><a href='$url?module=%s&id=%s&action=edit'>Edit</a></td>",
|
|
|
310 |
get_called_class(),
|
|
|
311 |
$this->data[static::$dbStructure['table']['primaryKey']]
|
|
|
312 |
);
|
61 |
rodolico |
313 |
|
59 |
rodolico |
314 |
$return = "<div class='show_record'>\n<table class='show_record'>\n<tr>" . implode("</tr>\n<tr>", $return ) . "</tr>\n</table>\n</div>";
|
|
|
315 |
|
|
|
316 |
/* Now, get the children records */
|
|
|
317 |
if ( isset( $_REQUEST['to_find'] ) ) {
|
|
|
318 |
$save = $_REQUEST['to_find'];
|
|
|
319 |
unset( $_REQUEST['to_find'] );
|
|
|
320 |
}
|
64 |
rodolico |
321 |
foreach ( static::$dbStructure['children'] as $class => $record) {
|
|
|
322 |
if ( isset( $record['filter'] ) ) { // we'll parse it against $this->data to fill in any variables
|
|
|
323 |
global $dbConnection;
|
|
|
324 |
$record['filter'] = $dbConnection->templateReplace( $record['filter'], $this->data );
|
|
|
325 |
//print "<pre>$filter\n" . print_r( $this->data, true) . "</pre>"; die;
|
|
|
326 |
} else {
|
69 |
rodolico |
327 |
if ( isset( $record['intermediateTable'] ) ) {
|
|
|
328 |
// they have defined an intermediate table, so we need to use that
|
|
|
329 |
$record['filter'] = sprintf(
|
|
|
330 |
'%s in (select %s from %s where %s = %s and removed is null)',
|
|
|
331 |
$record['remoteID'],
|
|
|
332 |
$record['remoteID'],
|
|
|
333 |
$record['intermediateTable'],
|
|
|
334 |
$record['myID'],
|
|
|
335 |
$this->id
|
|
|
336 |
);
|
|
|
337 |
} else {
|
|
|
338 |
$record['filter'] = static::$dbStructure['table']['primaryKey'] . ' = ' . $this->data[static::$dbStructure['table']['primaryKey']];
|
|
|
339 |
}
|
64 |
rodolico |
340 |
}
|
|
|
341 |
if ( ! isset( $record['name'] ) )
|
|
|
342 |
$record['name'] = $class;
|
|
|
343 |
//print "<pre>record\n" . print_r( $record, true) . "</pre>"; die;
|
69 |
rodolico |
344 |
|
66 |
rodolico |
345 |
$found = $class::showSelectionList( array( $record['filter'] ), array(), isset( $record['noAdd'] ) );
|
64 |
rodolico |
346 |
//print "<pre>[$found]</pre>"; die;
|
|
|
347 |
if ( $found )
|
66 |
rodolico |
348 |
$return .= "<div class='show_record'>\n<h3>$record[name]</h3>\n" . $found . '</div>';
|
59 |
rodolico |
349 |
}
|
|
|
350 |
if ( $save !== null ) {
|
|
|
351 |
$_REQUEST['to_find'] = $save;
|
|
|
352 |
}
|
|
|
353 |
return $return;
|
66 |
rodolico |
354 |
} // display
|
56 |
rodolico |
355 |
|
61 |
rodolico |
356 |
/**
|
63 |
rodolico |
357 |
* Function is basically a placeholder in case a extended class
|
|
|
358 |
* needs to do some additional processing when editing
|
|
|
359 |
*
|
|
|
360 |
* In some cases, the edit function can not handle additional fields.
|
|
|
361 |
* See the Device class, where device_type is a table with many-to-many
|
|
|
362 |
* linkages to device. Since this type of thing is very rare, I decided
|
|
|
363 |
* to just allow edit and post to call an additional function in
|
|
|
364 |
* those cases.
|
|
|
365 |
*
|
|
|
366 |
* Based on the action requested, will either return an HTML string
|
|
|
367 |
* which will be placed in the edit screen (for edit), or an array
|
|
|
368 |
* of SQL to be executed to update/add rows (for post).
|
|
|
369 |
*
|
|
|
370 |
* If the action is edit, there should be a <td></td> around the
|
|
|
371 |
* whole thing
|
|
|
372 |
*/
|
|
|
373 |
|
|
|
374 |
protected function editAdditionalFields() {
|
|
|
375 |
if ( $_REQUEST['action'] == 'edit' ) {
|
|
|
376 |
return '';
|
|
|
377 |
} elseif ($_REQUEST['action'] == 'post' ) {
|
|
|
378 |
return array();
|
|
|
379 |
}
|
|
|
380 |
}
|
|
|
381 |
|
69 |
rodolico |
382 |
protected function oneToOneHistoryLoad( $field ) {
|
|
|
383 |
//print "<pre>In oneToOneHistoryLoad, field is\n" . print_r( $field, true ) . '</pre>'; die;
|
|
|
384 |
global $dbConnection;
|
|
|
385 |
if ( isset( $field['displayQuery'] ) ) { // They want us to run a query
|
|
|
386 |
//
|
|
|
387 |
$return = $dbConnection->getOneRow(
|
|
|
388 |
$dbConnection->templateReplace( $field['displayQuery'],
|
|
|
389 |
array( 'id' => $this->data[static::$dbStructure['table']['primaryKey']] )
|
|
|
390 |
)
|
|
|
391 |
);
|
|
|
392 |
} elseif ( isset( $field['linkageTable'] ) ) { // we have to come up with the query ourselves
|
|
|
393 |
/* Since we have a linkage table, we assume the column name in the linkage table is the
|
|
|
394 |
* same as the primary key in the remote table (see the using).
|
|
|
395 |
* Everything else is chosen from the definition
|
|
|
396 |
*
|
|
|
397 |
* Note, on 1:1H, assume the linkage table has a full history with a null removed
|
|
|
398 |
* date for the current value
|
|
|
399 |
*/
|
|
|
400 |
$query = sprintf(
|
|
|
401 |
'select a.%s id,a.%s display from %s b join %s a using (%s) where b.%s = %s and b.removed is null',
|
|
|
402 |
$field['fieldname'],
|
|
|
403 |
$field['displayColumn'],
|
|
|
404 |
$field['linkageTable'],
|
|
|
405 |
$field['foreignTable'],
|
|
|
406 |
$field['foreignColumn'],
|
|
|
407 |
$field['linkageColumn'],
|
|
|
408 |
$this->data[static::$dbStructure['table']['primaryKey']],
|
|
|
409 |
$field['linkageTable']
|
|
|
410 |
);
|
|
|
411 |
//print "<pre>oneToOneHistory Query is \n$query</pre>"; die;
|
|
|
412 |
$return = $dbConnection->getOneRow( $query );
|
|
|
413 |
} // if..else
|
|
|
414 |
//print "<pre>return from oneToOneHistory is\n$return</pre>"; die;
|
|
|
415 |
return $return;
|
|
|
416 |
}
|
|
|
417 |
|
65 |
rodolico |
418 |
/**
|
69 |
rodolico |
419 |
* Gets all rows in a one to many relationship and returns it as an array
|
|
|
420 |
*
|
|
|
421 |
* If 'displayQuery' is defined, it should return one row for each match, with two columns
|
|
|
422 |
* 'id' is a unique ID, and 'display' is a textual display of the value. An
|
|
|
423 |
* order by clause can be used to determine the display order of the values
|
|
|
424 |
* returned.
|
|
|
425 |
*
|
|
|
426 |
* If 'displayQuery' is not defined, will build a query based on the definition.
|
|
|
427 |
*
|
|
|
428 |
* The returned array uses the 'id' field for the index and the 'display' field
|
|
|
429 |
* for the value
|
|
|
430 |
*
|
|
|
431 |
* @parameter array[] $field A Field Definition from $dbStructure
|
|
|
432 |
*
|
|
|
433 |
* @returns array[] where the the key is the id and the value is the display field
|
65 |
rodolico |
434 |
*/
|
69 |
rodolico |
435 |
public function oneToManyLoad ( $field ) {
|
|
|
436 |
global $dbConnection;
|
|
|
437 |
$query = '';
|
|
|
438 |
if ( isset( $field['displayQuery'] ) ) { // They want us to run a query
|
|
|
439 |
$query = $dbConnection->templateReplace(
|
|
|
440 |
$field['displayQuery'],
|
|
|
441 |
array( 'id' => $this->data[static::$dbStructure['table']['primaryKey']] )
|
|
|
442 |
);
|
|
|
443 |
} else {
|
|
|
444 |
$query = sprintf(
|
|
|
445 |
'select %s id, %s display from %s a join %s b using ( %s ) where a.%s = %s order by %s',
|
|
|
446 |
$field['foreignColumn'],
|
|
|
447 |
$field['foreignDisplayColumn'],
|
|
|
448 |
$field['linkageTable'],
|
|
|
449 |
$field['foreignTable'],
|
|
|
450 |
$field['foreignColumn'],
|
|
|
451 |
$field['linkageColumn'],
|
|
|
452 |
$this->data[static::$dbStructure['table']['primaryKey']],
|
|
|
453 |
$field['foreignDisplayColumn']
|
|
|
454 |
);
|
|
|
455 |
} // else
|
|
|
456 |
$return = $dbConnection->doSQL( $query, array( 'returnType' => 'associative' ) );
|
|
|
457 |
$returnArray = array();
|
|
|
458 |
foreach ( $return['returnData'] as $row => $data ) {
|
|
|
459 |
$returnArray[$data['id']] = $data['display'];
|
|
|
460 |
}
|
|
|
461 |
return $returnArray;
|
|
|
462 |
} // oneToManyLoad
|
|
|
463 |
|
|
|
464 |
|
|
|
465 |
/**
|
|
|
466 |
* Allow one to many types to be edited also
|
|
|
467 |
*/
|
65 |
rodolico |
468 |
protected function oneToManyEdit( $key, $record ) {
|
|
|
469 |
global $dbConnection;
|
66 |
rodolico |
470 |
|
|
|
471 |
$id = $this->__get('id') === null ? 0 : $this->__get('id');
|
65 |
rodolico |
472 |
$query = $dbConnection->templateReplace (
|
|
|
473 |
sprintf(
|
|
|
474 |
"select
|
|
|
475 |
~~foreignTable~~.~~foreignColumn~~ id,
|
|
|
476 |
~~foreignTable~~.~~foreignDisplayColumn~~ name,
|
|
|
477 |
not isnull(~~linkageColumn~~) checked
|
|
|
478 |
from
|
|
|
479 |
~~foreignTable~~ ~~foreignTable~~
|
|
|
480 |
left outer join (
|
|
|
481 |
select
|
|
|
482 |
~~linkageColumn~~,
|
|
|
483 |
~~foreignColumn~~
|
|
|
484 |
from
|
|
|
485 |
~~linkageTable~~
|
|
|
486 |
where
|
|
|
487 |
~~linkageColumn~~ = %s
|
|
|
488 |
) current using ( ~~foreignColumn~~ )
|
|
|
489 |
order by ~~foreignDisplayColumn~~
|
66 |
rodolico |
490 |
", $id
|
65 |
rodolico |
491 |
),
|
|
|
492 |
$record
|
|
|
493 |
);
|
|
|
494 |
//print "<pre>$query</pre>"; die;
|
|
|
495 |
$data = $dbConnection->doSQL( $query );
|
|
|
496 |
$data = $data['returnData'];
|
|
|
497 |
// save current state in data
|
|
|
498 |
$this->data[$key] = array();
|
|
|
499 |
foreach ( $data as $row ) {
|
|
|
500 |
if ( count( $row ) > 1 )
|
|
|
501 |
$this->data[$key][$row['id']] = $row['checked'];
|
|
|
502 |
}
|
|
|
503 |
//print "<pre>" . print_r($this->data, true) . "</pre>"; die;
|
|
|
504 |
//print "<pre>" . print_r($query, true) . "</pre>"; die;
|
|
|
505 |
$html = $dbConnection->htmlCheckBoxes( array( 'data' => $data, 'arrayName' => $key ) );
|
|
|
506 |
//print "<pre>" . print_r($html, true) . "</pre>"; die;
|
|
|
507 |
return $html;
|
66 |
rodolico |
508 |
} // oneToManyEdit
|
65 |
rodolico |
509 |
|
|
|
510 |
|
63 |
rodolico |
511 |
protected function edit() {
|
|
|
512 |
global $dbConnection;
|
|
|
513 |
|
|
|
514 |
// save everything we have right now
|
66 |
rodolico |
515 |
//$this->beforeEdit = $this->data;
|
|
|
516 |
$return = array();
|
|
|
517 |
//print "<pre>In Edit\n" . print_r( $this->data, true ) . "</pre>"; die;
|
63 |
rodolico |
518 |
foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
|
66 |
rodolico |
519 |
//print "<pre>Processing $record[displayName]\n</pre>";
|
|
|
520 |
if ( isset( $record['display'] ) && $record['display'] === false ) // we don't do things which aren't supposed to be shown
|
63 |
rodolico |
521 |
continue;
|
65 |
rodolico |
522 |
switch ( $record['type'] ) {
|
|
|
523 |
case '1:1H':
|
66 |
rodolico |
524 |
//print "<pre>Doing 1:1H\n</pre>";
|
63 |
rodolico |
525 |
// get a list of all options
|
66 |
rodolico |
526 |
$list = $record['class']::getAll( array(), array( 'workingon' => true ));
|
|
|
527 |
//print "<pre> list is\n" . print_r( $list, true ) . "</pre>";
|
69 |
rodolico |
528 |
$selectedRow = isset( $this->data[$key]['id'] ) ? $this->data[$key]['id'] : null;
|
66 |
rodolico |
529 |
//print "<pre>Selected value is $selectedRow</pre>";
|
63 |
rodolico |
530 |
$select = $dbConnection->htmlSelect( array (
|
|
|
531 |
'data' => $list,
|
69 |
rodolico |
532 |
'name' => $key, //$record['foreignColumn'],
|
63 |
rodolico |
533 |
'nullok' => true,
|
|
|
534 |
'selected' => isset( $selectedRow ) ? $selectedRow : -1,
|
|
|
535 |
//'class' =>
|
|
|
536 |
)
|
|
|
537 |
);
|
|
|
538 |
$return[] = sprintf(
|
|
|
539 |
"<td>%s</td><td>%s</td>",
|
|
|
540 |
$record['displayName'],
|
|
|
541 |
$select
|
|
|
542 |
);
|
65 |
rodolico |
543 |
break;
|
|
|
544 |
case '1:M' :
|
66 |
rodolico |
545 |
//print "<pre>Doing 1:M\n</pre>";
|
65 |
rodolico |
546 |
$checkBoxes = $this->oneToManyEdit( $key, $record );
|
|
|
547 |
$return[] = "<td>$record[displayName]</td>\n<td>$checkBoxes</td>";
|
|
|
548 |
break;
|
|
|
549 |
default:
|
66 |
rodolico |
550 |
//print "<pre>Adding $record[displayName]\n</pre>";
|
65 |
rodolico |
551 |
if ( isset( $record['canEdit'] ) && $record['canEdit'] == false ) {
|
|
|
552 |
$return[] = sprintf(
|
|
|
553 |
"<td>%s</td><td>%s</td>",
|
|
|
554 |
$record['displayName'],
|
66 |
rodolico |
555 |
isset( $this->data[$record['fieldname']] ) ? $this->data[$record['fieldname']] : ''
|
63 |
rodolico |
556 |
);
|
65 |
rodolico |
557 |
} else {
|
|
|
558 |
$return[] = sprintf(
|
|
|
559 |
"<td>%s</td><td><input type='text' name='%s' value='%s'></td>",
|
|
|
560 |
$record['displayName'],
|
|
|
561 |
$record['fieldname'],
|
|
|
562 |
$this->data[$record['fieldname']]
|
|
|
563 |
);
|
|
|
564 |
} // if..else
|
|
|
565 |
} // switch
|
|
|
566 |
} // foreach
|
|
|
567 |
//$return[] = $this->editAdditionalFields();
|
66 |
rodolico |
568 |
//print "<pre>In Edit, return is\n" . print_r( $return, true ) . "</pre>";
|
63 |
rodolico |
569 |
$return[] = "<td colspan='2'><input type='submit' name='Save' value='Save'></td>";
|
|
|
570 |
$hiddens = sprintf( "<input type='hidden' name='module' value='%s'>\n", get_called_class() ) .
|
66 |
rodolico |
571 |
sprintf( "<input type='hidden' name='id' value='%s'>\n", isset( $this->data[static::$dbStructure['table']['primaryKey']] ) ? $this->data[static::$dbStructure['table']['primaryKey']] : 0 ) .
|
63 |
rodolico |
572 |
"<input type='hidden' name='action' value='post'>\n";
|
|
|
573 |
return "<div class='show_record'>\n<form><table class='show_record'>\n<tr>" . implode("</tr>\n<tr>", $return ) . "</tr>\n</table>\n$hiddens</form></div>";
|
|
|
574 |
} // edit
|
|
|
575 |
|
64 |
rodolico |
576 |
|
|
|
577 |
/**
|
|
|
578 |
* Adds new row in linkage table after marking old row as inactive
|
|
|
579 |
*
|
|
|
580 |
* Returns two queries which should be executed to update a linkage table
|
|
|
581 |
*
|
|
|
582 |
* The "History" part of this is that the old rows are not removed.
|
|
|
583 |
* Instead, they are marked as removed and this new row is added.
|
|
|
584 |
*
|
|
|
585 |
* This function assumes the table has two date columns, created and removed
|
|
|
586 |
* created should be default current_timestamp so we do not manually update
|
|
|
587 |
* it here.
|
|
|
588 |
*/
|
|
|
589 |
public function post1to1History( $linkageTable, $myColumn, $linkedToColumn, $newValue ) {
|
|
|
590 |
$queries = array();
|
|
|
591 |
if ( $newValue != -1 ) {
|
|
|
592 |
$queries[] = sprintf(
|
|
|
593 |
"update %s set removed = date(now()) where %s = %s and removed is null",
|
|
|
594 |
$linkageTable,
|
|
|
595 |
$myColumn,
|
|
|
596 |
$this->__get('id')
|
|
|
597 |
);
|
|
|
598 |
$queries[] = sprintf(
|
|
|
599 |
"insert into %s ( %s, %s ) values ( %s, %s )",
|
|
|
600 |
$linkageTable,
|
|
|
601 |
$myColumn,
|
|
|
602 |
$linkedToColumn,
|
|
|
603 |
$this->__get('id'),
|
|
|
604 |
$newValue
|
|
|
605 |
);
|
|
|
606 |
}
|
|
|
607 |
return $queries;
|
|
|
608 |
}
|
|
|
609 |
|
65 |
rodolico |
610 |
protected function oneToManyPost( $key, $record ) {
|
|
|
611 |
global $dbConnection;
|
|
|
612 |
|
|
|
613 |
$request = $_REQUEST[$key];
|
|
|
614 |
$queries = array();
|
|
|
615 |
foreach ( $this->data[$key] as $index => $checked ) {
|
|
|
616 |
if ( $checked && empty( $request[$index] ) ) { // we unchecked something
|
|
|
617 |
$queries[] = $dbConnection->templateReplace(
|
|
|
618 |
sprintf(
|
|
|
619 |
'delete from ~~linkageTable~~ where ~~linkageColumn~~ = %s and ~~foreignColumn~~ = %s',
|
|
|
620 |
$this->__get('id'),
|
|
|
621 |
$index
|
|
|
622 |
),
|
|
|
623 |
$record
|
|
|
624 |
);
|
|
|
625 |
} elseif ( ! $checked && isset( $request[$index] ) ) { // we checked something
|
|
|
626 |
$queries[] = $dbConnection->templateReplace(
|
|
|
627 |
sprintf(
|
|
|
628 |
'insert into ~~linkageTable~~ (~~linkageColumn~~, ~~foreignColumn~~ ) values (%s, %s)',
|
|
|
629 |
$this->__get('id'),
|
|
|
630 |
$index
|
|
|
631 |
),
|
|
|
632 |
$record
|
|
|
633 |
);
|
|
|
634 |
} // if..elseif
|
|
|
635 |
} // foreach
|
|
|
636 |
return $queries;
|
|
|
637 |
}
|
|
|
638 |
|
64 |
rodolico |
639 |
/**
|
|
|
640 |
* Posts the results of a previous form
|
|
|
641 |
*
|
|
|
642 |
* After a form is filled in, will search for any values which have
|
|
|
643 |
* changed. It will generate a separate SQL statement for each of them
|
|
|
644 |
* and execute the queries in in batch
|
|
|
645 |
*
|
|
|
646 |
* Once this is done, will reload the values from the database, so
|
|
|
647 |
* we immediately know if there was a problem.
|
|
|
648 |
*/
|
63 |
rodolico |
649 |
protected function post() {
|
|
|
650 |
global $dbConnection;
|
|
|
651 |
|
67 |
rodolico |
652 |
//print "<pre>Function Post\n" . print_r($_REQUEST, true) . '</pre>'; die;
|
|
|
653 |
|
|
|
654 |
// first, if this is a new record, just insert a blank one and get the ID.
|
|
|
655 |
// A little bit of overhead, but it allows us
|
|
|
656 |
if ( empty( $this->data[static::$dbStructure['table']['primaryKey']] ) ) {
|
|
|
657 |
// by default, we pre-populate some fields and that means they won't get updated, so init structure
|
|
|
658 |
foreach ( $this->data as $key => $record ) {
|
|
|
659 |
if ( ! is_array( $this->data[$key] ) && $this->data[$key] )
|
|
|
660 |
$this->data[$key] = '';
|
|
|
661 |
}
|
|
|
662 |
$insertQuery = $dbConnection->insertQuery(
|
|
|
663 |
static::$dbStructure['table']['tableName'],
|
|
|
664 |
array( static::$dbStructure['table']['selectionDisplay'] => 'Blank Record' ) );
|
|
|
665 |
$this->data[static::$dbStructure['table']['primaryKey']] = $dbConnection->insert( $insertQuery );
|
|
|
666 |
|
69 |
rodolico |
667 |
print "<pre>\n" . print_r( $this->data , true ) . "</pre>"; die;
|
67 |
rodolico |
668 |
}
|
|
|
669 |
|
63 |
rodolico |
670 |
$queries = array();
|
|
|
671 |
$fields = array();
|
|
|
672 |
foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
|
|
|
673 |
if ( isset( $record['display'] ) && $record['display'] === false ) // we don't try to show links
|
|
|
674 |
continue;
|
|
|
675 |
if ( isset( $record['canEdit'] ) && ! $record['canEdit'] )
|
|
|
676 |
continue;
|
69 |
rodolico |
677 |
switch ( $record['type'] ) {
|
|
|
678 |
case '1:1H' :
|
|
|
679 |
if ( $_REQUEST[$key] != $this->data[$key]['id'] ) { // they have changed a value
|
|
|
680 |
//print "<pre>Doing 1:1H\nRequest\n" . $_REQUEST[$record['fieldname']] . "\nData" . $this->data[$record['fieldname']]['id'] . "</pre>"; die;
|
65 |
rodolico |
681 |
$queries = array_merge( $queries,
|
69 |
rodolico |
682 |
$this->post1to1History(
|
|
|
683 |
$record['linkageTable'],
|
|
|
684 |
$record['linkageColumn'],
|
|
|
685 |
$record['foreignColumn'],
|
|
|
686 |
$_REQUEST[$key]
|
|
|
687 |
)
|
|
|
688 |
);
|
|
|
689 |
} // if
|
|
|
690 |
break;
|
|
|
691 |
case '1:M' :
|
|
|
692 |
$queries = array_merge( $queries, $this->oneToManyPost( $key, $record ) );
|
|
|
693 |
break;
|
|
|
694 |
default:
|
|
|
695 |
if ( $_REQUEST[$record['fieldname']] != $this->data[$record['fieldname']] ) { // they have changed a value
|
65 |
rodolico |
696 |
$fields[$record['fieldname']] = $_REQUEST[$record['fieldname']];
|
69 |
rodolico |
697 |
}
|
|
|
698 |
} // switch
|
63 |
rodolico |
699 |
} // foreach
|
69 |
rodolico |
700 |
//print "<pre>posting fields in data\n" . print_r( $this->data, true ) . "</pre>"; die;
|
63 |
rodolico |
701 |
if ( count( $fields ) ) {
|
67 |
rodolico |
702 |
$queries[] = $dbConnection->updateQuery(
|
|
|
703 |
static::$dbStructure['table']['tableName'],
|
|
|
704 |
array( static::$dbStructure['table']['primaryKey'] => $this->data[static::$dbStructure['table']['primaryKey']] ),
|
|
|
705 |
$fields );
|
63 |
rodolico |
706 |
} // if there are fields
|
65 |
rodolico |
707 |
/*
|
64 |
rodolico |
708 |
print "<pre>Request" . print_r( $_REQUEST, true ) . "</pre>";
|
|
|
709 |
print "<pre>Data" . print_r( $this->data, true ) . "</pre>";
|
|
|
710 |
print "<pre>Queries" . print_r( $queries, true ) . "</pre>"; die;
|
65 |
rodolico |
711 |
*/
|
63 |
rodolico |
712 |
$dbConnection->runSQLScript( $queries );
|
|
|
713 |
$this->loadFromDB( $this->data[static::$dbStructure['table']['primaryKey']] );
|
|
|
714 |
} // post
|
|
|
715 |
|
|
|
716 |
/**
|
61 |
rodolico |
717 |
* Simple controller. Will determine what action is going on, then
|
|
|
718 |
* call the correct method, returning the HTML required
|
|
|
719 |
*/
|
56 |
rodolico |
720 |
public function run() {
|
63 |
rodolico |
721 |
if ( isset( $_REQUEST['action'] ) ) {
|
|
|
722 |
switch ( $_REQUEST['action'] ) {
|
66 |
rodolico |
723 |
case 'add':
|
|
|
724 |
// prepopulate some fields with what we've been working on
|
|
|
725 |
$this->data = array();
|
|
|
726 |
foreach ( static::$dbStructure['table']['fields'] as $fieldname => $record ) {
|
|
|
727 |
if ( isset( $record['class'] ) && isset( $_SESSION['workingon'][$record['class']] ) ) {
|
|
|
728 |
$this->data[$record['fieldname']] = $_SESSION['workingon'][$record['class']];
|
|
|
729 |
} else {
|
|
|
730 |
$this->data[$fieldname] = '';
|
|
|
731 |
} // if..else
|
|
|
732 |
} // foreach
|
|
|
733 |
$this->data['id'] = 0;
|
|
|
734 |
//print '<pre>' . print_r( $this->data, true ) . '</pre>'; die;
|
|
|
735 |
// now, fall through and do the edit
|
|
|
736 |
case 'edit':
|
|
|
737 |
$return = $this->edit();
|
|
|
738 |
break;
|
|
|
739 |
case 'post':
|
|
|
740 |
$this->post();
|
|
|
741 |
// fall through and display the record
|
|
|
742 |
default:
|
|
|
743 |
$return = $this->display();
|
63 |
rodolico |
744 |
} // switch
|
|
|
745 |
} else {
|
|
|
746 |
$return = $this->display();
|
|
|
747 |
}
|
|
|
748 |
return $return;
|
56 |
rodolico |
749 |
}
|
53 |
rodolico |
750 |
} // abstract class Camp
|
|
|
751 |
|
|
|
752 |
?>
|