From efa3eb27678aa88fa3f91ae01ee8639b28a8b18d Mon Sep 17 00:00:00 2001 From: franck Date: Thu, 8 Aug 2013 10:12:14 +0200 Subject: [PATCH] Category visibility toggle implementation --- core/lib/Thelia/Action/Category.php | 27 +++++- .../Admin/Controller/BaseAdminController.php | 10 ++- .../Admin/Controller/CategoryController.php | 82 ++++++++++++------- core/lib/Thelia/Core/Event/TheliaEvents.php | 5 ++ templates/admin/default/categories.html | 19 ++++- .../admin/default/includes/header.inc.html | 2 +- 6 files changed, 111 insertions(+), 34 deletions(-) diff --git a/core/lib/Thelia/Action/Category.php b/core/lib/Thelia/Action/Category.php index f77d6c964..c4f195dca 100644 --- a/core/lib/Thelia/Action/Category.php +++ b/core/lib/Thelia/Action/Category.php @@ -235,6 +235,29 @@ class Category implements EventSubscriberInterface $event->stopPropagation(); } + /** + * Toggle category visibility. No form used here + * + * @param ActionEvent $event + */ + public function toggleVisibility(ActionEvent $event) + { + $request = $event->getRequest(); + + $category = CategoryQuery::create()->findPk($request->get('id', 0)); + + if ($category !== null) { + + $category->setVisible($category->getVisible() ? false : true); + + $category->save(); + + $categoryEvent = new CategoryEvent($category); + + $event->getDispatcher()->dispatch(TheliaEvents::AFTER_CHANGECATEGORY, $categoryEvent); + } + } + /** * Returns an array of event names this subscriber listens to. * @@ -261,6 +284,8 @@ class Category implements EventSubscriberInterface "action.createCategory" => array("create", 128), "action.modifyCategory" => array("modify", 128), "action.deleteCategory" => array("delete", 128), - ); + + "action.toggleCategoryVisibility" => array("toggleVisibility", 128), + ); } } diff --git a/core/lib/Thelia/Admin/Controller/BaseAdminController.php b/core/lib/Thelia/Admin/Controller/BaseAdminController.php index 6aa15c001..3fd921308 100755 --- a/core/lib/Thelia/Admin/Controller/BaseAdminController.php +++ b/core/lib/Thelia/Admin/Controller/BaseAdminController.php @@ -87,8 +87,16 @@ class BaseAdminController extends ContainerAware } } + /** + * Return an empty response (after an ajax request, for example) + */ + protected function nullResponse() + { + return new Response(); + } + /** - * Render the givent template, and returns the result as an Http Response. + * Render the given template, and returns the result as an Http Response. * * @param $templateName the complete template name, with extension * @param array $args the template arguments diff --git a/core/lib/Thelia/Admin/Controller/CategoryController.php b/core/lib/Thelia/Admin/Controller/CategoryController.php index 2ce94c954..7b6a3fb2a 100644 --- a/core/lib/Thelia/Admin/Controller/CategoryController.php +++ b/core/lib/Thelia/Admin/Controller/CategoryController.php @@ -25,20 +25,10 @@ namespace Thelia\Admin\Controller; use Thelia\Model\CategoryQuery; use Thelia\Core\Security\Exception\AuthenticationException; + class CategoryController extends BaseAdminController { - public function indexAction() - { - // Show top level categories and products - $args = array( - 'action' => 'browse', - 'current_category_id' => 0 - ); - - return $this->browseCategory($args); - } - - public function createNewCategory($args) { + protected function createNewCategory($args) { $this->checkAuth("ADMIN", "admin.category.create"); @@ -48,14 +38,14 @@ class CategoryController extends BaseAdminController { return $this->render('categories', $args); } - public function editCategory($args) { + protected function editCategory($args) { $this->checkAuth("AMIN", "admin.category.edit"); return $this->render('edit_category', $args); } - public function deleteCategory($category_id) { + protected function deleteCategory($category_id) { $this->checkAuth("AMIN", "admin.category.delete"); @@ -70,13 +60,42 @@ class CategoryController extends BaseAdminController { ); } - public function browseCategory($args) { + protected function browseCategory($args) { $this->checkAuth("AMIN", "admin.catalog.view"); return $this->render('categories', $args); } + protected function visibilityToggle($args) { + + $this->checkAuth("AMIN", "admin.category.edit"); + + $this->dispatchEvent("toggleCategoryVisibility"); + + return $this->nullResponse(); + } + + protected function changePosition($args) { + + $this->checkAuth("AMIN", "admin.category.edit"); + + $this->dispatchEvent("changeCategoryPosition"); + + return $this->render('categories', $args); + } + + public function indexAction() + { + // Show top level categories and products + $args = array( + 'action' => 'browse', + 'current_category_id' => 0 + ); + + return $this->browseCategory($args); + } + public function processAction() { // Get the current action @@ -91,21 +110,24 @@ class CategoryController extends BaseAdminController { ); try { - // Browse categories - if ($action == 'browse') { - return $this->browseCategory($args); - } - // Create a new category - else if ($action == 'create') { - return $this->createNewCategory($args); - } - // Edit an existing category - else if ($action == 'edit') { - return $this->editCategory($args); - } - // Delete an existing category - else if ($action == 'delete') { - return $this->deleteCategory($id); + switch($action) { + case 'browse' : // Browse categories + return $this->browseCategory($args); + + case 'create' : // Create a new category + return $this->createNewCategory($args); + + case 'edit' : // Edit an existing category + return $this->editCategory($args); + + case 'delete' : // Delete an existing category + return $this->deleteCategory($id); + + case 'visibilityToggle' : // Toggle visibility + return $this->visibilityToggle($id); + + case 'changePosition' : // Change position + return $this->changePosition($args); } } catch(AuthenticationException $ex) { diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 138f7cb97..09f8fd309 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -109,4 +109,9 @@ final class TheliaEvents */ const AFTER_DELETECATEGORY = "action.after_deletecategory"; + /** + * Sent just after a successful change of a category in the database. + */ + const AFTER_CHANGECATEGORY = "action.after_changecategory"; + } \ No newline at end of file diff --git a/templates/admin/default/categories.html b/templates/admin/default/categories.html index 0abc9e10a..3b532f7b0 100644 --- a/templates/admin/default/categories.html +++ b/templates/admin/default/categories.html @@ -57,7 +57,13 @@ {module_include location='category_list_row'} - + {loop type="auth" name="can_change" context="admin" roles="ADMIN" permissions="admin.category.edit"} + + {/loop} + + {elseloop rel="can_change"} + + {/elseloop} @@ -222,6 +228,17 @@ $(function() { $('#'+'delete-category-id').val($(this).data('id')); }); + // Toggle category visibility + $(".categoryVisibleToggle").click(function() { + $.ajax({ + url : "{url path='admin/catalog/category'}", + data : { + id : $(this).data('id'), + action : 'visibilityToggle' + } + }); + }); + }) diff --git a/templates/admin/default/includes/header.inc.html b/templates/admin/default/includes/header.inc.html index 1f11208d9..4055b9ce3 100755 --- a/templates/admin/default/includes/header.inc.html +++ b/templates/admin/default/includes/header.inc.html @@ -88,7 +88,7 @@ {loop name="menu-auth-order" type="auth" context="admin" roles="ADMIN" permissions="admin.orders.view"}