Category visibility toggle implementation
This commit is contained in:
@@ -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),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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";
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user