Changeset 28934


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

feature 2809: sort albums by date

Location:
trunk
Files:
4 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
  • trunk/admin/themes/default/template/cat_list.tpl

    r26399 r28934  
    8686   
    8787    <p><strong>{'Sort order'|@translate}</strong>
    88       <br><label><input type="radio" value="asc" name="ascdesc" checked="checked">{'ascending'|@translate}</label>
    89       <br><label><input type="radio" value="desc" name="ascdesc">{'descending'|@translate}</label>
     88  {foreach from=$sort_orders key=sort_code item=sort_label}
     89      <br><label><input type="radio" value="{$sort_code}" name="order_by" {if $sort_code eq $sort_order_checked}checked="checked"{/if}> {$sort_label}</label>
     90  {/foreach}
    9091    </p>
    9192 
  • trunk/language/en_UK/common.lang.php

    r28709 r28934  
    423423$lang['Photo title'] = 'Photo title';
    424424$lang['Photo description'] = 'Photo description';
     425$lang['Album name, A &rarr; Z'] = 'Album name, A &rarr; Z';
     426$lang['Album name, Z &rarr; A'] = 'Album name, Z &rarr; A';
    425427?>
  • trunk/language/fr_FR/common.lang.php

    r28709 r28934  
    423423$lang['Photo title'] = 'Titre de la photo';
    424424$lang['Photo description'] = 'Description de la photo';
     425$lang['Album name, A &rarr; Z'] = 'Nom de l\'album, A &rarr; Z';
     426$lang['Album name, Z &rarr; A'] = 'Nom de l\'album, Z &rarr; A';
Note: See TracChangeset for help on using the changeset viewer.