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