Esempio n. 1
0
 public function editarAction(Request $request, $id)
 {
     $em = $this->getDoctrine()->getEntityManager();
     if (empty($id)) {
         $client = new Client();
     } else {
         $client = $em->getRepository('NiugClientBundle:Client')->find($id);
     }
     $mesInfo = array();
     $respostes = $client->getRespostes();
     foreach ($respostes as $resposta) {
         $mesInfo[] = array('id' => $resposta->getId(), 'pregunta' => $resposta->getPregunta()->getText(), 'resposta' => $resposta->getText());
     }
     $preguntesBd = $em->getRepository('NiugClientBundle:Pregunta')->findAll();
     foreach ($preguntesBd as $pregunta) {
         $preguntes[] = array('id' => $pregunta->getId(), 'text' => $pregunta->getText());
     }
     $form = $this->createFormBuilder($client)->add('nom', 'text', array('attr' => array('data-dojo-type' => 'dijit.form.TextBox')))->add('cognom1', 'text', array('attr' => array('data-dojo-type' => 'dijit.form.TextBox')))->add('cognom2', 'text', array('required' => false, 'attr' => array('data-dojo-type' => 'dijit.form.TextBox')))->add('mobil', 'text', array('required' => false, 'attr' => array('data-dojo-type' => 'dijit.form.TextBox')))->add('email', 'text', array('required' => false, 'attr' => array('data-dojo-type' => 'dijit.form.TextBox')))->add('poblacio', 'text', array('required' => false, 'attr' => array('data-dojo-type' => 'dijit.form.TextBox')))->add('provincia', 'text', array('required' => false, 'attr' => array('data-dojo-type' => 'dijit.form.TextBox')))->add('codiPostal', 'text', array('required' => false, 'attr' => array('data-dojo-type' => 'dijit.form.TextBox')))->getForm();
     if ($request->getMethod() == 'POST') {
         // Eliminar les preguntes del client
         foreach ($client->getRespostes() as $resposta) {
             $client->getRespostes()->removeElement($resposta);
             $em->remove($resposta);
         }
         $em->flush();
         $form->bindRequest($request);
         if (empty($id)) {
             $em->persist($client);
         }
         $em->flush();
         // Obtenir les preguntes/respostes del formulari
         $preguntes = $request->get('pregunta');
         $respostes = $request->get('resposta');
         if (isset($preguntes)) {
             $repositoriPregunta = $em->getRepository('NiugClientBundle:Pregunta');
             foreach ($preguntes as $clau => $preguntaText) {
                 // Afegir la pregunta a la bd
                 $pregunta = $repositoriPregunta->findOneByText($preguntaText);
                 if (!$pregunta) {
                     // Crear nova pregunta
                     $pregunta = new Pregunta();
                     $pregunta->setText($preguntaText);
                     $em->persist($pregunta);
                     $em->flush();
                 }
                 $resposta = new Resposta();
                 $resposta->setClient($client);
                 $resposta->setPregunta($pregunta);
                 $resposta->setText($respostes[$clau]);
                 $em->persist($resposta);
                 $em->flush();
             }
         }
         return $this->redirect($this->generateUrl('NiugClientBundle_clients'));
     } else {
         return $this->render('NiugClientBundle:Client:editar.html.twig', array('form' => $form->createView(), 'id' => $id, 'preguntes' => $preguntes, 'mesInfo' => $mesInfo));
     }
 }
Esempio n. 2
0
 public function guardarAction()
 {
     $request = $this->get('request')->request;
     echo "<pre>";
     print_r($request);
     die;
     $id = $request->get('id');
     $text = $request->get('text');
     $tipu = $request->get('tipu');
     $repositoriPreg = $this->getDoctrine()->getRepository('NiugClientBundle:Pregunta');
     $pregunta = $repositoriPreg->find($id);
     if (!$pregunta) {
         $pregunta = new Pregunta();
     }
     $pregunta->setText($text);
     $pregunta->setTipu($tipu);
     $em = $this->getDoctrine()->getManager();
     $em->persist($pregunta);
     $em->flush();
     return $this->redirect($this->generateUrl('NiugClientBundle_preguntes'));
 }