tax rule edition

This commit is contained in:
Etienne Roudeix
2013-10-11 11:09:23 +02:00
parent c1a8c27dee
commit 763026700d
11 changed files with 290 additions and 92 deletions

View File

@@ -27,6 +27,7 @@ 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;
@@ -71,9 +72,7 @@ class TaxRuleController extends AbstractCrudController
protected function getUpdateEvent($formData)
{
$event = new TaxRuleEvent(
TaxRuleQuery::create()->findPk($formData['id'])
);
$event = new TaxRuleEvent();
$event->setLocale($formData['locale']);
$event->setId($formData['id']);
@@ -83,6 +82,17 @@ class TaxRuleController extends AbstractCrudController
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();
@@ -112,6 +122,16 @@ class TaxRuleController extends AbstractCrudController
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;
@@ -134,11 +154,11 @@ class TaxRuleController extends AbstractCrudController
return $object->getId();
}
protected function getViewArguments()
protected function getViewArguments($country = null)
{
return array(
'tab' => $this->getRequest()->get('tab', 'data'),
'country' => $this->getRequest()->get('country', CountryQuery::create()->findOneByByDefault(1)->getIsoalpha3()),
'country' => $country === null ? $this->getRequest()->get('country', CountryQuery::create()->findOneByByDefault(1)->getId()) : $country,
);
}
@@ -164,12 +184,12 @@ class TaxRuleController extends AbstractCrudController
return $this->render('tax-rule-edit', array_merge($this->getViewArguments(), $this->getRouteArguments()));
}
protected function redirectToEditionTemplate()
protected function redirectToEditionTemplate($request = null, $country = null)
{
// We always return to the feature edition form
$this->redirectToRoute(
"admin.configuration.taxes-rules.update",
$this->getViewArguments(),
$this->getViewArguments($country),
$this->getRouteArguments()
);
}
@@ -181,4 +201,70 @@ class TaxRuleController extends AbstractCrudController
);
}
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();
}
}