Skip to content

Commit

Permalink
feature 2809: sort albums by date
Browse files Browse the repository at this point in the history
git-svn-id: http://piwigo.org/svn/trunk@28934 68402e56-0260-453c-a942-63ccdbb3a9ee
  • Loading branch information
plegall committed Jul 3, 2014
1 parent c14bedc commit f2704dc
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 7 deletions.
119 changes: 114 additions & 5 deletions admin/cat_list.php
Expand Up @@ -40,6 +40,15 @@
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 |
// +-----------------------------------------------------------------------+
Expand Down Expand Up @@ -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 |
// +-----------------------------------------------------------------------+
Expand Down Expand Up @@ -152,6 +232,11 @@ function save_categories_order($categories)
}
elseif (isset($_POST['submitAutoOrder']))
{
if (!isset($sort_orders[ $_POST['order_by'] ]))
{
die('Invalid sort order');
}

$query = '
SELECT id
FROM '.CATEGORIES_TABLE.'
Expand All @@ -166,9 +251,22 @@ function save_categories_order($categories)
}

$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.'
Expand All @@ -177,19 +275,28 @@ function save_categories_order($categories)
$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');
Expand Down Expand Up @@ -223,6 +330,8 @@ function save_categories_order($categories)
'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)),
));

// +-----------------------------------------------------------------------+
Expand Down
5 changes: 3 additions & 2 deletions admin/themes/default/template/cat_list.tpl
Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions language/en_UK/common.lang.php
Expand Up @@ -422,4 +422,6 @@
$lang['Apply on properties'] = 'Apply on properties';
$lang['Photo title'] = 'Photo title';
$lang['Photo description'] = 'Photo description';
$lang['Album name, A &rarr; Z'] = 'Album name, A &rarr; Z';
$lang['Album name, Z &rarr; A'] = 'Album name, Z &rarr; A';
?>
2 changes: 2 additions & 0 deletions language/fr_FR/common.lang.php
Expand Up @@ -422,3 +422,5 @@
$lang['Apply on properties'] = 'Appliquer sur les propriétés';
$lang['Photo title'] = 'Titre de la photo';
$lang['Photo description'] = 'Description de la photo';
$lang['Album name, A &rarr; Z'] = 'Nom de l\'album, A &rarr; Z';
$lang['Album name, Z &rarr; A'] = 'Nom de l\'album, Z &rarr; A';

0 comments on commit f2704dc

Please sign in to comment.