Added category creation and deletion

This commit is contained in:
franck
2013-08-08 02:39:42 +02:00
parent eb93049897
commit 64031a1883
55 changed files with 1569 additions and 255 deletions

View File

@@ -38,6 +38,9 @@ use Thelia\Tools\URL;
use Thelia\Tools\Redirect;
use Thelia\Core\Template\ParserContext;
use Thelia\Core\Event\ActionEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Thelia\Core\Factory\ActionEventFactory;
use Thelia\Core\Security\Exception\AuthorizationException;
/**
*
@@ -70,6 +73,19 @@ class BaseAdminController extends ContainerAware
return new Response($this->renderRaw(self::TEMPLATE_404), 404);
}
/**
* Check current admin user authorisations. An ADMIN role is assumed.
*
* @param unknown $permissions a single permission or an array of permissions.
*
* @throws AuthenticationException if permissions are not granted ti the current user.
*/
protected function checkAuth($permissions) {
if (! $this->getSecurityContext()->isGranted(array("ADMIN"), is_array($permissions) ? $permissions : array($permissions))) {
throw new AuthorizationException("Sorry, you're not allowed to perform this action");
}
}
/**
* Render the givent template, and returns the result as an Http Response.
@@ -118,12 +134,43 @@ class BaseAdminController extends ContainerAware
}
}
/**
* Create an action event,
*
* @return EventDispatcher
*/
protected function dispatchEvent($action)
{
// Create the
$eventFactory = new ActionEventFactory($this->getRequest(), $action, $this->container->getParameter("thelia.actionEvent"));
$actionEvent = $eventFactory->createActionEvent();
$this->getDispatcher()->dispatch("action.$action", $actionEvent);
if ($actionEvent->hasErrorForm()) {
$this->getParserContext()->setErrorForm($actionEvent->getErrorForm());
}
return $actionEvent;
}
/**
* Return the event dispatcher,
*
* @return EventDispatcherInterface
*/
protected function getDispatcher()
{
return $this->container->get('event_dispatcher');
}
/**
* Return the parser context,
*
* @return ParserContext
*/
protected function getParserContext($context = false)
protected function getParserContext()
{
return $this->container->get('thelia.parser.context');
}
@@ -150,17 +197,6 @@ class BaseAdminController extends ContainerAware
return $this->container->get('request');
}
/**
* Dispatch a Thelia event to modules
*
* @param string $eventName a TheliaEvent name, as defined in TheliaEvents class
* @param ActionEvent $event the event
*/
protected function dispatch($eventName, ActionEvent $event = null) {
$this->container->get("event_dispatcher")->dispatch($eventName, $event);
}
/**
* Returns the session from the current request
*

View File

@@ -23,6 +23,8 @@
namespace Thelia\Admin\Controller;
use Thelia\Model\CategoryQuery;
use Thelia\Core\Security\Exception\AuthenticationException;
class CategoryController extends BaseAdminController {
public function indexAction()
@@ -33,31 +35,83 @@ class CategoryController extends BaseAdminController {
'current_category_id' => 0
);
return $this->browseCategory($args);
}
public function createNewCategory($args) {
$this->checkAuth("ADMIN", "admin.category.create");
$this->dispatchEvent("createCategory");
// At this point, the form has error, and should be redisplayed.
return $this->render('categories', $args);
}
public function processAction($action)
public function editCategory($args) {
$this->checkAuth("AMIN", "admin.category.edit");
return $this->render('edit_category', $args);
}
public function deleteCategory($category_id) {
$this->checkAuth("AMIN", "admin.category.delete");
$category = CategoryQuery::create()->findPk($category_id);
$this->dispatchEvent("deleteCategory");
// Something was wrong, category was not deleted. Display parent category list
return $this->render(
'categories',
array('current_category_id' => $category->getParent())
);
}
public function browseCategory($args) {
$this->checkAuth("AMIN", "admin.catalog.view");
return $this->render('categories', $args);
}
public function processAction()
{
list($action, $id) = explode('/', $action);
// Get the current action
$action = $this->getRequest()->get('action', 'browse');
// Get the category ID
$id = $this->getRequest()->get('id', 0);
$args = array(
'action' => $action,
'current_category_id' => $id
);
// Browe categories
if ($action == 'browse') {
return $this->render('categories', $args);
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);
}
}
// Create a new category
else if ($action = 'create') {
return $this->render('edit_category', $args);
catch(AuthenticationException $ex) {
return $this->render('general_error', array(
"error_message" => $ex->getMessage())
);
}
// Edit an existing category
else if ($action = 'edit') {
return $this->render('edit_category', $args);
}
//return $this->render("categories");
}
}

View File

@@ -65,7 +65,7 @@ class SessionController extends BaseAdminController {
$this->getSecurityContext()->setUser($user);
// Log authentication success
AdminLog::append("Authentication successuful", $request, $user);
AdminLog::append("Authentication successful", $request, $user);
$this->dispatch(TheliaEvents::ADMIN_LOGIN);