Ignore:
Timestamp:
Jul 3, 2014, 3:18:12 PM (10 years ago)
Author:
plg
Message:

feature 2809: sort albums by date

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/cat_list.php

    r28587 r28934  
    4141}
    4242
     43$sort_orders = array(
     44  'name ASC' => l10n('Album name, A → Z'),
     45  'name DESC' => l10n('Album name, Z → A'),
     46  'date_creation DESC' => l10n('Date created, new → old'),
     47  'date_creation ASC' => l10n('Date created, old → new'),
     48  'date_available DESC' => l10n('Date posted, new → old'),
     49  'date_available ASC' => l10n('Date posted, old → new'),
     50  );
     51
    4352// +-----------------------------------------------------------------------+
    4453// |                               functions                               |
     
    8796}
    8897
     98function get_categories_ref_date($ids, $field='date_available', $minmax='max')
     99{
     100  // we need to work on the whole tree under each category, even if we don't
     101  // want to sort sub categories
     102  $category_ids = get_subcat_ids($ids);
     103 
     104  // search for the reference date of each album
     105  $query = '
     106SELECT
     107    category_id,
     108    '.$minmax.'('.$field.') as ref_date
     109  FROM '.IMAGE_CATEGORY_TABLE.'
     110    JOIN '.IMAGES_TABLE.' ON image_id = id
     111  WHERE category_id IN ('.implode(',', $category_ids).')
     112  GROUP BY category_id
     113;';
     114  $ref_dates = query2array($query, 'category_id', 'ref_date');
     115
     116  // the iterate on all albums (having a ref_date or not) to find the
     117  // reference_date, with a search on sub-albums
     118  $query = '
     119SELECT
     120    id,
     121    uppercats
     122  FROM '.CATEGORIES_TABLE.'
     123  WHERE id IN ('.implode(',', $category_ids).')
     124;';
     125  $uppercats_of = query2array($query, 'id', 'uppercats');
     126
     127  foreach (array_keys($uppercats_of) as $cat_id)
     128  {
     129    // find the subcats
     130    $subcat_ids = array();
     131   
     132    foreach ($uppercats_of as $id => $uppercats)
     133    {
     134      if (preg_match('/(^|,)'.$cat_id.'(,|$)/', $uppercats))
     135      {
     136        $subcat_ids[] = $id;
     137      }
     138    }
     139
     140    $to_compare = array();
     141    foreach ($subcat_ids as $id)
     142    {
     143      if (isset($ref_dates[$id]))
     144      {
     145        $to_compare[] = $ref_dates[$id];
     146      }
     147    }
     148
     149    if (count($to_compare) > 0)
     150    {
     151      $ref_dates[$cat_id] = 'max' == $minmax ? max($to_compare) : min($to_compare);
     152    }
     153    else
     154    {
     155      $ref_dates[$cat_id] = null;
     156    }
     157  }
     158
     159  // only return the list of $ids, not the sub-categories
     160  $return = array();
     161  foreach ($ids as $id)
     162  {
     163    $return[$id] = $ref_dates[$id];
     164  }
     165 
     166  return $return;
     167}
     168
    89169// +-----------------------------------------------------------------------+
    90170// |                            initialization                             |
     
    153233elseif (isset($_POST['submitAutoOrder']))
    154234{
     235  if (!isset($sort_orders[ $_POST['order_by'] ]))
     236  {
     237    die('Invalid sort order');
     238  }
     239
    155240  $query = '
    156241SELECT id
     
    167252 
    168253  $categories = array();
    169   $names = array();
    170   $id_uppercats = array();
    171  
     254  $sort = array();
     255
     256  list($order_by_field, $order_by_asc) = explode(' ', $_POST['order_by']);
     257 
     258  $order_by_date = false;
     259  if (strpos($order_by_field, 'date_') === 0)
     260  {
     261    $order_by_date = true;
     262   
     263    $ref_dates = get_categories_ref_date(
     264      $category_ids,
     265      $order_by_field,
     266      'ASC' == $order_by_asc ? 'min' : 'max'
     267      );
     268  }
     269
    172270  $query = '
    173271SELECT id, name, id_uppercat
     
    178276  while ($row = pwg_db_fetch_assoc($result))
    179277  {
     278    if ($order_by_date)
     279    {
     280      $sort[] = $ref_dates[ $row['id'] ];
     281    }
     282    else
     283    {
     284      $sort[] = $row['name'];
     285    }
     286   
    180287    $categories[] = array(
    181288      'id' => $row['id'],
    182289      'id_uppercat' => $row['id_uppercat'],
    183290      );
    184     $names[] = $row['name'];
    185291  }
    186292
    187293  array_multisort(
    188     $names,
     294    $sort,
    189295    SORT_REGULAR,
    190     'asc' == $_POST['ascdesc'] ? SORT_ASC : SORT_DESC,
     296    'ASC' == $order_by_asc ? SORT_ASC : SORT_DESC,
    191297    $categories
    192298    );
     299 
    193300  save_categories_order($categories);
    194301
     
    224331  'F_ACTION'=>$form_action,
    225332  'PWG_TOKEN' => get_pwg_token(),
     333  'sort_orders' => $sort_orders,
     334  'sort_order_checked' => array_shift(array_keys($sort_orders)),
    226335 ));
    227336
Note: See TracChangeset for help on using the changeset viewer.