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 |
*/
|
63 |
rodolico |
13 |
public function __construct( $id = 0 ) {
|
|
|
14 |
if ( $id )
|
|
|
15 |
$this->loadFromDB( $id );
|
|
|
16 |
} // __construct
|
|
|
17 |
|
|
|
18 |
public function loadFromDB( $id ) {
|
59 |
rodolico |
19 |
global $dbConnection;
|
|
|
20 |
|
|
|
21 |
$this->data = array();
|
|
|
22 |
$fields = array();
|
64 |
rodolico |
23 |
$calculatedFields = array();
|
59 |
rodolico |
24 |
foreach ( static::$dbStructure['table']['fields'] as $key => $data ) {
|
64 |
rodolico |
25 |
switch ( $data['type'] ) {
|
|
|
26 |
case '1:1H' : // one to one with history
|
|
|
27 |
$calculatedFields[] = $data['fieldname'];
|
|
|
28 |
$calculatedFields[] = $data['displayColumn'];
|
|
|
29 |
break;
|
|
|
30 |
case '1:M' : // one to many
|
|
|
31 |
$calculatedFields[] = $data['fieldname'];
|
|
|
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 |
|
|
|
48 |
//print "<pre>" . print_r( $calculatedFields, true ) . "</pre>"; die;
|
|
|
49 |
//print "<pre>" . print_r( $this->data, true ) . "</pre>"; die;
|
61 |
rodolico |
50 |
if ( count( $calculatedFields ) ) {
|
|
|
51 |
$query = sprintf( 'select distinct %s from %s %s',
|
|
|
52 |
implode( ',', $calculatedFields ),
|
|
|
53 |
static::$dbStructure['view']['viewName'],
|
|
|
54 |
static::makeWhereClause( array(
|
|
|
55 |
static::$dbStructure['table']['fields']['id']['fieldname'] .
|
|
|
56 |
' = ' .
|
|
|
57 |
$this->data[static::$dbStructure['table']['primaryKey']] )
|
|
|
58 |
)
|
|
|
59 |
);
|
|
|
60 |
//print "<pre>$query</pre>"; die;
|
|
|
61 |
$result = $dbConnection->getOneRow( $query );
|
|
|
62 |
foreach ( $calculatedFields as $field ) {
|
|
|
63 |
$this->data[$field] = empty( $result[$field] ) ? '' : $result[$field];
|
|
|
64 |
} // foreach
|
|
|
65 |
}
|
64 |
rodolico |
66 |
//print "<pre>Data after loadFromDB\n" . print_r($this->data, true) . "</pre>"; die;
|
63 |
rodolico |
67 |
} // loadFromDB
|
59 |
rodolico |
68 |
|
61 |
rodolico |
69 |
/**
|
|
|
70 |
* gets the contents of $fieldname
|
|
|
71 |
*
|
|
|
72 |
* @parameter string $fieldname name of the database field to retrieve
|
|
|
73 |
*
|
|
|
74 |
* @return string The value of $fieldname stored in structure
|
|
|
75 |
*/
|
59 |
rodolico |
76 |
public function __get( $fieldname ) {
|
|
|
77 |
return
|
|
|
78 |
isset( $this->data[static::$dbStructure['table']['fields'][$fieldname]['fieldname']] ) ?
|
|
|
79 |
$this->data[static::$dbStructure['table']['fields'][$fieldname]['fieldname']] :
|
|
|
80 |
null;
|
|
|
81 |
} // __get
|
|
|
82 |
|
|
|
83 |
|
55 |
rodolico |
84 |
/**
|
|
|
85 |
* Gets stats for an extended class.
|
|
|
86 |
*
|
|
|
87 |
* Gets number of active and inactive rows for a class, using the
|
|
|
88 |
* view ($viewName)
|
|
|
89 |
*
|
|
|
90 |
* @return int[] array of integers which is a count
|
|
|
91 |
*/
|
53 |
rodolico |
92 |
public static function getStats () {
|
|
|
93 |
global $dbConnection;
|
57 |
rodolico |
94 |
global $activeOnly;
|
53 |
rodolico |
95 |
|
|
|
96 |
$return = array();
|
|
|
97 |
$restrictions = array();
|
56 |
rodolico |
98 |
$trueClass = get_called_class();
|
57 |
rodolico |
99 |
$saveActive = $activeOnly;
|
|
|
100 |
$activeOnly = true;
|
54 |
rodolico |
101 |
|
|
|
102 |
$active = sprintf(
|
57 |
rodolico |
103 |
'select count(distinct %s) from %s %s',
|
54 |
rodolico |
104 |
static::$dbStructure['table']['primaryKey'],
|
61 |
rodolico |
105 |
static::$dbStructure['view']['viewName'],
|
57 |
rodolico |
106 |
self::makeWhereClause()
|
|
|
107 |
);
|
|
|
108 |
|
|
|
109 |
$activeOnly = false;
|
54 |
rodolico |
110 |
$inactive = sprintf(
|
57 |
rodolico |
111 |
'select count(distinct %s) from %s %s',
|
54 |
rodolico |
112 |
static::$dbStructure['table']['primaryKey'],
|
61 |
rodolico |
113 |
static::$dbStructure['view']['viewName'],
|
57 |
rodolico |
114 |
self::makeWhereClause( array( strtolower( $trueClass ) . "_removed is not null" ) )
|
|
|
115 |
);
|
|
|
116 |
$activeOnly = $saveActive;
|
|
|
117 |
|
|
|
118 |
//print "<pre>Active\n$active</pre><pre>Inactive\n$inactive</pre>"; die;
|
|
|
119 |
|
54 |
rodolico |
120 |
$return['active'] = $dbConnection->getOneDBValue( $active );
|
|
|
121 |
$return['inactive'] = $dbConnection->getOneDBValue( $inactive );
|
53 |
rodolico |
122 |
return $return;
|
|
|
123 |
}
|
|
|
124 |
|
61 |
rodolico |
125 |
/**
|
|
|
126 |
* Creates a where clause
|
|
|
127 |
*
|
|
|
128 |
* Merges $restrictions with any restrictions which may be in place
|
|
|
129 |
* for the current user. Also sets 'class_removed is null' if
|
|
|
130 |
* $activeOnly (global) is set
|
|
|
131 |
*
|
|
|
132 |
* @parameter string[] $restrictions array of conditionals
|
|
|
133 |
*
|
|
|
134 |
* @return string all restricions ANDED together
|
|
|
135 |
*/
|
57 |
rodolico |
136 |
protected static function makeWhereClause( $restrictions = array() ) {
|
|
|
137 |
global $activeOnly;
|
|
|
138 |
|
|
|
139 |
//print "<pre>" . print_r( $restrictions, true ) . "</pre>";
|
|
|
140 |
|
|
|
141 |
$trueClass = get_called_class();
|
|
|
142 |
foreach ( $_SESSION['restrictions'] as $class => $restriction ) {
|
|
|
143 |
$restrictions[] = $_SESSION['restrictions'][$class];
|
|
|
144 |
}
|
|
|
145 |
if ( $activeOnly ) {
|
|
|
146 |
$restrictions[] = strtolower( $trueClass ) . '_removed is null';
|
|
|
147 |
}
|
|
|
148 |
return count( $restrictions ) ? 'where ' . implode( ' and ', $restrictions ) : '';
|
|
|
149 |
} //
|
|
|
150 |
|
61 |
rodolico |
151 |
/**
|
|
|
152 |
* Creates a list of all records for a particular class and returns
|
|
|
153 |
* a UL with all LI's set as links (A's) to displaying a record.
|
|
|
154 |
*
|
|
|
155 |
* @parameter string[] $filter array of conditionals to limit if $list not set
|
|
|
156 |
* @parameter string[] $list An optional list of data to be displayed
|
|
|
157 |
*
|
|
|
158 |
* @return string A UL with each LI containing a link to an entry
|
|
|
159 |
*/
|
56 |
rodolico |
160 |
public static function showSelectionList( $filter = array(), $list = array() ) {
|
53 |
rodolico |
161 |
global $url;
|
59 |
rodolico |
162 |
$return = array();
|
56 |
rodolico |
163 |
if ( empty( $list ) ) {
|
|
|
164 |
$list = self::getAll( $filter );
|
|
|
165 |
}
|
64 |
rodolico |
166 |
//print "<pre>showSelectionList\n" . print_r( $list, true ) . "</pre>"; die;
|
56 |
rodolico |
167 |
$module = get_called_class();
|
53 |
rodolico |
168 |
foreach ( $list as $id => $name ) {
|
64 |
rodolico |
169 |
if ( $id )
|
|
|
170 |
$return[] = "<a href='$url?module=$module&id=$id'>$name</a>";
|
53 |
rodolico |
171 |
}
|
64 |
rodolico |
172 |
return count($return) ? "<ul>\n<li>" . implode( "</li>\n<li>", $return ) . "</li>\n</ul>" : '';
|
53 |
rodolico |
173 |
}
|
|
|
174 |
|
61 |
rodolico |
175 |
/**
|
|
|
176 |
* Gets all matching rows
|
|
|
177 |
*
|
|
|
178 |
* Does an SQL call to retrieve ID and Name for all rows matching
|
|
|
179 |
* $filter and $_REQUEST['to_find'].
|
|
|
180 |
*
|
|
|
181 |
* @parameter string[] $filter Optional list of conditionals for where clause
|
|
|
182 |
*
|
|
|
183 |
* @return array[] An array of indexes and names matching query
|
|
|
184 |
*/
|
56 |
rodolico |
185 |
public static function getAll( $filter = array() ) {
|
53 |
rodolico |
186 |
global $activeOnly;
|
|
|
187 |
global $dbConnection;
|
|
|
188 |
|
59 |
rodolico |
189 |
//print "<pre>" . print_r( $filter, true ) . "</pre>";
|
56 |
rodolico |
190 |
if ( isset( $_REQUEST['to_find'] ) ) {
|
|
|
191 |
$filter[] = sprintf( " %s like '%%%s%%'", static::$dbStructure['view']['selectionDisplay'], $_REQUEST['to_find']) ;
|
|
|
192 |
}
|
53 |
rodolico |
193 |
$return = array();
|
57 |
rodolico |
194 |
$query = sprintf( 'select distinct %s id, %s name from %s %s',
|
56 |
rodolico |
195 |
static::$dbStructure['view']['primaryKey'],
|
|
|
196 |
static::$dbStructure['view']['selectionDisplay'],
|
|
|
197 |
static::$dbStructure['view']['viewName'],
|
57 |
rodolico |
198 |
self::makeWhereClause( $filter )
|
53 |
rodolico |
199 |
);
|
56 |
rodolico |
200 |
$query .= " order by " . static::$dbStructure['view']['selectionDisplay'];
|
59 |
rodolico |
201 |
//print "<pre>$query</pre>\n";
|
53 |
rodolico |
202 |
$result = $dbConnection->doSQL( $query );
|
64 |
rodolico |
203 |
$result = $result['returnData'];
|
|
|
204 |
foreach ( $result as $row ) {
|
53 |
rodolico |
205 |
$return[$row['id']] = $row['name'];
|
|
|
206 |
}
|
|
|
207 |
return $return;
|
|
|
208 |
}
|
61 |
rodolico |
209 |
|
|
|
210 |
/**
|
|
|
211 |
* Returns the "name" of an instance
|
|
|
212 |
*
|
|
|
213 |
* Normally, this is $this->name, but some classes may want to put
|
|
|
214 |
* in additional data
|
|
|
215 |
*
|
|
|
216 |
* @return string Display name of this instance
|
|
|
217 |
*/
|
53 |
rodolico |
218 |
public function __toString() {
|
|
|
219 |
return $this->name;
|
|
|
220 |
}
|
|
|
221 |
|
61 |
rodolico |
222 |
/**
|
|
|
223 |
* Returns an HTML structure which can display a record
|
|
|
224 |
*
|
|
|
225 |
* Note that if the instance has children, they will be displayed
|
|
|
226 |
* also. Additionally, if it has fields marked as type 'calculated'
|
|
|
227 |
* it will perform the limited calculations.
|
|
|
228 |
*
|
|
|
229 |
* @return string HTML to display record
|
|
|
230 |
*/
|
56 |
rodolico |
231 |
public function display() {
|
64 |
rodolico |
232 |
//print '<pre>' . print_r( $this->data, true) . '</pre>'; die;
|
59 |
rodolico |
233 |
global $url;
|
56 |
rodolico |
234 |
$return = array();
|
59 |
rodolico |
235 |
$save = '';
|
|
|
236 |
|
|
|
237 |
/* get the records for this, including from other tables */
|
56 |
rodolico |
238 |
foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
|
59 |
rodolico |
239 |
if ( isset( $record['display'] ) && $record['display'] === false ) // we don't try to show links
|
|
|
240 |
continue;
|
64 |
rodolico |
241 |
if ( $record['type'] == '1:1H' ) { // this one is supposed to be set up as a link
|
59 |
rodolico |
242 |
$return[] = sprintf(
|
|
|
243 |
"<td>%s</td><td><a href='$url?module=%s&id=%s'>%s</a></td>",
|
|
|
244 |
$record['displayName'],
|
64 |
rodolico |
245 |
$record['class'],
|
|
|
246 |
$this->data[$record['fieldname']],
|
|
|
247 |
$this->data[$record['displayColumn']]
|
59 |
rodolico |
248 |
);
|
|
|
249 |
} else { // just pulling data from the table itself
|
|
|
250 |
$return[] = '<td>' . $record['displayName'] . "</td>\n<td>" . $this->data[$record['fieldname']] . '</td>';
|
|
|
251 |
}
|
56 |
rodolico |
252 |
}
|
61 |
rodolico |
253 |
$return[] = sprintf( "<td colspan='2'><a href='$url?module=%s&id=%s&action=edit'>Edit</a></td>",
|
|
|
254 |
get_called_class(),
|
|
|
255 |
$this->data[static::$dbStructure['table']['primaryKey']]
|
|
|
256 |
);
|
|
|
257 |
|
59 |
rodolico |
258 |
$return = "<div class='show_record'>\n<table class='show_record'>\n<tr>" . implode("</tr>\n<tr>", $return ) . "</tr>\n</table>\n</div>";
|
|
|
259 |
|
|
|
260 |
/* Now, get the children records */
|
|
|
261 |
if ( isset( $_REQUEST['to_find'] ) ) {
|
|
|
262 |
$save = $_REQUEST['to_find'];
|
|
|
263 |
unset( $_REQUEST['to_find'] );
|
|
|
264 |
}
|
64 |
rodolico |
265 |
foreach ( static::$dbStructure['children'] as $class => $record) {
|
|
|
266 |
if ( isset( $record['filter'] ) ) { // we'll parse it against $this->data to fill in any variables
|
|
|
267 |
global $dbConnection;
|
|
|
268 |
$record['filter'] = $dbConnection->templateReplace( $record['filter'], $this->data );
|
|
|
269 |
//print "<pre>$filter\n" . print_r( $this->data, true) . "</pre>"; die;
|
|
|
270 |
} else {
|
|
|
271 |
$record['filter'] = static::$dbStructure['table']['primaryKey'] . ' = ' . $this->data[static::$dbStructure['table']['primaryKey']];
|
|
|
272 |
}
|
|
|
273 |
if ( ! isset( $record['name'] ) )
|
|
|
274 |
$record['name'] = $class;
|
|
|
275 |
//print "<pre>record\n" . print_r( $record, true) . "</pre>"; die;
|
|
|
276 |
$found = $class::showSelectionList( array( $record['filter'] ) );
|
|
|
277 |
//print "<pre>[$found]</pre>"; die;
|
|
|
278 |
if ( $found )
|
|
|
279 |
$return .= "<div class='show_record'>\n<h3>$record[name]</h3>\n" . $class::showSelectionList( array( $record['filter'] ) ) . '</div>';
|
59 |
rodolico |
280 |
}
|
|
|
281 |
if ( $save !== null ) {
|
|
|
282 |
$_REQUEST['to_find'] = $save;
|
|
|
283 |
}
|
|
|
284 |
return $return;
|
56 |
rodolico |
285 |
}
|
|
|
286 |
|
61 |
rodolico |
287 |
/**
|
63 |
rodolico |
288 |
* Function is basically a placeholder in case a extended class
|
|
|
289 |
* needs to do some additional processing when editing
|
|
|
290 |
*
|
|
|
291 |
* In some cases, the edit function can not handle additional fields.
|
|
|
292 |
* See the Device class, where device_type is a table with many-to-many
|
|
|
293 |
* linkages to device. Since this type of thing is very rare, I decided
|
|
|
294 |
* to just allow edit and post to call an additional function in
|
|
|
295 |
* those cases.
|
|
|
296 |
*
|
|
|
297 |
* Based on the action requested, will either return an HTML string
|
|
|
298 |
* which will be placed in the edit screen (for edit), or an array
|
|
|
299 |
* of SQL to be executed to update/add rows (for post).
|
|
|
300 |
*
|
|
|
301 |
* If the action is edit, there should be a <td></td> around the
|
|
|
302 |
* whole thing
|
|
|
303 |
*/
|
|
|
304 |
|
|
|
305 |
protected function editAdditionalFields() {
|
|
|
306 |
if ( $_REQUEST['action'] == 'edit' ) {
|
|
|
307 |
return '';
|
|
|
308 |
} elseif ($_REQUEST['action'] == 'post' ) {
|
|
|
309 |
return array();
|
|
|
310 |
}
|
|
|
311 |
}
|
|
|
312 |
|
65 |
rodolico |
313 |
/**
|
|
|
314 |
* Allow device_types to be edited also
|
|
|
315 |
*/
|
|
|
316 |
protected function oneToManyEdit( $key, $record ) {
|
|
|
317 |
global $dbConnection;
|
|
|
318 |
$query = $dbConnection->templateReplace (
|
|
|
319 |
sprintf(
|
|
|
320 |
"select
|
|
|
321 |
~~foreignTable~~.~~foreignColumn~~ id,
|
|
|
322 |
~~foreignTable~~.~~foreignDisplayColumn~~ name,
|
|
|
323 |
not isnull(~~linkageColumn~~) checked
|
|
|
324 |
from
|
|
|
325 |
~~foreignTable~~ ~~foreignTable~~
|
|
|
326 |
left outer join (
|
|
|
327 |
select
|
|
|
328 |
~~linkageColumn~~,
|
|
|
329 |
~~foreignColumn~~
|
|
|
330 |
from
|
|
|
331 |
~~linkageTable~~
|
|
|
332 |
where
|
|
|
333 |
~~linkageColumn~~ = %s
|
|
|
334 |
) current using ( ~~foreignColumn~~ )
|
|
|
335 |
order by ~~foreignDisplayColumn~~
|
|
|
336 |
", $this->__get( 'id' )
|
|
|
337 |
),
|
|
|
338 |
$record
|
|
|
339 |
);
|
|
|
340 |
//print "<pre>$query</pre>"; die;
|
|
|
341 |
$data = $dbConnection->doSQL( $query );
|
|
|
342 |
$data = $data['returnData'];
|
|
|
343 |
// save current state in data
|
|
|
344 |
$this->data[$key] = array();
|
|
|
345 |
foreach ( $data as $row ) {
|
|
|
346 |
if ( count( $row ) > 1 )
|
|
|
347 |
$this->data[$key][$row['id']] = $row['checked'];
|
|
|
348 |
}
|
|
|
349 |
//print "<pre>" . print_r($this->data, true) . "</pre>"; die;
|
|
|
350 |
//print "<pre>" . print_r($query, true) . "</pre>"; die;
|
|
|
351 |
$html = $dbConnection->htmlCheckBoxes( array( 'data' => $data, 'arrayName' => $key ) );
|
|
|
352 |
//print "<pre>" . print_r($html, true) . "</pre>"; die;
|
|
|
353 |
return $html;
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
|
63 |
rodolico |
357 |
protected function edit() {
|
|
|
358 |
global $dbConnection;
|
|
|
359 |
|
|
|
360 |
// save everything we have right now
|
|
|
361 |
$this->beforeEdit = $this->data;
|
|
|
362 |
foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
|
|
|
363 |
if ( isset( $record['display'] ) && $record['display'] === false ) // we don't things which aren't supposed to be shown
|
|
|
364 |
continue;
|
65 |
rodolico |
365 |
switch ( $record['type'] ) {
|
|
|
366 |
case '1:1H':
|
63 |
rodolico |
367 |
// get a list of all options
|
65 |
rodolico |
368 |
$list = $record['class']::getAll();
|
63 |
rodolico |
369 |
|
|
|
370 |
//print '<pre>' . print_r( $record, true ) . "\n----\n" . print_r( $this->data, true ) . '</pre>'; die;
|
65 |
rodolico |
371 |
$selectedRow = $this->data[$record['fieldname']];
|
63 |
rodolico |
372 |
$select = $dbConnection->htmlSelect( array (
|
|
|
373 |
'data' => $list,
|
65 |
rodolico |
374 |
'name' => $record['foreignColumn'],
|
63 |
rodolico |
375 |
'nullok' => true,
|
|
|
376 |
'selected' => isset( $selectedRow ) ? $selectedRow : -1,
|
|
|
377 |
//'class' =>
|
|
|
378 |
)
|
|
|
379 |
);
|
|
|
380 |
$return[] = sprintf(
|
|
|
381 |
"<td>%s</td><td>%s</td>",
|
|
|
382 |
$record['displayName'],
|
|
|
383 |
$select
|
|
|
384 |
);
|
65 |
rodolico |
385 |
break;
|
|
|
386 |
case '1:M' :
|
|
|
387 |
$checkBoxes = $this->oneToManyEdit( $key, $record );
|
|
|
388 |
$return[] = "<td>$record[displayName]</td>\n<td>$checkBoxes</td>";
|
|
|
389 |
break;
|
|
|
390 |
default:
|
|
|
391 |
if ( isset( $record['canEdit'] ) && $record['canEdit'] == false ) {
|
|
|
392 |
$return[] = sprintf(
|
|
|
393 |
"<td>%s</td><td>%s</td>",
|
|
|
394 |
$record['displayName'],
|
|
|
395 |
$this->data[$record['fieldname']]
|
63 |
rodolico |
396 |
);
|
65 |
rodolico |
397 |
} else {
|
|
|
398 |
$return[] = sprintf(
|
|
|
399 |
"<td>%s</td><td><input type='text' name='%s' value='%s'></td>",
|
|
|
400 |
$record['displayName'],
|
|
|
401 |
$record['fieldname'],
|
|
|
402 |
$this->data[$record['fieldname']]
|
|
|
403 |
);
|
|
|
404 |
} // if..else
|
|
|
405 |
} // switch
|
|
|
406 |
} // foreach
|
|
|
407 |
//$return[] = $this->editAdditionalFields();
|
63 |
rodolico |
408 |
$return[] = "<td colspan='2'><input type='submit' name='Save' value='Save'></td>";
|
|
|
409 |
$hiddens = sprintf( "<input type='hidden' name='module' value='%s'>\n", get_called_class() ) .
|
|
|
410 |
sprintf( "<input type='hidden' name='id' value='%s'>\n", $this->data[static::$dbStructure['table']['primaryKey']] ) .
|
|
|
411 |
"<input type='hidden' name='action' value='post'>\n";
|
|
|
412 |
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>";
|
|
|
413 |
} // edit
|
|
|
414 |
|
64 |
rodolico |
415 |
|
|
|
416 |
/**
|
|
|
417 |
* Adds new row in linkage table after marking old row as inactive
|
|
|
418 |
*
|
|
|
419 |
* Returns two queries which should be executed to update a linkage table
|
|
|
420 |
*
|
|
|
421 |
* The "History" part of this is that the old rows are not removed.
|
|
|
422 |
* Instead, they are marked as removed and this new row is added.
|
|
|
423 |
*
|
|
|
424 |
* This function assumes the table has two date columns, created and removed
|
|
|
425 |
* created should be default current_timestamp so we do not manually update
|
|
|
426 |
* it here.
|
|
|
427 |
*/
|
|
|
428 |
public function post1to1History( $linkageTable, $myColumn, $linkedToColumn, $newValue ) {
|
|
|
429 |
$queries = array();
|
|
|
430 |
if ( $newValue != -1 ) {
|
|
|
431 |
$queries[] = sprintf(
|
|
|
432 |
"update %s set removed = date(now()) where %s = %s and removed is null",
|
|
|
433 |
$linkageTable,
|
|
|
434 |
$myColumn,
|
|
|
435 |
$this->__get('id')
|
|
|
436 |
);
|
|
|
437 |
$queries[] = sprintf(
|
|
|
438 |
"insert into %s ( %s, %s ) values ( %s, %s )",
|
|
|
439 |
$linkageTable,
|
|
|
440 |
$myColumn,
|
|
|
441 |
$linkedToColumn,
|
|
|
442 |
$this->__get('id'),
|
|
|
443 |
$newValue
|
|
|
444 |
);
|
|
|
445 |
}
|
|
|
446 |
return $queries;
|
|
|
447 |
}
|
|
|
448 |
|
65 |
rodolico |
449 |
protected function oneToManyPost( $key, $record ) {
|
|
|
450 |
global $dbConnection;
|
|
|
451 |
|
|
|
452 |
$request = $_REQUEST[$key];
|
|
|
453 |
$queries = array();
|
|
|
454 |
foreach ( $this->data[$key] as $index => $checked ) {
|
|
|
455 |
if ( $checked && empty( $request[$index] ) ) { // we unchecked something
|
|
|
456 |
$queries[] = $dbConnection->templateReplace(
|
|
|
457 |
sprintf(
|
|
|
458 |
'delete from ~~linkageTable~~ where ~~linkageColumn~~ = %s and ~~foreignColumn~~ = %s',
|
|
|
459 |
$this->__get('id'),
|
|
|
460 |
$index
|
|
|
461 |
),
|
|
|
462 |
$record
|
|
|
463 |
);
|
|
|
464 |
} elseif ( ! $checked && isset( $request[$index] ) ) { // we checked something
|
|
|
465 |
$queries[] = $dbConnection->templateReplace(
|
|
|
466 |
sprintf(
|
|
|
467 |
'insert into ~~linkageTable~~ (~~linkageColumn~~, ~~foreignColumn~~ ) values (%s, %s)',
|
|
|
468 |
$this->__get('id'),
|
|
|
469 |
$index
|
|
|
470 |
),
|
|
|
471 |
$record
|
|
|
472 |
);
|
|
|
473 |
} // if..elseif
|
|
|
474 |
} // foreach
|
|
|
475 |
return $queries;
|
|
|
476 |
}
|
|
|
477 |
|
64 |
rodolico |
478 |
/**
|
|
|
479 |
* Posts the results of a previous form
|
|
|
480 |
*
|
|
|
481 |
* After a form is filled in, will search for any values which have
|
|
|
482 |
* changed. It will generate a separate SQL statement for each of them
|
|
|
483 |
* and execute the queries in in batch
|
|
|
484 |
*
|
|
|
485 |
* Once this is done, will reload the values from the database, so
|
|
|
486 |
* we immediately know if there was a problem.
|
|
|
487 |
*/
|
63 |
rodolico |
488 |
protected function post() {
|
|
|
489 |
global $dbConnection;
|
|
|
490 |
|
|
|
491 |
$queries = array();
|
|
|
492 |
$fields = array();
|
|
|
493 |
foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
|
|
|
494 |
if ( isset( $record['display'] ) && $record['display'] === false ) // we don't try to show links
|
|
|
495 |
continue;
|
|
|
496 |
if ( isset( $record['canEdit'] ) && ! $record['canEdit'] )
|
|
|
497 |
continue;
|
64 |
rodolico |
498 |
if ( $_REQUEST[$record['fieldname']] != $this->data[$record['fieldname']] ) { // they have changed a value
|
65 |
rodolico |
499 |
switch ( $record['type'] ) {
|
|
|
500 |
case '1:1H' :
|
|
|
501 |
$queries = array_merge( $queries,
|
|
|
502 |
$this->post1to1History(
|
|
|
503 |
$record['linkageTable'],
|
|
|
504 |
$record['linkageColumn'],
|
|
|
505 |
$record['foreignColumn'],
|
|
|
506 |
$_REQUEST[$record['fieldname']]
|
|
|
507 |
)
|
64 |
rodolico |
508 |
);
|
65 |
rodolico |
509 |
break;
|
|
|
510 |
case '1:M' :
|
|
|
511 |
$queries = array_merge( $queries, $this->oneToManyPost( $key, $record ) );
|
|
|
512 |
break;
|
|
|
513 |
default:
|
|
|
514 |
$fields[$record['fieldname']] = $_REQUEST[$record['fieldname']];
|
|
|
515 |
} // case
|
64 |
rodolico |
516 |
} // if data has changed
|
63 |
rodolico |
517 |
} // foreach
|
|
|
518 |
//print "<pre>Fields\n" . print_r( $this->data, true ) . "</pre>"; die;
|
|
|
519 |
if ( count( $fields ) ) {
|
|
|
520 |
if ( isset( $this->data[static::$dbStructure['table']['primaryKey']] ) ) { // this is an update
|
|
|
521 |
$queries[] = $dbConnection->updateQuery(
|
|
|
522 |
static::$dbStructure['table']['tableName'],
|
|
|
523 |
array( static::$dbStructure['table']['primaryKey'] => $this->data[static::$dbStructure['table']['primaryKey']] ),
|
|
|
524 |
$fields );
|
|
|
525 |
} else { // this is an insert
|
|
|
526 |
$queries[] = $dbConnection->insertQuery(
|
|
|
527 |
static::$dbStructure['table']['tableName'],
|
|
|
528 |
$fields );
|
|
|
529 |
}
|
|
|
530 |
} // if there are fields
|
65 |
rodolico |
531 |
/*
|
64 |
rodolico |
532 |
print "<pre>Request" . print_r( $_REQUEST, true ) . "</pre>";
|
|
|
533 |
print "<pre>Data" . print_r( $this->data, true ) . "</pre>";
|
|
|
534 |
print "<pre>Queries" . print_r( $queries, true ) . "</pre>"; die;
|
65 |
rodolico |
535 |
*/
|
63 |
rodolico |
536 |
$dbConnection->runSQLScript( $queries );
|
|
|
537 |
$this->loadFromDB( $this->data[static::$dbStructure['table']['primaryKey']] );
|
|
|
538 |
} // post
|
|
|
539 |
|
|
|
540 |
/**
|
61 |
rodolico |
541 |
* Simple controller. Will determine what action is going on, then
|
|
|
542 |
* call the correct method, returning the HTML required
|
|
|
543 |
*/
|
56 |
rodolico |
544 |
public function run() {
|
63 |
rodolico |
545 |
if ( isset( $_REQUEST['action'] ) ) {
|
|
|
546 |
switch ( $_REQUEST['action'] ) {
|
|
|
547 |
case 'edit' : $return = $this->edit();
|
|
|
548 |
break;
|
|
|
549 |
case 'post' : $this->post();
|
|
|
550 |
default: $return = $this->display();
|
|
|
551 |
} // switch
|
|
|
552 |
} else {
|
|
|
553 |
$return = $this->display();
|
|
|
554 |
}
|
|
|
555 |
return $return;
|
56 |
rodolico |
556 |
}
|
53 |
rodolico |
557 |
} // abstract class Camp
|
|
|
558 |
|
|
|
559 |
?>
|