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