From c0c36bc1ec765d801287b3f8fffaf2b76fcbe68b Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Sat, 8 Nov 2014 18:15:56 +0100 Subject: [PATCH] cFGroup selection by Id --- Controller/ReportController.php | 36 ++++++++++++++++++++++++++++----- Form/ReportType.php | 18 ++++++++++++++++- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/Controller/ReportController.php b/Controller/ReportController.php index 2e3b068ef..3a5df68cb 100644 --- a/Controller/ReportController.php +++ b/Controller/ReportController.php @@ -30,8 +30,8 @@ class ReportController extends Controller $cFGroupsChoice = array(); - foreach ($cFGroups as $g) { - $cFGroupsChoice[$g->getId()] = $g->getName($request->getLocale()); + foreach ($cFGroups as $cFGroup) { + $cFGroupsChoice[$cFGroup->getId()] = $cFGroup->getName($request->getLocale()); } $form = $this->get('form.factory') @@ -40,7 +40,7 @@ class ReportController extends Controller 'action' => $this->generateUrl('report_new'), 'csrf_protection' => false )) - ->add('type', 'choice', array( + ->add('cFGroup', 'choice', array( 'choices' => $cFGroupsChoice )) ->getForm(); @@ -57,7 +57,20 @@ class ReportController extends Controller */ public function createAction(Request $request) { + $em = $this->getDoctrine()->getManager(); + $entity = new Report(); + + $cFGroupId = $request->query->get('cFGroup',null); + echo $cFGroupId; + die(); + + if($cFGroupId) { + $entity.setCFGroup( + $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($cFGroupID) + ); + } + $form = $this->createCreateForm($entity); $form->handleRequest($request); @@ -87,6 +100,7 @@ class ReportController extends Controller $form = $this->createForm(new ReportType(), $entity, array( 'action' => $this->generateUrl('report_create'), 'method' => 'POST', + 'em' => $this->getDoctrine()->getManager(), )); $form->add('submit', 'submit', array('label' => 'Create')); @@ -98,10 +112,21 @@ class ReportController extends Controller * Displays a form to create a new Report entity. * */ - public function newAction() + public function newAction(Request $request) { + $em = $this->getDoctrine()->getManager(); + $entity = new Report(); - $form = $this->createCreateForm($entity); + + $cFGroupId = $request->query->get('cFGroup'); + + if($cFGroupId) { + $entity->setCFGroup( + $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($cFGroupId) + ); + } + + $form = $this->createCreateForm($entity); return $this->render('ChillReportBundle:Report:new.html.twig', array( 'entity' => $entity, @@ -167,6 +192,7 @@ class ReportController extends Controller $form = $this->createForm(new ReportType(), $entity, array( 'action' => $this->generateUrl('report_update', array('id' => $entity->getId())), 'method' => 'PUT', + 'em' => $this->getDoctrine()->getManager(), )); $form->add('submit', 'submit', array('label' => 'Update')); diff --git a/Form/ReportType.php b/Form/ReportType.php index 566b8201e..065bac978 100644 --- a/Form/ReportType.php +++ b/Form/ReportType.php @@ -6,6 +6,8 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldsGroupToIdTransformer; + class ReportType extends AbstractType { /** @@ -14,13 +16,19 @@ class ReportType extends AbstractType */ public function buildForm(FormBuilderInterface $builder, array $options) { + $entityManager = $options['em']; + $transformer = new CustomFieldsGroupToIdTransformer($entityManager); + $builder ->add('user') ->add('person') ->add('date') ->add('scope') ->add('cFData') - ->add('cFGroup') + ->add( + $builder->create('cFGroup', 'text') + ->addModelTransformer($transformer) + ) ; } @@ -32,6 +40,14 @@ class ReportType extends AbstractType $resolver->setDefaults(array( 'data_class' => 'Chill\ReportBundle\Entity\Report' )); + + $resolver->setRequired(array( + 'em', + )); + + $resolver->setAllowedTypes(array( + 'em' => 'Doctrine\Common\Persistence\ObjectManager', + )); } /**