Commit e8ce6e91 by Krzysztof Kuźniar

Update module - media assets #2

parent a893eecd
...@@ -323,6 +323,7 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI ...@@ -323,6 +323,7 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI
$locale, $locale,
$currency $currency
); );
$assetProductData = [];
$assetProductStandard = []; $assetProductStandard = [];
if($productStandard['values']) { if($productStandard['values']) {
$attributeKeys = array_keys($productStandard['values']); $attributeKeys = array_keys($productStandard['values']);
...@@ -331,12 +332,17 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI ...@@ -331,12 +332,17 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI
foreach($assetAttributeCodes as $assetCode) { foreach($assetAttributeCodes as $assetCode) {
$data = reset($productStandard['values'][$assetCode]); $data = reset($productStandard['values'][$assetCode]);
if(isset($data['data']['main'])) {
$assetProductData[$assetCode] = isset($data['data']['main']) ? $data['data']['main'] : null;
unset($data['data']['main']);
}
$assetProductStandard[$assetCode] = implode(",", $data['data']); $assetProductStandard[$assetCode] = implode(",", $data['data']);
} }
} }
$productStandard = $this->converter->convert($productStandard, $options); $productStandard = $this->converter->convert($productStandard, $options);
$productStandard = array_merge($productStandard, $assetProductStandard); $productStandard = array_merge($productStandard, $assetProductStandard);
$productStandard = $this->updatePriceAndSelectData($productStandard, $options['scope'], $locale, $currency); $productStandard = $this->updatePriceAndSelectData($productStandard, $options['scope'], $locale, $currency);
...@@ -394,7 +400,9 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI ...@@ -394,7 +400,9 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI
if ($isDefaultStoreView || in_array($fieldType, ['pim_catalog_text', 'pim_catalog_textarea', 'pim_catalog_date', 'pim_catalog_number', 'pim_catalog_price_collection'])) { if ($isDefaultStoreView || in_array($fieldType, ['pim_catalog_text', 'pim_catalog_textarea', 'pim_catalog_date', 'pim_catalog_number', 'pim_catalog_price_collection'])) {
if (in_array($field, $this->standardAttributes)) { if (in_array($field, $this->standardAttributes)) {
$value = $this->formatValue($field, $productStandard[$field]); $value = $this->formatValue($field, $productStandard[$field]);
if (isset($this->attributeTypes[$field]) && $this->attributeTypes[$field] === 'pim_catalog_simpleselect' && $value) { if (isset($this->attributeTypes[$field]) && $this->attributeTypes[$field] === 'pim_catalog_simpleselect' && $value) {
$value = $this->connectorService->getOptionLabelByAttributeCodeAndLocale($field, $value, $locale); $value = $this->connectorService->getOptionLabelByAttributeCodeAndLocale($field, $value, $locale);
} }
...@@ -508,10 +516,16 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI ...@@ -508,10 +516,16 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI
} }
foreach ($this->mediaAttributes as $image) { foreach ($this->mediaAttributes as $image) {
if ($isDefaultStoreView || in_array($image, $this->localizableScopableAttributes)) { if ($isDefaultStoreView || in_array($image, $this->localizableScopableAttributes)) {
if (!empty($productStandard[$image])) { if (!empty($productStandard[$image])) {
$parent = !empty($productStandard['parent']) ? $productStandard['parent'] : false; $parent = !empty($productStandard['parent']) ? $productStandard['parent'] : false;
$assetAttributes = $this->connectorService->getAssetAttributeCodes();
if (in_array($image, $assetAttributes)) {
$this->getImageRoles($magentoData, $image, $assetProductData[$image], $parent);
} else {
$this->getImageRoles($magentoData, $image, $productStandard[$image], $parent); $this->getImageRoles($magentoData, $image, $productStandard[$image], $parent);
}
$magentoData['additional_images'] .= $this->multiValueSeparator . $productStandard[$image]; $magentoData['additional_images'] .= $this->multiValueSeparator . $productStandard[$image];
$magentoData['additional_images'] = ltrim($magentoData['additional_images'], $this->multiValueSeparator); $magentoData['additional_images'] = ltrim($magentoData['additional_images'], $this->multiValueSeparator);
} }
...@@ -679,6 +693,7 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI ...@@ -679,6 +693,7 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI
protected function formatValue($attributeCode, $value) protected function formatValue($attributeCode, $value)
{ {
if (isset($this->attributeTypes[$attributeCode]) && $this->attributeTypes[$attributeCode] === 'pim_catalog_date') { if (isset($this->attributeTypes[$attributeCode]) && $this->attributeTypes[$attributeCode] === 'pim_catalog_date') {
$value = $this->formatDate($value); $value = $this->formatDate($value);
} }
...@@ -1015,24 +1030,38 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI ...@@ -1015,24 +1030,38 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI
{ {
if ($parent) { if ($parent) {
if (isset($this->otherMappings['child_base_image']) && $this->otherMappings['child_base_image'] == $mediaAttribute) { if (isset($this->otherMappings['child_base_image']) && $this->otherMappings['child_base_image'] == $mediaAttribute) {
$magentoData['base_image'] = $media; $magentoData['base_image'] = $media;
} }
if (isset($this->otherMappings['child_small_image']) && $this->otherMappings['child_small_image'] == $mediaAttribute) { if (isset($this->otherMappings['child_small_image']) && $this->otherMappings['child_small_image'] == $mediaAttribute) {
$magentoData['small_image'] = $media; $magentoData['small_image'] = $media;
} }
if (isset($this->otherMappings['child_base_image']) && $this->otherMappings['child_thumbnail_image'] == $mediaAttribute) { if (isset($this->otherMappings['child_base_image']) && $this->otherMappings['child_thumbnail_image'] == $mediaAttribute) {
$magentoData['thumbnail_image'] = $media; $magentoData['thumbnail_image'] = $media;
} }
} else { } else {
if (isset($this->otherMappings['base_image']) && $this->otherMappings['base_image'] == $mediaAttribute) { if (isset($this->otherMappings['base_image']) && $this->otherMappings['base_image'] == $mediaAttribute) {
$magentoData['base_image'] = $media; $magentoData['base_image'] = $media;
} }
if (isset($this->otherMappings['small_image']) && $this->otherMappings['small_image'] == $mediaAttribute) { if (isset($this->otherMappings['small_image']) && $this->otherMappings['small_image'] == $mediaAttribute) {
$magentoData['small_image'] = $media; $magentoData['small_image'] = $media;
} }
if (isset($this->otherMappings['base_image']) && $this->otherMappings['thumbnail_image'] == $mediaAttribute) { if (isset($this->otherMappings['base_image']) && $this->otherMappings['thumbnail_image'] == $mediaAttribute) {
$magentoData['thumbnail_image'] = $media; $magentoData['thumbnail_image'] = $media;
} }
} }
} }
} }
...@@ -178,7 +178,8 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt ...@@ -178,7 +178,8 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt
$attrValue = $productStandard['values'][$mediaAttribute]; $attrValue = $productStandard['values'][$mediaAttribute];
if ($attributeType == 'pim_catalog_asset_group') { if ($attributeType == 'pim_catalog_asset_group') {
$childImageRoles = null;
$productImageRoles = null;
$attrValue = $this->connectorService->convertAssetToPath($attrValue); $attrValue = $this->connectorService->convertAssetToPath($attrValue);
if (!empty($productStandard[Prop::FIELD_PARENT])) { if (!empty($productStandard[Prop::FIELD_PARENT])) {
$imageRoles = $childImageRoles = $this->getChildImageRoles($mediaAttribute, $otherMappings); $imageRoles = $childImageRoles = $this->getChildImageRoles($mediaAttribute, $otherMappings);
...@@ -189,12 +190,21 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt ...@@ -189,12 +190,21 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt
$convertedImage = $this->convertArrayUrlToBase64( $convertedImage = $this->convertArrayUrlToBase64(
$attrValue, $attrValue,
$altData, $altData,
$mediaAttribute == $productStandard[Prop::FIELD_META_DATA][Prop::ATTRIBUTE_AS_IMAGE] ? 0 : count($productStandard['media_gallery_entries']) $mediaAttribute == $productStandard[Prop::FIELD_META_DATA][Prop::ATTRIBUTE_AS_IMAGE] ? 0 : count($productStandard['media_gallery_entries']),
$imageRoles
); );
if ($convertedImage) { if ($convertedImage) {
$productStandardAsset = $convertedImage; $productStandardAsset = $convertedImage;
} }
foreach ($productStandardAsset as $assetImage) {
if ($assetImage['types']) {
if ($productImageRoles) {
$productStandard['media_gallery_entries']['image_roles'][$assetImage['content']['name']] = $productImageRoles;
} elseif ($childImageRoles) {
$productStandard['media_gallery_entries']['child_image_roles'][$assetImage['content']['name']] = $childImageRoles;
}
}
}
} else { } else {
$childImageRoles = null; $childImageRoles = null;
...@@ -226,9 +236,11 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt ...@@ -226,9 +236,11 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt
} }
} }
} }
$productStandard['media_gallery_entries'] = array_merge($productStandardAsset, $productStandard['media_gallery_entries']);
unset($productStandard['values'][$mediaAttribute]); unset($productStandard['values'][$mediaAttribute]);
} }
$productStandard['media_gallery_entries'] = array_merge($productStandardAsset, $productStandard['media_gallery_entries']);
$videoAttributes = !empty($otherMappings['videoAttrsMapping']) ? $otherMappings['videoAttrsMapping'] : []; $videoAttributes = !empty($otherMappings['videoAttrsMapping']) ? $otherMappings['videoAttrsMapping'] : [];
foreach ($videoAttributes as $index => $videoAttribute) { foreach ($videoAttributes as $index => $videoAttribute) {
...@@ -627,7 +639,7 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt ...@@ -627,7 +639,7 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt
return $convertedItem; return $convertedItem;
} }
protected function convertArrayUrlToBase64($entry, $mediaAltText = '', $position = 0) protected function convertArrayUrlToBase64($entry, $mediaAltText = '', $position = 0, $imageRoles)
{ {
if (is_array($entry)) { if (is_array($entry)) {
...@@ -638,8 +650,9 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt ...@@ -638,8 +650,9 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt
$entries = $entry[0]['data']; $entries = $entry[0]['data'];
} }
$convertedItem = []; $convertedItem = [];
$imageType = true;
foreach($entries as $entry) { foreach($entries as $entry) {
$assetDetails = $this->connectorService->getAssetsByPath($entry);
$filename = explode('/', $entry); $filename = explode('/', $entry);
$filename = end($filename); $filename = end($filename);
...@@ -669,13 +682,16 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt ...@@ -669,13 +682,16 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt
'label' => $label, 'label' => $label,
'position' => $position, 'position' => $position,
'disabled' => false, 'disabled' => false,
'types' => $position ? [] : ["image", "small_image", "thumbnail"], 'types' => $assetDetails['thumbImage'] && $imageType ? $imageRoles : [],
'content' => [ 'content' => [
'base64_encoded_data' => $imageContent, 'base64_encoded_data' => $imageContent,
'type' => $this->guessMimetype($mimetype) ? : 'image/png', 'type' => $this->guessMimetype($mimetype) ? : 'image/png',
'name' => $filename, 'name' => $filename,
], ],
]; ];
if($assetDetails['thumbImage']) {
$imageType = false;
}
} }
return $convertedItem; return $convertedItem;
......
...@@ -243,7 +243,9 @@ class ProductWriter extends \BaseCsvProductWriter implements ...@@ -243,7 +243,9 @@ class ProductWriter extends \BaseCsvProductWriter implements
foreach ($item['values'][$attributeCode] as $index => $values) { foreach ($item['values'][$attributeCode] as $index => $values) {
if(!empty($values)) { if(!empty($values)) {
$mainImageAdded = true;
foreach($values['data'] as $dataKey => $value) { foreach($values['data'] as $dataKey => $value) {
$assetDetail = $this->connectorService->getAssetsByPath($value);
$shortFileKey = $this->shortPathGenerator->generateShortFilePath($value); $shortFileKey = $this->shortPathGenerator->generateShortFilePath($value);
if ($shortFileKey) { if ($shortFileKey) {
...@@ -253,6 +255,12 @@ class ProductWriter extends \BaseCsvProductWriter implements ...@@ -253,6 +255,12 @@ class ProductWriter extends \BaseCsvProductWriter implements
if ($imageUrl) { if ($imageUrl) {
$item['values'][$attributeCode][$index]['data'][$dataKey] = $imageUrl; $item['values'][$attributeCode][$index]['data'][$dataKey] = $imageUrl;
if($assetDetail['thumbImage'] && $mainImageAdded) {
$item['values'][$attributeCode][$index]['data']['main'] = $imageUrl;
$mainImageAdded = false;
}
} }
} }
} }
......
...@@ -1769,4 +1769,15 @@ class Magento2Connector ...@@ -1769,4 +1769,15 @@ class Magento2Connector
); );
} }
public function getAssetsByPath($path)
{
$assetDetails = [];
$damMedia = $this->container->get('dam.connector.service')->getMediaByFilepath($path);
$assetDetails['altText'] = $damMedia->getAsset()->getAlt();
$repo = $this->container->get('webkul_dam.repository.asset')->findOneBy(['thumbImage' => $path]);
$assetDetails['thumbImage'] = $repo ? true : false ;
return $assetDetails;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment