complete AreaController
This commit is contained in:
@@ -22,10 +22,14 @@
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Controller\Admin;
|
||||
|
||||
use Thelia\Core\Event\Area\AreaCreateEvent;
|
||||
use Thelia\Core\Event\Area\AreaDeleteEvent;
|
||||
use Thelia\Core\Event\Area\AreaUpdateEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Form\Area\AreaCreateForm;
|
||||
use Thelia\Form\Area\AreaModificationForm;
|
||||
use Thelia\Model\AreaQuery;
|
||||
|
||||
/**
|
||||
* Class AreaController
|
||||
@@ -34,18 +38,6 @@ use Thelia\Form\Area\AreaCreateForm;
|
||||
*/
|
||||
class AreaController extends AbstractCrudController
|
||||
{
|
||||
/* public function indexAction()
|
||||
{
|
||||
if (null !== $response = $this->checkAuth("admin.shipping-configuration.view")) return $response;
|
||||
return $this->render("shipping-configuration", array("display_shipping_configuration" => 20));
|
||||
}
|
||||
|
||||
public function updateAction($shipping_configuration_id)
|
||||
{
|
||||
return $this->render("shipping-configuration-edit", array(
|
||||
"shipping_configuration_id" => $shipping_configuration_id
|
||||
));
|
||||
}*/
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -65,6 +57,11 @@ class AreaController extends AbstractCrudController
|
||||
);
|
||||
}
|
||||
|
||||
protected function getAreaId()
|
||||
{
|
||||
return $this->getRequest()->get('area_id', 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the creation form for this object
|
||||
*/
|
||||
@@ -78,7 +75,7 @@ class AreaController extends AbstractCrudController
|
||||
*/
|
||||
protected function getUpdateForm()
|
||||
{
|
||||
return new AreaCreateForm($this->getRequest());
|
||||
return new AreaModificationForm($this->getRequest());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +85,11 @@ class AreaController extends AbstractCrudController
|
||||
*/
|
||||
protected function hydrateObjectForm($object)
|
||||
{
|
||||
// TODO: Implement hydrateObjectForm() method.
|
||||
$data = array(
|
||||
'name' => $object->getName()
|
||||
);
|
||||
|
||||
return new AreaModificationForm($this->getRequest(), 'form', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,27 +130,27 @@ class AreaController extends AbstractCrudController
|
||||
*/
|
||||
protected function getDeleteEvent()
|
||||
{
|
||||
// TODO: Implement getDeleteEvent() method.
|
||||
return new AreaDeleteEvent($this->getAreaId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the event contains the object, e.g. the action has updated the object in the event.
|
||||
*
|
||||
* @param unknown $event
|
||||
* @param \Thelia\Core\Event\Area\AreaEvent $event
|
||||
*/
|
||||
protected function eventContainsObject($event)
|
||||
{
|
||||
// TODO: Implement eventContainsObject() method.
|
||||
return $event->hasArea();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the created object from an event.
|
||||
*
|
||||
* @param unknown $createEvent
|
||||
* @param \Thelia\Core\Event\Area\AreaEvent $event
|
||||
*/
|
||||
protected function getObjectFromEvent($event)
|
||||
{
|
||||
// TODO: Implement getObjectFromEvent() method.
|
||||
return $event->getArea();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,27 +158,27 @@ class AreaController extends AbstractCrudController
|
||||
*/
|
||||
protected function getExistingObject()
|
||||
{
|
||||
// TODO: Implement getExistingObject() method.
|
||||
return AreaQuery::create()->findPk($this->getAreaId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object label form the object event (name, title, etc.)
|
||||
*
|
||||
* @param unknown $object
|
||||
* @param \Thelia\Model\Area $object
|
||||
*/
|
||||
protected function getObjectLabel($object)
|
||||
{
|
||||
// TODO: Implement getObjectLabel() method.
|
||||
return $object->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object ID from the object
|
||||
*
|
||||
* @param unknown $object
|
||||
* @param \Thelia\Model\Area $object
|
||||
*/
|
||||
protected function getObjectId($object)
|
||||
{
|
||||
// TODO: Implement getObjectId() method.
|
||||
return $object->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -195,7 +196,9 @@ class AreaController extends AbstractCrudController
|
||||
*/
|
||||
protected function renderEditionTemplate()
|
||||
{
|
||||
// TODO: Implement renderEditionTemplate() method.
|
||||
return $this->render('shipping-configuration-edit',array(
|
||||
'area_id' => $this->getAreaId()
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,7 +206,10 @@ class AreaController extends AbstractCrudController
|
||||
*/
|
||||
protected function redirectToEditionTemplate()
|
||||
{
|
||||
// TODO: Implement redirectToEditionTemplate() method.
|
||||
$this->redirectToRoute('admin.configuration.shipping-configuration.update.view', array(), array(
|
||||
"area_id" => $this->getAreaId()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,6 +217,6 @@ class AreaController extends AbstractCrudController
|
||||
*/
|
||||
protected function redirectToListTemplate()
|
||||
{
|
||||
return $this->render("shipping-configuration");
|
||||
$this->redirectToRoute('admin.configuration.shipping-configuration.default');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,24 +39,6 @@ use Thelia\Model\CountryQuery;
|
||||
class CountryController extends AbstractCrudController
|
||||
{
|
||||
|
||||
/**
|
||||
* @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 $viewPermissionIdentifier the 'view' permission identifier. Example: "admin.configuration.message.view"
|
||||
* @param string $createPermissionIdentifier the 'create' permission identifier. Example: "admin.configuration.message.create"
|
||||
* @param string $updatePermissionIdentifier the 'update' permission identifier. Example: "admin.configuration.message.update"
|
||||
* @param string $deletePermissionIdentifier the 'delete' permission identifier. Example: "admin.configuration.message.delete"
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(
|
||||
|
||||
270
core/lib/Thelia/Controller/Admin/TaxRuleController.php
Normal file
270
core/lib/Thelia/Controller/Admin/TaxRuleController.php
Normal file
@@ -0,0 +1,270 @@
|
||||
<?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\Core\Event\Tax\TaxRuleEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Form\TaxRuleCreationForm;
|
||||
use Thelia\Form\TaxRuleModificationForm;
|
||||
use Thelia\Form\TaxRuleTaxListUpdateForm;
|
||||
use Thelia\Model\CountryQuery;
|
||||
use Thelia\Model\TaxRuleQuery;
|
||||
|
||||
class TaxRuleController extends AbstractCrudController
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(
|
||||
'taxrule',
|
||||
'manual',
|
||||
'order',
|
||||
|
||||
'admin.configuration.taxrule.view',
|
||||
'admin.configuration.taxrule.create',
|
||||
'admin.configuration.taxrule.update',
|
||||
'admin.configuration.taxrule.delete',
|
||||
|
||||
TheliaEvents::TAX_RULE_CREATE,
|
||||
TheliaEvents::TAX_RULE_UPDATE,
|
||||
TheliaEvents::TAX_RULE_DELETE
|
||||
);
|
||||
}
|
||||
|
||||
protected function getCreationForm()
|
||||
{
|
||||
return new TaxRuleCreationForm($this->getRequest());
|
||||
}
|
||||
|
||||
protected function getUpdateForm()
|
||||
{
|
||||
return new TaxRuleModificationForm($this->getRequest());
|
||||
}
|
||||
|
||||
protected function getCreationEvent($formData)
|
||||
{
|
||||
$event = new TaxRuleEvent();
|
||||
|
||||
/* @todo fill event */
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
protected function getUpdateEvent($formData)
|
||||
{
|
||||
$event = new TaxRuleEvent();
|
||||
|
||||
$event->setLocale($formData['locale']);
|
||||
$event->setId($formData['id']);
|
||||
$event->setTitle($formData['title']);
|
||||
$event->setDescription($formData['description']);
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
protected function getUpdateTaxListEvent($formData)
|
||||
{
|
||||
$event = new TaxRuleEvent();
|
||||
|
||||
$event->setId($formData['id']);
|
||||
$event->setTaxList($formData['tax_list']);
|
||||
$event->setCountryList($formData['country_list']);
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
protected function getDeleteEvent()
|
||||
{
|
||||
$event = new TaxRuleEvent();
|
||||
|
||||
$event->setId(
|
||||
$this->getRequest()->get('tax_rule_id', 0)
|
||||
);
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
protected function eventContainsObject($event)
|
||||
{
|
||||
return $event->hasTaxRule();
|
||||
}
|
||||
|
||||
protected function hydrateObjectForm($object)
|
||||
{
|
||||
$data = array(
|
||||
'id' => $object->getId(),
|
||||
'locale' => $object->getLocale(),
|
||||
'title' => $object->getTitle(),
|
||||
'description' => $object->getDescription(),
|
||||
);
|
||||
|
||||
// Setup the object form
|
||||
return new TaxRuleModificationForm($this->getRequest(), "form", $data);
|
||||
}
|
||||
|
||||
protected function hydrateTaxUpdateForm($object)
|
||||
{
|
||||
$data = array(
|
||||
'id' => $object->getId(),
|
||||
);
|
||||
|
||||
// Setup the object form
|
||||
return new TaxRuleTaxListUpdateForm($this->getRequest(), "form", $data);
|
||||
}
|
||||
|
||||
protected function getObjectFromEvent($event)
|
||||
{
|
||||
return $event->hasTaxRule() ? $event->getTaxRule() : null;
|
||||
}
|
||||
|
||||
protected function getExistingObject()
|
||||
{
|
||||
return TaxRuleQuery::create()
|
||||
->joinWithI18n($this->getCurrentEditionLocale())
|
||||
->findOneById($this->getRequest()->get('tax_rule_id'));
|
||||
}
|
||||
|
||||
protected function getObjectLabel($object)
|
||||
{
|
||||
return $object->getTitle();
|
||||
}
|
||||
|
||||
protected function getObjectId($object)
|
||||
{
|
||||
return $object->getId();
|
||||
}
|
||||
|
||||
protected function getViewArguments($country = null)
|
||||
{
|
||||
return array(
|
||||
'tab' => $this->getRequest()->get('tab', 'data'),
|
||||
'country' => $country === null ? $this->getRequest()->get('country', CountryQuery::create()->findOneByByDefault(1)->getId()) : $country,
|
||||
);
|
||||
}
|
||||
|
||||
protected function getRouteArguments()
|
||||
{
|
||||
return array(
|
||||
'tax_rule_id' => $this->getRequest()->get('tax_rule_id'),
|
||||
);
|
||||
}
|
||||
|
||||
protected function renderListTemplate($currentOrder)
|
||||
{
|
||||
// We always return to the feature edition form
|
||||
return $this->render(
|
||||
'taxes-rules',
|
||||
array()
|
||||
);
|
||||
}
|
||||
|
||||
protected function renderEditionTemplate()
|
||||
{
|
||||
// We always return to the feature edition form
|
||||
return $this->render('tax-rule-edit', array_merge($this->getViewArguments(), $this->getRouteArguments()));
|
||||
}
|
||||
|
||||
protected function redirectToEditionTemplate($request = null, $country = null)
|
||||
{
|
||||
// We always return to the feature edition form
|
||||
$this->redirectToRoute(
|
||||
"admin.configuration.taxes-rules.update",
|
||||
$this->getViewArguments($country),
|
||||
$this->getRouteArguments()
|
||||
);
|
||||
}
|
||||
|
||||
protected function redirectToListTemplate()
|
||||
{
|
||||
$this->redirectToRoute(
|
||||
"admin.configuration.taxes-rules.list"
|
||||
);
|
||||
}
|
||||
|
||||
public function updateAction()
|
||||
{
|
||||
if (null !== $response = $this->checkAuth($this->updatePermissionIdentifier)) return $response;
|
||||
|
||||
$object = $this->getExistingObject();
|
||||
|
||||
if ($object != null) {
|
||||
|
||||
// Hydrate the form abd pass it to the parser
|
||||
$changeTaxesForm = $this->hydrateTaxUpdateForm($object);
|
||||
|
||||
// Pass it to the parser
|
||||
$this->getParserContext()->addForm($changeTaxesForm);
|
||||
}
|
||||
|
||||
return parent::updateAction();
|
||||
}
|
||||
|
||||
public function processUpdateTaxesAction()
|
||||
{
|
||||
// Check current user authorization
|
||||
if (null !== $response = $this->checkAuth('admin.configuration.taxrule.update')) return $response;
|
||||
|
||||
$error_msg = false;
|
||||
|
||||
// Create the form from the request
|
||||
$changeForm = new TaxRuleTaxListUpdateForm($this->getRequest());
|
||||
|
||||
try {
|
||||
// Check the form against constraints violations
|
||||
$form = $this->validateForm($changeForm, "POST");
|
||||
|
||||
// Get the form field values
|
||||
$data = $form->getData();
|
||||
|
||||
$changeEvent = $this->getUpdateTaxListEvent($data);
|
||||
|
||||
$this->dispatch(TheliaEvents::TAX_RULE_TAXES_UPDATE, $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 ($response == null) {
|
||||
$this->redirectToEditionTemplate($this->getRequest(), isset($data['country_list'][0]) ? $data['country_list'][0] : null);
|
||||
} else {
|
||||
return $response;
|
||||
}
|
||||
} 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' => 'taxrule')), $error_msg, $changeForm, $ex);
|
||||
|
||||
// At this point, the form has errors, and should be redisplayed.
|
||||
return $this->renderEditionTemplate();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user