Beispiel #1
0
    /**
     * @param array $data
     * @param \Shopware\Models\Article\Article $article
     * @throws \Shopware\Components\Api\Exception\CustomValidationException
     * @return array
     */
    private function prepareDownloadsAssociatedData($data, ArticleModel $article)
    {
        if (!isset($data['downloads'])) {
            return $data;
        }

        $downloads = array();
        foreach ($data['downloads'] as &$downloadData) {
            if (isset($downloadData['id'])) {
                $download = $this->getManager()
                                 ->getRepository('Shopware\Models\Article\Download')
                                 ->find($downloadData['id']);

                if (!$download instanceof \Shopware\Models\Article\Download) {
                    throw new ApiException\CustomValidationException(sprintf("Download by id %s not found", $downloadData['id']));
                }
            } else {
                $download = new \Shopware\Models\Article\Download();
            }

            if (isset($downloadData['link'])) {
                $path = $this->load($downloadData['link']);
                $file = new \Symfony\Component\HttpFoundation\File\File($path);

                $media = new \Shopware\Models\Media\Media();
                $media->setAlbumId(-6);
                $media->setAlbum($this->getManager()->find('Shopware\Models\Media\Album', -6));

                $media->setFile($file);
                $media->setDescription('');
                $media->setCreated(new \DateTime());
                $media->setUserId(0);

                try { //persist the model into the model manager
                    $this->getManager()->persist($media);
                    $this->getManager()->flush();
                } catch (\Doctrine\ORM\ORMException $e) {
                    throw new ApiException\CustomValidationException(sprintf("Some error occured while loading your image"));
                }

                $download->setFile($media->getPath());
                $download->setName($media->getName());
                $download->setSize($media->getFileSize());
            }

            $download->fromArray($downloadData);
            $downloads[] = $download;
        }
        $data['downloads'] = $downloads;

        return $data;
    }
Beispiel #2
0
 /**
  * Insert an article download
  *
  * @param unknown_type $article_download
  * @return unknown
  */
 public function sArticleDownload($article_download)
 {
     if (empty($article_download) || !is_array($article_download)) {
         return false;
     }
     $article_download['articleID'] = $this->sGetArticleID($article_download);
     if (empty($article_download['articleID'])) {
         return false;
     }
     if (empty($article_download["name"]) && empty($article_download["link"])) {
         return false;
     }
     if (empty($article_download["name"])) {
         $article_download["name"] = basename($article_download["link"]);
     }
     // In sw4 description will only be used for the media model.
     if (empty($article_download["description"])) {
         $article_download["description"] = basename($article_download["name"]);
     }
     if (!empty($article_download["link"])) {
         $article_download["new_link"] = Shopware()->DocPath('media_' . 'temp') . basename($article_download["link"]);
     } else {
         $article_download["new_link"] = Shopware()->DocPath('media_' . 'temp') . $article_download["name"];
     }
     if (strpos($article_download["link"], Shopware()->DocPath('media_' . 'temp')) !== false) {
         $this->sAPI->sSetError("Please don't copy your source files to media/temp", 10410);
         return false;
     }
     if (!empty($article_download["link"])) {
         if (file_exists($article_download["new_link"])) {
             unlink($article_download["new_link"]);
         }
         if (!copy($article_download["link"], $article_download["new_link"])) {
             return false;
         }
         if (!empty($article_download["unlink"]) && file_exists($article_download["link"])) {
             unlink($article_download["link"]);
         }
     } else {
         if (!file_exists($article_download["new_link"])) {
             return false;
         }
     }
     $article_download["size"] = filesize($article_download["new_link"]);
     $article_download["name"] = $article_download["name"];
     // Create new Media object and set the download
     $media = new \Shopware\Models\Media\Media();
     $file = new \Symfony\Component\HttpFoundation\File\File($article_download["new_link"]);
     $media->setDescription($article_download['description']);
     $media->setCreated(new DateTime());
     $identity = Shopware()->Auth()->getIdentity();
     if ($identity !== null) {
         $media->setUserId($identity->id);
     } else {
         $media->setUserId(0);
     }
     //set the upload file into the model. The model saves the file to the directory
     $media->setFile($file);
     $pathInfo = pathinfo($article_download["new_link"]);
     if ($media->getExtension() === null && $pathInfo['extension'] === null) {
         $this->sAPI->sSetError("In SW4 files must have an extension", 10409);
         return false;
     } elseif ($pathInfo['extension'] !== null) {
         $media->setExtension($pathInfo['extension']);
     }
     // Set article
     $articleRepository = $this->getArticleRepository();
     $article = $articleRepository->find((int) $article_download['articleID']);
     if ($article === null) {
         $this->sAPI->sSetError("Article '{$article_download['articleID']}' not found", 10408);
         return false;
     }
     $media->setArticles($article);
     //set media album, if no one passed set the unsorted album.
     if (isset($article_download["albumID"])) {
         $media->setAlbumId($article_download["albumID"]);
         $media->setAlbum(Shopware()->Models()->find('Shopware\\Models\\Media\\Album', $article_download["albumID"]));
     } else {
         $media->setAlbumId(-10);
         $media->setAlbum(Shopware()->Models()->find('Shopware\\Models\\Media\\Album', -10));
     }
     // Create new article download object and set values
     $articleDownload = new \Shopware\Models\Article\Download();
     // ArticleDownload does not get these infos automatically from media, so we set them manually
     $articleDownload->setArticle($article);
     $articleDownload->setSize($article_download["size"]);
     $articleDownload->setFile($media->getPath());
     $articleDownload->setName($article_download["name"]);
     $article->setDownloads($articleDownload);
     Shopware()->Models()->persist($media);
     Shopware()->Models()->persist($article);
     Shopware()->Models()->persist($articleDownload);
     Shopware()->Models()->flush();
     return (int) $articleDownload->getId();
 }