Changeset 2306 for trunk/admin/include


Ignore:
Timestamp:
Apr 21, 2008, 12:20:20 AM (17 years ago)
Author:
rvelices
Message:
  • merged function ordering() with update_global_rank() and also optimized the queries
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/functions.php

    r2304 r2306  
    666666
    667667/**
    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
    671670 * @return void
    672671 */
    673 function update_global_rank($id_uppercat = 'all')
    674 {
    675   $query = '
    676 SELECT id,rank
     672function update_global_rank()
     673{
     674  $query = '
     675SELECT id, if(id_uppercat is null,\'\',id_uppercat) AS id_uppercat, uppercats, rank, global_rank
    677676  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
    701685  $result = pwg_query($query);
    702686  while ($row = mysql_fetch_array($result))
    703687  {
    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;
    705702  }
    706703
    707704  $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(
    715709          '/(\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    }
    721723  }
    722724
     
    725727    array(
    726728      'primary' => array('id'),
    727       'update'  => array('global_rank')
     729      'update'  => array('rank', 'global_rank')
    728730      ),
    729731    $datas
    730732    );
     733  return count($datas);
    731734}
    732735
     
    874877    $datas
    875878    );
    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 categories
    882  * (insides its sub-category), even the newer that have none at te
    883  * beginning. For this, ordering function selects all categories ordered by
    884  * rank ASC then name ASC for each uppercat.
    885  *
    886  * @returns void
    887  */
    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_uppercat
    895   FROM '.CATEGORIES_TABLE.'
    896   ORDER BY id_uppercat,rank,name
    897 ;';
    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);
    913879}
    914880
     
    13531319
    13541320  update_uppercats();
    1355   ordering();
    13561321  update_global_rank();
    13571322
Note: See TracChangeset for help on using the changeset viewer.