Пример #1
0
 /**
  * createDocument
  *
  * @param string                                  $filename
  * @param string                                  $token
  * @param \Erichard\DmsBundle\Entity\DocumentNode $currentNode
  * @param \Erichard\DmsBundle\Entity\Document     $document
  */
 public function createDocument($filename, $token, $currentNode, $document)
 {
     $currentNode->removeEmptyMetadatas();
     if (null === $document->getId()) {
         $document->setName($filename);
     }
     $document->setOriginalName($filename);
     $document->setFilename($token);
     $document->removeEmptyMetadatas();
     $emn = $this->registry->getManager();
     $emn->persist($document);
     $emn->flush();
     foreach ($document->getNode()->getDocuments() as $sibling) {
         $sibling->removeEmptyMetadatas();
     }
     $storageTmpPath = $this->container->getParameter('dms.storage.tmp_path');
     $storagePath = $this->container->getParameter('dms.storage.path');
     $filesystem = $this->container->get('filesystem');
     $absTmpFilename = $storageTmpPath . '/' . $document->getFilename();
     $absFilename = $storagePath . '/' . $document->getComputedFilename();
     $finder = new Finder();
     $finder->files()->in($storageTmpPath)->name($document->getFilename());
     foreach ($finder as $file) {
         $this->addMetadatas($file, $document, 'document');
     }
     // Delete existing thumbnails
     if (is_dir($this->container->getParameter('dms.cache.path'))) {
         $finder = new Finder();
         $finder->files()->in($this->container->getParameter('dms.cache.path'))->name("{$document->getSlug()}.png");
         foreach ($finder as $file) {
             $filesystem->remove($file);
         }
     }
     // overwrite file
     if (is_file($absFilename)) {
         unlink($absFilename);
     } elseif (!$filesystem->exists(dirname($absFilename))) {
         $filesystem->mkdir(dirname($absFilename));
     }
     $filesystem->rename($absTmpFilename, $absFilename);
     $document->setFilename($document->getComputedFilename());
     $emn->persist($document);
     $emn->flush();
 }
 /**
  * addAction
  *
  * @param string      $node
  * @param null|string $document
  *
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  */
 public function addAction($node, $document = null)
 {
     $documentNode = $this->findNodeOrThrowError($node);
     $request = $this->get('request');
     if (null !== $document) {
         $document = $this->findDocumentOrThrowError($document, $node);
         $firstEdition = false;
     } else {
         $document = new Document($documentNode);
         $firstEdition = true;
     }
     if ($request->isMethod('GET')) {
         $params = array('node' => $documentNode);
         if (null !== $document->getId()) {
             $params['document'] = $document;
         }
         return $this->render('ErichardDmsBundle:Standard/Document:add.html.twig', $params);
     } else {
         $filename = $request->request->get('filename');
         $documentNode->removeEmptyMetadatas();
         if (null === $document->getId()) {
             $document->setName($filename);
         }
         $document->setOriginalName($filename);
         $document->setFilename($request->request->get('token'));
         $document->removeEmptyMetadatas();
         foreach ($document->getNode()->getDocuments() as $sibling) {
             $sibling->removeEmptyMetadatas();
         }
         $emn = $this->get('doctrine')->getManager();
         $emn->persist($document);
         $emn->flush();
         $storageTmpPath = $this->container->getParameter('dms.storage.tmp_path');
         $storagePath = $this->container->getParameter('dms.storage.path');
         $filesystem = $this->get('filesystem');
         $absTmpFilename = $storageTmpPath . '/' . $document->getFilename();
         $absFilename = $storagePath . '/' . $document->getComputedFilename();
         // Delete existing thumbnails
         if (is_dir($this->container->getParameter('dms.cache.path'))) {
             $finder = new Finder();
             $finder->files()->in($this->container->getParameter('dms.cache.path'))->name("{$document->getSlug()}.png");
             foreach ($finder as $file) {
                 $filesystem->remove($file);
             }
         }
         // overwrite file
         if (is_file($absFilename)) {
             unlink($absFilename);
         } elseif (!$filesystem->exists(dirname($absFilename))) {
             $filesystem->mkdir(dirname($absFilename));
         }
         $filesystem->rename($absTmpFilename, $absFilename);
         $document->setFilename($document->getComputedFilename());
         $emn->persist($document);
         $emn->flush();
         if ($this->get('event_dispatcher')->hasListeners(DmsEvents::DOCUMENT_ADD)) {
             $event = new DmsDocumentEvent($document);
             $this->get('event_dispatcher')->dispatch(DmsEvents::DOCUMENT_ADD, $event);
         }
         return $this->redirect($this->get('router')->generate('erichard_dms_edit_document', array('document' => $document->getSlug(), 'node' => $documentNode->getSlug(), 'first' => $firstEdition)));
     }
 }