Exemplo n.º 1
0
 /**
  * Import a reseau params infos from CSV file.
  * TODO: refactor this method for performance
  * 
  * @param Request $request
  */
 public function importCSVAction(Request $request)
 {
     $form = $this->createFormBuilder()->add('submitFile', FileType::class, array('label' => 'Fichier CSV'))->getForm();
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         ini_set('max_execution_time', 0);
         // TODO: Change pdf file names column in the CSV file
         $pdfDir = $this->container->getParameter('resources_dir');
         $file = $form->get('submitFile')->getData();
         $data = $this->csvToArray($file);
         $em = $this->getDoctrine()->getManager();
         foreach ($data as $row) {
             $stream = fopen($pdfDir . '/' . $row['contrat'] . '/' . $row['pdf_source'], 'rb');
             $param = new Parametrage();
             $pdfSource = new Pdf();
             $pdfSource->setCurrent(1);
             $reseau = $em->getRepository('DocBundle:Reseau')->findOneByCode($row['reseaux']);
             if (null === $reseau) {
                 throw $this->createNotFoundException("Le réseau " . $row['reseaux'] . " n'existe pas.");
             }
             $param->setReseau($reseau);
             $pdfSource->setFile(stream_get_contents($stream));
             $pdfSource->setTitle($row['pdf_source']);
             $pdfSource->setParametrage($param);
             $param->addPdfSource($pdfSource);
             $param->setContrat($row['contrat']);
             $param->setCollectivites($row['collectivites']);
             $param->setType($row['type']);
             $param->setLibelle($row['libelle']);
             $param->setPartenaires('Tous');
             $param->setOrdre($row['ordre']);
             $param->setReference($row['reference']);
             $em->persist($param);
         }
         $em->flush();
         return $this->redirectToRoute('parametrage_index');
     }
     ini_set('max_execution_time', 60);
     return $this->render('DocBundle:parametrage:csv_form.html.twig', array('form' => $form->createView()));
 }