tax management

This commit is contained in:
Etienne Roudeix
2013-10-16 11:46:30 +02:00
parent 085c2fa75c
commit 9365e9ee38
24 changed files with 393 additions and 12 deletions

View File

@@ -0,0 +1,45 @@
<?php
namespace Thelia\Core\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class TheliaType extends AbstractType
{
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'instance' => false,
'type' => false,
'options' => false,
));
$resolver->setAllowedTypes(array(
'instance' => array('Thelia\Type\TypeInterface'),
));
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars = array_replace($view->vars, array(
'instance' => $options['instance'],
'type' => $options['type'],
'options' => $options['options'],
));
}
public function getParent()
{
return 'form';
}
public function getName()
{
return 'thelia_type';
}
}

View File

@@ -254,7 +254,7 @@ abstract class BaseLoop
*
* @param $pagination
*
* @return mixed
* @return LoopResult
*/
abstract public function exec(&$pagination);

View File

@@ -22,7 +22,11 @@
/*************************************************************************************/
namespace Thelia\Core\Template\Smarty\Plugins;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
use Symfony\Component\Form\FormView;
use Thelia\Core\Form\Type\TheliaType;
use Thelia\Form\BaseForm;
use Thelia\Core\Template\Element\Exception\ElementNotFoundException;
use Symfony\Component\HttpFoundation\Request;
@@ -56,6 +60,8 @@ use Thelia\Core\Template\ParserContext;
*/
class Form extends AbstractSmartyPlugin
{
static private $taggedFieldsStack = null;
static private $taggedFieldsStackPosition = null;
protected $request;
protected $parserContext;
@@ -118,11 +124,16 @@ class Form extends AbstractSmartyPlugin
$template->assign("value", $fieldValue);
$template->assign("options", $formFieldView->vars);
// If Checkbox input type
if ($fieldVars['checked'] !== null) {
$this->renderFormFieldCheckBox($template, $formFieldView['checked']);
}
//data
$template->assign("data", $fieldVars['data']);
$template->assign("label", $fieldVars["label"]);
$template->assign("label_attr", $fieldVars["label_attr"]);
@@ -143,18 +154,51 @@ class Form extends AbstractSmartyPlugin
$template->assign("attr", implode(" ", $attr));
}
protected function assignFormTypeValues($template, $formFieldConfig, $formFieldView)
{
$formFieldType = $formFieldConfig->getType()->getInnerType();
/* access to choices */
if($formFieldType instanceof ChoiceType) {
$template->assign("choices", $formFieldView->vars['choices']);
}
/* access to collections */
if($formFieldType instanceof CollectionType) {
if( true === $formFieldConfig->getOption('prototype') ) {
} else {
}
}
/* access to thelia type */
if($formFieldType instanceof TheliaType) {
$template->assign("formType", $formFieldView->vars['type']);
switch($formFieldView->vars['type']) {
case "choice":
if(!isset($formFieldView->vars['options']['choices']) || !is_array($formFieldView->vars['options']['choices'])) {
//throw new
}
$choices = array();
foreach($formFieldView->vars['options']['choices'] as $value => $choice) {
$choices[] = new ChoiceView($value, $value, $choice);
}
$template->assign("choices", $choices);
break;
}
}
}
public function renderFormField($params, $content, \Smarty_Internal_Template $template, &$repeat)
{
if ($repeat) {
if ($repeat) {
$formFieldView = $this->getFormFieldView($params);
$formFieldView = $this->getFormFieldView($params);
$formFieldConfig = $this->getFormFieldConfig($params);
$template->assign("options", $formFieldView->vars);
/* access to choices */
if(isset($formFieldView->vars['choices'])) {
$template->assign("choices", $formFieldView->vars['choices']);
}
$this->assignFormTypeValues($template, $formFieldConfig, $formFieldView);
$value = $formFieldView->vars["value"];
/* FIXME: doesnt work. We got "This form should not contain extra fields." error.
@@ -185,6 +229,38 @@ $this->assignFieldValues($template, $formFieldView->vars["full_name"], $fieldVar
}
}
public function renderTaggedFormFields($params, $content, \Smarty_Internal_Template $template, &$repeat)
{
if(null === $content) {
self::$taggedFieldsStack = $this->getFormFieldsFromTag($params);
self::$taggedFieldsStackPosition = 0;
} else {
self::$taggedFieldsStackPosition++;
}
if(isset(self::$taggedFieldsStack[self::$taggedFieldsStackPosition])) {
$this->assignFieldValues(
$template,
self::$taggedFieldsStack[self::$taggedFieldsStackPosition]['view']->vars["full_name"],
self::$taggedFieldsStack[self::$taggedFieldsStackPosition]['view']->vars["value"],
self::$taggedFieldsStack[self::$taggedFieldsStackPosition]['view']->vars
);
$this->assignFormTypeValues($template, self::$taggedFieldsStack[self::$taggedFieldsStackPosition]['config'], self::$taggedFieldsStack[self::$taggedFieldsStackPosition]['view']);
self::$taggedFieldsStack[self::$taggedFieldsStackPosition]['view']->setRendered();
$repeat = true;
}
if (! $repeat) {
self::$taggedFieldsStack = null;
self::$taggedFieldsStackPosition = null;
}
return $content;
}
public function renderHiddenFormField($params, \Smarty_Internal_Template $template)
{
$attrFormat = '%s="%s"';
@@ -266,6 +342,48 @@ $this->assignFieldValues($template, $formFieldView->vars["full_name"], $fieldVar
return $instance->getView()[$fieldName];
}
protected function getFormFieldsFromTag($params)
{
$instance = $this->getInstanceFromParams($params);
$tag = $this->getParam($params, 'tag');
if (null == $tag)
throw new \InvalidArgumentException("'tag' parameter is missing");
$viewList = array();
foreach($instance->getView() as $view) {
if(isset($view->vars['attr']['tag']) && $tag == $view->vars['attr']['tag']) {
$fieldData = $instance->getForm()->all()[$view->vars['name']];
$viewList[] = array(
'view' => $view,
'config' => $fieldData->getConfig(),
);
}
}
return $viewList;
}
protected function getFormFieldConfig($params)
{
$instance = $this->getInstanceFromParams($params);
$fieldName = $this->getParam($params, 'field');
if (null == $fieldName) {
throw new \InvalidArgumentException("'field' parameter is missing");
}
$fieldData = $instance->getForm()->all()[$fieldName];
if (empty( $fieldData )) {
throw new \InvalidArgumentException(sprintf("Field name '%s' not found in form %s children", $fieldName, $instance->getName()));
}
return $fieldData->getConfig();
}
protected function getInstanceFromParams($params)
{
$instance = $this->getParam($params, 'form');
@@ -304,6 +422,7 @@ $this->assignFieldValues($template, $formFieldView->vars["full_name"], $fieldVar
return array(
new SmartyPluginDescriptor("block", "form", $this, "generateForm"),
new SmartyPluginDescriptor("block", "form_field", $this, "renderFormField"),
new SmartyPluginDescriptor("block", "form_tagged_fields", $this, "renderTaggedFormFields"),
new SmartyPluginDescriptor("function", "form_hidden_fields", $this, "renderHiddenFormField"),
new SmartyPluginDescriptor("function", "form_enctype", $this, "formEnctype"),
new SmartyPluginDescriptor("block", "form_error", $this, "formError")

View File

@@ -300,7 +300,7 @@ class TheliaLoop extends AbstractSmartyPlugin
/**
* @param $smartyParams
*
* @return object
* @return BaseLoop
* @throws \Thelia\Core\Template\Element\Exception\InvalidElementException
* @throws \Thelia\Core\Template\Element\Exception\ElementNotFoundException
*/

View File

@@ -24,6 +24,7 @@ namespace Thelia\Form;
use Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraints\NotBlank;
use Thelia\Core\Form\Type\TheliaType;
use Thelia\Core\Translation\Translator;
use Thelia\TaxEngine\TaxEngine;
use Thelia\TaxEngine\TaxType;
@@ -36,10 +37,12 @@ class TaxCreationForm extends BaseForm
{
$types = TaxEngine::getInstance()->getTaxTypeList();
$typeList = array();
$requirementList = array();
foreach($types as $type) {
$classPath = "\\Thelia\\TaxEngine\\TaxType\\$type";
$instance = new $classPath();
$typeList[$type] = $instance->getTitle();
$requirementList[$type] = $instance->getRequirementsList();
}
$this->formBuilder
@@ -57,6 +60,26 @@ class TaxCreationForm extends BaseForm
))
;
foreach($requirementList as $type => $requirements) {
foreach($requirements as $name => $requirementType) {
$this->formBuilder
->add($type . '_' . $name, new TheliaType(), array(
"instance" => $requirementType,
"constraints" => array(new NotBlank()),
"attr" => array(
"tag" => "requirements",
),
"data" => array(
"tax_type" => $type,
),
"label" => Translator::getInstance()->trans($name),
"type" => $requirementType->getFormType(),
"options" => $requirementType->getFormOptions(),
))
;
}
}
$this->addStandardDescFields(array('postscriptum', 'chapo', 'locale'));
}

View File

@@ -45,7 +45,7 @@ class featureSlicePercentTaxType extends BaseTaxType
public function getRequirementsList()
{
return array(
'featureId' => new ModelValidIdType('Currency'),
'featureId' => new ModelValidIdType('Feature'),
'slices' => new FloatToFloatArrayType(),
);
}

View File

@@ -50,4 +50,14 @@ class AlphaNumStringListType implements TypeInterface
{
return $this->isValid($values) ? explode(',', $values) : null;
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -44,4 +44,14 @@ class AlphaNumStringType implements TypeInterface
{
return $this->isValid($value) ? $value : null;
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -44,4 +44,14 @@ class AnyType implements TypeInterface
{
return $value;
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -49,4 +49,14 @@ class BooleanOrBothType implements TypeInterface
if ($value === self::ANY) return $value;
return $value === null ? null : filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -44,4 +44,14 @@ class BooleanType implements TypeInterface
{
return $value === null ? null : filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -69,4 +69,14 @@ class EnumListType implements TypeInterface
{
return in_array($value, $this->values);
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -52,4 +52,14 @@ class EnumType implements TypeInterface
{
return $this->isValid($value) ? $value : null;
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -54,4 +54,14 @@ class FloatToFloatArrayType implements TypeInterface
{
return $this->isValid($value) ? $value : null;
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -44,4 +44,14 @@ class FloatType implements TypeInterface
{
return $this->isValid($value) ? $value : null;
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -50,4 +50,14 @@ class IntListType implements TypeInterface
{
return $this->isValid($values) ? explode(',', $values) : null;
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -124,4 +124,14 @@ class IntToCombinedIntsListType implements TypeInterface
return true;
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -124,4 +124,14 @@ class IntToCombinedStringsListType implements TypeInterface
return true;
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -44,4 +44,14 @@ class IntType implements TypeInterface
{
return $this->isValid($value) ? $value : null;
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -46,4 +46,14 @@ class JsonType implements TypeInterface
{
return $this->isValid($value) ? json_decode($value, true) : null;
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -63,4 +63,14 @@ class ModelType implements TypeInterface
{
return $this->isValid($value) ? $value : null;
}
public function getFormType()
{
return 'text';
}
public function getFormOptions()
{
return array();
}
}

View File

@@ -67,4 +67,23 @@ class ModelValidIdType implements TypeInterface
return $this->isValid($value) ? $queryClass::create()->findPk($value) : null;
}
public function getFormType()
{
return 'choice';
}
public function getFormOptions()
{
$queryClass = $this->expectedModelActiveRecordQuery;
$choices = array();
foreach($queryClass::create()->find() as $item) {
$choices[$item->getId()] = method_exists($item, "getTitle") ? $item->getTitle() : $item->getId();
}
return array(
"choices" => $choices,
);
}
}

View File

@@ -35,4 +35,7 @@ interface TypeInterface
public function isValid($value);
public function getFormattedValue($value);
public function getFormType();
public function getFormOptions();
}