aboutsummaryrefslogtreecommitdiffstats
path: root/admin/cat_list.php
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 /admin/cat_list.php
parentc14bedcbd03054c447159bdfec33c0c57d1f4ba5 (diff)
feature 2809: sort albums by date
git-svn-id: http://piwigo.org/svn/trunk@28934 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to 'admin/cat_list.php')
-rw-r--r--admin/cat_list.php119
1 files changed, 114 insertions, 5 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)),
));
// +-----------------------------------------------------------------------+