Changeset 2306 for trunk/admin/include
- Timestamp:
- Apr 21, 2008, 12:20:20 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/admin/include/functions.php
r2304 r2306 666 666 667 667 /** 668 * updates the global_rank of categories under the given id_uppercat 669 * 670 * @param int id_uppercat 668 * order categories (update categories.rank and global_rank database fields) 669 * so that rank field are consecutive integers starting at 1 for each child 671 670 * @return void 672 671 */ 673 function update_global_rank( $id_uppercat = 'all')674 { 675 $query = ' 676 SELECT id, rank672 function update_global_rank() 673 { 674 $query = ' 675 SELECT id, if(id_uppercat is null,\'\',id_uppercat) AS id_uppercat, uppercats, rank, global_rank 677 676 FROM '.CATEGORIES_TABLE.' 678 ;'; 679 $result = pwg_query($query); 680 $ranks_array = array(); 681 while ($row = mysql_fetch_array($result)) 682 { 683 $ranks_array[$row['id']] = $row['rank']; 684 } 685 686 // which categories to update ? 687 $uppercats_array = array(); 688 689 $query = ' 690 SELECT id,uppercats 691 FROM '.CATEGORIES_TABLE; 692 if (is_numeric($id_uppercat)) 693 { 694 $query.= ' 695 WHERE uppercats REGEXP \'(^|,)'.$id_uppercat.'(,|$)\' 696 AND id != '.$id_uppercat.' 697 '; 698 } 699 $query.= ' 700 ;'; 677 ORDER BY id_uppercat,rank,name 678 ;'; 679 680 $cat_map = array(); 681 682 $current_rank = 0; 683 $current_uppercat = ''; 684 701 685 $result = pwg_query($query); 702 686 while ($row = mysql_fetch_array($result)) 703 687 { 704 $uppercats_array[$row['id']] = $row['uppercats']; 688 if ($row['id_uppercat'] != $current_uppercat) 689 { 690 $current_rank = 0; 691 $current_uppercat = $row['id_uppercat']; 692 } 693 ++$current_rank; 694 $cat = 695 array( 696 'rank' => $current_rank, 697 'rank_changed' =>$current_rank!=$row['rank'], 698 'global_rank' => $row['global_rank'], 699 'uppercats' => $row['uppercats'], 700 ); 701 $cat_map[ $row['id'] ] = $cat; 705 702 } 706 703 707 704 $datas = array(); 708 foreach ($uppercats_array as $id => $uppercats) 709 { 710 array_push( 711 $datas, 712 array( 713 'id' => $id, 714 'global_rank' => preg_replace( 705 706 foreach( $cat_map as $id=>$cat ) 707 { 708 $new_global_rank = preg_replace( 715 709 '/(\d+)/e', 716 "\$ranks_array['$1']", 717 str_replace(',', '.', $uppercats) 718 ), 719 ) 720 ); 710 "\$cat_map['$1']['rank']", 711 str_replace(',', '.', $cat['uppercats'] ) 712 ); 713 if ( $cat['rank_changed'] 714 or $new_global_rank!=$cat['global_rank'] 715 ) 716 { 717 $datas[] = array( 718 'id' => $id, 719 'rank' => $cat['rank'], 720 'global_rank' => $new_global_rank, 721 ); 722 } 721 723 } 722 724 … … 725 727 array( 726 728 'primary' => array('id'), 727 'update' => array(' global_rank')729 'update' => array('rank', 'global_rank') 728 730 ), 729 731 $datas 730 732 ); 733 return count($datas); 731 734 } 732 735 … … 874 877 $datas 875 878 ); 876 }877 878 /**879 * order categories (update categories.rank and global_rank database fields)880 *881 * the purpose of this function is to give a rank for all categories882 * (insides its sub-category), even the newer that have none at te883 * beginning. For this, ordering function selects all categories ordered by884 * rank ASC then name ASC for each uppercat.885 *886 * @returns void887 */888 function ordering()889 {890 $current_rank = 0;891 $current_uppercat = '';892 893 $query = '894 SELECT id, if(id_uppercat is null,\'\',id_uppercat) AS id_uppercat895 FROM '.CATEGORIES_TABLE.'896 ORDER BY id_uppercat,rank,name897 ;';898 $result = pwg_query($query);899 $datas = array();900 while ($row = mysql_fetch_array($result))901 {902 if ($row['id_uppercat'] != $current_uppercat)903 {904 $current_rank = 0;905 $current_uppercat = $row['id_uppercat'];906 }907 $data = array('id' => $row['id'], 'rank' => ++$current_rank);908 array_push($datas, $data);909 }910 911 $fields = array('primary' => array('id'), 'update' => array('rank'));912 mass_updates(CATEGORIES_TABLE, $fields, $datas);913 879 } 914 880 … … 1353 1319 1354 1320 update_uppercats(); 1355 ordering();1356 1321 update_global_rank(); 1357 1322
Note: See TracChangeset
for help on using the changeset viewer.