From 26a0b1e056dbba952344ef842a6e6b33576afa18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 29 Oct 2014 16:43:13 +0100 Subject: [PATCH] implementation of customFieldBundle --- .../Controller/CustomFieldController.php | 11 +- .../CustomFieldsGroupController.php | 224 ++++++++++++++++++ .../CustomFields/CustomFieldAddress.php | 74 ++++-- .../CustomFields/CustomFieldInterface.php | 22 +- .../CustomFields/CustomFieldText.php | 12 +- src/CL/CustomFieldsBundle/Entity/Adress.php | 1 - .../CustomFieldsBundle/Entity/BlopEntity.php | 1 - .../CustomFieldsBundle/Entity/BlopEntity2.php | 1 - .../CustomFieldsBundle/Entity/CustomField.php | 129 +++++++++- .../Entity/CustomFieldsGroup.php | 146 ++++++++++++ src/CL/CustomFieldsBundle/Form/AdressType.php | 30 +-- .../Form/CustomFieldType.php | 38 +-- .../Form/CustomFieldsGroupType.php | 40 ++++ .../CustomFieldDataTransformer.php | 4 +- .../Form/Type/CustomFieldType.php | 19 +- .../config/doctrine/CustomField.orm.yml | 17 +- .../config/doctrine/CustomFieldsGroup.orm.yml | 19 ++ .../Resources/config/routing.yml | 4 + .../config/routing/customfieldsgroup.yml | 30 +++ .../Resources/config/services.yml | 2 +- .../Resources/views/BlopEntity/edit.html.twig | 2 +- .../views/CustomFieldsGroup/edit.html.twig | 16 ++ .../views/CustomFieldsGroup/index.html.twig | 43 ++++ .../views/CustomFieldsGroup/new.html.twig | 15 ++ .../views/CustomFieldsGroup/show.html.twig | 36 +++ .../CustomFieldProvider.php} | 8 +- .../CustomFieldsGroupControllerTest.php | 55 +++++ 27 files changed, 902 insertions(+), 97 deletions(-) create mode 100644 src/CL/CustomFieldsBundle/Controller/CustomFieldsGroupController.php create mode 100644 src/CL/CustomFieldsBundle/Entity/CustomFieldsGroup.php create mode 100644 src/CL/CustomFieldsBundle/Form/CustomFieldsGroupType.php create mode 100644 src/CL/CustomFieldsBundle/Resources/config/doctrine/CustomFieldsGroup.orm.yml create mode 100644 src/CL/CustomFieldsBundle/Resources/config/routing/customfieldsgroup.yml create mode 100644 src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/edit.html.twig create mode 100644 src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/index.html.twig create mode 100644 src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/new.html.twig create mode 100644 src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/show.html.twig rename src/CL/CustomFieldsBundle/{CustomFields/CustomFieldCompiler.php => Service/CustomFieldProvider.php} (83%) create mode 100644 src/CL/CustomFieldsBundle/Tests/Controller/CustomFieldsGroupControllerTest.php diff --git a/src/CL/CustomFieldsBundle/Controller/CustomFieldController.php b/src/CL/CustomFieldsBundle/Controller/CustomFieldController.php index d13813364..73afcf002 100644 --- a/src/CL/CustomFieldsBundle/Controller/CustomFieldController.php +++ b/src/CL/CustomFieldsBundle/Controller/CustomFieldController.php @@ -36,7 +36,7 @@ class CustomFieldController extends Controller public function createAction(Request $request) { $entity = new CustomField(); - $form = $this->createCreateForm($entity); + $form = $this->createCreateForm($entity, $request->query->get('type', null)); $form->handleRequest($request); if ($form->isValid()) { @@ -57,14 +57,15 @@ class CustomFieldController extends Controller * Creates a form to create a CustomField entity. * * @param CustomField $entity The entity - * + * @param string * @return \Symfony\Component\Form\Form The form */ - private function createCreateForm(CustomField $entity) + private function createCreateForm(CustomField $entity, $type) { $form = $this->createForm('custom_field_choice', $entity, array( 'action' => $this->generateUrl('customfield_create'), 'method' => 'POST', + 'type' => $type )); $form->add('submit', 'submit', array('label' => 'Create')); @@ -76,10 +77,10 @@ class CustomFieldController extends Controller * Displays a form to create a new CustomField entity. * */ - public function newAction() + public function newAction(Request $request) { $entity = new CustomField(); - $form = $this->createCreateForm($entity); + $form = $this->createCreateForm($entity, $request->query->get('type')); return $this->render('CLCustomFieldsBundle:CustomField:new.html.twig', array( 'entity' => $entity, diff --git a/src/CL/CustomFieldsBundle/Controller/CustomFieldsGroupController.php b/src/CL/CustomFieldsBundle/Controller/CustomFieldsGroupController.php new file mode 100644 index 000000000..42635a85c --- /dev/null +++ b/src/CL/CustomFieldsBundle/Controller/CustomFieldsGroupController.php @@ -0,0 +1,224 @@ +getDoctrine()->getManager(); + + $entities = $em->getRepository('CLCustomFieldsBundle:CustomFieldsGroup')->findAll(); + + return $this->render('CLCustomFieldsBundle:CustomFieldsGroup:index.html.twig', array( + 'entities' => $entities, + )); + } + /** + * Creates a new CustomFieldsGroup entity. + * + */ + public function createAction(Request $request) + { + $entity = new CustomFieldsGroup(); + $form = $this->createCreateForm($entity); + $form->handleRequest($request); + + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($entity); + $em->flush(); + + return $this->redirect($this->generateUrl('customfieldsgroup_show', array('id' => $entity->getId()))); + } + + return $this->render('CLCustomFieldsBundle:CustomFieldsGroup:new.html.twig', array( + 'entity' => $entity, + 'form' => $form->createView(), + )); + } + + /** + * Creates a form to create a CustomFieldsGroup entity. + * + * @param CustomFieldsGroup $entity The entity + * + * @return \Symfony\Component\Form\Form The form + */ + private function createCreateForm(CustomFieldsGroup $entity) + { + $form = $this->createForm(new CustomFieldsGroupType(), $entity, array( + 'action' => $this->generateUrl('customfieldsgroup_create'), + 'method' => 'POST', + )); + + $form->add('submit', 'submit', array('label' => 'Create')); + + return $form; + } + + /** + * Displays a form to create a new CustomFieldsGroup entity. + * + */ + public function newAction() + { + $entity = new CustomFieldsGroup(); + $form = $this->createCreateForm($entity); + + return $this->render('CLCustomFieldsBundle:CustomFieldsGroup:new.html.twig', array( + 'entity' => $entity, + 'form' => $form->createView(), + )); + } + + /** + * Finds and displays a CustomFieldsGroup entity. + * + */ + public function showAction($id) + { + $em = $this->getDoctrine()->getManager(); + + $entity = $em->getRepository('CLCustomFieldsBundle:CustomFieldsGroup')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.'); + } + + $deleteForm = $this->createDeleteForm($id); + + return $this->render('CLCustomFieldsBundle:CustomFieldsGroup:show.html.twig', array( + 'entity' => $entity, + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Displays a form to edit an existing CustomFieldsGroup entity. + * + */ + public function editAction($id) + { + $em = $this->getDoctrine()->getManager(); + + $entity = $em->getRepository('CLCustomFieldsBundle:CustomFieldsGroup')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.'); + } + + $editForm = $this->createEditForm($entity); + $deleteForm = $this->createDeleteForm($id); + + return $this->render('CLCustomFieldsBundle:CustomFieldsGroup:edit.html.twig', array( + 'entity' => $entity, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Creates a form to edit a CustomFieldsGroup entity. + * + * @param CustomFieldsGroup $entity The entity + * + * @return \Symfony\Component\Form\Form The form + */ + private function createEditForm(CustomFieldsGroup $entity) + { + $form = $this->createForm(new CustomFieldsGroupType(), $entity, array( + 'action' => $this->generateUrl('customfieldsgroup_update', array('id' => $entity->getId())), + 'method' => 'PUT', + )); + + $form->add('submit', 'submit', array('label' => 'Update')); + + return $form; + } + /** + * Edits an existing CustomFieldsGroup entity. + * + */ + public function updateAction(Request $request, $id) + { + $em = $this->getDoctrine()->getManager(); + + $entity = $em->getRepository('CLCustomFieldsBundle:CustomFieldsGroup')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.'); + } + + $deleteForm = $this->createDeleteForm($id); + $editForm = $this->createEditForm($entity); + $editForm->handleRequest($request); + + if ($editForm->isValid()) { + $em->flush(); + + return $this->redirect($this->generateUrl('customfieldsgroup_edit', array('id' => $id))); + } + + return $this->render('CLCustomFieldsBundle:CustomFieldsGroup:edit.html.twig', array( + 'entity' => $entity, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + )); + } + /** + * Deletes a CustomFieldsGroup entity. + * + */ + public function deleteAction(Request $request, $id) + { + $form = $this->createDeleteForm($id); + $form->handleRequest($request); + + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $entity = $em->getRepository('CLCustomFieldsBundle:CustomFieldsGroup')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.'); + } + + $em->remove($entity); + $em->flush(); + } + + return $this->redirect($this->generateUrl('customfieldsgroup')); + } + + /** + * Creates a form to delete a CustomFieldsGroup entity by id. + * + * @param mixed $id The entity id + * + * @return \Symfony\Component\Form\Form The form + */ + private function createDeleteForm($id) + { + return $this->createFormBuilder() + ->setAction($this->generateUrl('customfieldsgroup_delete', array('id' => $id))) + ->setMethod('DELETE') + ->add('submit', 'submit', array('label' => 'Delete')) + ->getForm() + ; + } +} diff --git a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldAddress.php b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldAddress.php index e987a80c1..9c15194e2 100644 --- a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldAddress.php +++ b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldAddress.php @@ -6,6 +6,7 @@ use CL\CustomFieldsBundle\CustomFields\CustomFieldInterface; use CL\CustomFieldsBundle\Entity\CustomField; use Symfony\Component\Form\FormBuilderInterface; use Doctrine\ORM\EntityManagerInterface; +use CL\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer; use CL\CustomFieldsBundle\Form\AdressType; /** @@ -15,35 +16,31 @@ use CL\CustomFieldsBundle\Form\AdressType; */ class CustomFieldAddress implements CustomFieldInterface { - + + /** + * + * @var EntityManagerInterface + */ public $om; - + public function __construct(EntityManagerInterface $om) { $this->om = $om; } - - public function buildFormType(FormBuilderInterface $builder, CustomField $customField) + + public function buildForm(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; - } + $builder->add( + $builder->create('address', 'entity', array( + 'class' => 'CLCustomFieldsBundle:Adress', + 'multiple' => true, + 'expanded' => true + ) + )->addModelTransformer(new CustomFieldDataTransformer( + $this, + $customField) + ) + ); } public function getName() @@ -56,14 +53,39 @@ class CustomFieldAddress implements CustomFieldInterface } - public function transformFromEntity($value, CustomField $customField) + public function buildOptionsForm(FormBuilderInterface $builder) { - + return null; } - public function transformToEntity($value, CustomField $customField) + public function deserialize($serialized, CustomField $customField) { +// if ($serialized === NULL) { +// return null; +// } +// +// return $this->om->getRepository('CLCustomFieldsBundle:Adress') +// ->find($serialized); + return $this->om->getRepository('CLCustomFieldsBundle:Adress') + ->findBy(array('id' => $serialized)); + } + + /** + * + * @param \CL\CustomFieldsBundle\Entity\Adress $value + * @param CustomField $customField + * @return type + */ + public function serialize($value, CustomField $customField) + { + $arrayId = array(); + + foreach($value as $address) { + $arrayId[] = $address->getId(); + } + + return $arrayId; } } diff --git a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldInterface.php b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldInterface.php index 0ce07b564..51bdff0ea 100644 --- a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldInterface.php +++ b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldInterface.php @@ -18,21 +18,24 @@ interface CustomFieldInterface * @param \CL\CustomFieldsBundle\CustomField\CustomField $customField * @return \Symfony\Component\Form\FormTypeInterface the form type */ - public function buildFormType(FormBuilderInterface $builder, CustomField $customField); + public function buildForm(FormBuilderInterface $builder, CustomField $customField); /** + * transform the value into a format that can be stored in DB * - * @param type $value + * @param mixed $value * @param \CL\CustomFieldsBundle\CustomField\CustomField $customField */ - public function transformToEntity($value, CustomField $customField); + public function serialize($value, CustomField $customField); /** + * Transform the representation of the value, stored in db, into the + * value which may be used in the process. * - * @param type $value + * @param mixed $value * @param \CL\CustomFieldsBundle\CustomField\CustomField $customField */ - public function transformFromEntity($value, CustomField $customField); + public function deserialize($serialized, CustomField $customField); /** * @@ -42,4 +45,13 @@ interface CustomFieldInterface public function render($value, CustomField $customField); public function getName(); + + /** + * return a formType which allow to edit option for the custom type. + * This FormType is shown in admin + * + * @param \CL\CustomFieldsBundle\CustomField\FormBuilderInterface $builder + * @return \Symfony\Component\Form\FormTypeInterface|null the form type + */ + public function buildOptionsForm(FormBuilderInterface $builder); } diff --git a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldText.php b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldText.php index 348a6d76c..5cdf8c531 100644 --- a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldText.php +++ b/src/CL/CustomFieldsBundle/CustomFields/CustomFieldText.php @@ -15,7 +15,7 @@ use Symfony\Component\Form\FormBuilderInterface; */ class CustomFieldText implements CustomFieldInterface { - public function buildFormType(FormBuilderInterface $builder, CustomField $customField) + public function buildForm(FormBuilderInterface $builder, CustomField $customField) { $builder->add($customField->getSlug(), 'text', array( 'label' => $customField->getLabel() @@ -27,14 +27,14 @@ class CustomFieldText implements CustomFieldInterface } - public function transformFromEntity($value, CustomField $customField) + public function serialize($value, CustomField $customField) { return $value; } - public function transformToEntity($value, CustomField $customField) + public function deserialize($serialized, CustomField $customField) { - return $value; + return $serialized; } public function getName() @@ -42,4 +42,8 @@ class CustomFieldText implements CustomFieldInterface return 'text field'; } + public function buildOptionsForm(FormBuilderInterface $builder) + { + return null; + } } diff --git a/src/CL/CustomFieldsBundle/Entity/Adress.php b/src/CL/CustomFieldsBundle/Entity/Adress.php index b0eb5028e..49abb9403 100644 --- a/src/CL/CustomFieldsBundle/Entity/Adress.php +++ b/src/CL/CustomFieldsBundle/Entity/Adress.php @@ -57,4 +57,3 @@ class Adress return $this->data; } } - diff --git a/src/CL/CustomFieldsBundle/Entity/BlopEntity.php b/src/CL/CustomFieldsBundle/Entity/BlopEntity.php index a45def933..ff6dc6653 100644 --- a/src/CL/CustomFieldsBundle/Entity/BlopEntity.php +++ b/src/CL/CustomFieldsBundle/Entity/BlopEntity.php @@ -159,4 +159,3 @@ class BlopEntity var_dump($customFieldArray); } } - diff --git a/src/CL/CustomFieldsBundle/Entity/BlopEntity2.php b/src/CL/CustomFieldsBundle/Entity/BlopEntity2.php index 1cbb0caf9..b44465997 100644 --- a/src/CL/CustomFieldsBundle/Entity/BlopEntity2.php +++ b/src/CL/CustomFieldsBundle/Entity/BlopEntity2.php @@ -245,4 +245,3 @@ class BlopEntity2 return $this->customFieldData; } } - diff --git a/src/CL/CustomFieldsBundle/Entity/CustomField.php b/src/CL/CustomFieldsBundle/Entity/CustomField.php index f1ba2e21a..0ccd8433f 100644 --- a/src/CL/CustomFieldsBundle/Entity/CustomField.php +++ b/src/CL/CustomFieldsBundle/Entity/CustomField.php @@ -29,8 +29,22 @@ class CustomField */ private $active; + /** + * + * @var array + */ private $options = array(); + /** + * @var array + */ + private $name; + + /** + * @var float + */ + private $order; + /** * * @var int @@ -40,6 +54,10 @@ class CustomField const ONE_TO_ONE = 1; const ONE_TO_MANY = 2; + /** + * @var CustomFieldsGroup + */ + private $customFieldGroup; /** * Get id @@ -149,5 +167,114 @@ class CustomField { return $this->active; } -} + /** + * Get customFieldGroup + * + * @return CustomFieldsGroup + */ + public function getCustomFieldsGroup() + { + return $this->customFieldGroup; + } + + /** + * Set customFieldGroup + * + * @param \CL\CustomFieldsBundle\Entity\CustomFieldsGroup $customFieldGroup + * + * @return CustomField + */ + public function setCustomFieldsGroup(\CL\CustomFieldsBundle\Entity\CustomFieldsGroup $customFieldGroup = null) + { + $this->customFieldGroup = $customFieldGroup; + + return $this; + } + + /** + * Set name + * + * @param array $name + * + * @return CustomField + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * Get name + * + * @return array + */ + public function getName() + { + return $this->name; + } + + /** + * Set order + * + * @param float $order + * + * @return CustomField + */ + public function setOrder($order) + { + $this->order = $order; + + return $this; + } + + /** + * Get order + * + * @return float + */ + public function getOrder() + { + return $this->order; + } + + /** + * Set options + * + * @param array $options + * + * @return CustomField + */ + public function setOptions(array $options) + { + $this->options = $options; + + return $this; + } + + /** + * Set customFieldGroup + * + * @param \CL\CustomFieldsBundle\Entity\CustomFieldsGroup $customFieldGroup + * + * @return CustomField + */ + public function setCustomFieldGroup(\CL\CustomFieldsBundle\Entity\CustomFieldsGroup $customFieldGroup = null) + { + $this->customFieldGroup = $customFieldGroup; + + return $this; + } + + /** + * Get customFieldGroup + * + * @return \CL\CustomFieldsBundle\Entity\CustomFieldsGroup + */ + public function getCustomFieldGroup() + { + return $this->customFieldGroup; + } +} diff --git a/src/CL/CustomFieldsBundle/Entity/CustomFieldsGroup.php b/src/CL/CustomFieldsBundle/Entity/CustomFieldsGroup.php new file mode 100644 index 000000000..e1fd261e0 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Entity/CustomFieldsGroup.php @@ -0,0 +1,146 @@ +customFields = new \Doctrine\Common\Collections\ArrayCollection(); + } + + /** + * Add customField + * + * @param \CL\CustomFieldsBundle\Entity\CustomField $customField + * + * @return CustomFieldsGroup + */ + public function addCustomField(\CL\CustomFieldsBundle\Entity\CustomField $customField) + { + $this->customFields[] = $customField; + + return $this; + } + + /** + * Remove customField + * + * @param \CL\CustomFieldsBundle\Entity\CustomField $customField + */ + public function removeCustomField(\CL\CustomFieldsBundle\Entity\CustomField $customField) + { + $this->customFields->removeElement($customField); + } + + /** + * + * @return \Doctrine\Common\Collections\Collection + */ + public function getCustomFields() + { + return $this->customFields; + } + + + /** + * Get id + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * Set name + * + * @param array $name + * + * @return CustomFieldsGroup + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * Get name + * + * @return array + */ + public function getName($language = null) + { + //TODO set this in a service, PLUS twig function + if ($language) { + if (isset($this->name[$language])) { + return $this->name[$language]; + } else { + foreach ($this->name as $name) { + if (!empty($name)) { + return $name; + } + } + } + + return ''; + + } else { + return $this->name; + } + } + + /** + * Set entity + * + * @param string $entity + * + * @return CustomFieldsGroup + */ + public function setEntity($entity) + { + $this->entity = $entity; + + return $this; + } + + /** + * Get entity + * + * @return string + */ + public function getEntity() + { + return $this->entity; + } + +} diff --git a/src/CL/CustomFieldsBundle/Form/AdressType.php b/src/CL/CustomFieldsBundle/Form/AdressType.php index ce577cf1a..4731b819f 100644 --- a/src/CL/CustomFieldsBundle/Form/AdressType.php +++ b/src/CL/CustomFieldsBundle/Form/AdressType.php @@ -13,26 +13,28 @@ 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', 'entity', array( + + )) + ; + } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { - $resolver->setDefaults(array( - 'data_class' => 'CL\CustomFieldsBundle\Entity\Adress', - 'class' => 'CL\CustomFieldsBundle\Entity\Adress' - )); +// $resolver->setDefaults(array( +// 'data_class' => 'CL\CustomFieldsBundle\Entity\Adress', +// 'class' => 'CL\CustomFieldsBundle\Entity\Adress' +// )); } public function getParent() diff --git a/src/CL/CustomFieldsBundle/Form/CustomFieldType.php b/src/CL/CustomFieldsBundle/Form/CustomFieldType.php index 57d6f0b03..c4afba705 100644 --- a/src/CL/CustomFieldsBundle/Form/CustomFieldType.php +++ b/src/CL/CustomFieldsBundle/Form/CustomFieldType.php @@ -5,21 +5,23 @@ 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\Service\CustomFieldProvider; use CL\CustomFieldsBundle\Entity\CustomField; class CustomFieldType extends AbstractType { /** * - * @var \CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler + * @var CustomFieldProvider */ - private $customFieldCompiler; + private $customFieldProvider; + + private $culture = 'fr'; - public function __construct(CustomFieldCompiler $compiler) + public function __construct(CustomFieldProvider $compiler) { - $this->customFieldCompiler = $compiler; + $this->customFieldProvider = $compiler; } /** * @param FormBuilderInterface $builder @@ -30,25 +32,26 @@ class CustomFieldType extends AbstractType $customFieldsList = array(); - foreach ($this->customFieldCompiler->getAllFields() as $key => $field) { + foreach ($this->customFieldProvider->getAllFields() as $key => $field) { $customFieldsList[$key] = $field->getName(); } $builder ->add('label') - ->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' - ) + ->add('customFieldsGroup', 'entity', array( + 'class' => 'CLCustomFieldsBundle:CustomFieldsGroup', + 'property' => 'name['.$this->culture.']' )) ; + + //add options field + $optionsType = $this->customFieldProvider + ->getCustomFieldByType($options['type']) + ->buildOptionsForm($builder); + if ($optionsType) { + $builder->add('options', $optionsType); + } } /** @@ -58,7 +61,8 @@ class CustomFieldType extends AbstractType { $resolver->setDefaults(array( 'data_class' => 'CL\CustomFieldsBundle\Entity\CustomField' - )); + )); + $resolver->addAllowedTypes(array('type' => 'string')); } /** diff --git a/src/CL/CustomFieldsBundle/Form/CustomFieldsGroupType.php b/src/CL/CustomFieldsBundle/Form/CustomFieldsGroupType.php new file mode 100644 index 000000000..c1bc39e5a --- /dev/null +++ b/src/CL/CustomFieldsBundle/Form/CustomFieldsGroupType.php @@ -0,0 +1,40 @@ +add('name') + ->add('entity') + ; + } + + /** + * @param OptionsResolverInterface $resolver + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'CL\CustomFieldsBundle\Entity\CustomFieldsGroup' + )); + } + + /** + * @return string + */ + public function getName() + { + return 'cl_customfieldsbundle_customfieldsgroup'; + } +} diff --git a/src/CL/CustomFieldsBundle/Form/DataTransformer/CustomFieldDataTransformer.php b/src/CL/CustomFieldsBundle/Form/DataTransformer/CustomFieldDataTransformer.php index a61712ff5..08db1f5ce 100644 --- a/src/CL/CustomFieldsBundle/Form/DataTransformer/CustomFieldDataTransformer.php +++ b/src/CL/CustomFieldsBundle/Form/DataTransformer/CustomFieldDataTransformer.php @@ -30,13 +30,13 @@ class CustomFieldDataTransformer implements DataTransformerInterface public function reverseTransform($value) { - return $this->customFieldDefinition->transformFromEntity($value, + return $this->customFieldDefinition->serialize($value, $this->customField); } public function transform($value) { - return $this->customFieldDefinition->transformToEntity($value, + return $this->customFieldDefinition->deserialize($value, $this->customField); } diff --git a/src/CL/CustomFieldsBundle/Form/Type/CustomFieldType.php b/src/CL/CustomFieldsBundle/Form/Type/CustomFieldType.php index c59f5ffd0..8f257e23d 100644 --- a/src/CL/CustomFieldsBundle/Form/Type/CustomFieldType.php +++ b/src/CL/CustomFieldsBundle/Form/Type/CustomFieldType.php @@ -16,7 +16,7 @@ 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\Service\CustomFieldProvider; use CL\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer; class CustomFieldType extends AbstractType @@ -36,7 +36,7 @@ class CustomFieldType extends AbstractType /** * @param ObjectManager $om */ - public function __construct(ObjectManager $om, CustomFieldCompiler $compiler) + public function __construct(ObjectManager $om, CustomFieldProvider $compiler) { $this->om = $om; $this->customFieldCompiler = $compiler; @@ -50,20 +50,19 @@ class CustomFieldType extends AbstractType foreach ($customFields as $cf) { - $builder->add( - $builder->create( - $cf->getSlug(), + //$builder->add( + //$builder->create( + //$cf->getSlug(), $this->customFieldCompiler ->getCustomFieldByType($cf->getType()) - ->buildFormType($builder, $cf), - array('mapped' => true) - ) + ->buildForm($builder, $cf); + /* ) ->addModelTransformer(new CustomFieldDataTransformer( $this->customFieldCompiler ->getCustomFieldByType($cf->getType()), $cf) - ) - ); + )*/ + //); // if($cf->getType() === 'ManyToOne(Adress)') { // $builder->add($cf->getLabel(), 'entity', array( diff --git a/src/CL/CustomFieldsBundle/Resources/config/doctrine/CustomField.orm.yml b/src/CL/CustomFieldsBundle/Resources/config/doctrine/CustomField.orm.yml index e28a5a5cc..d56dd46ac 100644 --- a/src/CL/CustomFieldsBundle/Resources/config/doctrine/CustomField.orm.yml +++ b/src/CL/CustomFieldsBundle/Resources/config/doctrine/CustomField.orm.yml @@ -8,18 +8,23 @@ CL\CustomFieldsBundle\Entity\CustomField: generator: strategy: AUTO fields: - label: - type: string - length: 255 + name: + type: json_array slug: type: string length: 255 type: type: string length: 255 - relation: - type: integer - default: 1 active: type: boolean + order: + type: float + options: + type: json_array lifecycleCallbacks: { } + manyToOne: + customFieldGroup: + targetEntity: CL\CustomFieldsBundle\Entity\CustomFieldsGroup + inversedBy: customFields +#TODO: add an unique constraint slug+customFieldsGroup \ No newline at end of file diff --git a/src/CL/CustomFieldsBundle/Resources/config/doctrine/CustomFieldsGroup.orm.yml b/src/CL/CustomFieldsBundle/Resources/config/doctrine/CustomFieldsGroup.orm.yml new file mode 100644 index 000000000..38eea439c --- /dev/null +++ b/src/CL/CustomFieldsBundle/Resources/config/doctrine/CustomFieldsGroup.orm.yml @@ -0,0 +1,19 @@ +CL\CustomFieldsBundle\Entity\CustomFieldsGroup: + type: entity + table: null + id: + id: + type: integer + id: true + generator: + strategy: AUTO + fields: + name: + type: json_array + entity: + type: string + length: 255 + oneToMany: + customFields: + targetEntity: CL\CustomFieldsBundle\Entity\CustomField + mappedBy: customFieldGroup \ No newline at end of file diff --git a/src/CL/CustomFieldsBundle/Resources/config/routing.yml b/src/CL/CustomFieldsBundle/Resources/config/routing.yml index cb112926f..eec602761 100644 --- a/src/CL/CustomFieldsBundle/Resources/config/routing.yml +++ b/src/CL/CustomFieldsBundle/Resources/config/routing.yml @@ -1,3 +1,7 @@ +cl_custom_fields_customfieldsgroup: + resource: "@CLCustomFieldsBundle/Resources/config/routing/customfieldsgroup.yml" + prefix: /customfieldsgroup + cl_custom_fields_blopentity2: resource: "@CLCustomFieldsBundle/Resources/config/routing/blopentity2.yml" prefix: /blopentity2 diff --git a/src/CL/CustomFieldsBundle/Resources/config/routing/customfieldsgroup.yml b/src/CL/CustomFieldsBundle/Resources/config/routing/customfieldsgroup.yml new file mode 100644 index 000000000..930aecb51 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Resources/config/routing/customfieldsgroup.yml @@ -0,0 +1,30 @@ +customfieldsgroup: + path: / + defaults: { _controller: "CLCustomFieldsBundle:CustomFieldsGroup:index" } + +customfieldsgroup_show: + path: /{id}/show + defaults: { _controller: "CLCustomFieldsBundle:CustomFieldsGroup:show" } + +customfieldsgroup_new: + path: /new + defaults: { _controller: "CLCustomFieldsBundle:CustomFieldsGroup:new" } + +customfieldsgroup_create: + path: /create + defaults: { _controller: "CLCustomFieldsBundle:CustomFieldsGroup:create" } + requirements: { _method: post } + +customfieldsgroup_edit: + path: /{id}/edit + defaults: { _controller: "CLCustomFieldsBundle:CustomFieldsGroup:edit" } + +customfieldsgroup_update: + path: /{id}/update + defaults: { _controller: "CLCustomFieldsBundle:CustomFieldsGroup:update" } + requirements: { _method: post|put } + +customfieldsgroup_delete: + path: /{id}/delete + defaults: { _controller: "CLCustomFieldsBundle:CustomFieldsGroup:delete" } + requirements: { _method: post|delete } diff --git a/src/CL/CustomFieldsBundle/Resources/config/services.yml b/src/CL/CustomFieldsBundle/Resources/config/services.yml index 090d2b620..40330aeef 100644 --- a/src/CL/CustomFieldsBundle/Resources/config/services.yml +++ b/src/CL/CustomFieldsBundle/Resources/config/services.yml @@ -3,7 +3,7 @@ parameters: services: chill.custom_field_compiler: - class: CL\CustomFieldsBundle\CustomFields\CustomFieldCompiler + class: CL\CustomFieldsBundle\Service\CustomFieldProvider call: - [setContainer, ["@service_container"]] diff --git a/src/CL/CustomFieldsBundle/Resources/views/BlopEntity/edit.html.twig b/src/CL/CustomFieldsBundle/Resources/views/BlopEntity/edit.html.twig index 1a1da6e9e..6ce9b1cb5 100644 --- a/src/CL/CustomFieldsBundle/Resources/views/BlopEntity/edit.html.twig +++ b/src/CL/CustomFieldsBundle/Resources/views/BlopEntity/edit.html.twig @@ -5,7 +5,7 @@ {{ form_start(edit_form) }} - {{ form_row(edit_form.customField.label0) }} + {{ form_row(edit_form.customField) }} {{ form_rest(edit_form) }} diff --git a/src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/edit.html.twig b/src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/edit.html.twig new file mode 100644 index 000000000..9c4b082f7 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/edit.html.twig @@ -0,0 +1,16 @@ +{% extends '::base.html.twig' %} + +{% block body -%} +

CustomFieldsGroup edit

+ + {{ form(edit_form) }} + + +{% endblock %} diff --git a/src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/index.html.twig b/src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/index.html.twig new file mode 100644 index 000000000..46d105c01 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/index.html.twig @@ -0,0 +1,43 @@ +{% extends '::base.html.twig' %} + +{% block body -%} +

CustomFieldsGroup list

+ + + + + + + + + + + + {% for entity in entities %} + + + + + + + {% endfor %} + +
IdNameEntityActions
{{ entity.id }}{{ entity.name['fr'] }}{{ entity.entity }} + +
+ + + {% endblock %} diff --git a/src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/new.html.twig b/src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/new.html.twig new file mode 100644 index 000000000..eaa7b47d8 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/new.html.twig @@ -0,0 +1,15 @@ +{% extends '::base.html.twig' %} + +{% block body -%} +

CustomFieldsGroup creation

+ + {{ form(form) }} + + +{% endblock %} diff --git a/src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/show.html.twig b/src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/show.html.twig new file mode 100644 index 000000000..489fc6590 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Resources/views/CustomFieldsGroup/show.html.twig @@ -0,0 +1,36 @@ +{% extends '::base.html.twig' %} + +{% block body -%} +

CustomFieldsGroup

+ + + + + + + + + + + + + + + + +
Id{{ entity.id }}
Name{{ entity.name }}
Entity{{ entity.entity }}
+ + +{% endblock %} diff --git a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldCompiler.php b/src/CL/CustomFieldsBundle/Service/CustomFieldProvider.php similarity index 83% rename from src/CL/CustomFieldsBundle/CustomFields/CustomFieldCompiler.php rename to src/CL/CustomFieldsBundle/Service/CustomFieldProvider.php index 562747e48..69d2aadb0 100644 --- a/src/CL/CustomFieldsBundle/CustomFields/CustomFieldCompiler.php +++ b/src/CL/CustomFieldsBundle/Service/CustomFieldProvider.php @@ -1,7 +1,7 @@ */ -class CustomFieldCompiler implements ContainerAwareInterface +class CustomFieldProvider implements ContainerAwareInterface { private $servicesByType = array(); @@ -43,6 +43,10 @@ class CustomFieldCompiler implements ContainerAwareInterface public function setContainer(ContainerInterface $container = null) { + if ($container === null) { + throw new \LogicException('container should not be null'); + } + $this->container = $container; } diff --git a/src/CL/CustomFieldsBundle/Tests/Controller/CustomFieldsGroupControllerTest.php b/src/CL/CustomFieldsBundle/Tests/Controller/CustomFieldsGroupControllerTest.php new file mode 100644 index 000000000..81808ca32 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Tests/Controller/CustomFieldsGroupControllerTest.php @@ -0,0 +1,55 @@ +request('GET', '/customfieldsgroup/'); + $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /customfieldsgroup/"); + $crawler = $client->click($crawler->selectLink('Create a new entry')->link()); + + // Fill in the form and submit it + $form = $crawler->selectButton('Create')->form(array( + 'cl_customfieldsbundle_customfieldsgroup[field_name]' => 'Test', + // ... other fields to fill + )); + + $client->submit($form); + $crawler = $client->followRedirect(); + + // Check data in the show view + $this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")'); + + // Edit the entity + $crawler = $client->click($crawler->selectLink('Edit')->link()); + + $form = $crawler->selectButton('Update')->form(array( + 'cl_customfieldsbundle_customfieldsgroup[field_name]' => 'Foo', + // ... other fields to fill + )); + + $client->submit($form); + $crawler = $client->followRedirect(); + + // Check the element contains an attribute with value equals "Foo" + $this->assertGreaterThan(0, $crawler->filter('[value="Foo"]')->count(), 'Missing element [value="Foo"]'); + + // Delete the entity + $client->submit($crawler->selectButton('Delete')->form()); + $crawler = $client->followRedirect(); + + // Check the entity has been delete on the list + $this->assertNotRegExp('/Foo/', $client->getResponse()->getContent()); + } + + */ +}