/** * 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 \Shopware\Components\Api\Resource\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 (strtolower($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 (PlentymarketsMappingExceptionCorrupt $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 \Shopware\Models\Article\Image(); $image->setMain(2); $image->setMedia($media); $image->setPath($media->getName()); $image->setExtension($media->getExtension()); $image->setPosition($Image->Position); if (!empty($Image->Names)) { foreach ($Image->Names->item as $imageName) { if ($imageName->Lang == 'de') { // set the image title in German $image->setDescription($imageName->Name); } } if (!empty($Image->Names->item[0]->AlternativeText)) { 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(); $attribute->{setAttribute . $plenty_attributeID}($Image->Names->item[0]->AlternativeText); $image->setAttribute($attribute); } // 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) { if (!empty($Image->Names)) { // 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 (!$shopwareId2PlentyPosition) { return; } ksort($shopwareId2PlentyPosition); $mainImageId = reset($shopwareId2PlentyPosition); /** @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); if (!empty($Response_GetItemsVariantImages->Images)) { 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 \Shopware\Models\Article\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()->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; $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(); }