/**
  * @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()));
 }
Esempio n. 2
0
 /**
  * uploadDocument has to be the only one fonction used to persist Document.
  *
  * @param \Mgate\PubliBundle\Entity\RelatedDocument $document
  * @param \Mgate\PubliBundle\Entity\RelatedDocument $relatedDocument
  * @param bool                                      $deleteIfExist
  *
  * @return \Mgate\PubliBundle\Entity\Document
  *
  * @throws \Exception
  * @throws UploadException
  */
 public function uploadDocument(Document $document, RelatedDocument $relatedDocument = null, $deleteIfExist = false)
 {
     // Relations
     if ($relatedDocument) {
         $document->setRelation($relatedDocument);
         $relatedDocument->setDocument($document);
     }
     // Store each Junior documents in a distinct subdirectory
     if (!array_key_exists('id', $this->junior)) {
         throw new \Exception('Votre version de Incipio est obsolète. Contactez dsi@N7consulting.fr (incorrect parameters junior : id)');
     }
     $juniorId = $this->junior['id'];
     $document->setSubdirectory($juniorId);
     $document->setRootDir($this->kernel->getRootDir());
     // Authorized Storage Size Overflow
     $totalSize = $document->getSize() + $this->getRepository()->getTotalSize();
     if ($totalSize > $this->junior['authorizedStorageSize']) {
         throw new UploadException('Vous n\'avez plus d\'espace disponible ! Vous pouvez en demander plus à dsi@N7consulting.fr.');
     }
     // Delete every document with the same name
     if ($deleteIfExist) {
         $docs = $this->getRepository()->findBy(array('name' => $document->getName()));
         foreach ($docs as $doc) {
             if ($doc->getRelation()) {
                 $relation = $doc->getRelation();
                 $doc->setRelation();
                 $this->em->remove($relation);
             }
             $this->em->remove($doc);
         }
         //persistence de tout à la fin des actions.
         $this->em->flush();
     }
     $this->persistAndFlush($document);
     return $document;
 }