/** * @param array $data * @param \Shopware\Models\Article\Article $article * @throws \Shopware\Components\Api\Exception\CustomValidationException * @return array */ private function prepareImageAssociatedData($data, ArticleModel $article) { if (!isset($data['images'])) { return $data; } $images = $article->getImages(); $position = 1; foreach ($data['images'] as &$imageData) { if (isset($imageData['id'])) { $image = $this->getManager() ->getRepository('Shopware\Models\Article\Image') ->find($imageData['id']); if (!$image instanceof \Shopware\Models\Article\Image) { throw new ApiException\CustomValidationException(sprintf("Image by id %s not found", $imageData['id'])); } } else { $image = new \Shopware\Models\Article\Image(); } if (isset($imageData['link'])) { $path = $this->load($imageData['link']); $file = new \Symfony\Component\HttpFoundation\File\File($path); $media = new \Shopware\Models\Media\Media(); $media->setAlbumId(-1); $media->setAlbum($this->getManager()->find('Shopware\Models\Media\Album', -1)); $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")); } $image->setMain(2); $image->setMedia($media); $image->setPosition($position); $image->setArticle($article); $position++; $image->setPath($media->getName()); $image->setExtension($media->getExtension()); } $image->fromArray($imageData); $images->add($image); } unset($data['images']); return $data; }
/** * @param $options * @param $imageData * @param $parent \Shopware\Models\Article\Image */ protected function createImagesForOptions($options, $imageData, $parent) { $articleId = $parent->getArticle()->getId(); $imageData['path'] = null; $imageData['parent'] = $parent; $join = ''; foreach ($options as $option) { $alias = 'alias' . $option->getId(); $join = $join . ' INNER JOIN s_article_configurator_option_relations alias' . $option->getId() . ' ON ' . $alias . '.option_id = ' . $option->getId() . ' AND ' . $alias . '.article_id = d.id '; } $sql = "SELECT d.id\n FROM s_articles_details d\n " . $join . "\n WHERE d.articleID = " . (int) $articleId; $details = Shopware()->Db()->fetchCol($sql); foreach ($details as $detailId) { $detail = Shopware()->Models()->getReference('Shopware\\Models\\Article\\Detail', $detailId); $image = new \Shopware\Models\Article\Image(); $image->fromArray($imageData); $image->setArticleDetail($detail); Shopware()->Models()->persist($image); } Shopware()->Models()->flush(); }
/** * Helper method which creates images for variants based on the image mappings * @param $articleId */ protected function recreateVariantImages($articleId) { $builder = Shopware()->Models()->createQueryBuilder(); $images = $builder->select(array('images', 'mappings', 'rules', 'option'))->from('Shopware\\Models\\Article\\Image', 'images')->innerJoin('images.mappings', 'mappings')->leftJoin('mappings.rules', 'rules')->leftJoin('rules.option', 'option')->where('images.articleId = ?1')->andWhere('images.parentId IS NULL')->setParameter(1, $articleId)->getQuery(); $images = $images->execute(); /** @var \Shopware\Models\Article\Image $image */ foreach ($images as $image) { $query = $this->getArticleRepository()->getArticleImageDataQuery($image->getId()); $imageData = $query->getOneOrNullResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY); $this->getArticleRepository()->getDeleteImageChildrenQuery($image->getId())->execute(); foreach ($image->getMappings() as $mapping) { $options = array(); foreach ($mapping->getRules() as $rule) { $options[] = $rule->getOption(); } $imageData['path'] = null; $imageData['parent'] = $image; $details = $this->getArticleRepository()->getDetailsForOptionIdsQuery($articleId, $options)->getResult(); foreach ($details as $detail) { $newImage = new \Shopware\Models\Article\Image(); $newImage->fromArray($imageData); $newImage->setArticleDetail($detail); Shopware()->Models()->persist($newImage); Shopware()->Models()->flush(); } } } }
/** * @param $options * @param $imageData * @param $parent \Shopware\Models\Article\Image */ protected function createImagesForOptions($options, $imageData, $parent) { $articleId = $parent->getArticle()->getId(); $imageData['path'] = null; $imageData['parent'] = $parent; $details = $this->getRepository()->getDetailsForOptionIdsQuery($articleId, $options)->getResult(); foreach($details as $detail) { $image = new \Shopware\Models\Article\Image(); $image->fromArray($imageData); $image->setArticleDetail($detail); Shopware()->Models()->persist($image); } Shopware()->Models()->flush(); }