/** * Retrieves the images from plentymarkets and adds them to the item. * * @return number */ public function image() { $plentyId2ShopwareId = array(); $shopwareId2PlentyPosition = array(); $Request_GetItemsImages = new PlentySoapRequest_GetItemsImages(); $Request_GetItemsImages->Page = 0; $Request_GetItemsImages->SKU = $this->PLENTY_itemId; // Cleanup $this->purge(); /** * @var Media $mediaResource */ $mediaResource = \Shopware\Components\Api\Manager::getResource('Media'); do { /** * @var PlentySoapResponse_GetItemsImages $Response_GetItemsImages */ $Response_GetItemsImages = PlentymarketsSoapClient::getInstance()->GetItemsImages($Request_GetItemsImages); if ($Response_GetItemsImages->Success == false) { PlentymarketsLogger::getInstance()->error('Sync:Item:Image', 'The images for the plentymarkets item id »' . $this->PLENTY_itemId . '« could not be retrieved', 3200); continue; } /** * @var $Image PlentySoapResponse_ObjectItemImage */ foreach ($Response_GetItemsImages->ItemsImages->item as $Image) { $shopwareStoreIds = array(); /** * @var $reference PlentySoapResponse_ObjectGetItemImageReference */ foreach ($Image->References->item as $reference) { if ($reference->ReferenceType == 'Mandant') { try { if (PlentymarketsMappingController::getShopByPlentyID($reference->ReferenceValue) > 0) { $shopwareStoreId = PlentymarketsMappingController::getShopByPlentyID($reference->ReferenceValue); } } catch (PlentymarketsMappingExceptionNotExistant $E) { continue; } if (isset($shopwareStoreId)) { $shopwareStoreIds[] = $shopwareStoreId; } } } // Skip the image if it should not be shown if (empty($shopwareStoreIds)) { continue; } else { try { $media = $this->updateMedia($Image, $mediaResource); } catch (PlentymarketsMappingExceptionNotExistant $e) { $media = $this->createMedia($Image, $mediaResource); } catch (NotFoundException $e) { $media = $this->createMedia($Image, $mediaResource); } catch (Exception $e) { PlentymarketsLogger::getInstance()->error('Sync:Item:Image', $e->getMessage(), 3200); continue; } $image = new Image(); $image->setMain(2); $image->setMedia($media); $image->setPath($media->getName()); $image->setExtension($media->getExtension()); // get the main language of the shop //$mainLangData = array_values(PlentymarketsTranslation::getInstance()->getShopMainLanguage($shopwareStoreId)); //$mainLang = PlentymarketsTranslation::getInstance()->getPlentyLocaleFormat($mainLangData[0]['locale']); foreach ($Image->Names->item as $imageName) { if ($imageName->Lang == 'de') { // set the image title in German $image->setDescription($imageName->Name); } } if (!is_null(PlentymarketsConfig::getInstance()->getItemImageAltAttributeID()) && PlentymarketsConfig::getInstance()->getItemImageAltAttributeID() > 0 && PlentymarketsConfig::getInstance()->getItemImageAltAttributeID() <= 3) { // get the attribute number for alternative text from connector's settings $plenty_attributeID = PlentymarketsConfig::getInstance()->getItemImageAltAttributeID(); // set the value for the attribute number $attribute = new \Shopware\Models\Attribute\ArticleImage(); call_user_func(array($attribute, 'setAttribute' . $plenty_attributeID), $Image->Names->item[0]->AlternativeText); $image->setAttribute($attribute); } $image->setPosition($Image->Position); // Generate the thumbnails if (version_compare(Shopware::VERSION, '4.2') != 1) { PlentymarketsImportItemImageThumbnailController::getInstance()->addMediaResource($media); } Shopware()->Models()->persist($image); Shopware()->Models()->flush(); $imageId = $image->getId(); foreach ($shopwareStoreIds as $shopwareStoreId) { // import the image title translations for all active shops of the image $this->importImageTitleTranslation($imageId, $Image->Names->item, $shopwareStoreId); } $plentyId2ShopwareId[$Image->ImageID] = $imageId; $shopwareId2PlentyPosition[$Image->Position] = $imageId; Shopware()->DB()->query(' UPDATE s_articles_img SET articleID = ? WHERE id = ? ', array($this->SHOPWARE_itemId, $imageId)); } } } while (++$Request_GetItemsImages->Page < $Response_GetItemsImages->Pages); if (empty($shopwareId2PlentyPosition)) { return; } ksort($shopwareId2PlentyPosition); $mainImageId = reset($shopwareId2PlentyPosition); if (empty($mainImageId)) { return; } /** * @var Shopware\Models\Article\Image $mainImage */ $mainImage = Shopware()->Models()->find('Shopware\\Models\\Article\\Image', $mainImageId); $mainImage->setMain(1); Shopware()->Models()->persist($mainImage); Shopware()->Models()->flush(); // Get the variant images $Request_GetItemsVariantImages = new PlentySoapRequest_GetItemsVariantImages(); $Request_GetItemsVariantImages->Items = array(); $RequestObject_GetItemsVariantImages = new PlentySoapRequestObject_GetItemsVariantImages(); $RequestObject_GetItemsVariantImages->ItemID = $this->PLENTY_itemId; $Request_GetItemsVariantImages->Items[] = $RequestObject_GetItemsVariantImages; /** * @var PlentySoapResponse_GetItemsVariantImages $Response_GetItemsVariantImages */ $Response_GetItemsVariantImages = PlentymarketsSoapClient::getInstance()->GetItemsVariantImages($Request_GetItemsVariantImages); foreach ($Response_GetItemsVariantImages->Images->item as $GetItemsVariantImagesImage) { try { $shopwareOptionId = PlentymarketsMappingController::getAttributeOptionByPlentyID($GetItemsVariantImagesImage->AttributeValueID); /** * @var \Shopware\Models\Article\Configurator\Option $shopwareOption */ $shopwareOption = Shopware()->Models()->find('Shopware\\Models\\Article\\Configurator\\Option', $shopwareOptionId); } catch (PlentymarketsMappingExceptionNotExistant $e) { continue; } if (!isset($plentyId2ShopwareId[$GetItemsVariantImagesImage->ImageID])) { continue; } /** @var Shopware\Models\Article\Image $shopwareImage */ $shopwareImageId = $plentyId2ShopwareId[$GetItemsVariantImagesImage->ImageID]; /** * @var Image $shopwareImage */ $shopwareImage = Shopware()->Models()->find('Shopware\\Models\\Article\\Image', $shopwareImageId); $mapping = new Shopware\Models\Article\Image\Mapping(); $mapping->setImage($shopwareImage); $rule = new Shopware\Models\Article\Image\Rule(); $rule->setMapping($mapping); $rule->setOption($shopwareOption); $mapping->getRules()->add($rule); $shopwareImage->setMappings($mapping); Shopware()->Models()->persist($mapping); $details = Shopware()->Db()->fetchCol(' SELECT d.id FROM s_articles_details d INNER JOIN s_article_configurator_option_relations alias16 ON alias16.option_id = ' . $shopwareOptionId . ' AND alias16.article_id = d.id WHERE d.articleID = ' . $this->SHOPWARE_itemId . ' '); foreach ($details as $detailId) { /** * Get the detail object * * @var Shopware\Models\Article\Detail $detail */ $detail = Shopware()->Models()->getReference('Shopware\\Models\\Article\\Detail', $detailId); // Create the variant image $variantImage = new Shopware\Models\Article\Image(); $variantImage->setExtension($shopwareImage->getExtension()); $variantImage->setMain($shopwareImage->getMain()); $variantImage->setParent($shopwareImage); $variantImage->setArticleDetail($detail); // And persist it Shopware()->Models()->persist($variantImage); } } Shopware()->Db()->update('s_articles', array('changetime' => date('c', time())), 'id=' . $this->SHOPWARE_itemId); Shopware()->Models()->flush(); }
/** * @param Option[] $options * @param $imageData * @param $parent 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 = $this->db->fetchCol($sql); foreach ($details as $detailId) { $detail = $this->manager->getReference(Detail::class, $detailId); $image = new Image(); $image->fromArray($imageData); $image->setArticleDetail($detail); $this->manager->persist($image); } }
/** * @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(); }
/** * Helper function which returns a single image mapping * for the passed variant image and variant model. * * @param Image $image * @param Detail $variant * @return null|Image\Mapping */ protected function getVariantMappingOfImage(Image $image, Detail $variant) { $parent = $image->getParent(); /**@var $mapping Image\Mapping*/ foreach ($parent->getMappings() as $mapping) { $match = true; /**@var $rule Image\Rule*/ foreach ($mapping->getRules() as $rule) { $option = $this->getCollectionElementByProperty($variant->getConfiguratorOptions(), 'id', $rule->getOption()->getId()); if (!$option) { $match = false; break; } } if ($match) { return $mapping; } } return null; }
/** * Helper function to map the media data into an article image * * @param ArticleModel $article * @param Image $image * @param MediaModel $media * @return Image */ public function updateArticleImageWithMedia(ArticleModel $article, Image $image, MediaModel $media) { $image->setMain(2); $image->setMedia($media); $image->setArticle($article); $image->setPath($media->getName()); $image->setExtension($media->getExtension()); $image->setDescription($media->getDescription()); return $image; }