From 54db55467be4d2f819b9474dbf2dccc338c97cdc Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Thu, 23 Jan 2014 09:33:09 +0100 Subject: [PATCH] Fixed injection of TaxManager in TaxCreationForm instance --- .../Thelia/Controller/Admin/TaxController.php | 7 +++--- .../Controller/Admin/TaxRuleController.php | 17 +++++++++++++ .../Thelia/Core/Template/ParserContext.php | 2 +- .../Core/Template/Smarty/Plugins/Form.php | 25 +++++++++---------- core/lib/Thelia/Core/Thelia.php | 2 +- core/lib/Thelia/Form/TaxCreationForm.php | 16 +++++++++--- 6 files changed, 47 insertions(+), 22 deletions(-) diff --git a/core/lib/Thelia/Controller/Admin/TaxController.php b/core/lib/Thelia/Controller/Admin/TaxController.php index 69824a3cd..e89ffba9c 100644 --- a/core/lib/Thelia/Controller/Admin/TaxController.php +++ b/core/lib/Thelia/Controller/Admin/TaxController.php @@ -50,14 +50,14 @@ class TaxController extends AbstractCrudController protected function getCreationForm() { - $form = new TaxCreationForm($this->getRequest()); + $form = new TaxCreationForm($this->getRequest(), 'form', array(), array(), $this->container->get('thelia.taxEngine')); return $form; } protected function getUpdateForm() { - return new TaxModificationForm($this->getRequest()); + return new TaxModificationForm($this->getRequest(), 'form', array(), array(), $this->container->get('thelia.taxEngine')); } protected function getCreationEvent($formData) @@ -114,7 +114,7 @@ class TaxController extends AbstractCrudController ); // Setup the object form - return new TaxModificationForm($this->getRequest(), "form", $data); + return new TaxModificationForm($this->getRequest(), "form", $data, array(), $this->container->get('thelia.taxEngine')); } protected function getObjectFromEvent($event) @@ -158,7 +158,6 @@ class TaxController extends AbstractCrudController protected function renderListTemplate($currentOrder) { - // We always return to the feature edition form return $this->render( 'taxes-rules', array() diff --git a/core/lib/Thelia/Controller/Admin/TaxRuleController.php b/core/lib/Thelia/Controller/Admin/TaxRuleController.php index 0f64c3ccb..513f5859a 100644 --- a/core/lib/Thelia/Controller/Admin/TaxRuleController.php +++ b/core/lib/Thelia/Controller/Admin/TaxRuleController.php @@ -32,6 +32,7 @@ use Thelia\Form\TaxRuleModificationForm; use Thelia\Form\TaxRuleTaxListUpdateForm; use Thelia\Model\CountryQuery; use Thelia\Model\TaxRuleQuery; +use Thelia\Form\TaxCreationForm; class TaxRuleController extends AbstractCrudController { @@ -50,6 +51,22 @@ class TaxRuleController extends AbstractCrudController ); } + public function defaultAction() { + + // In the tax rule template we use the TaxCreationForm. + // + // The TaxCreationForm require the TaxEngine, but we cannot pass it from the Parser Form plugin, + // as the container is not passed to forms by this plugin. + // + // So we create an instance of TaxCreationForm here (we have the container), and put it in the ParserContext. + // This way, the Form plugin will use this instance, instead on creating it. + $taxCreationForm = new TaxCreationForm($this->getRequest(), 'form', array(), array(), $this->container->get('thelia.taxEngine')); + + $this->getParserContext()->addForm($taxCreationForm); + + return parent::defaultAction(); + } + protected function getCreationForm() { return new TaxRuleCreationForm($this->getRequest()); diff --git a/core/lib/Thelia/Core/Template/ParserContext.php b/core/lib/Thelia/Core/Template/ParserContext.php index 1f80f7cd5..9e676da89 100755 --- a/core/lib/Thelia/Core/Template/ParserContext.php +++ b/core/lib/Thelia/Core/Template/ParserContext.php @@ -50,7 +50,7 @@ class ParserContext implements \IteratorAggregate */ public function addForm(BaseForm $form) { - $this->set($form->getName(), $form); + $this->set(get_class($form)/*$form->getName()*/, $form); return $this; } diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php index ee49fe1ec..9a1279da9 100755 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php @@ -97,15 +97,25 @@ class Form extends AbstractSmartyPlugin throw new \InvalidArgumentException("Missing 'name' parameter in form arguments"); } - $instance = $this->createInstance($name); + if (!isset($this->formDefinition[$name])) { + throw new ElementNotFoundException(sprintf("%s form does not exists", $name)); + } + + $formClass = $this->formDefinition[$name]; // Check if parser context contains our form - $form = $this->parserContext->getForm($instance->getName()); + $form = $this->parserContext->getForm($formClass); if (null != $form) { // Re-use the form $instance = $form; } + else { + // Create a new one + $class = new \ReflectionClass($formClass); + + $instance = $class->newInstance($this->request, "form"); + } $instance->createView(); @@ -406,17 +416,6 @@ class Form extends AbstractSmartyPlugin return $instance; } - protected function createInstance($name) - { - if (!isset($this->formDefinition[$name])) { - throw new ElementNotFoundException(sprintf("%s form does not exists", $name)); - } - - $class = new \ReflectionClass($this->formDefinition[$name]); - - return $class->newInstance($this->request, "form"); - } - /** * @return an array of SmartyPluginDescriptor */ diff --git a/core/lib/Thelia/Core/Thelia.php b/core/lib/Thelia/Core/Thelia.php index 844eb3d29..8cb1271d3 100755 --- a/core/lib/Thelia/Core/Thelia.php +++ b/core/lib/Thelia/Core/Thelia.php @@ -87,7 +87,7 @@ class Thelia extends Kernel $con->setAttribute(ConnectionWrapper::PROPEL_ATTR_CACHE_PREPARES, true); if ($this->isDebug()) { $serviceContainer->setLogger('defaultLogger', \Thelia\Log\Tlog::getInstance()); - $con->useDebug(true); + //$con->useDebug(true); } } diff --git a/core/lib/Thelia/Form/TaxCreationForm.php b/core/lib/Thelia/Form/TaxCreationForm.php index d92f67ba1..964766779 100644 --- a/core/lib/Thelia/Form/TaxCreationForm.php +++ b/core/lib/Thelia/Form/TaxCreationForm.php @@ -40,12 +40,22 @@ class TaxCreationForm extends BaseForm { use StandardDescriptionFieldsTrait; + protected $taxEngine = null; + + public function __construct(Request $request, $type= "form", $data = array(), $options = array(), TaxEngine $taxEngine = null) { + + $this->taxEngine = $taxEngine; + + parent::__construct($request, $type, $data, $options); + } + protected function buildForm($change_mode = false) { - // FIXME : SHOULD be extracted from the container - $taxEngine = new TaxEngine($this->getRequest()); + if ($this->taxEngine == null) { + throw new \LogicException("The TaxEngine should be passed to this form before using it."); + } - $types = $taxEngine->getTaxTypeList(); + $types = $this->taxEngine->getTaxTypeList(); $typeList = array(); $requirementList = array();