feature 2809: sort albums by date

git-svn-id: http://piwigo.org/svn/trunk@28934 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
plegall 2014-07-03 13:18:12 +00:00
commit f2704dcb62
4 changed files with 121 additions and 7 deletions

View file

@ -40,6 +40,15 @@ if (!empty($_POST) or isset($_GET['delete']))
check_pwg_token();
}
$sort_orders = array(
'name ASC' => l10n('Album name, A → Z'),
'name DESC' => l10n('Album name, Z → A'),
'date_creation DESC' => l10n('Date created, new → old'),
'date_creation ASC' => l10n('Date created, old → new'),
'date_available DESC' => l10n('Date posted, new → old'),
'date_available ASC' => l10n('Date posted, old → new'),
);
// +-----------------------------------------------------------------------+
// | functions |
// +-----------------------------------------------------------------------+
@ -86,6 +95,77 @@ function save_categories_order($categories)
update_global_rank();
}
function get_categories_ref_date($ids, $field='date_available', $minmax='max')
{
// we need to work on the whole tree under each category, even if we don't
// want to sort sub categories
$category_ids = get_subcat_ids($ids);
// search for the reference date of each album
$query = '
SELECT
category_id,
'.$minmax.'('.$field.') as ref_date
FROM '.IMAGE_CATEGORY_TABLE.'
JOIN '.IMAGES_TABLE.' ON image_id = id
WHERE category_id IN ('.implode(',', $category_ids).')
GROUP BY category_id
;';
$ref_dates = query2array($query, 'category_id', 'ref_date');
// the iterate on all albums (having a ref_date or not) to find the
// reference_date, with a search on sub-albums
$query = '
SELECT
id,
uppercats
FROM '.CATEGORIES_TABLE.'
WHERE id IN ('.implode(',', $category_ids).')
;';
$uppercats_of = query2array($query, 'id', 'uppercats');
foreach (array_keys($uppercats_of) as $cat_id)
{
// find the subcats
$subcat_ids = array();
foreach ($uppercats_of as $id => $uppercats)
{
if (preg_match('/(^|,)'.$cat_id.'(,|$)/', $uppercats))
{
$subcat_ids[] = $id;
}
}
$to_compare = array();
foreach ($subcat_ids as $id)
{
if (isset($ref_dates[$id]))
{
$to_compare[] = $ref_dates[$id];
}
}
if (count($to_compare) > 0)
{
$ref_dates[$cat_id] = 'max' == $minmax ? max($to_compare) : min($to_compare);
}
else
{
$ref_dates[$cat_id] = null;
}
}
// only return the list of $ids, not the sub-categories
$return = array();
foreach ($ids as $id)
{
$return[$id] = $ref_dates[$id];
}
return $return;
}
// +-----------------------------------------------------------------------+
// | initialization |
// +-----------------------------------------------------------------------+
@ -152,6 +232,11 @@ elseif (isset($_POST['submitManualOrder']))
}
elseif (isset($_POST['submitAutoOrder']))
{
if (!isset($sort_orders[ $_POST['order_by'] ]))
{
die('Invalid sort order');
}
$query = '
SELECT id
FROM '.CATEGORIES_TABLE.'
@ -166,9 +251,22 @@ SELECT id
}
$categories = array();
$names = array();
$id_uppercats = array();
$sort = array();
list($order_by_field, $order_by_asc) = explode(' ', $_POST['order_by']);
$order_by_date = false;
if (strpos($order_by_field, 'date_') === 0)
{
$order_by_date = true;
$ref_dates = get_categories_ref_date(
$category_ids,
$order_by_field,
'ASC' == $order_by_asc ? 'min' : 'max'
);
}
$query = '
SELECT id, name, id_uppercat
FROM '.CATEGORIES_TABLE.'
@ -177,19 +275,28 @@ SELECT id, name, id_uppercat
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
if ($order_by_date)
{
$sort[] = $ref_dates[ $row['id'] ];
}
else
{
$sort[] = $row['name'];
}
$categories[] = array(
'id' => $row['id'],
'id_uppercat' => $row['id_uppercat'],
);
$names[] = $row['name'];
}
array_multisort(
$names,
$sort,
SORT_REGULAR,
'asc' == $_POST['ascdesc'] ? SORT_ASC : SORT_DESC,
'ASC' == $order_by_asc ? SORT_ASC : SORT_DESC,
$categories
);
save_categories_order($categories);
$page['infos'][] = l10n('Albums automatically sorted');
@ -223,6 +330,8 @@ $template->assign(array(
'CATEGORIES_NAV'=>$navigation,
'F_ACTION'=>$form_action,
'PWG_TOKEN' => get_pwg_token(),
'sort_orders' => $sort_orders,
'sort_order_checked' => array_shift(array_keys($sort_orders)),
));
// +-----------------------------------------------------------------------+

View file

@ -85,8 +85,9 @@ jQuery(document).ready(function(){
<input type="hidden" name="pwg_token" value="{$PWG_TOKEN}">
<p><strong>{'Sort order'|@translate}</strong>
<br><label><input type="radio" value="asc" name="ascdesc" checked="checked">{'ascending'|@translate}</label>
<br><label><input type="radio" value="desc" name="ascdesc">{'descending'|@translate}</label>
{foreach from=$sort_orders key=sort_code item=sort_label}
<br><label><input type="radio" value="{$sort_code}" name="order_by" {if $sort_code eq $sort_order_checked}checked="checked"{/if}> {$sort_label}</label>
{/foreach}
</p>
<p>