Ignore:
Timestamp:
Jul 23, 2010, 11:33:56 PM (14 years ago)
Author:
plg
Message:

feature 408: ability to automatically sort sub-categories. The number of SQL
queries is constant, no matter the deepth of your tree.

+ refactor to avoid duplicate code (ascending/desceding should be only a
single parameter in a function, not 20 lines of duplicated code)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/cat_list.php

    r6363 r6698  
    5656function save_categories_order($categories)
    5757{
     58  $current_rank_for_id_uppercat = array();
    5859  $current_rank = 0;
     60 
    5961  $datas = array();
    60   foreach ($categories as $id)
    61   {
    62     array_push($datas, array('id' => $id, 'rank' => ++$current_rank));
     62  foreach ($categories as $category)
     63  {
     64    if (is_array($category))
     65    {
     66      $id = $category['id'];
     67      $id_uppercat = $category['id_uppercat'];
     68
     69      if (!isset($current_rank_for_id_uppercat[$id_uppercat]))
     70      {
     71        $current_rank_for_id_uppercat[$id_uppercat] = 0;
     72      }
     73      $current_rank = ++$current_rank_for_id_uppercat[$id_uppercat];
     74    }
     75    else
     76    {
     77      $id = $category;
     78      $current_rank++;
     79    }
     80   
     81    array_push($datas, array('id' => $id, 'rank' => $current_rank));
    6382  }
    6483  $fields = array('primary' => array('id'), 'update' => array('rank'));
     
    112131else if (isset($_POST['submitOrder']))
    113132{
    114   asort($_POST['catOrd'], SORT_NUMERIC);
    115   save_categories_order(array_keys($_POST['catOrd']));
    116 
    117   array_push(
    118     $page['infos'],
    119     l10n('Categories manual order was saved')
    120     );
    121 }
    122 // sort categories alpha-numerically
    123 else if (isset($_POST['submitOrderAlphaNum']))
    124 {
    125   $query = '
    126 SELECT id, name
     133  if ('manual' == $_POST['order_type'])
     134  {
     135    asort($_POST['catOrd'], SORT_NUMERIC);
     136    save_categories_order(array_keys($_POST['catOrd']));
     137
     138    array_push(
     139      $page['infos'],
     140      l10n('Categories manual order was saved')
     141      );
     142  }
     143  else
     144  {
     145    $query = '
     146SELECT id
    127147  FROM '.CATEGORIES_TABLE.'
    128148  WHERE id_uppercat '.
    129149    (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).'
    130150;';
    131   $result = pwg_query($query);
    132   while ($row = pwg_db_fetch_assoc($result))
    133   {
    134     $categories[ $row['id'] ] = strtolower($row['name']);
    135   }
    136 
    137   asort($categories, SORT_REGULAR);
    138   save_categories_order(array_keys($categories));
    139 
    140   array_push(
    141     $page['infos'],
    142     l10n('Categories ordered alphanumerically')
    143     );
    144 }
    145 // sort categories alpha-numerically reverse
    146 else if (isset($_POST['submitOrderAlphaNumReverse']))
    147 {
    148   $query = '
    149 SELECT id, name
     151    $category_ids = array_from_query($query, 'id');
     152
     153    if (isset($_POST['recursive']))
     154    {
     155      $category_ids = get_subcat_ids($category_ids);
     156    }
     157
     158    $categories = array();
     159    $names = array();
     160    $id_uppercats = array();
     161 
     162    $query = '
     163SELECT id, name, id_uppercat
    150164  FROM '.CATEGORIES_TABLE.'
    151   WHERE id_uppercat '.
    152     (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).'
     165  WHERE id IN ('.implode(',', $category_ids).')
    153166;';
    154   $result = pwg_query($query);
    155   while ($row = pwg_db_fetch_assoc($result))
    156   {
    157     $categories[ $row['id'] ] = strtolower($row['name']);
    158   }
    159 
    160   arsort($categories, SORT_REGULAR);
    161   save_categories_order(array_keys($categories));
    162 
    163   array_push(
    164     $page['infos'],
    165     l10n('Categories ordered alphanumerically reverse')
    166     );
     167    $result = pwg_query($query);
     168    while ($row = pwg_db_fetch_assoc($result))
     169    {
     170      array_push(
     171        $categories,
     172        array(
     173          'id' => $row['id'],
     174          'id_uppercat' => $row['id_uppercat'],
     175          )
     176        );
     177      array_push(
     178        $names,
     179        $row['name']
     180        );
     181    }
     182
     183    array_multisort(
     184      $names,
     185      SORT_REGULAR,
     186      'asc' == $_POST['ascdesc'] ? SORT_ASC : SORT_DESC,
     187      $categories
     188      );
     189    save_categories_order($categories);
     190
     191    array_push(
     192      $page['infos'],
     193      l10n('Categories automatically sorted')
     194      );
     195  }
    167196}
    168197
Note: See TracChangeset for help on using the changeset viewer.