Commit e8ce6e91 by Krzysztof Kuźniar

Update module - media assets #2

parent a893eecd
......@@ -323,6 +323,7 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI
$locale,
$currency
);
$assetProductData = [];
$assetProductStandard = [];
if($productStandard['values']) {
$attributeKeys = array_keys($productStandard['values']);
......@@ -331,12 +332,17 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI
foreach($assetAttributeCodes as $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']);
}
}
$productStandard = $this->converter->convert($productStandard, $options);
$productStandard = array_merge($productStandard, $assetProductStandard);
$productStandard = $this->updatePriceAndSelectData($productStandard, $options['scope'], $locale, $currency);
......@@ -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 (in_array($field, $this->standardAttributes)) {
$value = $this->formatValue($field, $productStandard[$field]);
if (isset($this->attributeTypes[$field]) && $this->attributeTypes[$field] === 'pim_catalog_simpleselect' && $value) {
$value = $this->connectorService->getOptionLabelByAttributeCodeAndLocale($field, $value, $locale);
}
......@@ -450,7 +458,7 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI
}
}
}
/* url_prefix setting apply */
if (!empty($magentoData['url_key']) && $magentoData['product_type'] === 'configurable') {
if (!empty($this->otherSettings['urlKeyPrefix'])) {
......@@ -506,12 +514,18 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI
}
$magentoData['additional_image_labels'] = (is_array($labels) && !empty($labels)) ? implode($this->multiValueSeparator, $labels) : '';
}
foreach ($this->mediaAttributes as $image) {
if ($isDefaultStoreView || in_array($image, $this->localizableScopableAttributes)) {
if (!empty($productStandard[$image])) {
$parent = !empty($productStandard['parent']) ? $productStandard['parent'] : false;
$this->getImageRoles($magentoData, $image, $productStandard[$image], $parent);
$assetAttributes = $this->connectorService->getAssetAttributeCodes();
if (in_array($image, $assetAttributes)) {
$this->getImageRoles($magentoData, $image, $assetProductData[$image], $parent);
} else {
$this->getImageRoles($magentoData, $image, $productStandard[$image], $parent);
}
$magentoData['additional_images'] .= $this->multiValueSeparator . $productStandard[$image];
$magentoData['additional_images'] = ltrim($magentoData['additional_images'], $this->multiValueSeparator);
}
......@@ -679,6 +693,7 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI
protected function formatValue($attributeCode, $value)
{
if (isset($this->attributeTypes[$attributeCode]) && $this->attributeTypes[$attributeCode] === 'pim_catalog_date') {
$value = $this->formatDate($value);
}
......@@ -1015,24 +1030,38 @@ class ProductConverter implements \ArrayConverterInterface, \StepExecutionAwareI
{
if ($parent) {
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) {
$magentoData['small_image'] = $media;
}
if (isset($this->otherMappings['child_base_image']) && $this->otherMappings['child_thumbnail_image'] == $mediaAttribute) {
$magentoData['thumbnail_image'] = $media;
}
} else {
if (isset($this->otherMappings['base_image']) && $this->otherMappings['base_image'] == $mediaAttribute) {
$magentoData['base_image'] = $media;
}
if (isset($this->otherMappings['small_image']) && $this->otherMappings['small_image'] == $mediaAttribute) {
$magentoData['small_image'] = $media;
}
if (isset($this->otherMappings['base_image']) && $this->otherMappings['thumbnail_image'] == $mediaAttribute) {
$magentoData['thumbnail_image'] = $media;
}
}
}
}
......@@ -178,7 +178,8 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt
$attrValue = $productStandard['values'][$mediaAttribute];
if ($attributeType == 'pim_catalog_asset_group') {
$childImageRoles = null;
$productImageRoles = null;
$attrValue = $this->connectorService->convertAssetToPath($attrValue);
if (!empty($productStandard[Prop::FIELD_PARENT])) {
$imageRoles = $childImageRoles = $this->getChildImageRoles($mediaAttribute, $otherMappings);
......@@ -189,12 +190,21 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt
$convertedImage = $this->convertArrayUrlToBase64(
$attrValue,
$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) {
$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 {
$childImageRoles = null;
......@@ -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]);
}
$productStandard['media_gallery_entries'] = array_merge($productStandardAsset, $productStandard['media_gallery_entries']);
$videoAttributes = !empty($otherMappings['videoAttrsMapping']) ? $otherMappings['videoAttrsMapping'] : [];
foreach ($videoAttributes as $index => $videoAttribute) {
......@@ -627,7 +639,7 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt
return $convertedItem;
}
protected function convertArrayUrlToBase64($entry, $mediaAltText = '', $position = 0)
protected function convertArrayUrlToBase64($entry, $mediaAltText = '', $position = 0, $imageRoles)
{
if (is_array($entry)) {
......@@ -638,9 +650,10 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt
$entries = $entry[0]['data'];
}
$convertedItem = [];
$imageType = true;
foreach($entries as $entry) {
$assetDetails = $this->connectorService->getAssetsByPath($entry);
$filename = explode('/', $entry);
$filename = end($filename);
try {
......@@ -669,15 +682,18 @@ class ProductProcessor extends \PimProductProcessor implements \ItemProcessorInt
'label' => $label,
'position' => $position,
'disabled' => false,
'types' => $position ? [] : ["image", "small_image", "thumbnail"],
'types' => $assetDetails['thumbImage'] && $imageType ? $imageRoles : [],
'content' => [
'base64_encoded_data' => $imageContent,
'type' => $this->guessMimetype($mimetype) ? : 'image/png',
'name' => $filename,
],
];
if($assetDetails['thumbImage']) {
$imageType = false;
}
}
return $convertedItem;
}
......
......@@ -236,14 +236,16 @@ class ProductWriter extends \BaseCsvProductWriter implements
});
$identifier = $this->getItemIdentifier($item);
foreach ($mediaAttributeTypes as $attributeCode => $attributeType) {
if ($attributeType == 'pim_catalog_asset_group') {
$item['values'][$attributeCode] = $this->connectorService->convertAssetToPath($item['values'][$attributeCode]);
foreach ($item['values'][$attributeCode] as $index => $values) {
if(!empty($values)) {
$mainImageAdded = true;
foreach($values['data'] as $dataKey => $value) {
$assetDetail = $this->connectorService->getAssetsByPath($value);
$shortFileKey = $this->shortPathGenerator->generateShortFilePath($value);
if ($shortFileKey) {
......@@ -253,6 +255,12 @@ class ProductWriter extends \BaseCsvProductWriter implements
if ($imageUrl) {
$item['values'][$attributeCode][$index]['data'][$dataKey] = $imageUrl;
if($assetDetail['thumbImage'] && $mainImageAdded) {
$item['values'][$attributeCode][$index]['data']['main'] = $imageUrl;
$mainImageAdded = false;
}
}
}
}
......@@ -277,7 +285,7 @@ class ProductWriter extends \BaseCsvProductWriter implements
}
}
}
return $item;
}
......
......@@ -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