Fixed injection of TaxManager in TaxCreationForm instance

This commit is contained in:
Franck Allimant
2014-01-23 09:33:09 +01:00
parent 9d1c1e3979
commit 54db55467b
6 changed files with 47 additions and 22 deletions

View File

@@ -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()

View File

@@ -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());

View File

@@ -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;
}

View File

@@ -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
*/

View File

@@ -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);
}
}

View File

@@ -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();