diff --git a/.gitignore b/.gitignore index 12df07698..1d66ca65d 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,4 @@ app/config/parameters.yml .DS_Store *bower_components bin/* -/tmp/* +/tmp/* \ No newline at end of file diff --git a/src/CL/CustomFieldsBundle/Controller/AdressController.php b/src/CL/CustomFieldsBundle/Controller/AdressController.php new file mode 100644 index 000000000..fd5c84de1 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Controller/AdressController.php @@ -0,0 +1,224 @@ +getDoctrine()->getManager(); + + $entities = $em->getRepository('CLCustomFieldsBundle:Adress')->findAll(); + + return $this->render('CLCustomFieldsBundle:Adress:index.html.twig', array( + 'entities' => $entities, + )); + } + /** + * Creates a new Adress entity. + * + */ + public function createAction(Request $request) + { + $entity = new Adress(); + $form = $this->createCreateForm($entity); + $form->handleRequest($request); + + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($entity); + $em->flush(); + + return $this->redirect($this->generateUrl('adress_show', array('id' => $entity->getId()))); + } + + return $this->render('CLCustomFieldsBundle:Adress:new.html.twig', array( + 'entity' => $entity, + 'form' => $form->createView(), + )); + } + + /** + * Creates a form to create a Adress entity. + * + * @param Adress $entity The entity + * + * @return \Symfony\Component\Form\Form The form + */ + private function createCreateForm(Adress $entity) + { + $form = $this->createForm(new AdressType(), $entity, array( + 'action' => $this->generateUrl('adress_create'), + 'method' => 'POST', + )); + + $form->add('submit', 'submit', array('label' => 'Create')); + + return $form; + } + + /** + * Displays a form to create a new Adress entity. + * + */ + public function newAction() + { + $entity = new Adress(); + $form = $this->createCreateForm($entity); + + return $this->render('CLCustomFieldsBundle:Adress:new.html.twig', array( + 'entity' => $entity, + 'form' => $form->createView(), + )); + } + + /** + * Finds and displays a Adress entity. + * + */ + public function showAction($id) + { + $em = $this->getDoctrine()->getManager(); + + $entity = $em->getRepository('CLCustomFieldsBundle:Adress')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find Adress entity.'); + } + + $deleteForm = $this->createDeleteForm($id); + + return $this->render('CLCustomFieldsBundle:Adress:show.html.twig', array( + 'entity' => $entity, + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Displays a form to edit an existing Adress entity. + * + */ + public function editAction($id) + { + $em = $this->getDoctrine()->getManager(); + + $entity = $em->getRepository('CLCustomFieldsBundle:Adress')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find Adress entity.'); + } + + $editForm = $this->createEditForm($entity); + $deleteForm = $this->createDeleteForm($id); + + return $this->render('CLCustomFieldsBundle:Adress:edit.html.twig', array( + 'entity' => $entity, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Creates a form to edit a Adress entity. + * + * @param Adress $entity The entity + * + * @return \Symfony\Component\Form\Form The form + */ + private function createEditForm(Adress $entity) + { + $form = $this->createForm(new AdressType(), $entity, array( + 'action' => $this->generateUrl('adress_update', array('id' => $entity->getId())), + 'method' => 'PUT', + )); + + $form->add('submit', 'submit', array('label' => 'Update')); + + return $form; + } + /** + * Edits an existing Adress entity. + * + */ + public function updateAction(Request $request, $id) + { + $em = $this->getDoctrine()->getManager(); + + $entity = $em->getRepository('CLCustomFieldsBundle:Adress')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find Adress entity.'); + } + + $deleteForm = $this->createDeleteForm($id); + $editForm = $this->createEditForm($entity); + $editForm->handleRequest($request); + + if ($editForm->isValid()) { + $em->flush(); + + return $this->redirect($this->generateUrl('adress_edit', array('id' => $id))); + } + + return $this->render('CLCustomFieldsBundle:Adress:edit.html.twig', array( + 'entity' => $entity, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + )); + } + /** + * Deletes a Adress 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:Adress')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find Adress entity.'); + } + + $em->remove($entity); + $em->flush(); + } + + return $this->redirect($this->generateUrl('adress')); + } + + /** + * Creates a form to delete a Adress 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('adress_delete', array('id' => $id))) + ->setMethod('DELETE') + ->add('submit', 'submit', array('label' => 'Delete')) + ->getForm() + ; + } +} diff --git a/src/CL/CustomFieldsBundle/Entity/Adress.php b/src/CL/CustomFieldsBundle/Entity/Adress.php new file mode 100644 index 000000000..9a472f279 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Entity/Adress.php @@ -0,0 +1,60 @@ +data; + } + + /** + * Get id + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * Set data + * + * @param string $data + * + * @return Adress + */ + public function setData($data) + { + $this->data = $data; + + return $this; + } + + /** + * Get data + * + * @return string + */ + public function getData() + { + return $this->data; + } +} + diff --git a/src/CL/CustomFieldsBundle/Entity/BlopEntity.php b/src/CL/CustomFieldsBundle/Entity/BlopEntity.php index bbd6324c7..82d0fdf99 100644 --- a/src/CL/CustomFieldsBundle/Entity/BlopEntity.php +++ b/src/CL/CustomFieldsBundle/Entity/BlopEntity.php @@ -27,6 +27,8 @@ class BlopEntity */ private $customField; + private $adress; + /** * Get id @@ -38,6 +40,23 @@ class BlopEntity return $this->id; } + public function setAdress($a) + { + $this->adress = $a; + + return $this; + } + + /** + * Get field1 + * + * @return string + */ + public function getAdress() + { + return $this->adress; + } + /** * Set field1 * diff --git a/src/CL/CustomFieldsBundle/Form/AdressType.php b/src/CL/CustomFieldsBundle/Form/AdressType.php new file mode 100644 index 000000000..7192949b7 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Form/AdressType.php @@ -0,0 +1,39 @@ +add('data') + ; + } + + /** + * @param OptionsResolverInterface $resolver + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'CL\CustomFieldsBundle\Entity\Adress' + )); + } + + /** + * @return string + */ + public function getName() + { + return 'cl_customfieldsbundle_adress'; + } +} diff --git a/src/CL/CustomFieldsBundle/Form/BlopEntityType.php b/src/CL/CustomFieldsBundle/Form/BlopEntityType.php index ea38d1dc6..3856b3105 100644 --- a/src/CL/CustomFieldsBundle/Form/BlopEntityType.php +++ b/src/CL/CustomFieldsBundle/Form/BlopEntityType.php @@ -20,6 +20,7 @@ class BlopEntityType extends AbstractType $builder ->add('field1') ->add('field2') + ->add('adress', new AdressType()) ->add('customField',new CustomFieldType($entityManager)) ; } @@ -30,7 +31,8 @@ class BlopEntityType extends AbstractType public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( - 'data_class' => 'CL\CustomFieldsBundle\Entity\BlopEntity' + 'data_class' => 'CL\CustomFieldsBundle\Entity\BlopEntity', + 'cascade_validation' => true )); // supprimer ça en definissant dans services diff --git a/src/CL/CustomFieldsBundle/Form/DataTransformer/JsonCustomFieldToArrayTransformer.php b/src/CL/CustomFieldsBundle/Form/DataTransformer/JsonCustomFieldToArrayTransformer.php index 14097e1cc..372b218b7 100644 --- a/src/CL/CustomFieldsBundle/Form/DataTransformer/JsonCustomFieldToArrayTransformer.php +++ b/src/CL/CustomFieldsBundle/Form/DataTransformer/JsonCustomFieldToArrayTransformer.php @@ -3,16 +3,127 @@ namespace CL\CustomFieldsBundle\Form\DataTransformer; use Symfony\Component\Form\DataTransformerInterface; - +use Doctrine\Common\Persistence\ObjectManager; class JsonCustomFieldToArrayTransformer implements DataTransformerInterface { - public function transform($jsonCustomField) - { - return json_decode($jsonCustomField,true); - } + /** + * @var ObjectManager + */ + private $om; - public function reverseTransform($array) - { - return json_encode($array); - } -} + /** + * @param ObjectManager $om + */ + public function __construct(ObjectManager $om) + { + $this->om = $om; + + $customFields = $this->om + ->getRepository('CLCustomFieldsBundle:CustomField') + ->findAll(); + + $customFieldsLablels = array_map( + function($e) { return $e->getLabel(); }, + $customFields); + + $customFieldsByLabel = array_combine($customFieldsLablels, $customFields); + + /* + echo "
- -
"; + + var_dump($customFields); + + echo "
- -
"; + + var_dump($customFieldsLablels); + + echo "
- -
"; + + var_dump($customFieldsByLabel); + */ + + $this->customField = $customFieldsByLabel; + } + + public function transform($customFieldsJSON) + { + echo $customFieldsJSON; + $customFieldsArray = json_decode($customFieldsJSON,true); + + /* + echo "
- -
"; + + var_dump($customFieldsArray); + + echo "
- -
"; + */ + + + $customFieldsArrayRet = array(); + + foreach ($customFieldsArray as $key => $value) { + $traited = false; + if(array_key_exists($key, $this->customField)) { + /* + echo "
- - - -
"; + echo $value; + echo "
- - - -
"; + */ + + if($this->customField[$key]->getType() === 'ManyToOne(Address)') { + $customFieldsArrayRet[$key] = $this->om + ->getRepository('CLCustomFieldsBundle:Adress') + ->findOneById($value); + + $traited = true; + } + } + + if(! $traited) { + $customFieldsArrayRet[$key] = $value; + } + } + + var_dump($customFieldsArray); + + return $customFieldsArrayRet; + } + + public function reverseTransform($customFieldsArray) + { + /* + echo "
- - - -
"; + + var_dump($customFieldsArray); + + echo "
- - - -
"; + */ + + $customFieldsArrayRet = array(); + + foreach ($customFieldsArray as $key => $value) { + $traited = false; + if(array_key_exists($key, $this->customField)) { + if($this->customField[$key]->getType() === 'ManyToOne(Address)') { + $customFieldsArrayRet[$key] = $value->getId(); + + /* + echo "
- - - -
"; + echo $value->getId(); + echo "
- - - -
"; + */ + + $traited = true; + } + } + + if(! $traited) { + $customFieldsArrayRet[$key] = $value; + } + } + + var_dump($customFieldsArrayRet); + + return json_encode($customFieldsArrayRet); + } +} \ No newline at end of file diff --git a/src/CL/CustomFieldsBundle/Form/Type/CustomFieldType.php b/src/CL/CustomFieldsBundle/Form/Type/CustomFieldType.php index 9edfc0a03..88b515bc2 100644 --- a/src/CL/CustomFieldsBundle/Form/Type/CustomFieldType.php +++ b/src/CL/CustomFieldsBundle/Form/Type/CustomFieldType.php @@ -38,9 +38,17 @@ class CustomFieldType extends AbstractType ->findAll(); foreach ($customFields as $cf) { - $builder->add($cf->getLabel(), $cf->getType()); + if($cf->getType() === 'ManyToOne(Address)') { + $builder->add($cf->getLabel(), 'entity', array( + 'class' => 'CLCustomFieldsBundle:Adress', + 'property' => 'data', + )); + } else { + $builder->add($cf->getLabel(), $cf->getType()); + } } - $builder->addViewTransformer(new JsonCustomFieldToArrayTransformer()); + + $builder->addViewTransformer(new JsonCustomFieldToArrayTransformer($this->om)); } public function getName() diff --git a/src/CL/CustomFieldsBundle/Resources/config/doctrine/Adress.orm.yml b/src/CL/CustomFieldsBundle/Resources/config/doctrine/Adress.orm.yml new file mode 100644 index 000000000..fc06fc2a0 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Resources/config/doctrine/Adress.orm.yml @@ -0,0 +1,14 @@ +CL\CustomFieldsBundle\Entity\Adress: + type: entity + table: null + id: + id: + type: integer + id: true + generator: + strategy: AUTO + fields: + data: + type: string + length: 255 + lifecycleCallbacks: { } diff --git a/src/CL/CustomFieldsBundle/Resources/config/doctrine/BlopEntity.orm.yml b/src/CL/CustomFieldsBundle/Resources/config/doctrine/BlopEntity.orm.yml index edba3af69..a2f9ec5c2 100644 --- a/src/CL/CustomFieldsBundle/Resources/config/doctrine/BlopEntity.orm.yml +++ b/src/CL/CustomFieldsBundle/Resources/config/doctrine/BlopEntity.orm.yml @@ -16,4 +16,8 @@ CL\CustomFieldsBundle\Entity\BlopEntity: length: 255 customField: type: json_array + manyToOne: + adress: + targetEntity: CL\CustomFieldsBundle\Entity\Adress + cascade: [persist] lifecycleCallbacks: { } diff --git a/src/CL/CustomFieldsBundle/Resources/config/routing.yml b/src/CL/CustomFieldsBundle/Resources/config/routing.yml index 2c158cc05..cd618c827 100644 --- a/src/CL/CustomFieldsBundle/Resources/config/routing.yml +++ b/src/CL/CustomFieldsBundle/Resources/config/routing.yml @@ -1,3 +1,7 @@ +cl_custom_fields_adress: + resource: "@CLCustomFieldsBundle/Resources/config/routing/adress.yml" + prefix: /adress + cl_custom_fields_customfield: resource: "@CLCustomFieldsBundle/Resources/config/routing/customfield.yml" prefix: /customfield diff --git a/src/CL/CustomFieldsBundle/Resources/config/routing/adress.yml b/src/CL/CustomFieldsBundle/Resources/config/routing/adress.yml new file mode 100644 index 000000000..0d27772cb --- /dev/null +++ b/src/CL/CustomFieldsBundle/Resources/config/routing/adress.yml @@ -0,0 +1,30 @@ +adress: + path: / + defaults: { _controller: "CLCustomFieldsBundle:Adress:index" } + +adress_show: + path: /{id}/show + defaults: { _controller: "CLCustomFieldsBundle:Adress:show" } + +adress_new: + path: /new + defaults: { _controller: "CLCustomFieldsBundle:Adress:new" } + +adress_create: + path: /create + defaults: { _controller: "CLCustomFieldsBundle:Adress:create" } + requirements: { _method: post } + +adress_edit: + path: /{id}/edit + defaults: { _controller: "CLCustomFieldsBundle:Adress:edit" } + +adress_update: + path: /{id}/update + defaults: { _controller: "CLCustomFieldsBundle:Adress:update" } + requirements: { _method: post|put } + +adress_delete: + path: /{id}/delete + defaults: { _controller: "CLCustomFieldsBundle:Adress:delete" } + requirements: { _method: post|delete } diff --git a/src/CL/CustomFieldsBundle/Resources/views/Adress/edit.html.twig b/src/CL/CustomFieldsBundle/Resources/views/Adress/edit.html.twig new file mode 100644 index 000000000..16495743b --- /dev/null +++ b/src/CL/CustomFieldsBundle/Resources/views/Adress/edit.html.twig @@ -0,0 +1,16 @@ +{% extends '::base.html.twig' %} + +{% block body -%} +

Adress edit

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

Adress list

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

Adress creation

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

Adress

+ + + + + + + + + + + + +
Id{{ entity.id }}
Data{{ entity.data }}
+ + +{% endblock %} diff --git a/src/CL/CustomFieldsBundle/Tests/Controller/AdressControllerTest.php b/src/CL/CustomFieldsBundle/Tests/Controller/AdressControllerTest.php new file mode 100644 index 000000000..6c6a360b7 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Tests/Controller/AdressControllerTest.php @@ -0,0 +1,55 @@ +request('GET', '/adress/'); + $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /adress/"); + $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_adress[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_adress[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()); + } + + */ +}