aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorplegall <plg@piwigo.org>2014-07-03 13:18:12 +0000
committerplegall <plg@piwigo.org>2014-07-03 13:18:12 +0000
commitf2704dcb6207a8f89fe5b1f6f2ffe6a3f9358cc6 (patch)
tree2a1f8908527ed60fa5227fd8bcb365e689503996
parentc14bedcbd03054c447159bdfec33c0c57d1f4ba5 (diff)
feature 2809: sort albums by date
git-svn-id: http://piwigo.org/svn/trunk@28934 68402e56-0260-453c-a942-63ccdbb3a9ee
-rw-r--r--admin/cat_list.php119
-rw-r--r--admin/themes/default/template/cat_list.tpl5
-rw-r--r--language/en_UK/common.lang.php2
-rw-r--r--language/fr_FR/common.lang.php2
4 files changed, 121 insertions, 7 deletions
diff --git a/admin/cat_list.php b/admin/cat_list.php
index 631a69354..59141d583 100644
--- a/admin/cat_list.php
+++ b/admin/cat_list.php
@@ -40,6 +40,15 @@ if (!empty($_POST) or isset($_GET['delete']))
check_pwg_token();
}
+$sort_orders = array(
+ 'name ASC' => l10n('Album name, A &rarr; Z'),
+ 'name DESC' => l10n('Album name, Z &rarr; A'),
+ 'date_creation DESC' => l10n('Date created, new &rarr; old'),
+ 'date_creation ASC' => l10n('Date created, old &rarr; new'),
+ 'date_available DESC' => l10n('Date posted, new &rarr; old'),
+ 'date_available ASC' => l10n('Date posted, old &rarr; 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)),
));
// +-----------------------------------------------------------------------+
diff --git a/admin/themes/default/template/cat_list.tpl b/admin/themes/default/template/cat_list.tpl
index 5fe54b9eb..a8f976cf7 100644
--- a/admin/themes/default/template/cat_list.tpl
+++ b/admin/themes/default/template/cat_list.tpl
@@ -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>
diff --git a/language/en_UK/common.lang.php b/language/en_UK/common.lang.php
index 024eeb7ec..0c1fc0dcd 100644
--- a/language/en_UK/common.lang.php
+++ b/language/en_UK/common.lang.php
@@ -422,4 +422,6 @@ $lang['No results for'] = 'No results for';
$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';
?> \ No newline at end of file
diff --git a/language/fr_FR/common.lang.php b/language/fr_FR/common.lang.php
index 4f2b48e35..24545377a 100644
--- a/language/fr_FR/common.lang.php
+++ b/language/fr_FR/common.lang.php
@@ -422,3 +422,5 @@ $lang['No results for'] = 'Aucun résultat pour';
$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';