[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%); |
---|
[136] | 13 | $app->sslOn(); |
---|
[1] | 14 | |
---|
| 15 | require_once 'codebase/lib/PageNumbers.inc.php'; |
---|
[152] | 16 | require_once 'codebase/lib/Cache.inc.php'; |
---|
[1] | 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'; |
---|
[137] | 21 | require_once 'codebase/lib/Lock.inc.php'; |
---|
| 22 | require_once 'codebase/lib/Version.inc.php'; |
---|
[20] | 23 | %ADMIN_UPLOAD_INCLUDE% |
---|
[1] | 24 | |
---|
[143] | 25 | /******************************************************************** |
---|
| 26 | * CONFIG |
---|
| 27 | ********************************************************************/ |
---|
[42] | 28 | |
---|
[1] | 29 | // Titles and navigation header. |
---|
[202] | 30 | $nav->add(_("%TITLE%"), null); |
---|
[42] | 31 | |
---|
[1] | 32 | // The object to validate form input. |
---|
| 33 | $fv = new FormValidator(); |
---|
| 34 | |
---|
[152] | 35 | // Configure the prefs object. |
---|
[153] | 36 | $tmp_prefs = new Prefs('%NAME_PLURAL%'); |
---|
| 37 | $tmp_prefs->setParam(array('persistent' => false)); |
---|
[136] | 38 | |
---|
[152] | 39 | // Configure the cache object. |
---|
[153] | 40 | $cache = new Cache('%NAME_PLURAL%'); |
---|
[152] | 41 | $cache->setParam(array('enable' => true)); |
---|
| 42 | |
---|
[1] | 43 | %SORT_ORDER% |
---|
| 44 | |
---|
[152] | 45 | // Instantiate page numbers. Total items are set and calculation is done in the getCachedList function. |
---|
[1] | 46 | $page = new PageNumbers(); |
---|
| 47 | $page->setPerPage(getFormData('per_page'), 100); |
---|
| 48 | $page->setPageNumber(getFormData('page_number')); |
---|
| 49 | |
---|
[20] | 50 | // Search limiters retain their values between page requests. |
---|
[136] | 51 | $app->carryQuery('search_query'); |
---|
[154] | 52 | $app->carryQuery('filter___///__'); |
---|
[20] | 53 | %ADMIN_UPLOAD_CONFIG% |
---|
[143] | 54 | /******************************************************************** |
---|
| 55 | * MAIN |
---|
| 56 | ********************************************************************/ |
---|
[20] | 57 | %ADMIN_UPLOAD_INIT% |
---|
[1] | 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. |
---|
[20] | 60 | if (getFormData('boomerang', false) && isset($_SERVER['HTTP_REFERER'])) { |
---|
[136] | 61 | $app->setBoomerangURL($_SERVER['HTTP_REFERER'], '%NAME_PLURAL%'); |
---|
[1] | 62 | } |
---|
| 63 | |
---|
| 64 | if (getFormData('break_list_cache', false)) { |
---|
[152] | 65 | // Remove any stale cached list data. |
---|
| 66 | $cache->delete('list'); |
---|
[1] | 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(); |
---|
[202] | 76 | $nav->add(_("Add %ITEM_TITLE%")); |
---|
[1] | 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%')); |
---|
[202] | 84 | $nav->add(_("Edit %ITEM_TITLE%")); |
---|
[1] | 85 | $main_template = '%ADMIN_FORM_TEMPLATE%'; |
---|
| 86 | break; |
---|
| 87 | |
---|
| 88 | case 'del' : |
---|
| 89 | // $auth->requireAccessClearance(ZONE_ADMIN_%NAME_UPPER%_FUNC_DELETE); |
---|
[20] | 90 | deleteRecord(getFormData('%PRIMARY_KEY%'));%ADMIN_UPLOAD_DEL% |
---|
[136] | 91 | if ($app->validBoomerangURL('%NAME_PLURAL%')) { |
---|
[1] | 92 | // Display boomerang page. |
---|
[136] | 93 | $app->dieBoomerangURL('%NAME_PLURAL%'); |
---|
[1] | 94 | } |
---|
| 95 | // Display default page. |
---|
[136] | 96 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
[1] | 97 | break; |
---|
| 98 | |
---|
| 99 | case 'insert' : |
---|
| 100 | // $auth->requireAccessClearance(ZONE_ADMIN_%NAME_UPPER%_FUNC_ADD); |
---|
| 101 | if (getFormdata('cancel', false)) { |
---|
[136] | 102 | if ($app->validBoomerangURL('%NAME_PLURAL%')) { |
---|
[22] | 103 | // Display boomerang page. |
---|
[136] | 104 | $app->dieBoomerangURL('%NAME_PLURAL%'); |
---|
[22] | 105 | } |
---|
| 106 | // Display default page. |
---|
[136] | 107 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
[1] | 108 | } |
---|
| 109 | validateInput(); |
---|
| 110 | if ($fv->anyErrors()) { |
---|
| 111 | $frm =& addRecordForm(); |
---|
| 112 | $frm = array_merge($frm, getFormData()); |
---|
[202] | 113 | $nav->add(_("Add %ITEM_TITLE%")); |
---|
[1] | 114 | $main_template = '%ADMIN_FORM_TEMPLATE%'; |
---|
[22] | 115 | } else { |
---|
| 116 | $%PRIMARY_KEY% = insertRecord(getFormData());%ADMIN_UPLOAD_INSERT% |
---|
[1] | 117 | if (getFormdata('repeat', false)) { |
---|
| 118 | // Display function again. |
---|
[136] | 119 | $app->dieURL($_SERVER['PHP_SELF'] . '?op=add'); |
---|
| 120 | } else if ($app->validBoomerangURL('%NAME_PLURAL%')) { |
---|
[1] | 121 | // Display boomerang page. |
---|
[136] | 122 | $app->dieBoomerangURL('%NAME_PLURAL%'); |
---|
[1] | 123 | } |
---|
| 124 | // Display default page. |
---|
[136] | 125 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
[1] | 126 | } |
---|
| 127 | break; |
---|
| 128 | |
---|
| 129 | case 'update' : |
---|
| 130 | // $auth->requireAccessClearance(ZONE_ADMIN_%NAME_UPPER%_FUNC_EDIT); |
---|
| 131 | if (getFormdata('reset', false)) { |
---|
[136] | 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%')); |
---|
[1] | 134 | } |
---|
| 135 | if (getFormdata('cancel', false)) { |
---|
| 136 | // Remove lock |
---|
| 137 | $lock->select('%DB_TBL%', '%PRIMARY_KEY%', getFormData('%PRIMARY_KEY%')); |
---|
| 138 | $lock->remove(); |
---|
[136] | 139 | if ($app->validBoomerangURL('%NAME_PLURAL%')) { |
---|
[1] | 140 | // Display boomerang page. |
---|
[136] | 141 | $app->dieBoomerangURL('%NAME_PLURAL%'); |
---|
[1] | 142 | } |
---|
| 143 | // Display default page. |
---|
[136] | 144 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
[1] | 145 | } |
---|
| 146 | validateInput(); |
---|
| 147 | if ($fv->anyErrors()) { |
---|
| 148 | $frm =& editRecordForm(getFormData('%PRIMARY_KEY%')); |
---|
| 149 | $frm = array_merge($frm, getFormData()); |
---|
[202] | 150 | $nav->add(_("Edit %ITEM_TITLE%")); |
---|
[1] | 151 | $main_template = '%ADMIN_FORM_TEMPLATE%'; |
---|
[20] | 152 | } else {%ADMIN_UPLOAD_UPDATE% |
---|
[1] | 153 | updateRecord(getFormData()); |
---|
| 154 | if (getFormdata('repeat', false)) { |
---|
| 155 | // Display edit function with next available ID. |
---|
[136] | 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"); |
---|
[1] | 157 | if (list($next_id) = mysql_fetch_row($qid)) { |
---|
[136] | 158 | $app->dieURL($_SERVER['PHP_SELF'] . '?op=edit&%PRIMARY_KEY%=' . $next_id); |
---|
[1] | 159 | } else { |
---|
[136] | 160 | $app->raiseMsg(_("Cannot edit next, the end of the list was reached"), MSG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 161 | } |
---|
[136] | 162 | } else if ($app->validBoomerangURL('%NAME_PLURAL%')) { |
---|
[1] | 163 | // Display boomerang page. |
---|
[136] | 164 | $app->dieBoomerangURL('%NAME_PLURAL%'); |
---|
[1] | 165 | } |
---|
| 166 | // Display default page. |
---|
[136] | 167 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
[1] | 168 | } |
---|
| 169 | break; |
---|
| 170 | |
---|
| 171 | case _("Save rank") : |
---|
| 172 | // $auth->requireAccessClearance(ZONE_ADMIN_PROGRAMS_FUNC_REORDER); |
---|
| 173 | updateRank(getFormData('rank')); |
---|
[136] | 174 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
[1] | 175 | break; |
---|
| 176 | |
---|
| 177 | default : |
---|
| 178 | // $auth->requireAccessClearance(ZONE_ADMIN_%NAME_UPPER%_FUNC_LIST, _("Permission to view %NAME_PLURAL% list denied.")); |
---|
[152] | 179 | $list =& getCachedList(); |
---|
[1] | 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 | |
---|
[143] | 192 | /******************************************************************** |
---|
| 193 | * FUNCTIONS |
---|
| 194 | ********************************************************************/ |
---|
[1] | 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', |
---|
[10] | 204 | 'submit_buttons' => array( |
---|
[295] | 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'), |
---|
[1] | 208 | ), |
---|
| 209 | ); |
---|
| 210 | |
---|
| 211 | return $frm; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | function &editRecordForm($id) |
---|
| 215 | { |
---|
[22] | 216 | global $lock; |
---|
[136] | 217 | $db =& DB::getInstance(); |
---|
[295] | 218 | $app =& App::getInstance(); |
---|
[136] | 219 | |
---|
[1] | 220 | $lock->select('%DB_TBL%', '%PRIMARY_KEY%', $id); |
---|
| 221 | if ($lock->isLocked() && !$lock->isMine()) { |
---|
| 222 | $lock->dieErrorPage(); |
---|
[10] | 223 | } |
---|
| 224 | |
---|
| 225 | // Get the information for the form. |
---|
[136] | 226 | $qid = $db->query(" |
---|
[42] | 227 | SELECT * |
---|
[10] | 228 | FROM %DB_TBL% |
---|
[136] | 229 | WHERE %PRIMARY_KEY% = '" . $db->escapeString($id) . "' |
---|
[10] | 230 | "); |
---|
| 231 | if (!$frm = mysql_fetch_assoc($qid)) { |
---|
[136] | 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(); |
---|
[10] | 235 | } |
---|
| 236 | |
---|
| 237 | // Lock this record. |
---|
[154] | 238 | $lock->set('%DB_TBL%', '%PRIMARY_KEY%', $id, $frm['__///__']); |
---|
[42] | 239 | |
---|
[10] | 240 | // Set misc values for the form. |
---|
| 241 | $frm = array_merge(array( |
---|
| 242 | %SET_VALUES_DEFAULT%, |
---|
| 243 | 'new_op' => 'update', |
---|
| 244 | 'submit_buttons' => array( |
---|
[295] | 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'), |
---|
[10] | 249 | ), |
---|
| 250 | ), $frm); |
---|
| 251 | |
---|
| 252 | return $frm; |
---|
[1] | 253 | } |
---|
| 254 | |
---|
| 255 | function deleteRecord($id) |
---|
| 256 | { |
---|
[22] | 257 | global $lock; |
---|
[153] | 258 | global $cache; |
---|
[136] | 259 | $db =& DB::getInstance(); |
---|
[295] | 260 | $app =& App::getInstance(); |
---|
[136] | 261 | |
---|
[1] | 262 | $lock->select('%DB_TBL%', '%PRIMARY_KEY%', $id); |
---|
| 263 | if ($lock->isLocked() && !$lock->isMine()) { |
---|
| 264 | $lock->dieErrorPage(); |
---|
[21] | 265 | } |
---|
[1] | 266 | |
---|
[152] | 267 | // Remove any stale cached list data. |
---|
| 268 | $cache->delete('list'); |
---|
[42] | 269 | |
---|
[21] | 270 | // Get the information for this object. |
---|
[136] | 271 | $qid = $db->query(" |
---|
[154] | 272 | SELECT __///__ |
---|
[21] | 273 | FROM %DB_TBL% |
---|
[136] | 274 | WHERE %PRIMARY_KEY% = '" . $db->escapeString($id) . "' |
---|
[21] | 275 | "); |
---|
| 276 | if (! list($name) = mysql_fetch_row($qid)) { |
---|
[136] | 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(); |
---|
[1] | 280 | } |
---|
[42] | 281 | |
---|
[21] | 282 | // Delete the record. |
---|
[136] | 283 | $db->query("DELETE FROM %DB_TBL% WHERE %PRIMARY_KEY% = '" . $db->escapeString($id) . "'"); |
---|
[42] | 284 | |
---|
[141] | 285 | $app->raiseMsg(sprintf(_("The %ITEM_TITLE% <em>%s</em> has been deleted."), $name), MSG_SUCCESS, __FILE__, __LINE__); |
---|
[21] | 286 | |
---|
| 287 | // Unlock record. |
---|
| 288 | $lock->remove(); |
---|
[1] | 289 | } |
---|
| 290 | |
---|
| 291 | function insertRecord($frm) |
---|
| 292 | { |
---|
| 293 | global $auth; |
---|
[153] | 294 | global $cache; |
---|
[136] | 295 | $db =& DB::getInstance(); |
---|
[295] | 296 | $app =& App::getInstance(); |
---|
[136] | 297 | |
---|
[152] | 298 | // Remove any stale cached list data. |
---|
| 299 | $cache->delete('list'); |
---|
[42] | 300 | |
---|
[1] | 301 | %INSERT% |
---|
[136] | 302 | $last_insert_id = mysql_insert_id($db->getDBH()); |
---|
[1] | 303 | |
---|
| 304 | // Create version. |
---|
[159] | 305 | $version = Version::getInstance($auth); |
---|
[154] | 306 | $version->create('%DB_TBL%', '%PRIMARY_KEY%', $last_insert_id, $frm['__///__']); |
---|
[42] | 307 | |
---|
[154] | 308 | $app->raiseMsg(sprintf(_("The %ITEM_TITLE% <em>%s</em> has been added."), $frm['__///__']), MSG_SUCCESS, __FILE__, __LINE__); |
---|
[42] | 309 | |
---|
[1] | 310 | return $last_insert_id; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | function updateRecord($frm) |
---|
| 314 | { |
---|
[159] | 315 | global $auth; |
---|
| 316 | global $lock; |
---|
[153] | 317 | global $cache; |
---|
[295] | 318 | $db =& DB::getInstance(); |
---|
[136] | 319 | $app =& App::getInstance(); |
---|
| 320 | |
---|
[1] | 321 | $lock->select('%DB_TBL%', '%PRIMARY_KEY%', $frm['%PRIMARY_KEY%']); |
---|
| 322 | if ($lock->isLocked() && !$lock->isMine()) { |
---|
| 323 | $lock->dieErrorPage(); |
---|
[21] | 324 | } |
---|
[1] | 325 | |
---|
[152] | 326 | // Remove any stale cached list data. |
---|
| 327 | $cache->delete('list'); |
---|
[21] | 328 | |
---|
[1] | 329 | %UPDATE% |
---|
[42] | 330 | |
---|
[21] | 331 | // Create version. |
---|
[159] | 332 | $version = Version::getInstance($auth); |
---|
[154] | 333 | $version->create('%DB_TBL%', '%PRIMARY_KEY%', $frm['%PRIMARY_KEY%'], $frm['__///__']); |
---|
[1] | 334 | |
---|
[154] | 335 | $app->raiseMsg(sprintf(_("The %ITEM_TITLE% <em>%s</em> has been updated."), $frm['__///__']), MSG_SUCCESS, __FILE__, __LINE__); |
---|
[21] | 336 | |
---|
| 337 | // Unlock record. |
---|
| 338 | $lock->remove(); |
---|
[1] | 339 | } |
---|
| 340 | |
---|
[152] | 341 | function &getCachedList() |
---|
[1] | 342 | { |
---|
| 343 | global $page; |
---|
| 344 | global $so; |
---|
[153] | 345 | global $tmp_prefs; |
---|
| 346 | global $cache; |
---|
[152] | 347 | $db =& DB::getInstance(); |
---|
[295] | 348 | $app =& App::getInstance(); |
---|
[152] | 349 | |
---|
[1] | 350 | $where_clause = ''; |
---|
[42] | 351 | |
---|
[1] | 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 | } |
---|
[42] | 359 | |
---|
[154] | 360 | if (getFormData('filter___///__', false)) { |
---|
[1] | 361 | // Limit by filter. |
---|
[154] | 362 | $where_clause .= (empty($where_clause) ? 'WHERE' : 'AND') . " __///__ = '" . $db->escapeString(getFormData('filter___///__')) . "'"; |
---|
[1] | 363 | } |
---|
[42] | 364 | |
---|
[1] | 365 | // Count the total number of records so we can do something about the page numbers. |
---|
[136] | 366 | $qid = $db->query(" |
---|
[42] | 367 | SELECT COUNT(*) |
---|
| 368 | FROM %DB_TBL% |
---|
[1] | 369 | $where_clause |
---|
| 370 | "); |
---|
| 371 | list($num_results) = mysql_fetch_row($qid); |
---|
[42] | 372 | |
---|
[1] | 373 | // Set page numbers now we know (needed for next step). |
---|
| 374 | $page->setTotalItems($num_results); |
---|
| 375 | $page->calculate(); |
---|
[42] | 376 | |
---|
[1] | 377 | // Final SQL, with sort and page limiters. |
---|
| 378 | $sql = " |
---|
[42] | 379 | SELECT |
---|
| 380 | %DB_TBL%.*, |
---|
[295] | 381 | a1.username AS added_by_username, |
---|
| 382 | a2.username AS modified_by_username |
---|
[1] | 383 | FROM %DB_TBL% |
---|
[295] | 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) |
---|
[1] | 386 | $where_clause |
---|
| 387 | " . $so->getSortOrderSQL() . " |
---|
| 388 | " . $page->getLimitSQL() . " |
---|
| 389 | "; |
---|
[42] | 390 | |
---|
[152] | 391 | // Use a cash hash to determine if the result-set has changed. |
---|
[1] | 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); |
---|
[153] | 398 | if ($tmp_prefs->get('cache_hash') != $cache_hash) { |
---|
[152] | 399 | $cache->delete('list'); |
---|
[153] | 400 | $tmp_prefs->set('cache_hash', $cache_hash); |
---|
[1] | 401 | } |
---|
[42] | 402 | |
---|
[152] | 403 | // First try to return from the cache. |
---|
| 404 | if ($cache->exists('list')) { |
---|
[295] | 405 | $list = $cache->get('list'); |
---|
| 406 | return $list; |
---|
[152] | 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 | } |
---|
[42] | 414 | |
---|
[152] | 415 | // Save this list into the cache. |
---|
| 416 | if (isset($list) && !empty($list)) { |
---|
| 417 | $cache->set('list', $list); |
---|
[1] | 418 | } |
---|
| 419 | |
---|
| 420 | return $list; |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | function updateRank($ranks) |
---|
[42] | 424 | { |
---|
[153] | 425 | global $cache; |
---|
[136] | 426 | $db =& DB::getInstance(); |
---|
[295] | 427 | $app =& App::getInstance(); |
---|
[136] | 428 | |
---|
[1] | 429 | if (!is_array($ranks)) { |
---|
[136] | 430 | $app->logMsg('Saving rank failed, data posted is not an array: ' . $ranks, LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 431 | return false; |
---|
| 432 | } |
---|
| 433 | |
---|
[152] | 434 | // Remove any stale cached list data. |
---|
| 435 | $cache->delete('list'); |
---|
[42] | 436 | |
---|
[1] | 437 | // Count the ranks with invalid numbers |
---|
| 438 | $unspecified_counter = 0; |
---|
[42] | 439 | |
---|
[1] | 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) { |
---|
[20] | 443 | // Unspecified entries receive a sort order of 10000. |
---|
| 444 | $new_rank = 10000; |
---|
[42] | 445 | $unspecified_counter++; |
---|
[1] | 446 | } |
---|
[136] | 447 | $db->query(" |
---|
[1] | 448 | UPDATE %DB_TBL% SET |
---|
[136] | 449 | rank = '" . $db->escapeString($new_rank) . "' |
---|
| 450 | WHERE %PRIMARY_KEY% = '" . $db->escapeString($id) . "' |
---|
[1] | 451 | "); |
---|
| 452 | } |
---|
[42] | 453 | |
---|
[136] | 454 | $app->raiseMsg(_("Records have been reordered with the new rank."), MSG_SUCCESS, __FILE__, __LINE__); |
---|
[1] | 455 | if ($unspecified_counter > 0) { |
---|
[136] | 456 | $app->raiseMsg(sprintf(_("%s items with unspecified ranks were automatically assigned a rank of 10000."), $unspecified_counter), MSG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 457 | } |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | ?> |
---|