From 5b33fbace5c0f38a86d2519b388fbff25c28b3bc Mon Sep 17 00:00:00 2001
From: Nicolas
Date: Wed, 18 Sep 2013 22:48:53 +0200
Subject: [PATCH] Improve and simplify categories page
---
admin/categories.php | 99 +++++++++++++++++++++++------------------
admin/js/_categories.js | 13 +-----
2 files changed, 58 insertions(+), 54 deletions(-)
diff --git a/admin/categories.php b/admin/categories.php
index fb992f68b..12e0cc0b1 100644
--- a/admin/categories.php
+++ b/admin/categories.php
@@ -15,10 +15,34 @@ require dirname(__FILE__).'/../inc/admin/prepend.php';
dcPage::check('categories');
# Remove a categories
-if (!empty($_POST['categories']) && !empty($_POST['delete'])) {
+if (!empty($_POST['delete'])) {
+ $keys = array_keys($_POST['delete']);
+ $cat_id = (int) $keys[0];
+
+ # Check if category to delete exists
+ $c = $core->blog->getCategory((integer) $cat_id);
+ if ($c->isEmpty()) {
+ http::redirect('categories.php?unknown=1');
+ }
+ unset($c);
+
+ try {
+ # Delete category
+ $core->blog->delCategory($cat_id);
+ http::redirect('categories.php?del=1');
+ } catch (Exception $e) {
+ $core->error->add($e->getMessage());
+ }
+}
+
+# move post into a category
+if (!empty($_POST['mov']) && !empty($_POST['mov_cat'])) {
try {
# Check if category where to move posts exists
- $mov_cat = (int) $_POST['mov_cat'];
+ $keys = array_keys($_POST['mov']);
+ $cat_id = (int) $keys[0];
+ $mov_cat = (int) $_POST['mov_cat'][$cat_id];
+
$mov_cat = $mov_cat ? $mov_cat : null;
if ($mov_cat !== null) {
$c = $core->blog->getCategory($mov_cat);
@@ -26,29 +50,12 @@ if (!empty($_POST['categories']) && !empty($_POST['delete'])) {
throw new Exception(__('Category where to move posts does not exist'));
}
unset($c);
-
- if (in_array($mov_cat, $_POST['categories'])) {
- throw new Exception(__('The entries cannot be moved to the category you choose to delete.'));
- }
}
-
- foreach ($_POST['categories'] as $cat_id) {
- # Check if category to delete exists
- $c = $core->blog->getCategory((integer) $cat_id);
- if ($c->isEmpty()) {
- continue;
- }
- unset($c);
-
- # Move posts
- if ($mov_cat != $cat_id) {
- $core->blog->changePostsCategory($cat_id,$mov_cat);
- }
-
- # Delete category
- $core->blog->delCategory($cat_id);
+ # Move posts
+ if ($mov_cat != $cat_id) {
+ $core->blog->changePostsCategory($cat_id,$mov_cat);
}
- http::redirect('categories.php?del='.count($_POST['categories']));
+ http::redirect('categories.php?move=1');
} catch (Exception $e) {
$core->error->add($e->getMessage());
}
@@ -104,15 +111,18 @@ dcPage::open(__('Categories'),$starting_script,
);
if (!empty($_GET['del'])) {
- dcPage::success(__('The category has been successfully removed.',
- 'The categories have been successfully removed.',
- (int) $_GET['del']
- )
- );
+ dcPage::success(__('The category has been successfully removed.'));
}
if (!empty($_GET['reord'])) {
dcPage::success(__('Categories have been successfully reordered.'));
}
+if (!empty($_GET['unknown'])) {
+ dcPage::success(__('This category does not exist.'));
+}
+if (!empty($_GET['move'])) {
+ dcPage::success(__('Entries have been successfully moved to the category you choose.'));
+}
+
$categories_combo = dcAdminCombos::getCategoriesCombo($rs);
echo
@@ -146,13 +156,28 @@ else
}
echo
- ''.
- form::checkbox(array('categories[]','cat-'.$rs->cat_id),$rs->cat_id,null,$rs->nb_total>0?'notempty':'').
+ '
'.
''.
' ('.
sprintf(($rs->nb_post > 1 ? __('%d entries') : __('%d entry') ),$rs->nb_post).''.
', '.__('total:').' '.$rs->nb_total.') '.
- ''.__('URL:').' '.html::escapeHTML($rs->cat_url).'
';
+ ''.__('URL:').' '.html::escapeHTML($rs->cat_url).'
';
+
+ if ($rs->nb_total>0) {
+ // remove current category
+ echo
+ form::combo('mov_cat['.$rs->cat_id.']',array_filter($categories_combo, function($cat) use ($rs) {return ($cat->value!=$rs->cat_id);}),'','').
+ '';
+
+ $attr_disabled = ' disabled="disabled"';
+ $input_class = 'disabled ';
+ } else {
+ $attr_disabled = '';
+ $input_class = '';
+ }
+ echo
+ ''.
+ '
';
$level = $rs->level;
}
@@ -163,18 +188,6 @@ else
echo
'';
- echo
- ''.
- '
'.
- '
'.
- ' '.
- form::combo('mov_cat',$categories_combo,'','').
- '
'.
- '
'.
- ''.
- '
'.
- '
';
-
echo ''.__('Categories order').'
';
if ($core->auth->check('categories',$core->blog->id) && $rs->count()>1) {
diff --git a/admin/js/_categories.js b/admin/js/_categories.js
index 59c35bbaf..19ec06dbd 100644
--- a/admin/js/_categories.js
+++ b/admin/js/_categories.js
@@ -13,17 +13,8 @@ $(function() {
});
}
- $('.checkboxes-helpers').each(function() {
- dotclear.checkboxesHelpers(this);
- });
-
- $('input[name="delete"]').click(function() {
- var nb_ckecked = $('input[name="categories[]"]:checked').length;
- if (nb_ckecked==0) {
- return false;
- }
-
- return window.confirm(dotclear.msg.confirm_delete_categories.replace('%s',nb_ckecked));
+ $('input[name^="delete"]').click(function() {
+ return window.confirm(dotclear.msg.confirm_delete_category.replace('%s',$(this).parent().find('label a').text()));
});
$('input[name="reset"]').click(function() {