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