diff --git a/src/CL/CustomFieldsBundle/Controller/BlopEntity2Controller.php b/src/CL/CustomFieldsBundle/Controller/BlopEntity2Controller.php new file mode 100644 index 000000000..ca455cdff --- /dev/null +++ b/src/CL/CustomFieldsBundle/Controller/BlopEntity2Controller.php @@ -0,0 +1,226 @@ +getDoctrine()->getManager(); + + $entities = $em->getRepository('CLCustomFieldsBundle:BlopEntity2')->findAll(); + + return $this->render('CLCustomFieldsBundle:BlopEntity2:index.html.twig', array( + 'entities' => $entities, + )); + } + /** + * Creates a new BlopEntity2 entity. + * + */ + public function createAction(Request $request) + { + $entity = new BlopEntity2(); + $form = $this->createCreateForm($entity); + $form->handleRequest($request); + + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($entity); + $em->flush(); + + return $this->redirect($this->generateUrl('blopentity2_show', array('id' => $entity->getId()))); + } + + return $this->render('CLCustomFieldsBundle:BlopEntity2:new.html.twig', array( + 'entity' => $entity, + 'form' => $form->createView(), + )); + } + + /** + * Creates a form to create a BlopEntity2 entity. + * + * @param BlopEntity2 $entity The entity + * + * @return \Symfony\Component\Form\Form The form + */ + private function createCreateForm(BlopEntity2 $entity) + { + $form = $this->createForm(new BlopEntity2Type(), $entity, array( + 'action' => $this->generateUrl('blopentity2_create'), + 'method' => 'POST', + 'em' => $this->getDoctrine()->getManager(), + )); + + $form->add('submit', 'submit', array('label' => 'Create')); + + return $form; + } + + /** + * Displays a form to create a new BlopEntity2 entity. + * + */ + public function newAction() + { + $entity = new BlopEntity2(); + $form = $this->createCreateForm($entity); + + return $this->render('CLCustomFieldsBundle:BlopEntity2:new.html.twig', array( + 'entity' => $entity, + 'form' => $form->createView(), + )); + } + + /** + * Finds and displays a BlopEntity2 entity. + * + */ + public function showAction($id) + { + $em = $this->getDoctrine()->getManager(); + + $entity = $em->getRepository('CLCustomFieldsBundle:BlopEntity2')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find BlopEntity2 entity.'); + } + + $deleteForm = $this->createDeleteForm($id); + + return $this->render('CLCustomFieldsBundle:BlopEntity2:show.html.twig', array( + 'entity' => $entity, + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Displays a form to edit an existing BlopEntity2 entity. + * + */ + public function editAction($id) + { + $em = $this->getDoctrine()->getManager(); + + $entity = $em->getRepository('CLCustomFieldsBundle:BlopEntity2')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find BlopEntity2 entity.'); + } + + $editForm = $this->createEditForm($entity); + $deleteForm = $this->createDeleteForm($id); + + return $this->render('CLCustomFieldsBundle:BlopEntity2:edit.html.twig', array( + 'entity' => $entity, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + )); + } + + /** + * Creates a form to edit a BlopEntity2 entity. + * + * @param BlopEntity2 $entity The entity + * + * @return \Symfony\Component\Form\Form The form + */ + private function createEditForm(BlopEntity2 $entity) + { + $form = $this->createForm(new BlopEntity2Type(), $entity, array( + 'action' => $this->generateUrl('blopentity2_update', array('id' => $entity->getId())), + 'method' => 'PUT', + 'em' => $this->getDoctrine()->getManager(), + )); + + $form->add('submit', 'submit', array('label' => 'Update')); + + return $form; + } + /** + * Edits an existing BlopEntity2 entity. + * + */ + public function updateAction(Request $request, $id) + { + $em = $this->getDoctrine()->getManager(); + + $entity = $em->getRepository('CLCustomFieldsBundle:BlopEntity2')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find BlopEntity2 entity.'); + } + + $deleteForm = $this->createDeleteForm($id); + $editForm = $this->createEditForm($entity); + $editForm->handleRequest($request); + + if ($editForm->isValid()) { + $em->flush(); + + return $this->redirect($this->generateUrl('blopentity2_edit', array('id' => $id))); + } + + return $this->render('CLCustomFieldsBundle:BlopEntity2:edit.html.twig', array( + 'entity' => $entity, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + )); + } + /** + * Deletes a BlopEntity2 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:BlopEntity2')->find($id); + + if (!$entity) { + throw $this->createNotFoundException('Unable to find BlopEntity2 entity.'); + } + + $em->remove($entity); + $em->flush(); + } + + return $this->redirect($this->generateUrl('blopentity2')); + } + + /** + * Creates a form to delete a BlopEntity2 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('blopentity2_delete', array('id' => $id))) + ->setMethod('DELETE') + ->add('submit', 'submit', array('label' => 'Delete')) + ->getForm() + ; + } +} diff --git a/src/CL/CustomFieldsBundle/Entity/BlopEntity2.php b/src/CL/CustomFieldsBundle/Entity/BlopEntity2.php new file mode 100644 index 000000000..1cbb0caf9 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Entity/BlopEntity2.php @@ -0,0 +1,248 @@ + obj dans des choses qui existent deja +// preFlush avant mise a jour de la db +// - met a jour les donnees liees au json et le json +// perPersist idem mais pour le persist + +/** + * BlopEntity2 + */ +class BlopEntity2 +{ + /** + * @var integer + */ + private $id; + + /** + * @var array + */ + private $customFieldData; + private $customFieldDataArray = array(); // customField apres json_decode + private $customFieldDataUnfolded = array(); // mise des entity de customFieldDataArray + + private $customFieldConfigs = array(); + + private $customFieldConfigsLoaded = false; + + // CHARGE DE LA DB LA CONFIG DES CUSTOM FIELDS + public function loadCustomFieldConfig(LifecycleEventArgs $args) + { + $em = $args->getObjectManager(); + + $customFields = $em + ->getRepository('CLCustomFieldsBundle:CustomField') + ->findAll(); + + $customFieldsLablels = array_map( + function($e) { return $e->getLabel(); }, + $customFields); + + $this->customFieldConfigs = array_combine($customFieldsLablels, $customFields); + $this->customFieldConfigsLoaded = true; + } + + // A PARTIR DU JSON CREE LES OBJETS (MIS DANS customFieldDataUnfolded) + public function unfoldCustomFieldData(LifecycleEventArgs $args) + { + $em = $args->getObjectManager(); + + $customFieldDataArray = json_decode($this->customFieldData,true); + $customFieldDataUnfolded = array(); + + foreach ($this->customFieldConfigs as $key => $cfConfig) { + $type = $cfConfig->getType(); + if(strpos($type,'ManyToMany') === 0) { + $fieldUnfolded = new ArrayCollection(); + + if(array_key_exists($key, $customFieldDataArray)) { + $entityClass = substr($type, 11, -1); + + foreach ($customFieldDataArray[$key] as $idEntity) { + $fieldUnfolded->add($em + ->getRepository('CLCustomFieldsBundle:' . $entityClass) + ->findOneById($idEntity)); + } + } + + $customFieldDataUnfolded[$key] = $fieldUnfolded; + } else if(strpos($type,'ManyToOne') === 0) { + $entityClass = 'Adress'; // substr($type,10,-1); + if(array_key_exists($key, $customFieldDataArray)) { + $customFieldDataUnfolded[$key] = $em + ->getRepository('CLCustomFieldsBundle:' . $entityClass) + ->findOneById($customFieldDataArray[$key]); + } else { + // TODO : doit tjs avoir un id + $em + ->getRepository('CLCustomFieldsBundle:' . $entityClass) + ->findOneById(1); + } + } + else if ($type === 'text') { + if(array_key_exists($key, $customFieldDataArray)) { + $customFieldDataUnfolded[$key] = $customFieldDataArray[$key]; + } else { + $customFieldDataUnfolded[$key] = ''; + } + } + } + + $this->customFieldDataUnfolded = $customFieldDataUnfolded; + } + + // AVANT PERSIST LES ELEMENTS QUI N'ONT PAS D'ID DOIVENT EN AVOIR UN (OM->PERSIST(OBJ)) + // PUIS PASSAGE DES OBJETS (SE TROUVANT DANS customFieldDataUnfolded) VERS + // LE JSON (SE TROUVANT DANS customFieldData) + public function prePersist(LifecycleEventArgs $args) + { + $em = $args->getObjectManager(); + + $this->loadCustomFieldConfig($args); + + foreach ($this->customFieldDataUnfolded as $key => $unfoldedData) { + $type = $this->customFieldConfigs[$key]->getType(); + if(strpos($type,'ManyToMany') === 0) { + foreach ($this->customFieldDataUnfolded[$key] as $entity) { + if(! $entity->getId()) { + $em->persist($entity); + } + } + } else if(strpos($type,'ManyToOne') === 0) { + if(! $this->customFieldDataUnfolded[$key]->getId()) { + $em->persist($this->customFieldDataUnfolded[$key]); + } + } + } + + $this->customFieldDataUnfoldedToCustomField(); + } + + // PUIS PASSAGE DES OBJETS (SE TROUVANT DANS customFieldDataUnfolded) VERS + // LE JSON (SE TROUVANT DANS customFieldData) + public function preFlush(PreFlushEventArgs $args) + { + $this->customFieldDataUnfoldedToCustomField(); + } + + // PUIS PASSAGE DES OBJETS (SE TROUVANT DANS customFieldDataUnfolded) VERS + // LE JSON (SE TROUVANT DANS customFieldData) + public function customFieldDataUnfoldedToCustomField() + { + // MISE A JOUR DE customFieldDataArray + foreach ($this->customFieldConfigs as $key => $cfConfig) { + $type = $cfConfig->getType(); + if(strpos($type,'ManyToMany') === 0) { + $arrayMapRet = array(); + foreach ($this->customFieldDataUnfolded[$key] as $entity) { + $arrayMapRet[] = $entity->getId(); + } + $this->customFieldDataArray[$key] = $arrayMapRet; // array_map(function($e) { $e->getId(); }, $this->customFieldDataUnfolded[$key]); + } else if(strpos($type,'ManyToOne') === 0) { + if(array_key_exists($key, $this->customFieldDataUnfolded)) { + $this->customFieldDataArray[$key] = $this->customFieldDataUnfolded[$key]->getId(); + } else { + // normalement $this->customFieldDataArray[$key] ne doit pas exister + if(array_key_exists($key, $this->customFieldDataArray)) { + throw new Exception("Error Processing Request", 1); + } + //retirer de $this->customFieldDataArray[$key] + } + } else if ($type === 'text') { + $this->customFieldDataArray[$key] = $this->customFieldDataUnfolded[$key]; + } + } + + // MISE A JOUR DE CustomFieldData + $this->setCustomFieldData(json_encode($this->customFieldDataArray)); + } + + public function __set($fieldName, $value) { + $setMethodName = 'set' . ucfirst($fieldName); + + if(method_exists($this, $setMethodName)) { + return $this->{$setMethodName}($value); + } + + if(array_key_exists($fieldName, $this->customFieldConfigs)) { + $this->customFieldDataUnfolded[$fieldName] = $value; + } else if (!$this->customFieldConfigsLoaded) { // nouvel object pas eu d'appel doctrine avant + $this->customFieldDataUnfolded[$fieldName] = $value; + } else { + throw new Exception("Error Processing Request", 1); + } + } + + public function __get($fieldName) { + $getMethodName = 'get' . ucfirst($fieldName); + + if(method_exists($this, $getMethodName)) { + return $this->{$getMethodName}(); + } + + if(array_key_exists($fieldName, $this->customFieldDataUnfolded)) { + return $this->customFieldDataUnfolded[$fieldName]; + } else if (!$this->customFieldConfigsLoaded) { // nouvel object pas eu d'appel doctrine avant + return null; + } else if(array_key_exists($fieldName, $this->customFieldConfigs)) { // pas init + return null; + } else { + throw new Exception("Error Processing Request", 1); + } + } + + + /** + * Get id + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * Set customField + * + * @param array $customField + * + * @return BlopEntity2 + */ + public function setCustomFieldData($customFieldData) + { + $this->customFieldData = $customFieldData; + return $this; + } + + /** + * Get customField + * + * @return array + */ + public function getCustomFieldData() + { + return $this->customFieldData; + } +} + diff --git a/src/CL/CustomFieldsBundle/Form/BlopEntity2Type.php b/src/CL/CustomFieldsBundle/Form/BlopEntity2Type.php new file mode 100644 index 000000000..8ccf8f2d4 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Form/BlopEntity2Type.php @@ -0,0 +1,69 @@ +getRepository('CLCustomFieldsBundle:CustomField') + ->findAll(); + + 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)') { + $builder->add($cf->getLabel(), 'entity', array( + 'class' => 'CLCustomFieldsBundle:Adress', + 'property' => 'data', + 'multiple' => true + )); + } else if ($cf->getType() === 'text') { + $builder->add($cf->getLabel(), 'text'); + } + } + } + + /** + * @param OptionsResolverInterface $resolver + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'CL\CustomFieldsBundle\Entity\BlopEntity2' + )); + + // supprimer ça en definissant dans services + $resolver->setRequired(array( + 'em', + )); + + $resolver->setAllowedTypes(array( + 'em' => 'Doctrine\Common\Persistence\ObjectManager', + )); + } + + /** + * @return string + */ + public function getName() + { + return 'cl_customfieldsbundle_blopentity2'; + } +} diff --git a/src/CL/CustomFieldsBundle/Resources/config/doctrine/BlopEntity2.orm.yml b/src/CL/CustomFieldsBundle/Resources/config/doctrine/BlopEntity2.orm.yml new file mode 100644 index 000000000..c5f3b60ef --- /dev/null +++ b/src/CL/CustomFieldsBundle/Resources/config/doctrine/BlopEntity2.orm.yml @@ -0,0 +1,16 @@ +CL\CustomFieldsBundle\Entity\BlopEntity2: + type: entity + table: null + id: + id: + type: integer + id: true + generator: + strategy: AUTO + fields: + customFieldData: + type: json_array + lifecycleCallbacks: + postLoad: [ loadCustomFieldConfig, unfoldCustomFieldData ] + preFlush: [ preFlush ] + prePersist: [ prePersist ] \ 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 cd618c827..cb112926f 100644 --- a/src/CL/CustomFieldsBundle/Resources/config/routing.yml +++ b/src/CL/CustomFieldsBundle/Resources/config/routing.yml @@ -1,3 +1,7 @@ +cl_custom_fields_blopentity2: + resource: "@CLCustomFieldsBundle/Resources/config/routing/blopentity2.yml" + prefix: /blopentity2 + cl_custom_fields_adress: resource: "@CLCustomFieldsBundle/Resources/config/routing/adress.yml" prefix: /adress diff --git a/src/CL/CustomFieldsBundle/Resources/config/routing/blopentity2.yml b/src/CL/CustomFieldsBundle/Resources/config/routing/blopentity2.yml new file mode 100644 index 000000000..eeba6f199 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Resources/config/routing/blopentity2.yml @@ -0,0 +1,30 @@ +blopentity2: + path: / + defaults: { _controller: "CLCustomFieldsBundle:BlopEntity2:index" } + +blopentity2_show: + path: /{id}/show + defaults: { _controller: "CLCustomFieldsBundle:BlopEntity2:show" } + +blopentity2_new: + path: /new + defaults: { _controller: "CLCustomFieldsBundle:BlopEntity2:new" } + +blopentity2_create: + path: /create + defaults: { _controller: "CLCustomFieldsBundle:BlopEntity2:create" } + requirements: { _method: post } + +blopentity2_edit: + path: /{id}/edit + defaults: { _controller: "CLCustomFieldsBundle:BlopEntity2:edit" } + +blopentity2_update: + path: /{id}/update + defaults: { _controller: "CLCustomFieldsBundle:BlopEntity2:update" } + requirements: { _method: post|put } + +blopentity2_delete: + path: /{id}/delete + defaults: { _controller: "CLCustomFieldsBundle:BlopEntity2:delete" } + requirements: { _method: post|delete } diff --git a/src/CL/CustomFieldsBundle/Resources/views/BlopEntity2/edit.html.twig b/src/CL/CustomFieldsBundle/Resources/views/BlopEntity2/edit.html.twig new file mode 100644 index 000000000..c60322926 --- /dev/null +++ b/src/CL/CustomFieldsBundle/Resources/views/BlopEntity2/edit.html.twig @@ -0,0 +1,16 @@ +{% extends '::base.html.twig' %} + +{% block body -%} +

BlopEntity2 edit

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

BlopEntity2 list

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

BlopEntity2 creation

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

BlopEntity2

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