Finished admin controllers refactoriing

This commit is contained in:
franck
2013-09-12 13:39:47 +02:00
parent 14e0eff0f5
commit 3f82421879
22 changed files with 2048 additions and 923 deletions

View File

@@ -0,0 +1,448 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Controller\Admin;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Core\Event\UpdatePositionEvent;
use Thelia\Core\Event\ToggleVisibilityEvent;
/**
* Manages currencies sent by mail
*
* @author Franck Allimant <franck@cqfdev.fr>
*/
abstract class AbstractCrudController extends BaseAdminController
{
protected $objectName;
// List ordering
protected $defaultListOrder;
// Permissions
protected $viewPermissionIdentifier;
protected $createPermissionIdentifier;
protected $updatePermissionIdentifier;
protected $deletePermissionIdentifier;
// Events
protected $createEventIdentifier;
protected $updateEventIdentifier;
protected $deleteEventIdentifier;
protected $visibilityToggleEventIdentifier;
protected $changePositionEventIdentifier;
public function __construct(
$objectName,
$defaultListOrder = null,
$viewPermissionIdentifier,
$createPermissionIdentifier,
$updatePermissionIdentifier,
$deletePermissionIdentifier,
$createEventIdentifier,
$updateEventIdentifier,
$deleteEventIdentifier,
$visibilityToggleEventIdentifier = null,
$changePositionEventIdentifier = null
) {
$this->objectName = $objectName;
$this->defaultListOrder = $defaultListOrder;
$this->viewPermissionIdentifier = $viewPermissionIdentifier;
$this->createPermissionIdentifier = $createPermissionIdentifier;
$this->updatePermissionIdentifier = $updatePermissionIdentifier;
$this->deletePermissionIdentifier = $deletePermissionIdentifier;
$this->createEventIdentifier = $createEventIdentifier;
$this->updateEventIdentifier = $updateEventIdentifier;
$this->deleteEventIdentifier = $deleteEventIdentifier;
$this->visibilityToggleEventIdentifier = $visibilityToggleEventIdentifier;
$this->changePositionEventIdentifier = $changePositionEventIdentifier;
}
/**
* Return the creation form for this object
*/
protected abstract function getCreationForm();
/**
* Return the update form for this object
*/
protected abstract function getUpdateForm();
/**
* Hydrate the update form for this object, before passing it to the update template
*
* @param unknown $object
*/
protected abstract function hydrateObjectForm($object);
/**
* Creates the creation event with the provided form data
*
* @param unknown $formData
*/
protected abstract function getCreationEvent($formData);
/**
* Creates the update event with the provided form data
*
* @param unknown $formData
*/
protected abstract function getUpdateEvent($formData);
/**
* Creates the delete event with the provided form data
*/
protected abstract function getDeleteEvent();
/**
* Return true if the event contains the object, e.g. the action has updated the object in the event.
*
* @param unknown $event
*/
protected abstract function eventContainsObject($event);
/**
* Get the created object from an event.
*
* @param unknown $createEvent
*/
protected abstract function getObjectFromEvent($event);
/**
* Load an existing object from the database
*/
protected abstract function getExistingObject();
/**
* Returns the object label form the object event (name, title, etc.)
*
* @param unknown $object
*/
protected abstract function getObjectLabel($object);
/**
* Returns the object ID from the object
*
* @param unknown $object
*/
protected abstract function getObjectId($object);
/**
* Render the main list template
*
* @param unknown $currentOrder, if any, null otherwise.
*/
protected abstract function renderListTemplate($currentOrder);
/**
* Render the edition template
*/
protected abstract function renderEditionTemplate();
/**
* Redirect to the edition template
*/
protected abstract function redirectToEditionTemplate();
/**
* Redirect to the list template
*/
protected abstract function redirectToListTemplate();
protected function createUpdatePositionEvent($positionChangeMode, $positionValue) {
throw new \LogicException ("Position Update is not supported for this object");
}
protected function createToggleVisibilityEvent() {
throw new \LogicException ("Toggle Visibility is not supported for this object");
}
/**
* Render the object list, ensuring the sort order is set.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
protected function renderList()
{
$order = null;
if ($this->defaultListOrder != null) {
$orderSessionIdentifier = sprintf("admin.%s.currentListOrder", $this->objectName);
// Find the current order
$order = $this->getRequest()->get(
'order',
$this->getSession()->get($orderSessionIdentifier, $this->defaultListOrder)
);
// Store the current sort order in session
$this->getSession()->set($orderSessionIdentifier, $order);
}
return $this->renderListTemplate($order);
}
/**
* The default action is displaying the list.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function defaultAction()
{
if (null !== $response = $this->checkAuth($this->viewPermissionIdentifier)) return $response;
return $this->renderList();
}
/**
* Create a new object
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function createAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth($this->createPermissionIdentifier)) return $response;
$error_msg = false;
// Create the Creation Form
$creationForm = $this->getCreationForm($this->getRequest());
try {
// Validate the form, create the event and dispatch it.
$form = $this->validateForm($creationForm, "POST");
$data = $form->getData();
$createEvent = $this->getCreationEvent($data);
$this->dispatch($this->createEventIdentifier, $createEvent);
if (! $this->eventContainsObject($createEvent))
throw new \LogicException(
$this->getTranslator()->trans("No %obj was created.", array('%obj', $this->objectName)));
if (null !== $createdObject = $this->getObjectFromEvent($createEvent)) {
// Log object creation
$this->adminLogAppend(sprintf("%s %s (ID %s) created", ucfirst($this->objectName), $this->getObjectLabel($createdObject), $this->getObjectId($createdObject)));
}
// Substitute _ID_ in the URL with the ID of the created object
$successUrl = str_replace('_ID_', $this->getObjectId($createdObject), $creationForm->getSuccessUrl());
// Redirect to the success URL
$this->redirect($successUrl);
}
catch (FormValidationException $ex) {
// Form cannot be validated
$error_msg = $this->createStandardFormValidationErrorMessage($ex);
}
catch (\Exception $ex) {
// Any other error
$error_msg = $ex->getMessage();
}
$this->setupFormErrorContext(
$this->getTranslator()->trans("%obj creation", array('%obj' => $this->objectName)), $error_msg, $creationForm, $ex);
// At this point, the form has error, and should be redisplayed.
return $this->renderList();
}
/**
* Load a object for modification, and display the edit template.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function changeAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth($this->updatePermissionIdentifier)) return $response;
// Load the object
$object = $this->getExistingObject($this->getRequest());
if ($object != null) {
// Hydrate the form abd pass it to the parser
$changeForm = $this->hydrateObjectForm($object);
// Pass it to the parser
$this->getParserContext()->addForm($changeForm);
}
// Render the edition template.
return $this->renderEditionTemplate();
}
/**
* Save changes on a modified object, and either go back to the object list, or stay on the edition page.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function saveChangeAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth($this->updatePermissionIdentifier)) return $response;
$error_msg = false;
// Create the form from the request
$changeForm = $this->getUpdateForm($this->getRequest());
try {
// Check the form against constraints violations
$form = $this->validateForm($changeForm, "POST");
// Get the form field values
$data = $form->getData();
$changeEvent = $this->getUpdateEvent($data);
$this->dispatch($this->updateEventIdentifier, $changeEvent);
if (! $this->eventContainsObject($changeEvent))
throw new \LogicException(
$this->getTranslator()->trans("No %obj was updated.", array('%obj', $this->objectName)));
// Log object modification
if (null !== $changedObject = $this->getObjectFromEvent($changeEvent)) {
$this->adminLogAppend(sprintf("%s %s (ID %s) modified", ucfirst($this->objectName), $this->getObjectLabel($changedObject), $this->getObjectId($changedObject)));
}
// If we have to stay on the same page, do not redirect to the succesUrl,
// just redirect to the edit page again.
if ($this->getRequest()->get('save_mode') == 'stay') {
$this->redirectToEditionTemplate($this->getRequest());
}
// Redirect to the success URL
$this->redirect($changeForm->getSuccessUrl());
}
catch (FormValidationException $ex) {
// Form cannot be validated
$error_msg = $this->createStandardFormValidationErrorMessage($ex);
}
catch (\Exception $ex) {
// Any other error
$error_msg = $ex->getMessage();
}
$this->setupFormErrorContext(
$this->getTranslator()->trans("%obj modification", array('%obj' => $this->objectName)), $error_msg, $changeForm, $ex);
// At this point, the form has errors, and should be redisplayed.
return $this->renderEditionTemplate();
}
/**
* Update object position (only for objects whichsupport that)
*/
public function updatePositionAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth($this->updatePermissionIdentifier)) return $response;
try {
$mode = $this->getRequest()->get('mode', null);
if ($mode == 'up')
$mode = UpdatePositionEvent::POSITION_UP;
else if ($mode == 'down')
$mode = UpdatePositionEvent::POSITION_DOWN;
else
$mode = UpdatePositionEvent::POSITION_ABSOLUTE;
$position = $this->getRequest()->get('position', null);
$event = $this->createUpdatePositionEvent($mode, $position);
$this->dispatch($this->changePositionEventIdentifier, $event);
}
catch (\Exception $ex) {
// Any error
return $this->errorPage($ex);
}
$this->redirectToListTemplate();
}
/**
* Online status toggle (only for object which support it)
*/
public function setToggleVisibilityAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth($this->updatePermissionIdentifier)) return $response;
$changeEvent = $this->createToggleVisibilityEvent($this->getRequest());
// Create and dispatch the change event
$changeEvent->setIsDefault(true);
try {
$this->dispatch($this->visibilityToggleEventIdentifier, $changeEvent);
} catch (\Exception $ex) {
// Any error
return $this->errorPage($ex);
}
$this->redirectToRoute('admin.categories.default');
}
/**
* Delete an object
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function deleteAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth($this->deletePermissionIdentifier)) return $response;
// Get the currency id, and dispatch the delet request
$deleteEvent = $this->getDeleteEvent();
$this->dispatch($this->deleteEventIdentifier, $deleteEvent);
if (null !== $deletedObject = $this->getObjectFromEvent($deleteEvent)) {
$this->adminLogAppend(
sprintf("%s %s (ID %s) deleted", ucfirst($this->objectName), $this->getObjectLabel($deletedObject), $this->getObjectId($deletedObject)));
}
$this->redirectToListTemplate();
}
}

View File

@@ -1,7 +1,7 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
@@ -17,7 +17,7 @@
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
@@ -25,361 +25,140 @@ namespace Thelia\Controller\Admin;
use Thelia\Core\Event\AttributeDeleteEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Tools\URL;
use Thelia\Core\Event\AttributeUpdateEvent;
use Thelia\Core\Event\AttributeCreateEvent;
use Thelia\Log\Tlog;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Core\Security\Exception\AuthorizationException;
use Thelia\Model\AttributeQuery;
use Thelia\Form\AttributeModificationForm;
use Thelia\Form\AttributeCreationForm;
use Thelia\Core\Event\AttributeUpdatePositionEvent;
use Thelia\Form\AttributeValueCreationForm;
use Thelia\Core\Event\AttributeValueCreateEvent;
use Thelia\Core\Event\AttributeValueDeleteEvent;
use Thelia\Core\Event\UpdatePositionEvent;
/**
* Manages product attributes
* Manages attributes sent by mail
*
* @author Franck Allimant <franck@cqfdev.fr>
*/
class AttributeController extends BaseAdminController
class AttributeController extends AbstractCrudController
{
/**
* Render the attributes list, ensuring the sort order is set.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
protected function renderList() {
public function __construct() {
parent::__construct(
'attribute',
'manual',
// Find the current order
$order = $this->getRequest()->get(
'order',
$this->getSession()->get('admin.attribute_order', 'manual')
'admin.configuration.attributes.view',
'admin.configuration.attributes.create',
'admin.configuration.attributes.update',
'admin.configuration.attributes.delete',
TheliaEvents::ATTRIBUTE_CREATE,
TheliaEvents::ATTRIBUTE_UPDATE,
TheliaEvents::ATTRIBUTE_DELETE,
null, // No visibility toggle
TheliaEvents::ATTRIBUTE_UPDATE_POSITION
);
}
protected function getCreationForm() {
return new AttributeCreationForm($this->getRequest());
}
protected function getUpdateForm() {
return new AttributeModificationForm($this->getRequest());
}
protected function getCreationEvent($formData) {
$createEvent = new AttributeCreateEvent();
$createEvent
->setTitle($formData['title'])
->setLocale($formData["locale"])
->setAddToAllTemplates($formData['add_to_all'])
;
return $createEvent;
}
protected function getUpdateEvent($formData) {
$changeEvent = new AttributeUpdateEvent($formData['id']);
// Create and dispatch the change event
$changeEvent
->setLocale($formData["locale"])
->setTitle($formData['title'])
->setChapo($formData['chapo'])
->setDescription($formData['description'])
->setPostscriptum($formData['postscriptum'])
;
return $changeEvent;
}
protected function createUpdatePositionEvent($positionChangeMode, $positionValue) {
return new UpdatePositionEvent(
$this->getRequest()->get('attribute_id', null),
$positionChangeMode,
$positionValue
);
}
protected function getDeleteEvent() {
return new AttributeDeleteEvent($this->getRequest()->get('attribute_id'));
}
protected function eventContainsObject($event) {
return $event->hasAttribute();
}
protected function hydrateObjectForm($object) {
$data = array(
'id' => $object->getId(),
'locale' => $object->getLocale(),
'title' => $object->getTitle(),
'chapo' => $object->getChapo(),
'description' => $object->getDescription(),
'postscriptum' => $object->getPostscriptum()
);
// Store the current sort order in session
$this->getSession()->set('admin.attribute_order', $order);
return $this->render('attributes', array('order' => $order));
// Setup the object form
$changeForm = new AttributeModificationForm($this->getRequest(), "form", $data);
}
/**
* The default action is displaying the product attributes list.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function defaultAction() {
if (null !== $response = $this->checkAuth("admin.configuration.attributes.view")) return $response;
return $this->renderList();
protected function getObjectFromEvent($event) {
return $event->hasAttribute() ? $event->getAttribute() : null;
}
/**
* Create a new product attribute object
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function createAction() {
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.attributes.create")) return $response;
$error_msg = false;
// Create the Creation Form
$creationForm = new AttributeCreationForm($this->getRequest());
try {
// Validate the form, create the AttributeCreation event and dispatch it.
$form = $this->validateForm($creationForm, "POST");
$data = $form->getData();
$createEvent = new AttributeCreateEvent();
$createEvent
->setTitle($data['title'])
->setLocale($data["locale"])
->setAddToAllTemplates($data['add_to_all'])
;
$this->dispatch(TheliaEvents::ATTRIBUTE_CREATE, $createEvent);
if (! $createEvent->hasAttribute()) throw new \LogicException($this->getTranslator()->trans("No product attribute was created."));
$createdObject = $createEvent->getAttribute();
// Log product attribute creation
$this->adminLogAppend(sprintf("Attribute %s (ID %s) created", $createdObject->getTitle(), $createdObject->getId()));
// Substitute _ID_ in the URL with the ID of the created object
$successUrl = str_replace('_ID_', $createdObject->getId(), $creationForm->getSuccessUrl());
// Redirect to the success URL
$this->redirect($successUrl);
}
catch (FormValidationException $ex) {
// Form cannot be validated
$error_msg = $this->createStandardFormValidationErrorMessage($ex);
}
catch (\Exception $ex) {
// Any other error
$error_msg = $ex->getMessage();
}
$this->setupFormErrorContext("product attribute creation", $error_msg, $creationForm, $ex);
// At this point, the form has error, and should be redisplayed.
return $this->renderList();
protected function getExistingObject() {
return AttributeQuery::create()
->joinWithI18n($this->getCurrentEditionLocale())
->findOneById($this->getRequest()->get('attribute_id'));
}
/**
* Create a new product attribute value object
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function createValueAction() {
protected function getObjectLabel($object) {
return $object->getName();
}
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.attribute-values.create")) return $response;
protected function getObjectId($object) {
return $object->getId();
}
$error_msg = false;
protected function renderListTemplate($currentOrder) {
return $this->render('attributes', array('order' => $currentOrder));
}
// Create the Creation Form
$creationForm = new AttributeValueCreationForm($this->getRequest());
try {
// Validate the form, create the AttributeCreation event and dispatch it.
$form = $this->validateForm($creationForm, "POST");
$data = $form->getData();
$createEvent = new AttributeValueCreateEvent();
$createEvent
->setTitle($data['title'])
->setLocale($data["locale"])
->setAttributeId($data["attribute_id"])
;
$this->dispatch(TheliaEvents::ATTRIBUTE_VALUE_CREATE, $createEvent);
if (! $createEvent->hasAttribute()) throw new \LogicException($this->getTranslator()->trans("No product attribute value was created."));
$createdObject = $createEvent->getAttributeValue();
// Log product attribute creation
$this->adminLogAppend(sprintf("Attribute value %s (ID %s) created", $createdObject->getTitle(), $createdObject->getId()));
// Redirect to the success URL
$this->redirect($creationForm->getSuccessUrl());
}
catch (FormValidationException $ex) {
// Form cannot be validated
$error_msg = $this->createStandardFormValidationErrorMessage($ex);
}
catch (\Exception $ex) {
// Any other error
$error_msg = $ex->getMessage();
}
$this->setupFormErrorContext("product attribute value creation", $error_msg, $creationForm, $ex);
// At this point, the form has error, and should be redisplayed on the edition page
protected function renderEditionTemplate() {
return $this->render('attribute-edit', array('attribute_id' => $this->getRequest()->get('attribute_id')));
}
/**
* Load a product attribute object for modification, and display the edit template.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function changeAction() {
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.attributes.update")) return $response;
// Load the product attribute object
$attribute = AttributeQuery::create()
->joinWithI18n($this->getCurrentEditionLocale())
->findOneById($this->getRequest()->get('attribute_id'));
if ($attribute != null) {
// Prepare the data that will hydrate the form
$data = array(
'id' => $attribute->getId(),
'locale' => $attribute->getLocale(),
'title' => $attribute->getTitle(),
'chapo' => $attribute->getChapo(),
'description' => $attribute->getDescription(),
'postscriptum' => $attribute->getPostscriptum()
);
// Setup the object form
$changeForm = new AttributeModificationForm($this->getRequest(), "form", $data);
// Pass it to the parser
$this->getParserContext()->addForm($changeForm);
}
// Render the edition template.
return $this->render('attribute-edit', array('attribute_id' => $this->getRequest()->get('attribute_id')));
protected function redirectToEditionTemplate() {
$this->redirectToRoute(
"admin.configuration.attributes.update",
array('attribute_id' => $this->getRequest()->get('attribute_id'))
);
}
/**
* Save changes on a modified product attribute object, and either go back to the product attribute list, or stay on the edition page.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function saveChangeAction() {
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.attributes.update")) return $response;
$error_msg = false;
// Create the form from the request
$changeForm = new AttributeModificationForm($this->getRequest());
// Get the attribute ID
$attribute_id = $this->getRequest()->get('attribute_id');
try {
// Check the form against constraints violations
$form = $this->validateForm($changeForm, "POST");
// Get the form field values
$data = $form->getData();
$changeEvent = new AttributeUpdateEvent($data['id']);
// Create and dispatch the change event
$changeEvent
->setLocale($data["locale"])
->setTitle($data['title'])
->setChapo($data['chapo'])
->setDescription($data['description'])
->setPostscriptum($data['postscriptum'])
;
$this->dispatch(TheliaEvents::ATTRIBUTE_UPDATE, $changeEvent);
if (! $changeEvent->hasAttribute()) throw new \LogicException($this->getTranslator()->trans("No product attribute was updated."));
// Log product attribute modification
$changedObject = $changeEvent->getAttribute();
$this->adminLogAppend(sprintf("Attribute %s (ID %s) modified", $changedObject->getTitle(), $changedObject->getId()));
// If we have to stay on the same page, do not redirect to the succesUrl,
// just redirect to the edit page again.
if ($this->getRequest()->get('save_mode') == 'stay') {
$this->redirectToRoute(
"admin.configuration.attributes.update",
array('attribute_id' => $attribute_id)
);
}
// Redirect to the success URL
$this->redirect($changeForm->getSuccessUrl());
}
catch (FormValidationException $ex) {
// Form cannot be validated
$error_msg = $this->createStandardFormValidationErrorMessage($ex);
}
catch (\Exception $ex) {
// Any other error
$error_msg = $ex->getMessage();
}
$this->setupFormErrorContext("product attribute modification", $error_msg, $changeForm, $ex);
// At this point, the form has errors, and should be redisplayed.
return $this->render('attribute-edit', array('attribute_id' => $attribute_id));
}
/**
* Update product attribute position
*/
public function updatePositionAction() {
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.attributes.update")) return $response;
try {
$mode = $this->getRequest()->get('mode', null);
if ($mode == 'up')
$mode = AttributeUpdatePositionEvent::POSITION_UP;
else if ($mode == 'down')
$mode = AttributeUpdatePositionEvent::POSITION_DOWN;
else
$mode = AttributeUpdatePositionEvent::POSITION_ABSOLUTE;
$position = $this->getRequest()->get('position', null);
$event = new AttributeUpdatePositionEvent(
$this->getRequest()->get('attribute_id', null),
$mode,
$this->getRequest()->get('position', null)
);
$this->dispatch(TheliaEvents::ATTRIBUTE_UPDATE_POSITION, $event);
}
catch (\Exception $ex) {
// Any error
return $this->errorPage($ex);
}
$this->redirectToRoute('admin.configuration.attributes.default');
}
/**
* Delete a product attribute object
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function deleteAction() {
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.product attributes.delete")) return $response;
// Get the product attribute id, and dispatch the delet request
$event = new AttributeDeleteEvent($this->getRequest()->get('attribute_id'));
$this->dispatch(TheliaEvents::ATTRIBUTE_DELETE, $event);
if ($event->hasAttribute())
$this->adminLogAppend(sprintf("Attribute %s (ID %s) deleted", $event->getAttribute()->getTitle(), $event->getAttribute()->getId()));
$this->redirectToRoute('admin.configuration.attributes.default');
}
/**
* Delete a product attribute value object
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function deleteValueAction() {
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.product attribute-values.delete")) return $response;
// Get the product attribute id, and dispatch the delet request
$event = new AttributeValueDeleteEvent($this->getRequest()->get('value_id'));
$this->dispatch(TheliaEvents::ATTRIBUTE_VALUE_DELETE, $event);
if ($event->hasAttributeValue())
$this->adminLogAppend(sprintf("Attribute value %s (ID %s) deleted", $event->getAttributeValue()->getTitle(), $event->getAttributeValue()->getId()));
protected function redirectToListTemplate() {
$this->redirectToRoute('admin.configuration.attributes.default');
}
}

View File

@@ -25,228 +25,144 @@ namespace Thelia\Controller\Admin;
use Thelia\Core\Event\ConfigDeleteEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Tools\URL;
use Thelia\Core\Event\ConfigUpdateEvent;
use Thelia\Core\Event\ConfigCreateEvent;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Model\ConfigQuery;
use Thelia\Form\ConfigModificationForm;
use Thelia\Form\ConfigCreationForm;
use Thelia\Core\Event\UpdatePositionEvent;
/**
* Manages Thelmia system variables, aka Config objects.
* Manages variables sent by mail
*
* @author Franck Allimant <franck@cqfdev.fr>
*/
class ConfigController extends BaseAdminController
class ConfigController extends AbstractCrudController
{
/**
* Render the currencies list, ensuring the sort order is set.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
protected function renderList()
{
// Find the current order
$order = $this->getRequest()->get(
'order',
$this->getSession()->get('admin.variables_order', 'name')
public function __construct() {
parent::__construct(
'variable',
'name',
'admin.configuration.variables.view',
'admin.configuration.variables.create',
'admin.configuration.variables.update',
'admin.configuration.variables.delete',
TheliaEvents::CONFIG_CREATE,
TheliaEvents::CONFIG_UPDATE,
TheliaEvents::CONFIG_DELETE,
null, // No visibility toggle
null // no position change
);
}
protected function getCreationForm() {
return new ConfigCreationForm($this->getRequest());
}
protected function getUpdateForm() {
return new ConfigModificationForm($this->getRequest());
}
protected function getCreationEvent($data) {
$createEvent = new ConfigCreateEvent();
$createEvent
->setEventName($data['name'])
->setValue($data['value'])
->setLocale($data["locale"])
->setTitle($data['title'])
->setHidden($data['hidden'])
->setSecured($data['secured'])
;
return $createEvent;
}
protected function getUpdateEvent($data) {
$changeEvent = new ConfigUpdateEvent($data['id']);
// Create and dispatch the change event
$changeEvent
->setEventName($data['name'])
->setValue($data['value'])
->setHidden($data['hidden'])
->setSecured($data['secured'])
->setLocale($data["locale"])
->setTitle($data['title'])
->setChapo($data['chapo'])
->setDescription($data['description'])
->setPostscriptum($data['postscriptum'])
;
return $changeEvent;
}
protected function getDeleteEvent() {
return new ConfigDeleteEvent($this->getRequest()->get('variable_id'));
}
protected function eventContainsObject($event) {
return $event->hasConfig();
}
protected function hydrateObjectForm($object) {
// Prepare the data that will hydrate the form
$data = array(
'id' => $object->getId(),
'name' => $object->getName(),
'value' => $object->getValue(),
'hidden' => $object->getHidden(),
'secured' => $object->getSecured(),
'locale' => $object->getLocale(),
'title' => $object->getTitle(),
'chapo' => $object->getChapo(),
'description' => $object->getDescription(),
'postscriptum' => $object->getPostscriptum()
);
// Store the current sort order in session
$this->getSession()->set('admin.variables_order', $order);
return $this->render('variables', array('order' => $order));
// Setup the object form
return new ConfigModificationForm($this->getRequest(), "form", $data);
}
/**
* The default action is displaying the variables list.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function defaultAction()
{
if (null !== $response = $this->checkAuth("admin.configuration.variables.view")) return $response;
return $this->renderList();
protected function getObjectFromEvent($event) {
return $event->hasConfig() ? $event->getConfig() : null;
}
/**
* Create a new config object
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function createAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.variables.create")) return $response;
$message = false;
// Create the Creation Form
$creationForm = new ConfigCreationForm($this->getRequest());
try {
// Validate the form, create the ConfigCreation event and dispatch it.
$form = $this->validateForm($creationForm, "POST");
$data = $form->getData();
$createEvent = new ConfigCreateEvent();
$createEvent
->setEventName($data['name'])
->setValue($data['value'])
->setLocale($data["locale"])
->setTitle($data['title'])
->setHidden($data['hidden'])
->setSecured($data['secured'])
;
$this->dispatch(TheliaEvents::CONFIG_CREATE, $createEvent);
if (! $createEvent->hasConfig()) throw new \LogicException($this->getTranslator()->trans("No variable was created."));
$createdObject = $createEvent->getConfig();
// Log config creation
$this->adminLogAppend(sprintf("Variable %s (ID %s) created", $createdObject->getName(), $createdObject->getId()));
// Substitute _ID_ in the URL with the ID of the created object
$successUrl = str_replace('_ID_', $createdObject->getId(), $creationForm->getSuccessUrl());
// Redirect to the success URL
$this->redirect($successUrl);
} catch (FormValidationException $ex) {
// Form cannot be validated
$message = $this->createStandardFormValidationErrorMessage($ex);
} catch (\Exception $ex) {
// Any other error
$message = $ex->getMessage();
}
$this->setupFormErrorContext("variable creation", $message, $creationForm, $ex);
// At this point, the form has error, and should be redisplayed.
return $this->renderList();
protected function getExistingObject() {
return ConfigQuery::create()
->joinWithI18n($this->getCurrentEditionLocale())
->findOneById($this->getRequest()->get('variable_id'));
}
/**
* Load a config object for modification, and display the edit template.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function changeAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.variables.update")) return $response;
protected function getObjectLabel($object) {
return $object->getName();
}
// Load the config object
$config = ConfigQuery::create()
->joinWithI18n($this->getCurrentEditionLocale())
->findOneById($this->getRequest()->get('variable_id'));
protected function getObjectId($object) {
return $object->getId();
}
if ($config != null) {
protected function renderListTemplate($currentOrder) {
return $this->render('variables', array('order' => $currentOrder));
}
// Prepare the data that will hydrate the form
$data = array(
'id' => $config->getId(),
'name' => $config->getName(),
'value' => $config->getValue(),
'hidden' => $config->getHidden(),
'secured' => $config->getSecured(),
'locale' => $config->getLocale(),
'title' => $config->getTitle(),
'chapo' => $config->getChapo(),
'description' => $config->getDescription(),
'postscriptum' => $config->getPostscriptum()
);
// Setup the object form
$changeForm = new ConfigModificationForm($this->getRequest(), "form", $data);
// Pass it to the parser
$this->getParserContext()->addForm($changeForm);
}
// Render the edition template.
protected function renderEditionTemplate() {
return $this->render('variable-edit', array('variable_id' => $this->getRequest()->get('variable_id')));
}
/**
* Save changes on a modified config object, and either go back to the variable list, or stay on the edition page.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function saveChangeAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.variables.update")) return $response;
protected function redirectToEditionTemplate() {
$this->redirectToRoute(
"admin.configuration.variables.update",
array('variable_id' => $this->getRequest()->get('variable_id'))
);
}
$message = false;
// Create the form from the request
$changeForm = new ConfigModificationForm($this->getRequest());
// Get the variable ID
$variable_id = $this->getRequest()->get('variable_id');
try {
// Check the form against constraints violations
$form = $this->validateForm($changeForm, "POST");
// Get the form field values
$data = $form->getData();
$changeEvent = new ConfigUpdateEvent($data['id']);
// Create and dispatch the change event
$changeEvent
->setEventName($data['name'])
->setValue($data['value'])
->setHidden($data['hidden'])
->setSecured($data['secured'])
->setLocale($data["locale"])
->setTitle($data['title'])
->setChapo($data['chapo'])
->setDescription($data['description'])
->setPostscriptum($data['postscriptum'])
;
$this->dispatch(TheliaEvents::CONFIG_UPDATE, $changeEvent);
if (! $changeEvent->hasConfig()) throw new \LogicException($this->getTranslator()->trans("No variable was updated."));
// Log config modification
$changedObject = $changeEvent->getConfig();
$this->adminLogAppend(sprintf("Variable %s (ID %s) modified", $changedObject->getName(), $changedObject->getId()));
// If we have to stay on the same page, do not redirect to the succesUrl,
// just redirect to the edit page again.
if ($this->getRequest()->get('save_mode') == 'stay') {
$this->redirectToRoute(
"admin.configuration.variables.update",
array('variable_id' => $variable_id)
);
}
// Redirect to the success URL
$this->redirect($changeForm->getSuccessUrl());
} catch (FormValidationException $ex) {
// Form cannot be validated
$message = $this->createStandardFormValidationErrorMessage($ex);
} catch (\Exception $ex) {
// Any other error
$message = $ex->getMessage();
}
$this->setupFormErrorContext("variable edition", $message, $changeForm, $ex);
// At this point, the form has errors, and should be redisplayed.
return $this->render('variable-edit', array('variable_id' => $variable_id));
protected function redirectToListTemplate() {
$this->redirectToRoute('admin.configuration.variables.default');
}
/**
@@ -271,25 +187,4 @@ class ConfigController extends BaseAdminController
$this->redirectToRoute('admin.configuration.variables.default');
}
/**
* Delete a config object
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function deleteAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.variables.delete")) return $response;
// Get the config id, and dispatch the delet request
$event = new ConfigDeleteEvent($this->getRequest()->get('variable_id'));
$this->dispatch(TheliaEvents::CONFIG_DELETE, $event);
if ($event->hasConfig())
$this->adminLogAppend(sprintf("Variable %s (ID %s) deleted", $event->getConfig()->getName(), $event->getConfig()->getId()));
$this->redirectToRoute('admin.configuration.variables.default');
}
}
}

View File

@@ -103,11 +103,6 @@ class CurrencyController extends AbstractCrudController
);
}
protected function createToggleVisibilityEvent() {
return new ToggleVisibilityEvent($this->getRequest()->get('currency_id', null));
}
protected function getDeleteEvent() {
return new CurrencyDeleteEvent($this->getRequest()->get('currency_id'));
}

View File

@@ -24,11 +24,8 @@
namespace Thelia\Controller\Admin;
use Thelia\Core\Event\MessageDeleteEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Tools\URL;
use Thelia\Core\Event\MessageUpdateEvent;
use Thelia\Core\Event\TheliaEvents;use Thelia\Core\Event\MessageUpdateEvent;
use Thelia\Core\Event\MessageCreateEvent;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Model\MessageQuery;
use Thelia\Form\MessageModificationForm;
use Thelia\Form\MessageCreationForm;
@@ -38,217 +35,124 @@ use Thelia\Form\MessageCreationForm;
*
* @author Franck Allimant <franck@cqfdev.fr>
*/
class MessageController extends BaseAdminController
class MessageController extends AbstractCrudController
{
/**
* Render the messages list
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
protected function renderList()
{
public function __construct() {
parent::__construct(
'message',
null,
'admin.configuration.messages.view',
'admin.configuration.messages.create',
'admin.configuration.messages.update',
'admin.configuration.messages.delete',
TheliaEvents::MESSAGE_CREATE,
TheliaEvents::MESSAGE_UPDATE,
TheliaEvents::MESSAGE_DELETE,
null, // No visibility toggle
null // No position update
);
}
protected function getCreationForm() {
return new MessageCreationForm($this->getRequest());
}
protected function getUpdateForm() {
return new MessageModificationForm($this->getRequest());
}
protected function getCreationEvent($formData) {
$createEvent = new MessageCreateEvent();
$createEvent
->setMessageName($formData['name'])
->setLocale($formData["locale"])
->setTitle($formData['title'])
->setSecured($formData['secured'])
;
return $createEvent;
}
protected function getUpdateEvent($formData) {
$changeEvent = new MessageUpdateEvent($formData['id']);
// Create and dispatch the change event
$changeEvent
->setMessageName($formData['name'])
->setSecured($formData['secured'])
->setLocale($formData["locale"])
->setTitle($formData['title'])
->setSubject($formData['subject'])
->setHtmlMessage($formData['html_message'])
->setTextMessage($formData['text_message'])
;
return $changeEvent;
}
protected function getDeleteEvent() {
return new MessageDeleteEvent($this->getRequest()->get('message_id'));
}
protected function eventContainsObject($event) {
return $event->hasMessage();
}
protected function hydrateObjectForm($object) {
// Prepare the data that will hydrate the form
$data = array(
'id' => $object->getId(),
'name' => $object->getName(),
'secured' => $object->getSecured(),
'locale' => $object->getLocale(),
'title' => $object->getTitle(),
'subject' => $object->getSubject(),
'html_message' => $object->getHtmlMessage(),
'text_message' => $object->getTextMessage()
);
// Setup the object form
return new MessageModificationForm($this->getRequest(), "form", $data);
}
protected function getObjectFromEvent($event) {
return $event->hasMessage() ? $event->getMessage() : null;
}
protected function getExistingObject() {
return MessageQuery::create()
->joinWithI18n($this->getCurrentEditionLocale())
->findOneById($this->getRequest()->get('message_id'));
}
protected function getObjectLabel($object) {
return $object->getName();
}
protected function getObjectId($object) {
return $object->getId();
}
protected function renderListTemplate($currentOrder) {
return $this->render('messages');
}
/**
* The default action is displaying the messages list.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function defaultAction()
{
if (null !== $response = $this->checkAuth("admin.configuration.messages.view")) return $response;
return $this->renderList();
}
/**
* Create a new message object
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function createAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.messages.create")) return $response;
$message = false;
// Create the creation Form
$creationForm = new MessageCreationForm($this->getRequest());
try {
// Validate the form, create the MessageCreation event and dispatch it.
$form = $this->validateForm($creationForm, "POST");
$data = $form->getData();
$createEvent = new MessageCreateEvent();
$createEvent
->setMessageName($data['name'])
->setLocale($data["locale"])
->setTitle($data['title'])
->setSecured($data['secured'])
;
$this->dispatch(TheliaEvents::MESSAGE_CREATE, $createEvent);
if (! $createEvent->hasMessage()) throw new \LogicException($this->getTranslator()->trans("No message was created."));
$createdObject = $createEvent->getMessage();
$this->adminLogAppend(sprintf("Message %s (ID %s) created", $createdObject->getName(), $createdObject->getId()));
// Substitute _ID_ in the URL with the ID of the created object
$successUrl = str_replace('_ID_', $createdObject->getId(), $creationForm->getSuccessUrl());
// Redirect to the success URL
$this->redirect($successUrl);
} catch (FormValidationException $ex) {
// Form cannot be validated
$message = $this->createStandardFormValidationErrorMessage($ex);
} catch (\Exception $ex) {
// Any other error
$message = $ex->getMessage();
}
$this->setupFormErrorContext("message modification", $message, $creationForm, $ex);
// At this point, the form has error, and should be redisplayed.
return $this->render('messages');
}
/**
* Load a message object for modification, and display the edit template.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function changeAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.messages.update")) return $response;
// Load the message object
$message = MessageQuery::create()
->joinWithI18n($this->getCurrentEditionLocale())
->findOneById($this->getRequest()->get('message_id'));
if ($message != null) {
// Prepare the data that will hydrate the form
$data = array(
'id' => $message->getId(),
'name' => $message->getName(),
'secured' => $message->getSecured(),
'locale' => $message->getLocale(),
'title' => $message->getTitle(),
'subject' => $message->getSubject(),
'html_message' => $message->getHtmlMessage(),
'text_message' => $message->getTextMessage()
);
// Setup the object form
$changeForm = new MessageModificationForm($this->getRequest(), "form", $data);
// Pass it to the parser
$this->getParserContext()->addForm($changeForm);
}
// Render the edition template.
protected function renderEditionTemplate() {
return $this->render('message-edit', array('message_id' => $this->getRequest()->get('message_id')));
}
/**
* Save changes on a modified message object, and either go back to the message list, or stay on the edition page.
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function saveChangeAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.messages.update")) return $response;
$message = false;
// Create the form from the request
$changeForm = new MessageModificationForm($this->getRequest());
// Get the message ID
$message_id = $this->getRequest()->get('message_id');
try {
// Check the form against constraints violations
$form = $this->validateForm($changeForm, "POST");
// Get the form field values
$data = $form->getData();
$changeEvent = new MessageUpdateEvent($data['id']);
// Create and dispatch the change event
$changeEvent
->setMessageName($data['name'])
->setSecured($data['secured'])
->setLocale($data["locale"])
->setTitle($data['title'])
->setSubject($data['subject'])
->setHtmlMessage($data['html_message'])
->setTextMessage($data['text_message'])
;
$this->dispatch(TheliaEvents::MESSAGE_UPDATE, $changeEvent);
if (! $changeEvent->hasMessage()) throw new \LogicException($this->getTranslator()->trans("No message was updated."));
$changedObject = $changeEvent->getMessage();
$this->adminLogAppend(sprintf("Variable %s (ID %s) modified", $changedObject->getName(), $changedObject->getId()));
// If we have to stay on the same page, do not redirect to the succesUrl,
// just redirect to the edit page again.
if ($this->getRequest()->get('save_mode') == 'stay') {
$this->redirectToRoute(
"admin.configuration.messages.update",
array('message_id' => $message_id)
);
}
// Redirect to the success URL
$this->redirect($changeForm->getSuccessUrl());
} catch (FormValidationException $ex) {
// Form cannot be validated
$message = $this->createStandardFormValidationErrorMessage($ex);
} catch (\Exception $ex) {
// Any other error
$message = $ex->getMessage();
}
$this->setupFormErrorContext("message modification", $message, $changeForm, $ex);
// At this point, the form has errors, and should be redisplayed.
return $this->render('message-edit', array('message_id' => $message_id));
protected function redirectToEditionTemplate() {
$this->redirectToRoute(
"admin.configuration.messages.update",
array('message_id' => $this->getRequest()->get('message_id'))
);
}
/**
* Delete a message object
*
* @return Symfony\Component\HttpFoundation\Response the response
*/
public function deleteAction()
{
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.messages.delete")) return $response;
// Get the message id, and dispatch the delet request
$event = new MessageDeleteEvent($this->getRequest()->get('message_id'));
$this->dispatch(TheliaEvents::MESSAGE_DELETE, $event);
if ($event->hasMessage())
$this->adminLogAppend(sprintf("Message %s (ID %s) deleted", $event->getMessage()->getName(), $event->getMessage()->getId()));
protected function redirectToListTemplate() {
$this->redirectToRoute('admin.configuration.messages.default');
}
}