Example #1
0
 /**
  * @param array $image
  * @return int
  */
 public function importArticleImage(array $image)
 {
     if (empty($image) || !is_array($image)) {
         return false;
     }
     $image = $this->prepareImageData($image);
     if (empty($image['articleID']) || empty($image['image']) && empty($image['name'])) {
         return false;
     }
     $image['main'] = $this->setMain($image['main'], $image['articleID']);
     if (!strpos($image["image"], "http://") && !strpos($image["image"], "https://")) {
         $image["image"] = "http://" . $image["image"];
     }
     $uploadFile = $this->copyImage($image['image'], $image['name']);
     if ($uploadFile === false) {
         return false;
     }
     $media = new Media();
     $file = new File($uploadFile);
     $identity = Shopware()->Auth()->getIdentity();
     $userId = $identity !== null ? $identity->id : 0;
     $media->setUserId($userId);
     $media->setDescription($image['description']);
     $media->setCreated(new \DateTime());
     $media->setFile($file);
     /* @var ArticleRepository $articleRepository */
     $articleRepository = $this->getArticleRepository();
     $article = $articleRepository->find((int) $image['articleID']);
     if (!$article instanceof Article) {
         $this->logger->error("Article '{$image['articleID']}' not found!");
         return false;
     }
     $media->setAlbumId($image['albumID']);
     /* @var \Shopware\Models\Media\Album $album */
     $album = $this->em->find('Shopware\\Models\\Media\\Album', $image['albumID']);
     $media->setAlbum($album);
     $articleImage = new Image();
     list($width, $height) = getimagesize($uploadFile);
     $articleImage->setDescription($image['description']);
     $articleImage->setMedia($media);
     $articleImage->setArticle($article);
     $articleImage->setWidth($width);
     $articleImage->setHeight($height);
     $articleImage->setPath($image['name']);
     $articleImage->setExtension($media->getExtension());
     $articleImage->setPosition($image['position']);
     $articleImage->setMain($image['main']);
     $articleImage->setRelations($image['relations']);
     $article->setImages($articleImage);
     $this->em->persist($media);
     $this->em->persist($article);
     $this->em->persist($articleImage);
     $this->em->flush();
     return $articleImage->getId();
 }