Exemple #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;
    }
Exemple #2
0
 /**
  * Internal helper function to duplicate the download configuration from the passed article
  * to the new article.
  * @param integer $articleId
  * @param integer $newArticleId
  */
 protected function duplicateArticleDownloads($articleId, $newArticleId)
 {
     /** @var $article \Shopware\Models\Article\Article */
     $article = Shopware()->Models()->find('Shopware\\Models\\Article\\Article', $newArticleId);
     $builder = Shopware()->Models()->createQueryBuilder();
     $downloads = $builder->select(array('downloads', 'attribute'))->from('Shopware\\Models\\Article\\Download', 'downloads')->leftJoin('downloads.attribute', 'attribute')->where('downloads.articleId = ?1')->setParameter(1, $articleId)->getQuery()->getArrayResult();
     foreach ($downloads as $data) {
         $download = new \Shopware\Models\Article\Download();
         $download->fromArray($data);
         $download->setArticle($article);
         Shopware()->Models()->persist($download);
     }
     Shopware()->Models()->flush();
 }