From f702d76e681aff44c672c0dd7517545ea1da2800 Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Mon, 17 Nov 2014 17:59:06 +0100 Subject: [PATCH] Adding CustomFieldsDefaultGroup Entity for specifying that a group is a default group ref #307 --- .../CustomFieldsDefaultGroupController.php | 77 +++++++++++++ Entity/CustomFieldsDefaultGroup.php | 101 ++++++++++++++++++ .../doctrine/CustomFieldsDefaultGroup.orm.yml | 20 ++++ Resources/config/routing.yml | 13 ++- .../routing/customfieldsdefaultgroup.yml | 7 ++ .../CustomFieldsDefaultGroup/list.html.twig | 30 ++++++ 6 files changed, 243 insertions(+), 5 deletions(-) create mode 100644 Controller/CustomFieldsDefaultGroupController.php create mode 100644 Entity/CustomFieldsDefaultGroup.php create mode 100644 Resources/config/doctrine/CustomFieldsDefaultGroup.orm.yml create mode 100644 Resources/config/routing/customfieldsdefaultgroup.yml create mode 100644 Resources/views/CustomFieldsDefaultGroup/list.html.twig diff --git a/Controller/CustomFieldsDefaultGroupController.php b/Controller/CustomFieldsDefaultGroupController.php new file mode 100644 index 000000000..860d3c554 --- /dev/null +++ b/Controller/CustomFieldsDefaultGroupController.php @@ -0,0 +1,77 @@ +getDoctrine()->getManager(); + + $defaultGroups = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsDefaultGroup')->findAll(); + + $form = $this->get('form.factory') + ->createNamedBuilder(null, 'form', null, array( + 'method' => 'GET', + 'action' => $this->generateUrl('customfieldsdefaultgroup_set'), + 'csrf_protection' => false + )) + ->add('cFGroup', 'entity', array( + 'class' => 'ChillCustomFieldsBundle:CustomFieldsGroup', + 'property' => 'name[fr]' + )) + ->getForm(); + + return $this->render('ChillCustomFieldsBundle:CustomFieldsDefaultGroup:list.html.twig', array( + 'defaultGroups' => $defaultGroups, + 'form' => $form->createView() + )); + } + + /** + * Set the CustomField Group with id $cFGroupId as default + */ + public function setAGroupAsDefaultAction(Request $request) + { + $cFGroupId = $request->query->get('cFGroup'); + + $em = $this->getDoctrine()->getManager(); + + $cFGroup = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->findOneById($cFGroupId); + + if(!$cFGroup) { + throw new Exception("No CF GROUP with ID".$cFGroupId, 1); + } + + $cFDefaultGroup = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsDefaultGroup') + ->findOneByEntity($cFGroup->getEntity()); + + if($cFDefaultGroup) { + $em->remove($cFDefaultGroup); + $em->flush(); + } + + $newCFDefaultGroup = new CustomFieldsDefaultGroup(); + $newCFDefaultGroup->setCustomFieldsGroup($cFGroup); + $newCFDefaultGroup->setEntity($cFGroup->getEntity()); + + $em->persist($newCFDefaultGroup); + $em->flush(); + + return $this->redirect($this->generateUrl('customfieldsdefaultgroup')); + } +} \ No newline at end of file diff --git a/Entity/CustomFieldsDefaultGroup.php b/Entity/CustomFieldsDefaultGroup.php new file mode 100644 index 000000000..61858294d --- /dev/null +++ b/Entity/CustomFieldsDefaultGroup.php @@ -0,0 +1,101 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\CustomFieldsBundle\Entity; + +/** + * CustomFieldsDefaultGroup + */ +class CustomFieldsDefaultGroup +{ + /** + * @var integer + */ + private $id; + + /** + * @var string + */ + private $entity; + + /** + * @var CustomFieldsGroup + */ + private $customFieldsGroup; + + /** + * Get id + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * Set entity + * + * @param string $entity + * + * @return CustomFieldsDefaultGroup + */ + public function setEntity($entity) + { + $this->entity = $entity; + + return $this; + } + + /** + * Get entity + * + * @return string + */ + public function getEntity() + { + return $this->entity; + } + + /** + * Set customFieldsGroup + * + * @param CustomFieldsGroup $customFieldsGroup + * + * @return CustomFieldsDefaultGroup + */ + public function setCustomFieldsGroup($customFieldsGroup) + { + $this->customFieldsGroup = $customFieldsGroup; + + return $this; + } + + /** + * Get customFieldsGroup + * + * @return CustomFieldsGroup + */ + public function getCustomFieldsGroup() + { + return $this->customFieldsGroup; + } +} diff --git a/Resources/config/doctrine/CustomFieldsDefaultGroup.orm.yml b/Resources/config/doctrine/CustomFieldsDefaultGroup.orm.yml new file mode 100644 index 000000000..7d8f6b665 --- /dev/null +++ b/Resources/config/doctrine/CustomFieldsDefaultGroup.orm.yml @@ -0,0 +1,20 @@ +Chill\CustomFieldsBundle\Entity\CustomFieldsDefaultGroup: + type: entity + table: null + id: + id: + type: integer + id: true + generator: + strategy: AUTO + fields: + entity: + type: string + length: 255 + manyToOne: + customFieldsGroup: + targetEntity: Chill\CustomFieldsBundle\Entity\CustomFieldsGroup + inversedBy: customFields + uniqueConstraints: + unique_entity: + columns: [ entity ] \ No newline at end of file diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index 27fc0b21e..da9956fda 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -1,20 +1,23 @@ -cl_custom_fields_customfieldsgroup: +chill_customfields_customfieldsgroup: resource: "@ChillCustomFieldsBundle/Resources/config/routing/customfieldsgroup.yml" prefix: / -cl_custom_fields_blopentity2: +chill_customfields_blopentity2: resource: "@ChillCustomFieldsBundle/Resources/config/routing/blopentity2.yml" prefix: /blopentity2 -cl_custom_fields_adress: +chill_customfields_adress: resource: "@ChillCustomFieldsBundle/Resources/config/routing/adress.yml" prefix: /adress -cl_custom_fields_customfield: +chill_customfields_customfield: resource: "@ChillCustomFieldsBundle/Resources/config/routing/customfield.yml" prefix: / -cl_custom_fields_blopentity: +chill_customfields_blopentity: resource: "@ChillCustomFieldsBundle/Resources/config/routing/blopentity.yml" prefix: / +chill_customfields_customfieldsdefaultgroup: + resource: "@ChillCustomFieldsBundle/Resources/config/routing/customfieldsdefaultgroup.yml" + prefix: / \ No newline at end of file diff --git a/Resources/config/routing/customfieldsdefaultgroup.yml b/Resources/config/routing/customfieldsdefaultgroup.yml new file mode 100644 index 000000000..7c961035a --- /dev/null +++ b/Resources/config/routing/customfieldsdefaultgroup.yml @@ -0,0 +1,7 @@ +customfieldsdefaultgroup: + path: /{_locale}/admin/customfieldsdefaultgroup/ + defaults: { _controller: "ChillCustomFieldsBundle:CustomFieldsDefaultGroup:list" } + +customfieldsdefaultgroup_set: + path: /{_locale}/admin/customfieldsdefaultgroup/set/group/as/default/ + defaults: { _controller: "ChillCustomFieldsBundle:CustomFieldsDefaultGroup:setAGroupAsDefault" } diff --git a/Resources/views/CustomFieldsDefaultGroup/list.html.twig b/Resources/views/CustomFieldsDefaultGroup/list.html.twig new file mode 100644 index 000000000..00a697b56 --- /dev/null +++ b/Resources/views/CustomFieldsDefaultGroup/list.html.twig @@ -0,0 +1,30 @@ +{% extends '::base.html.twig' %} + +{% block body -%} +

CustomFieldsDefaultGroup list

+ + + + + + + + + + {% for defaultGroup in defaultGroups %} + + + + + {% endfor %} + +
EntityCustomFieldGroup
{{ defaultGroup.entity }}{{ defaultGroup.customFieldsGroup.name['fr'] }}
+ + {{ form_start(form) }} + {{ form_row(form.cFGroup) }} + + + {{ form_end(form) }} +{% endblock %}