예제 #1
0
 /**
  * Create a Document from an embed media
  *
  * @param Pimple\Container $container description
  *
  * @return Document
  */
 public function createDocumentFromFeed(Container $container)
 {
     $url = $this->downloadThumbnail();
     if (!$this->exists()) {
         throw new \Exception('no.embed.document.found');
     }
     if (false !== $url) {
         $existingDocument = $container['em']->getRepository('RZ\\Roadiz\\Core\\Entities\\Document')->findOneBy(['filename' => $url]);
     } else {
         $existingDocument = $container['em']->getRepository('RZ\\Roadiz\\Core\\Entities\\Document')->findOneBy(['embedId' => $this->embedId, 'embedPlatform' => static::$platform]);
     }
     if (null !== $existingDocument) {
         throw new EntityAlreadyExistsException('embed.document.already_exists');
     }
     $document = new Document();
     $document->setEmbedId($this->embedId);
     $document->setEmbedPlatform(static::$platform);
     if (false !== $url) {
         /*
          * Move file from documents file root to its folder.
          */
         $document->setFilename($url);
         $document->setMimeType('image/jpeg');
         if (!file_exists(Document::getFilesFolder() . '/' . $document->getFolder())) {
             mkdir(Document::getFilesFolder() . '/' . $document->getFolder());
         }
         rename(Document::getFilesFolder() . '/' . $url, $document->getAbsolutePath());
     }
     $container['em']->persist($document);
     /*
      * Create document metas
      * for each translation
      */
     $translations = $container['em']->getRepository('RZ\\Roadiz\\Core\\Entities\\Translation')->findAll();
     foreach ($translations as $translation) {
         $documentTr = new DocumentTranslation();
         $documentTr->setDocument($document);
         $documentTr->setTranslation($translation);
         $documentTr->setName($this->getMediaTitle());
         $documentTr->setDescription($this->getMediaDescription());
         $documentTr->setCopyright($this->getMediaCopyright());
         $container['em']->persist($documentTr);
     }
     $container['em']->flush();
     return $document;
 }