From ee7d62f9d5f82fe7df37ce452a7ec07f5001990b Mon Sep 17 00:00:00 2001 From: touffies Date: Fri, 29 Nov 2013 09:50:13 +0100 Subject: [PATCH] Extend AbstractCrudController to manage the SEO methods --- .../Admin/AbstractCrudController.php | 111 +---------- .../Admin/AbstractSeoCrudController.php | 183 ++++++++++++++++++ .../Controller/Admin/ProductController.php | 2 +- 3 files changed, 187 insertions(+), 109 deletions(-) create mode 100644 core/lib/Thelia/Controller/Admin/AbstractSeoCrudController.php diff --git a/core/lib/Thelia/Controller/Admin/AbstractCrudController.php b/core/lib/Thelia/Controller/Admin/AbstractCrudController.php index 6fe387319..19d0b782d 100644 --- a/core/lib/Thelia/Controller/Admin/AbstractCrudController.php +++ b/core/lib/Thelia/Controller/Admin/AbstractCrudController.php @@ -23,12 +23,9 @@ namespace Thelia\Controller\Admin; -use Thelia\Core\Event\UpdatePositionEvent; -use Thelia\Core\Event\UpdateSeoEvent; use Thelia\Core\Security\AccessManager; - use Thelia\Form\Exception\FormValidationException; -use Thelia\Form\SeoForm; +use Thelia\Core\Event\UpdatePositionEvent; /** * An abstract CRUD controller for Thelia ADMIN, to manage basic CRUD operations on a givent object. @@ -37,7 +34,7 @@ use Thelia\Form\SeoForm; */ abstract class AbstractCrudController extends BaseAdminController { - protected $objectName; + protected $objectName; // List ordering protected $defaultListOrder; @@ -52,7 +49,6 @@ abstract class AbstractCrudController extends BaseAdminController protected $deleteEventIdentifier; protected $visibilityToggleEventIdentifier; protected $changePositionEventIdentifier; - protected $updateSeoEventIdentifier; /** * @param string $objectName the lower case object name. Example. "message" @@ -68,7 +64,6 @@ abstract class AbstractCrudController extends BaseAdminController * * @param string $visibilityToggleEventIdentifier the dispatched visibility toggle TheliaEvent identifier, or null if the object has no visible options. Example: TheliaEvents::MESSAGE_TOGGLE_VISIBILITY * @param string $changePositionEventIdentifier the dispatched position change TheliaEvent identifier, or null if the object has no position. Example: TheliaEvents::MESSAGE_UPDATE_POSITION - * @param string $updateSeoEventIdentifier the dispatched update SEO change TheliaEvent identifier, or null if the object has no SEO. Example: TheliaEvents::MESSAGE_UPDATE_SEO */ public function __construct( $objectName, @@ -82,8 +77,7 @@ abstract class AbstractCrudController extends BaseAdminController $updateEventIdentifier, $deleteEventIdentifier, $visibilityToggleEventIdentifier = null, - $changePositionEventIdentifier = null, - $updateSeoEventIdentifier = null + $changePositionEventIdentifier = null ) { $this->objectName = $objectName; @@ -97,7 +91,6 @@ abstract class AbstractCrudController extends BaseAdminController $this->deleteEventIdentifier = $deleteEventIdentifier; $this->visibilityToggleEventIdentifier = $visibilityToggleEventIdentifier; $this->changePositionEventIdentifier = $changePositionEventIdentifier; - $this->updateSeoEventIdentifier = $updateSeoEventIdentifier; } /** @@ -245,17 +238,6 @@ abstract class AbstractCrudController extends BaseAdminController return null; } - /** - * Put in this method post object update SEO processing if required. - * - * @param unknown $seoUpdateEvent the update event - * @return Response a response, or null to continue normal processing - */ - protected function performAdditionalUpdateSeoAction($updateSeoEvent) - { - return null; - } - /** * Return the current list order identifier, updating it in the same time. */ @@ -441,93 +423,6 @@ abstract class AbstractCrudController extends BaseAdminController return $this->renderEditionTemplate(); } - - /** - * Return the update SEO form for this object - */ - protected function getUpdateSeoForm() - { - return new SeoForm($this->getRequest()); - } - - /** - * Creates the update SEO event with the provided form data - * - * @param $formData - * @return UpdateSeoEvent - */ - protected function getUpdateSeoEvent($formData) - { - - $updateSeoEvent = new UpdateSeoEvent($formData['id']); - - $updateSeoEvent - ->setLocale($formData['locale']) - ->setMetaTitle($formData['meta_title']) - ->setMetaDescription($formData['meta_description']) - ->setMetaKeyword($formData['meta_keyword']) - ; - - // Create and dispatch the change event - return $updateSeoEvent; - } - - /** - * Update SEO modification, and either go back to the object list, or stay on the edition page. - * - * @return Thelia\Core\HttpFoundation\Response the response - */ - - public function processUpdateSeoAction() - { - // Check current user authorization - if (null !== $response = $this->checkAuth($this->resourceCode, array(), AccessManager::UPDATE)) return $response; - - $error_msg = false; - - // Create the form from the request - $updateSeoForm = $this->getUpdateSeoForm($this->getRequest()); - - try { - - // Check the form against constraints violations - $form = $this->validateForm($updateSeoForm, "POST"); - - // Get the form field values - $data = $form->getData(); - - $updateSeoEvent = $this->getUpdateSeoEvent($data); - - $this->dispatch($this->updateSeoEventIdentifier, $updateSeoEvent); - - } catch (FormValidationException $ex) { - // Form cannot be validated - $error_msg = $this->createStandardFormValidationErrorMessage($ex); - } catch (\Exception $ex) { - // Any error - return $this->errorPage($ex); - } - - - $response = $this->performAdditionalUpdateSeoAction($updateSeoEvent); - - if ($response == null) { - // If we have to stay on the same page, do not redirect to the successUrl, - // 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($updateSeoForm->getSuccessUrl()); - } else { - return $response; - } - - // At this point, the form has errors, and should be redisplayed. - return $this->renderEditionTemplate(); - } - /** * Update object position (only for objects whichsupport that) * diff --git a/core/lib/Thelia/Controller/Admin/AbstractSeoCrudController.php b/core/lib/Thelia/Controller/Admin/AbstractSeoCrudController.php new file mode 100644 index 000000000..744c4af17 --- /dev/null +++ b/core/lib/Thelia/Controller/Admin/AbstractSeoCrudController.php @@ -0,0 +1,183 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Controller\Admin; + +use Thelia\Core\Event\UpdateSeoEvent; +use Thelia\Core\Security\AccessManager; + +use Thelia\Form\Exception\FormValidationException; +use Thelia\Form\SeoForm; + +/** + * Extend abstract CRUD controller to manage basic CRUD + SEO operations on a given object. + * + * @author Christophe Laffont + */ +abstract class AbstractSeoCrudController extends AbstractCrudController +{ + // Events + protected $updateSeoEventIdentifier; + + /** + * @param string $objectName the lower case object name. Example. "message" + * + * @param string $defaultListOrder the default object list order, or null if list is not sortable. Example: manual + * @param string $orderRequestParameterName Name of the request parameter that set the list order (null if list is not sortable) + * + * @param string $resourceCode the 'resource' code. Example: "admin.configuration.message" + * + * @param string $createEventIdentifier the dispatched create TheliaEvent identifier. Example: TheliaEvents::MESSAGE_CREATE + * @param string $updateEventIdentifier the dispatched update TheliaEvent identifier. Example: TheliaEvents::MESSAGE_UPDATE + * @param string $deleteEventIdentifier the dispatched delete TheliaEvent identifier. Example: TheliaEvents::MESSAGE_DELETE + * + * @param string $visibilityToggleEventIdentifier the dispatched visibility toggle TheliaEvent identifier, or null if the object has no visible options. Example: TheliaEvents::MESSAGE_TOGGLE_VISIBILITY + * @param string $changePositionEventIdentifier the dispatched position change TheliaEvent identifier, or null if the object has no position. Example: TheliaEvents::MESSAGE_UPDATE_POSITION + * @param string $updateSeoEventIdentifier the dispatched update SEO change TheliaEvent identifier, or null if the object has no SEO. Example: TheliaEvents::MESSAGE_UPDATE_SEO + */ + public function __construct( + $objectName, + + $defaultListOrder = null, + $orderRequestParameterName = null, + + $resourceCode, + + $createEventIdentifier, + $updateEventIdentifier, + $deleteEventIdentifier, + $visibilityToggleEventIdentifier = null, + $changePositionEventIdentifier = null, + $updateSeoEventIdentifier = null + ) + { + parent::__construct( + $objectName, + $defaultListOrder, + $orderRequestParameterName, + $resourceCode, + $createEventIdentifier, + $updateEventIdentifier, + $deleteEventIdentifier, + $visibilityToggleEventIdentifier, + $changePositionEventIdentifier + ); + + $this->updateSeoEventIdentifier = $updateSeoEventIdentifier; + + } + + /** + * Put in this method post object update SEO processing if required. + * + * @param unknown $updateSeoEvent the update event + * @return Response a response, or null to continue normal processing + */ + protected function performAdditionalUpdateSeoAction($updateSeoEvent) + { + return null; + } + + /** + * Return the update SEO form for this object + */ + protected function getUpdateSeoForm() + { + return new SeoForm($this->getRequest()); + } + + /** + * Creates the update SEO event with the provided form data + * + * @param $formData + * @return UpdateSeoEvent + */ + protected function getUpdateSeoEvent($formData) + { + + $updateSeoEvent = new UpdateSeoEvent($formData['id']); + + $updateSeoEvent + ->setLocale($formData['locale']) + ->setMetaTitle($formData['meta_title']) + ->setMetaDescription($formData['meta_description']) + ->setMetaKeyword($formData['meta_keyword']) + ; + + // Create and dispatch the change event + return $updateSeoEvent; + } + + /** + * Update SEO modification, and either go back to the object list, or stay on the edition page. + * + * @return Thelia\Core\HttpFoundation\Response the response + */ + public function processUpdateSeoAction() + { + // Check current user authorization + if (null !== $response = $this->checkAuth($this->resourceCode, array(), AccessManager::UPDATE)) return $response; + + // Create the form from the request + $updateSeoForm = $this->getUpdateSeoForm($this->getRequest()); + + try { + + // Check the form against constraints violations + $form = $this->validateForm($updateSeoForm, "POST"); + + // Get the form field values + $data = $form->getData(); + + $updateSeoEvent = $this->getUpdateSeoEvent($data); + + $this->dispatch($this->updateSeoEventIdentifier, $updateSeoEvent); + + } catch (FormValidationException $ex) { + // Form cannot be validated + return $this->createStandardFormValidationErrorMessage($ex); + } catch (\Exception $ex) { + // Any error + return $this->errorPage($ex); + } + + + $response = $this->performAdditionalUpdateSeoAction($updateSeoEvent); + + if ($response == null) { + // If we have to stay on the same page, do not redirect to the successUrl, + // 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($updateSeoForm->getSuccessUrl()); + } else { + return $response; + } + + // At this point, the form has errors, and should be redisplayed. + return $this->renderEditionTemplate(); + } +} diff --git a/core/lib/Thelia/Controller/Admin/ProductController.php b/core/lib/Thelia/Controller/Admin/ProductController.php index 109e95a68..abc233684 100644 --- a/core/lib/Thelia/Controller/Admin/ProductController.php +++ b/core/lib/Thelia/Controller/Admin/ProductController.php @@ -79,7 +79,7 @@ use Thelia\Tools\NumberFormat; * * @author Franck Allimant */ -class ProductController extends AbstractCrudController +class ProductController extends AbstractSeoCrudController { public function __construct() {