/** * @Security("has_role('ROLE_ADMIN')") */ public function uploadNewDoctypeAction(Request $request) { $data = array(); $form = $this->createForm(new DocTypeType(), $data); $session = $request->getSession(); if ($request->getMethod() == 'POST') { $form->handleRequest($this->get('request')); if ($form->isValid()) { $data = $form->getData(); // Création d'un fichier temporaire $file = $data['template']; $filename = sha1(uniqid(mt_rand(), true)); $filename .= '.' . $file->guessExtension(); $file->move('tmp/', $filename); $docxFullPath = 'tmp/' . $filename; // Extraction des infos XML $templatesXML = $this->getDocxContent($docxFullPath); $relationship = $this->getDocxRelationShip($docxFullPath); // Nettoyage des XML $templatesXMLTraite = array(); foreach ($templatesXML as $templateName => $templateXML) { $this->cleanDocxFields($templateXML); $this->cleanDocxTableRow($templateXML); $this->cleanDocxParagraph($templateXML); $this->linkDocxImages($templateXML, $relationship); $this->arrayPushAssoc($templatesXMLTraite, $templateName, $templateXML); } // Enregistrement dans le fichier temporaire $zip = new \ZipArchive(); $zip->open($docxFullPath); foreach ($templatesXMLTraite as $templateXMLName => $templateXMLContent) { $zip->deleteName('word/' . $templateXMLName); $zip->addFromString('word/' . $templateXMLName, $templateXMLContent); } $zip->close(); if (array_key_exists('etude', $data)) { $etude = $data['etude']; } else { $etude = null; } // Vérification du template (document étude) if ($etude && ($data['name'] == self::DOCTYPE_AVANT_PROJET || $data['name'] == self::DOCTYPE_CONVENTION_CLIENT || $data['name'] == self::DOCTYPE_SUIVI_ETUDE) && $data['verification'] && $this->publipostage($docxFullPath, self::ROOTNAME_ETUDE, $etude->getId(), true)) { $session->getFlashBag()->add('success', 'Le template a été vérifié, il ne contient pas d\'erreur'); } if (array_key_exists('etudiant', $data)) { $etudiant = $data['etudiant']; } else { $etudiant = null; } $etudiant = $data['etudiant']; // Vérification du template (document étudiant) if ($etudiant && ($data['name'] == self::DOCTYPE_CONVENTION_ETUDIANT || $data['name'] == self::DOCTYPE_DECLARATION_ETUDIANT_ETR) && $data['verification'] && $this->publipostage($docxFullPath, self::ROOTNAME_ETUDIANT, $etudiant->getId(), true)) { $session->getFlashBag()->add('success', 'Le template a été vérifié, il ne contient pas d\'erreur'); } // Enregistrement du template $em = $this->getDoctrine()->getManager(); $user = $this->getUser(); $personne = $user->getPersonne(); $file = new File($docxFullPath); $doc = new Document(); $doc->setAuthor($personne)->setName($data['name'])->setFile($file); $kernel = $this->get('kernel'); $doc->setRootDir($kernel->getRootDir()); $em->persist($doc); $docs = $em->getRepository('MgatePubliBundle:Document')->findBy(array('name' => $doc->getName())); foreach ($docs as $doc) { $doc->setRootDir($kernel->getRootDir()); $em->remove($doc); } $em->flush(); $session->getFlashBag()->add('success', 'Le document a été mis à jour : '); return $this->redirect($this->generateUrl('Mgate_publi_documenttype_upload')); } } return $this->render('MgatePubliBundle:DocType:upload.html.twig', array('form' => $form->createView())); }
/** * Upload un fichier de type UploadedFile sur le serveur. * * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file * @param array $authorizedMIMEType * @param string $name * @param \Mgate\PubliBundle\Entity\RelatedDocument $relatedDocument * @param bool $deleteIfExist * * @return \Mgate\PubliBundle\Entity\Document * * @throws \Exception */ public function uploadDocumentFromFile(UploadedFile $file, array $authorizedMIMEType, $name, RelatedDocument $relatedDocument = null, $deleteIfExist = false) { $document = new Document(); // MIME-type Check if (!in_array($file->getMimeType(), $authorizedMIMEType)) { // Erreur throw new \Exception('Le type de fichier n\'est pas autorisé.'); } // Author $user = $this->securityContext->getToken()->getUser(); $personne = $user->getPersonne(); $document->setAuthor($personne); // File $document->setFile($file); $document->setName($name); return $this->uploadDocument($document, $relatedDocument, $deleteIfExist); }