diff --git a/src/CL/CustomFieldsBundle/CLCustomFieldsBundle.php b/src/CL/CustomFieldsBundle/CLCustomFieldsBundle.php index a1d19190e..d731128b5 100644 --- a/src/CL/CustomFieldsBundle/CLCustomFieldsBundle.php +++ b/src/CL/CustomFieldsBundle/CLCustomFieldsBundle.php @@ -3,7 +3,13 @@ namespace CL\CustomFieldsBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; +use CL\CustomFieldsBundle\DependencyInjection\CustomFieldCompilerPass; class CLCustomFieldsBundle extends Bundle { + public function build(\Symfony\Component\DependencyInjection\ContainerBuilder $container) + { + parent::build($container); + $container->addCompilerPass(new CustomFieldCompilerPass()); + } } diff --git a/src/CL/CustomFieldsBundle/Controller/CustomFieldController.php b/src/CL/CustomFieldsBundle/Controller/CustomFieldController.php index 3df531544..d13813364 100644 --- a/src/CL/CustomFieldsBundle/Controller/CustomFieldController.php +++ b/src/CL/CustomFieldsBundle/Controller/CustomFieldController.php @@ -62,7 +62,7 @@ class CustomFieldController extends Controller */ private function createCreateForm(CustomField $entity) { - $form = $this->createForm(new CustomFieldType(), $entity, array( + $form = $this->createForm('custom_field_choice', $entity, array( 'action' => $this->generateUrl('customfield_create'), 'method' => 'POST', )); @@ -142,7 +142,7 @@ class CustomFieldController extends Controller */ private function createEditForm(CustomField $entity) { - $form = $this->createForm(new CustomFieldType(), $entity, array( + $form = $this->createForm('custom_field_choice', $entity, array( 'action' => $this->generateUrl('customfield_update', array('id' => $entity->getId())), 'method' => 'PUT', )); diff --git a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldAddress.php b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldAddress.php new file mode 100644 index 000000000..e987a80c1 --- /dev/null +++ b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldAddress.php @@ -0,0 +1,69 @@ + + */ +class CustomFieldAddress implements CustomFieldInterface +{ + + public $om; + + public function __construct(EntityManagerInterface $om) + { + $this->om = $om; + } + + public function buildFormType(FormBuilderInterface $builder, CustomField $customField) + { + switch ($customField->getRelation()) + { + case CustomField::ONE_TO_ONE : + $builder->build( + $builder->create($customField->getSlug(), + new AddressType() + ) + ); + break; + case CustomField::ONE_TO_MANY : + $builder->build( + $builder->create($customField->getSlug(), + new AddressType(), + array( + 'multiple' => true + )) + ); + break; + } + } + + public function getName() + { + return 'CF address'; + } + + public function render($value, CustomField $customField) + { + + } + + public function transformFromEntity($value, CustomField $customField) + { + + } + + public function transformToEntity($value, CustomField $customField) + { + + } + +} diff --git a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldChoiceWithOther.php b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldChoiceWithOther.php new file mode 100644 index 000000000..9b0085f2f --- /dev/null +++ b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldChoiceWithOther.php @@ -0,0 +1,19 @@ + + */ +class CustomFieldChoiceWithOther +{ + //put your code here +} diff --git a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldCompiler.php b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldCompiler.php new file mode 100644 index 000000000..562747e48 --- /dev/null +++ b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldCompiler.php @@ -0,0 +1,54 @@ + + */ +class CustomFieldCompiler implements ContainerAwareInterface +{ + private $servicesByType = array(); + + /** + * + * @var \Symfony\Component\DependencyInjection\ContainerInterface + */ + private $container; + + public function addCustomField($serviceName, $type) + { + $this->servicesByType[$type] = $serviceName; + } + + /** + * + * @param string $type + * @return CustomFieldInterface + */ + public function getCustomFieldByType($type) + { + if (isset($this->servicesByType[$type])) { + return $this->servicesByType[$type]; + } else { + throw new \LogicException('the custom field with type '.$type.' ' + . 'is not found'); + } + } + + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } + + public function getAllFields() + { + return $this->servicesByType; + } + +} diff --git a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldInterface.php b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldInterface.php new file mode 100644 index 000000000..0ce07b564 --- /dev/null +++ b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldInterface.php @@ -0,0 +1,45 @@ + + */ +interface CustomFieldInterface +{ + + /** + * + * @param \CL\CustomFieldsBundle\CustomField\FormBuilderInterface $builder + * @param \CL\CustomFieldsBundle\CustomField\CustomField $customField + * @return \Symfony\Component\Form\FormTypeInterface the form type + */ + public function buildFormType(FormBuilderInterface $builder, CustomField $customField); + + /** + * + * @param type $value + * @param \CL\CustomFieldsBundle\CustomField\CustomField $customField + */ + public function transformToEntity($value, CustomField $customField); + + /** + * + * @param type $value + * @param \CL\CustomFieldsBundle\CustomField\CustomField $customField + */ + public function transformFromEntity($value, CustomField $customField); + + /** + * + * @param type $value + * @param \CL\CustomFieldsBundle\CustomField\CustomField $customField + */ + public function render($value, CustomField $customField); + + public function getName(); +} diff --git a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldText.php b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldText.php new file mode 100644 index 000000000..348a6d76c --- /dev/null +++ b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldText.php @@ -0,0 +1,45 @@ + + */ +class CustomFieldText implements CustomFieldInterface +{ + public function buildFormType(FormBuilderInterface $builder, CustomField $customField) + { + $builder->add($customField->getSlug(), 'text', array( + 'label' => $customField->getLabel() + )); + } + + public function render($value, CustomField $customField) + { + + } + + public function transformFromEntity($value, CustomField $customField) + { + return $value; + } + + public function transformToEntity($value, CustomField $customField) + { + return $value; + } + + public function getName() + { + return 'text field'; + } + +} diff --git a/src/CL/CustomFieldsBundle/DependencyInjection/CustomFieldCompilerPass.php b/src/CL/CustomFieldsBundle/DependencyInjection/CustomFieldCompilerPass.php new file mode 100644 index 000000000..e1f1d5d1f --- /dev/null +++ b/src/CL/CustomFieldsBundle/DependencyInjection/CustomFieldCompilerPass.php @@ -0,0 +1,43 @@ + + */ +class CustomFieldCompilerPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition('chill.custom_field_compiler')) { + throw new \LogicException('service chill.custom_field_compiler ' + . 'is not defined.'); + } + + $definition = $container->getDefinition( + 'chill.custom_field_compiler' + ); + + $taggedServices = $container->findTaggedServiceIds( + 'chill.custom_field' + ); + + foreach ($taggedServices as $id => $tagAttributes) { + foreach ($tagAttributes as $attributes) { + $definition->addMethodCall( + 'addCustomField', + array(new Reference($id), $attributes["type"]) + ); + } + } + } + +} diff --git a/src/CL/CustomFieldsBundle/Entity/BlopEntity.php b/src/CL/CustomFieldsBundle/Entity/BlopEntity.php index 82d0fdf99..a45def933 100644 --- a/src/CL/CustomFieldsBundle/Entity/BlopEntity.php +++ b/src/CL/CustomFieldsBundle/Entity/BlopEntity.php @@ -25,7 +25,7 @@ class BlopEntity /** * @var array */ - private $customField; + private $customField = array(); private $adress; @@ -112,7 +112,7 @@ class BlopEntity * * @return BlopEntity */ - public function setCustomField($customField) + public function setCustomField(array $customField) { $this->customField = $customField; @@ -126,7 +126,8 @@ class BlopEntity */ public function getCustomField() { - return $this->customField; + + return $this->customField; } public function cfGet($key) diff --git a/src/CL/CustomFieldsBundle/Entity/CustomField.php b/src/CL/CustomFieldsBundle/Entity/CustomField.php index 524d3f71d..f1ba2e21a 100644 --- a/src/CL/CustomFieldsBundle/Entity/CustomField.php +++ b/src/CL/CustomFieldsBundle/Entity/CustomField.php @@ -16,6 +16,8 @@ class CustomField * @var string */ private $label; + + private $slug; /** * @var string @@ -26,6 +28,17 @@ class CustomField * @var boolean */ private $active; + + private $options = array(); + + /** + * + * @var int + */ + private $relation = 1; + + const ONE_TO_ONE = 1; + const ONE_TO_MANY = 2; /** @@ -37,8 +50,13 @@ class CustomField { return $this->id; } + + function getSlug() + { + return $this->slug; + } - /** + /** * Set label * * @param string $label @@ -48,10 +66,19 @@ class CustomField public function setLabel($label) { $this->label = $label; + + if ($this->slug === NULL) { + $this->slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $label); + } return $this; } + function getOptions() + { + return $this->options; + } + /** * Get label * @@ -85,7 +112,20 @@ class CustomField { return $this->type; } + + function getRelation() + { + return $this->relation; + } + function setRelation($relation) + { + $this->relation = $relation; + + return $this; + } + + /** * Set active * diff --git a/src/CL/CustomFieldsBundle/Form/AdressType.php b/src/CL/CustomFieldsBundle/Form/AdressType.php index 7192949b7..ce577cf1a 100644 --- a/src/CL/CustomFieldsBundle/Form/AdressType.php +++ b/src/CL/CustomFieldsBundle/Form/AdressType.php @@ -6,18 +6,23 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; +/** + * @internal Ne fonctionne pas encore + */ class AdressType extends AbstractType { - /** - * @param FormBuilderInterface $builder - * @param array $options - */ - public function buildForm(FormBuilderInterface $builder, array $options) - { - $builder - ->add('data') - ; - } + + +// /** +// * @param FormBuilderInterface $builder +// * @param array $options +// */ +// public function buildForm(FormBuilderInterface $builder, array $options) +// { +// $builder +// ->add('data') +// ; +// } /** * @param OptionsResolverInterface $resolver @@ -25,15 +30,21 @@ class AdressType extends AbstractType public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( - 'data_class' => 'CL\CustomFieldsBundle\Entity\Adress' + 'data_class' => 'CL\CustomFieldsBundle\Entity\Adress', + 'class' => 'CL\CustomFieldsBundle\Entity\Adress' )); } - + + public function getParent() + { + return 'entity'; + } + /** * @return string */ public function getName() { - return 'cl_customfieldsbundle_adress'; + return 'adress'; } } diff --git a/src/CL/CustomFieldsBundle/Form/BlopEntityType.php b/src/CL/CustomFieldsBundle/Form/BlopEntityType.php index 3856b3105..a86dd6275 100644 --- a/src/CL/CustomFieldsBundle/Form/BlopEntityType.php +++ b/src/CL/CustomFieldsBundle/Form/BlopEntityType.php @@ -20,8 +20,8 @@ class BlopEntityType extends AbstractType $builder ->add('field1') ->add('field2') - ->add('adress', new AdressType()) - ->add('customField',new CustomFieldType($entityManager)) + //->add('adress', new AdressType()) + ->add('customField', 'custom_field') ; } diff --git a/src/CL/CustomFieldsBundle/Form/CustomFieldType.php b/src/CL/CustomFieldsBundle/Form/CustomFieldType.php index 2f2e6b339..57d6f0b03 100644 --- a/src/CL/CustomFieldsBundle/Form/CustomFieldType.php +++ b/src/CL/CustomFieldsBundle/Form/CustomFieldType.php @@ -5,19 +5,49 @@ namespace CL\CustomFieldsBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler; +use CL\CustomFieldsBundle\Entity\CustomField; class CustomFieldType extends AbstractType { + /** + * + * @var \CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler + */ + private $customFieldCompiler; + + + public function __construct(CustomFieldCompiler $compiler) + { + $this->customFieldCompiler = $compiler; + } /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { + + $customFieldsList = array(); + + foreach ($this->customFieldCompiler->getAllFields() as $key => $field) { + $customFieldsList[$key] = $field->getName(); + } + $builder ->add('label') - ->add('type') + ->add('type', 'choice', array( + 'choices' => $customFieldsList, + 'expanded' => false, + 'multiple' => false + )) ->add('active') + ->add('relation', 'choice', array( + 'choices' => array( + CustomField::ONE_TO_ONE => 'one to one ', + CustomField::ONE_TO_MANY => 'one to many' + ) + )) ; } @@ -36,6 +66,6 @@ class CustomFieldType extends AbstractType */ public function getName() { - return 'cl_customfieldsbundle_customfield'; + return 'custom_field_choice'; } } diff --git a/src/CL/CustomFieldsBundle/Form/DataTransformer/CustomFieldDataTransformer.php b/src/CL/CustomFieldsBundle/Form/DataTransformer/CustomFieldDataTransformer.php new file mode 100644 index 000000000..a61712ff5 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Form/DataTransformer/CustomFieldDataTransformer.php @@ -0,0 +1,43 @@ + + */ +class CustomFieldDataTransformer implements DataTransformerInterface +{ + private $customFieldDefinition; + + /** + * + * @var \CL\CustomFieldsBundle\Entity\CustomField + */ + private $customField; + + public function __construct(CustomFieldInterface $customFieldDefinition, + CustomField $customField) + { + $this->customFieldDefinition = $customFieldDefinition; + $this->customField = $customField; + } + + public function reverseTransform($value) + { + return $this->customFieldDefinition->transformFromEntity($value, + $this->customField); + } + + public function transform($value) + { + return $this->customFieldDefinition->transformToEntity($value, + $this->customField); + } + +} diff --git a/src/CL/CustomFieldsBundle/Form/Type/CustomFieldType.php b/src/CL/CustomFieldsBundle/Form/Type/CustomFieldType.php index a664d874f..c59f5ffd0 100644 --- a/src/CL/CustomFieldsBundle/Form/Type/CustomFieldType.php +++ b/src/CL/CustomFieldsBundle/Form/Type/CustomFieldType.php @@ -16,68 +16,103 @@ use Symfony\Component\Form\FormBuilderInterface; use CL\CustomFieldsBundle\Form\DataTransformer\JsonCustomFieldToArrayTransformer; use Doctrine\Common\Persistence\ObjectManager; use CL\CustomFieldsBundle\Form\AdressType; +use CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler; +use CL\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer; class CustomFieldType extends AbstractType { - /** - * @var ObjectManager - */ - private $om; - /** - * @param ObjectManager $om - */ - public function __construct(ObjectManager $om) - { - $this->om = $om; - } + /** + * @var ObjectManager + */ + private $om; - public function buildForm(FormBuilderInterface $builder, array $options) - { - $customFields = $this->om - ->getRepository('CLCustomFieldsBundle:CustomField') - ->findAll(); + /** + * + * @var CustomFieldCompiler + */ + private $customFieldCompiler; - foreach ($customFields as $cf) { - if($cf->getType() === 'ManyToOne(Adress)') { - $builder->add($cf->getLabel(), 'entity', array( - 'class' => 'CLCustomFieldsBundle:Adress', - 'property' => 'data' - )); - } else if ($cf->getType() === 'ManyToOnePersist(Adress)') { - $builder->add($cf->getLabel(), new AdressType()); - } else if($cf->getType() === 'ManyToMany(Adress)') { + /** + * @param ObjectManager $om + */ + public function __construct(ObjectManager $om, CustomFieldCompiler $compiler) + { + $this->om = $om; + $this->customFieldCompiler = $compiler; + } - $adress = $this->om - ->getRepository('CLCustomFieldsBundle:Adress') + public function buildForm(FormBuilderInterface $builder, array $options) + { + $customFields = $this->om + ->getRepository('CLCustomFieldsBundle:CustomField') ->findAll(); - $adressId = array_map( - function($e) { return $e->getId(); }, - $adress); + foreach ($customFields as $cf) { - $adressLabel = array_map( - function($e) { return (string) $e; }, - $adress); + $builder->add( + $builder->create( + $cf->getSlug(), + $this->customFieldCompiler + ->getCustomFieldByType($cf->getType()) + ->buildFormType($builder, $cf), + array('mapped' => true) + ) + ->addModelTransformer(new CustomFieldDataTransformer( + $this->customFieldCompiler + ->getCustomFieldByType($cf->getType()), + $cf) + ) + ); - $addressChoices = array_combine($adressId, $adressLabel); - - - $builder->add($cf->getLabel(), 'choice', array( - 'choices' => $addressChoices, - 'multiple' => true - )); +// if($cf->getType() === 'ManyToOne(Adress)') { +// $builder->add($cf->getLabel(), 'entity', array( +// 'class' => 'CLCustomFieldsBundle:Adress', +// 'property' => 'data' +// )); +// } else if ($cf->getType() === 'ManyToOnePersist(Adress)') { +// $builder->add($cf->getLabel(), new AdressType()); +// } else if($cf->getType() === 'ManyToMany(Adress)') { +// +// $adress = $this->om +// ->getRepository('CLCustomFieldsBundle:Adress') +// ->findAll(); +// +// $adressId = array_map( +// function($e) { return $e->getId(); }, +// $adress); +// +// $adressLabel = array_map( +// function($e) { return (string) $e; }, +// $adress); +// +// $addressChoices = array_combine($adressId, $adressLabel); +// +// +// $builder->add($cf->getLabel(), 'choice', array( +// 'choices' => $addressChoices, +// 'multiple' => true +// )); +// } +// else { +// $builder->add($cf->getLabel(), $cf->getType()); +// } } - else { - $builder->add($cf->getLabel(), $cf->getType()); - } - } - $builder->addViewTransformer(new JsonCustomFieldToArrayTransformer($this->om)); - } + //$builder->addViewTransformer(new JsonCustomFieldToArrayTransformer($this->om)); + } + + public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver) + { + $resolver + //->addAllowedTypes(array('context' => 'string')) + //->setRequired(array('context')) + ; + } + + public function getName() + { + return 'custom_field'; + } - public function getName() - { - return 'custom_field'; - } } \ No newline at end of file diff --git a/src/CL/CustomFieldsBundle/Resources/config/doctrine/CustomField.orm.yml b/src/CL/CustomFieldsBundle/Resources/config/doctrine/CustomField.orm.yml index 55e238dcd..e28a5a5cc 100644 --- a/src/CL/CustomFieldsBundle/Resources/config/doctrine/CustomField.orm.yml +++ b/src/CL/CustomFieldsBundle/Resources/config/doctrine/CustomField.orm.yml @@ -11,9 +11,15 @@ CL\CustomFieldsBundle\Entity\CustomField: label: type: string length: 255 + slug: + type: string + length: 255 type: type: string length: 255 + relation: + type: integer + default: 1 active: type: boolean lifecycleCallbacks: { } diff --git a/src/CL/CustomFieldsBundle/Resources/config/services.yml b/src/CL/CustomFieldsBundle/Resources/config/services.yml index a033f9c55..090d2b620 100644 --- a/src/CL/CustomFieldsBundle/Resources/config/services.yml +++ b/src/CL/CustomFieldsBundle/Resources/config/services.yml @@ -2,6 +2,34 @@ parameters: # cl_custom_fields.example.class: CL\CustomFieldsBundle\Example services: -# cl_custom_fields.example: -# class: %cl_custom_fields.example.class% -# arguments: [@service_id, "plain_value", %parameter%] + chill.custom_field_compiler: + class: CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler + call: + - [setContainer, ["@service_container"]] + + chill.custom_field.custom_field_choice_type: + class: CL\CustomFieldsBundle\Form\CustomFieldType + arguments: + - "@chill.custom_field_compiler" + tags: + - { name: 'form.type', alias: 'custom_field_choice' } + + chill.custom_field.custom_field_type: + class: CL\CustomFieldsBundle\Form\Type\CustomFieldType + arguments: + - "@doctrine.orm.entity_manager" + - "@chill.custom_field_compiler" + tags: + - { name: 'form.type', alias: 'custom_field' } + + chill.custom_field.text: + class: CL\CustomFieldsBundle\CustomFields\CustomFieldText + tags: + - { name: 'chill.custom_field', type: 'text' } + + chill.custom_field.address: + class: CL\CustomFieldsBundle\CustomFields\CustomFieldAddress + arguments: + - "@doctrine.orm.entity_manager" + tags: + - { name: 'chill.custom_field', type: 'address' } diff --git a/src/CL/CustomFieldsBundle/Resources/views/BlopEntity/edit.html.twig b/src/CL/CustomFieldsBundle/Resources/views/BlopEntity/edit.html.twig index 1e842fc8c..1a1da6e9e 100644 --- a/src/CL/CustomFieldsBundle/Resources/views/BlopEntity/edit.html.twig +++ b/src/CL/CustomFieldsBundle/Resources/views/BlopEntity/edit.html.twig @@ -3,7 +3,13 @@ {% block body -%}

BlopEntity edit

- {{ form(edit_form) }} + {{ form_start(edit_form) }} + + {{ form_row(edit_form.customField.label0) }} + + {{ form_rest(edit_form) }} + + {{ form_end(edit_form) }}