1 | <?php |
---|
2 | /** |
---|
3 | * %ADMIN_SCRIPT% |
---|
4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
5 | * |
---|
6 | * Generated by module_maker.cli.php on %DATE% |
---|
7 | */ |
---|
8 | |
---|
9 | require_once dirname(__FILE__) . '/_config.inc.php'; |
---|
10 | |
---|
11 | $auth->requireLogin(); |
---|
12 | // $auth->requireAccessClearance(ZONE_ADMIN_%NAME_UPPER%); |
---|
13 | $app->sslOn(); |
---|
14 | |
---|
15 | require_once 'codebase/lib/PageNumbers.inc.php'; |
---|
16 | require_once 'codebase/lib/Cache.inc.php'; |
---|
17 | require_once 'codebase/lib/FormValidator.inc.php'; |
---|
18 | require_once 'codebase/lib/SortOrder.inc.php'; |
---|
19 | require_once 'codebase/lib/TemplateGlue.inc.php'; |
---|
20 | require_once 'codebase/lib/Prefs.inc.php'; |
---|
21 | require_once 'codebase/lib/Lock.inc.php'; |
---|
22 | require_once 'codebase/lib/Version.inc.php'; |
---|
23 | %ADMIN_UPLOAD_INCLUDE% |
---|
24 | |
---|
25 | /******************************************************************** |
---|
26 | * CONFIG |
---|
27 | ********************************************************************/ |
---|
28 | |
---|
29 | // Titles and navigation header. |
---|
30 | $nav->add(_("%TITLE%"), null); |
---|
31 | |
---|
32 | // The object to validate form input. |
---|
33 | $fv = new FormValidator(); |
---|
34 | |
---|
35 | // Configure the prefs object. |
---|
36 | $tmp_prefs = new Prefs('%NAME_PLURAL%'); |
---|
37 | $tmp_prefs->setParam(array('persistent' => false)); |
---|
38 | |
---|
39 | // Configure the cache object. |
---|
40 | $cache = new Cache('%NAME_PLURAL%'); |
---|
41 | $cache->setParam(array('enable' => true)); |
---|
42 | |
---|
43 | %SORT_ORDER% |
---|
44 | |
---|
45 | // Instantiate page numbers. Total items are set and calculation is done in the getCachedList function. |
---|
46 | $page = new PageNumbers(); |
---|
47 | $page->setPerPage(getFormData('per_page'), 100); |
---|
48 | $page->setPageNumber(getFormData('page_number')); |
---|
49 | |
---|
50 | // Search limiters retain their values between page requests. |
---|
51 | $app->carryQuery('search_query'); |
---|
52 | $app->carryQuery('filter___///__'); |
---|
53 | %ADMIN_UPLOAD_CONFIG% |
---|
54 | /******************************************************************** |
---|
55 | * MAIN |
---|
56 | ********************************************************************/ |
---|
57 | %ADMIN_UPLOAD_INIT% |
---|
58 | // We may want to use the add/edit interface from another script, so this |
---|
59 | // allows us to remember which page we came from so we can go back there. |
---|
60 | if (getFormData('boomerang', false) && isset($_SERVER['HTTP_REFERER'])) { |
---|
61 | $app->setBoomerangURL($_SERVER['HTTP_REFERER'], '%NAME_PLURAL%'); |
---|
62 | } |
---|
63 | |
---|
64 | if (getFormData('break_list_cache', false)) { |
---|
65 | // Remove any stale cached list data. |
---|
66 | $cache->delete('list'); |
---|
67 | } |
---|
68 | |
---|
69 | // What action to take. |
---|
70 | switch (getFormData('op')) { |
---|
71 | |
---|
72 | case 'add' : |
---|
73 | // $auth->requireAccessClearance(ZONE_ADMIN_%NAME_UPPER%_FUNC_ADD); |
---|
74 | // Initialize variables for the form template. |
---|
75 | $frm =& addRecordForm(); |
---|
76 | $nav->add(_("Add %ITEM_TITLE%")); |
---|
77 | $main_template = '%ADMIN_FORM_TEMPLATE%'; |
---|
78 | break; |
---|
79 | |
---|
80 | case 'edit' : |
---|
81 | // $auth->requireAccessClearance(ZONE_ADMIN_%NAME_UPPER%_FUNC_EDIT); |
---|
82 | // Initialize variables for the form template. |
---|
83 | $frm =& editRecordForm(getFormData('%PRIMARY_KEY%')); |
---|
84 | $nav->add(_("Edit %ITEM_TITLE%")); |
---|
85 | $main_template = '%ADMIN_FORM_TEMPLATE%'; |
---|
86 | break; |
---|
87 | |
---|
88 | case 'del' : |
---|
89 | // $auth->requireAccessClearance(ZONE_ADMIN_%NAME_UPPER%_FUNC_DELETE); |
---|
90 | deleteRecord(getFormData('%PRIMARY_KEY%'));%ADMIN_UPLOAD_DEL% |
---|
91 | if ($app->validBoomerangURL('%NAME_PLURAL%')) { |
---|
92 | // Display boomerang page. |
---|
93 | $app->dieBoomerangURL('%NAME_PLURAL%'); |
---|
94 | } |
---|
95 | // Display default page. |
---|
96 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
97 | break; |
---|
98 | |
---|
99 | case 'insert' : |
---|
100 | // $auth->requireAccessClearance(ZONE_ADMIN_%NAME_UPPER%_FUNC_ADD); |
---|
101 | if (getFormdata('cancel', false)) { |
---|
102 | if ($app->validBoomerangURL('%NAME_PLURAL%')) { |
---|
103 | // Display boomerang page. |
---|
104 | $app->dieBoomerangURL('%NAME_PLURAL%'); |
---|
105 | } |
---|
106 | // Display default page. |
---|
107 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
108 | } |
---|
109 | validateInput(); |
---|
110 | if ($fv->anyErrors()) { |
---|
111 | $frm =& addRecordForm(); |
---|
112 | $frm = array_merge($frm, getFormData()); |
---|
113 | $nav->add(_("Add %ITEM_TITLE%")); |
---|
114 | $main_template = '%ADMIN_FORM_TEMPLATE%'; |
---|
115 | } else { |
---|
116 | $%PRIMARY_KEY% = insertRecord(getFormData());%ADMIN_UPLOAD_INSERT% |
---|
117 | if (getFormdata('repeat', false)) { |
---|
118 | // Display function again. |
---|
119 | $app->dieURL($_SERVER['PHP_SELF'] . '?op=add'); |
---|
120 | } else if ($app->validBoomerangURL('%NAME_PLURAL%')) { |
---|
121 | // Display boomerang page. |
---|
122 | $app->dieBoomerangURL('%NAME_PLURAL%'); |
---|
123 | } |
---|
124 | // Display default page. |
---|
125 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
126 | } |
---|
127 | break; |
---|
128 | |
---|
129 | case 'update' : |
---|
130 | // $auth->requireAccessClearance(ZONE_ADMIN_%NAME_UPPER%_FUNC_EDIT); |
---|
131 | if (getFormdata('reset', false)) { |
---|
132 | $app->raiseMsg(_("Saved values have been reloaded."), MSG_NOTICE, __FILE__, __LINE__); |
---|
133 | $app->dieURL($_SERVER['PHP_SELF'] . '?op=edit&%PRIMARY_KEY%=' . getFormData('%PRIMARY_KEY%')); |
---|
134 | } |
---|
135 | if (getFormdata('cancel', false)) { |
---|
136 | // Remove lock |
---|
137 | $lock->select('%DB_TBL%', '%PRIMARY_KEY%', getFormData('%PRIMARY_KEY%')); |
---|
138 | $lock->remove(); |
---|
139 | if ($app->validBoomerangURL('%NAME_PLURAL%')) { |
---|
140 | // Display boomerang page. |
---|
141 | $app->dieBoomerangURL('%NAME_PLURAL%'); |
---|
142 | } |
---|
143 | // Display default page. |
---|
144 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
145 | } |
---|
146 | validateInput(); |
---|
147 | if ($fv->anyErrors()) { |
---|
148 | $frm =& editRecordForm(getFormData('%PRIMARY_KEY%')); |
---|
149 | $frm = array_merge($frm, getFormData()); |
---|
150 | $nav->add(_("Edit %ITEM_TITLE%")); |
---|
151 | $main_template = '%ADMIN_FORM_TEMPLATE%'; |
---|
152 | } else {%ADMIN_UPLOAD_UPDATE% |
---|
153 | updateRecord(getFormData()); |
---|
154 | if (getFormdata('repeat', false)) { |
---|
155 | // Display edit function with next available ID. |
---|
156 | $qid = $db->query("SELECT %PRIMARY_KEY% FROM %DB_TBL% WHERE %PRIMARY_KEY% > '" . $db->escapeString(getFormData('%PRIMARY_KEY%')) . "' ORDER BY %PRIMARY_KEY% ASC LIMIT 1"); |
---|
157 | if (list($next_id) = mysql_fetch_row($qid)) { |
---|
158 | $app->dieURL($_SERVER['PHP_SELF'] . '?op=edit&%PRIMARY_KEY%=' . $next_id); |
---|
159 | } else { |
---|
160 | $app->raiseMsg(_("Cannot edit next, the end of the list was reached"), MSG_NOTICE, __FILE__, __LINE__); |
---|
161 | } |
---|
162 | } else if ($app->validBoomerangURL('%NAME_PLURAL%')) { |
---|
163 | // Display boomerang page. |
---|
164 | $app->dieBoomerangURL('%NAME_PLURAL%'); |
---|
165 | } |
---|
166 | // Display default page. |
---|
167 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
168 | } |
---|
169 | break; |
---|
170 | |
---|
171 | case _("Save rank") : |
---|
172 | // $auth->requireAccessClearance(ZONE_ADMIN_PROGRAMS_FUNC_REORDER); |
---|
173 | updateRank(getFormData('rank')); |
---|
174 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
175 | break; |
---|
176 | |
---|
177 | default : |
---|
178 | // $auth->requireAccessClearance(ZONE_ADMIN_%NAME_UPPER%_FUNC_LIST, _("Permission to view %NAME_PLURAL% list denied.")); |
---|
179 | $list =& getCachedList(); |
---|
180 | $main_template = '%ADMIN_LIST_TEMPLATE%'; |
---|
181 | break; |
---|
182 | } |
---|
183 | |
---|
184 | /****************************************************************************** |
---|
185 | * TEMPLATE INITIALIZATION |
---|
186 | *****************************************************************************/ |
---|
187 | |
---|
188 | include 'header.ihtml'; |
---|
189 | include $main_template; |
---|
190 | include 'footer.ihtml'; |
---|
191 | |
---|
192 | /******************************************************************** |
---|
193 | * FUNCTIONS |
---|
194 | ********************************************************************/ |
---|
195 | |
---|
196 | %FORM_VALIDATION% |
---|
197 | |
---|
198 | function &addRecordForm() |
---|
199 | { |
---|
200 | // Set default values for the reset of the fields. |
---|
201 | $frm = array( |
---|
202 | %SET_VALUES_DEFAULT%, |
---|
203 | 'new_op' => 'insert', |
---|
204 | 'submit_buttons' => array( |
---|
205 | array('name' => 'submit', 'value' => _("Add %ITEM_TITLE%"), 'accesskey' => 's'), |
---|
206 | array('name' => 'repeat', 'value' => _("Add & repeat"), 'accesskey' => 'r'), |
---|
207 | array('name' => 'cancel', 'value' => _("Cancel"), 'accesskey' => 'c'), |
---|
208 | ), |
---|
209 | ); |
---|
210 | |
---|
211 | return $frm; |
---|
212 | } |
---|
213 | |
---|
214 | function &editRecordForm($id) |
---|
215 | { |
---|
216 | global $lock; |
---|
217 | $db =& DB::getInstance(); |
---|
218 | $app =& App::getInstance(); |
---|
219 | |
---|
220 | $lock->select('%DB_TBL%', '%PRIMARY_KEY%', $id); |
---|
221 | if ($lock->isLocked() && !$lock->isMine()) { |
---|
222 | $lock->dieErrorPage(); |
---|
223 | } |
---|
224 | |
---|
225 | // Get the information for the form. |
---|
226 | $qid = $db->query(" |
---|
227 | SELECT * |
---|
228 | FROM %DB_TBL% |
---|
229 | WHERE %PRIMARY_KEY% = '" . $db->escapeString($id) . "' |
---|
230 | "); |
---|
231 | if (!$frm = mysql_fetch_assoc($qid)) { |
---|
232 | $app->logMsg('Could not find record with %PRIMARY_KEY%: ' . $id, LOG_WARNING, __FILE__, __LINE__); |
---|
233 | $app->raiseMsg(sprintf(_("The requested record %s could not be found."), $id), MSG_ERR, __FILE__, __LINE__); |
---|
234 | $app->dieBoomerangURL(); |
---|
235 | } |
---|
236 | |
---|
237 | // Lock this record. |
---|
238 | $lock->set('%DB_TBL%', '%PRIMARY_KEY%', $id, $frm['__///__']); |
---|
239 | |
---|
240 | // Set misc values for the form. |
---|
241 | $frm = array_merge(array( |
---|
242 | %SET_VALUES_DEFAULT%, |
---|
243 | 'new_op' => 'update', |
---|
244 | 'submit_buttons' => array( |
---|
245 | array('name' => 'submit', 'value' => _("Save changes"), 'accesskey' => 's'), |
---|
246 | array('name' => 'repeat', 'value' => _("Save & edit next"), 'accesskey' => 'e'), |
---|
247 | array('name' => 'reset', 'value' => _("Reset"), 'accesskey' => 'r'), |
---|
248 | array('name' => 'cancel', 'value' => _("Cancel"), 'accesskey' => 'c'), |
---|
249 | ), |
---|
250 | ), $frm); |
---|
251 | |
---|
252 | return $frm; |
---|
253 | } |
---|
254 | |
---|
255 | function deleteRecord($id) |
---|
256 | { |
---|
257 | global $lock; |
---|
258 | global $cache; |
---|
259 | $db =& DB::getInstance(); |
---|
260 | $app =& App::getInstance(); |
---|
261 | |
---|
262 | $lock->select('%DB_TBL%', '%PRIMARY_KEY%', $id); |
---|
263 | if ($lock->isLocked() && !$lock->isMine()) { |
---|
264 | $lock->dieErrorPage(); |
---|
265 | } |
---|
266 | |
---|
267 | // Remove any stale cached list data. |
---|
268 | $cache->delete('list'); |
---|
269 | |
---|
270 | // Get the information for this object. |
---|
271 | $qid = $db->query(" |
---|
272 | SELECT __///__ |
---|
273 | FROM %DB_TBL% |
---|
274 | WHERE %PRIMARY_KEY% = '" . $db->escapeString($id) . "' |
---|
275 | "); |
---|
276 | if (! list($name) = mysql_fetch_row($qid)) { |
---|
277 | $app->logMsg('Could not find record with %PRIMARY_KEY%: ' . $id, LOG_WARNING, __FILE__, __LINE__); |
---|
278 | $app->raiseMsg(sprintf(_("The requested record %s could not be found."), $id), MSG_ERR, __FILE__, __LINE__); |
---|
279 | $app->dieBoomerangURL(); |
---|
280 | } |
---|
281 | |
---|
282 | // Delete the record. |
---|
283 | $db->query("DELETE FROM %DB_TBL% WHERE %PRIMARY_KEY% = '" . $db->escapeString($id) . "'"); |
---|
284 | |
---|
285 | $app->raiseMsg(sprintf(_("The %ITEM_TITLE% <em>%s</em> has been deleted."), $name), MSG_SUCCESS, __FILE__, __LINE__); |
---|
286 | |
---|
287 | // Unlock record. |
---|
288 | $lock->remove(); |
---|
289 | } |
---|
290 | |
---|
291 | function insertRecord($frm) |
---|
292 | { |
---|
293 | global $auth; |
---|
294 | global $cache; |
---|
295 | $db =& DB::getInstance(); |
---|
296 | $app =& App::getInstance(); |
---|
297 | |
---|
298 | // Remove any stale cached list data. |
---|
299 | $cache->delete('list'); |
---|
300 | |
---|
301 | %INSERT% |
---|
302 | $last_insert_id = mysql_insert_id($db->getDBH()); |
---|
303 | |
---|
304 | // Create version. |
---|
305 | $version = Version::getInstance($auth); |
---|
306 | $version->create('%DB_TBL%', '%PRIMARY_KEY%', $last_insert_id, $frm['__///__']); |
---|
307 | |
---|
308 | $app->raiseMsg(sprintf(_("The %ITEM_TITLE% <em>%s</em> has been added."), $frm['__///__']), MSG_SUCCESS, __FILE__, __LINE__); |
---|
309 | |
---|
310 | return $last_insert_id; |
---|
311 | } |
---|
312 | |
---|
313 | function updateRecord($frm) |
---|
314 | { |
---|
315 | global $auth; |
---|
316 | global $lock; |
---|
317 | global $cache; |
---|
318 | $db =& DB::getInstance(); |
---|
319 | $app =& App::getInstance(); |
---|
320 | |
---|
321 | $lock->select('%DB_TBL%', '%PRIMARY_KEY%', $frm['%PRIMARY_KEY%']); |
---|
322 | if ($lock->isLocked() && !$lock->isMine()) { |
---|
323 | $lock->dieErrorPage(); |
---|
324 | } |
---|
325 | |
---|
326 | // Remove any stale cached list data. |
---|
327 | $cache->delete('list'); |
---|
328 | |
---|
329 | %UPDATE% |
---|
330 | |
---|
331 | // Create version. |
---|
332 | $version = Version::getInstance($auth); |
---|
333 | $version->create('%DB_TBL%', '%PRIMARY_KEY%', $frm['%PRIMARY_KEY%'], $frm['__///__']); |
---|
334 | |
---|
335 | $app->raiseMsg(sprintf(_("The %ITEM_TITLE% <em>%s</em> has been updated."), $frm['__///__']), MSG_SUCCESS, __FILE__, __LINE__); |
---|
336 | |
---|
337 | // Unlock record. |
---|
338 | $lock->remove(); |
---|
339 | } |
---|
340 | |
---|
341 | function &getCachedList() |
---|
342 | { |
---|
343 | global $page; |
---|
344 | global $so; |
---|
345 | global $tmp_prefs; |
---|
346 | global $cache; |
---|
347 | $db =& DB::getInstance(); |
---|
348 | $app =& App::getInstance(); |
---|
349 | |
---|
350 | $where_clause = ''; |
---|
351 | |
---|
352 | // Build search query if available. |
---|
353 | if (getFormData('search_query', false)) { |
---|
354 | $qry_words = preg_split('/[^\w]/', getFormData('search_query')); |
---|
355 | for ($i=0; $i<sizeof($qry_words); $i++) { |
---|
356 | %SEARCH% |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | if (getFormData('filter___///__', false)) { |
---|
361 | // Limit by filter. |
---|
362 | $where_clause .= (empty($where_clause) ? 'WHERE' : 'AND') . " __///__ = '" . $db->escapeString(getFormData('filter___///__')) . "'"; |
---|
363 | } |
---|
364 | |
---|
365 | // Count the total number of records so we can do something about the page numbers. |
---|
366 | $qid = $db->query(" |
---|
367 | SELECT COUNT(*) |
---|
368 | FROM %DB_TBL% |
---|
369 | $where_clause |
---|
370 | "); |
---|
371 | list($num_results) = mysql_fetch_row($qid); |
---|
372 | |
---|
373 | // Set page numbers now we know (needed for next step). |
---|
374 | $page->setTotalItems($num_results); |
---|
375 | $page->calculate(); |
---|
376 | |
---|
377 | // Final SQL, with sort and page limiters. |
---|
378 | $sql = " |
---|
379 | SELECT |
---|
380 | %DB_TBL%.*, |
---|
381 | a1.username AS added_by_username, |
---|
382 | a2.username AS modified_by_username |
---|
383 | FROM %DB_TBL% |
---|
384 | LEFT JOIN user_tbl a1 ON (%DB_TBL%.added_by_user_id = a1.user_id) |
---|
385 | LEFT JOIN user_tbl a2 ON (%DB_TBL%.modified_by_user_id = a2.user_id) |
---|
386 | $where_clause |
---|
387 | " . $so->getSortOrderSQL() . " |
---|
388 | " . $page->getLimitSQL() . " |
---|
389 | "; |
---|
390 | |
---|
391 | // Use a cash hash to determine if the result-set has changed. |
---|
392 | // A unique key for this query, with the total_items in case db records |
---|
393 | // were added since the last cache. This identifies a unique set of |
---|
394 | // cached data, but we must refer to the list that is cached by a more |
---|
395 | // generic name. so that we can flush the cache (if records updated) |
---|
396 | // without knowing the hash. |
---|
397 | $cache_hash = md5($sql . '|' . $page->total_items); |
---|
398 | if ($tmp_prefs->get('cache_hash') != $cache_hash) { |
---|
399 | $cache->delete('list'); |
---|
400 | $tmp_prefs->set('cache_hash', $cache_hash); |
---|
401 | } |
---|
402 | |
---|
403 | // First try to return from the cache. |
---|
404 | if ($cache->exists('list')) { |
---|
405 | $list = $cache->get('list'); |
---|
406 | return $list; |
---|
407 | } |
---|
408 | |
---|
409 | // The list was not cached, so issue the real query. |
---|
410 | $qid = $db->query($sql); |
---|
411 | while ($row = mysql_fetch_assoc($qid)) { |
---|
412 | $list[] = $row; |
---|
413 | } |
---|
414 | |
---|
415 | // Save this list into the cache. |
---|
416 | if (isset($list) && !empty($list)) { |
---|
417 | $cache->set('list', $list); |
---|
418 | } |
---|
419 | |
---|
420 | return $list; |
---|
421 | } |
---|
422 | |
---|
423 | function updateRank($ranks) |
---|
424 | { |
---|
425 | global $cache; |
---|
426 | $db =& DB::getInstance(); |
---|
427 | $app =& App::getInstance(); |
---|
428 | |
---|
429 | if (!is_array($ranks)) { |
---|
430 | $app->logMsg('Saving rank failed, data posted is not an array: ' . $ranks, LOG_ERR, __FILE__, __LINE__); |
---|
431 | return false; |
---|
432 | } |
---|
433 | |
---|
434 | // Remove any stale cached list data. |
---|
435 | $cache->delete('list'); |
---|
436 | |
---|
437 | // Count the ranks with invalid numbers |
---|
438 | $unspecified_counter = 0; |
---|
439 | |
---|
440 | // Go through the array of new ranks. |
---|
441 | foreach ($ranks as $id => $new_rank) { |
---|
442 | if ('' == trim($new_rank) || !is_numeric($new_rank) || $new_rank > 2147483646) { |
---|
443 | // Unspecified entries receive a sort order of 10000. |
---|
444 | $new_rank = 10000; |
---|
445 | $unspecified_counter++; |
---|
446 | } |
---|
447 | $db->query(" |
---|
448 | UPDATE %DB_TBL% SET |
---|
449 | rank = '" . $db->escapeString($new_rank) . "' |
---|
450 | WHERE %PRIMARY_KEY% = '" . $db->escapeString($id) . "' |
---|
451 | "); |
---|
452 | } |
---|
453 | |
---|
454 | $app->raiseMsg(_("Records have been reordered with the new rank."), MSG_SUCCESS, __FILE__, __LINE__); |
---|
455 | if ($unspecified_counter > 0) { |
---|
456 | $app->raiseMsg(sprintf(_("%s items with unspecified ranks were automatically assigned a rank of 10000."), $unspecified_counter), MSG_NOTICE, __FILE__, __LINE__); |
---|
457 | } |
---|
458 | } |
---|
459 | |
---|
460 | ?> |
---|