Skip to content

Commit

Permalink
Merge pull request magento#3218 from magento-tsg/2.2-develop-pr47
Browse files Browse the repository at this point in the history
[TSG] Backporting for 2.2 (pr47) (2.2.8)
  • Loading branch information
Alexander Akimov authored Sep 26, 2018
2 parents 111681d + f721e2f commit 37f5661
Show file tree
Hide file tree
Showing 15 changed files with 462 additions and 42 deletions.
89 changes: 75 additions & 14 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,7 @@ protected function _saveProducts()
$tierPrices = [];
$mediaGallery = [];
$labelsForUpdate = [];
$imagesForChangeVisibility = [];
$uploadedImages = [];
$previousType = null;
$prevAttributeSet = null;
Expand Down Expand Up @@ -1662,21 +1663,24 @@ protected function _saveProducts()
}

// 5. Media gallery phase
$disabledImages = [];
list($rowImages, $rowLabels) = $this->getImagesFromRow($rowData);
$storeId = !empty($rowData[self::COL_STORE])
? $this->getStoreIdByCode($rowData[self::COL_STORE])
: Store::DEFAULT_STORE_ID;
if (isset($rowData['_media_is_disabled']) && strlen(trim($rowData['_media_is_disabled']))) {
$disabledImages = array_flip(
explode($this->getMultipleValueSeparator(), $rowData['_media_is_disabled'])
);
$imageHiddenStates = $this->getImagesHiddenStates($rowData);
foreach (array_keys($imageHiddenStates) as $image) {
if (array_key_exists($rowSku, $existingImages)
&& array_key_exists($image, $existingImages[$rowSku])
) {
$rowImages[self::COL_MEDIA_IMAGE][] = $image;
$uploadedImages[$image] = $image;
}

if (empty($rowImages)) {
foreach (array_keys($disabledImages) as $disabledImage) {
$rowImages[self::COL_MEDIA_IMAGE][] = $disabledImage;
}
$rowImages[self::COL_MEDIA_IMAGE][] = $image;
}
}

$rowData[self::COL_MEDIA_IMAGE] = [];

/*
Expand Down Expand Up @@ -1710,12 +1714,22 @@ protected function _saveProducts()

if ($uploadedFile && !isset($mediaGallery[$storeId][$rowSku][$uploadedFile])) {
if (isset($existingImages[$rowSku][$uploadedFile])) {
$currentFileData = $existingImages[$rowSku][$uploadedFile];
if (isset($rowLabels[$column][$columnImageKey])
&& $rowLabels[$column][$columnImageKey] != $existingImages[$rowSku][$uploadedFile]['label']
&& $rowLabels[$column][$columnImageKey] != $currentFileData['label']
) {
$labelsForUpdate[] = [
'label' => $rowLabels[$column][$columnImageKey],
'imageData' => $existingImages[$rowSku][$uploadedFile]
'imageData' => $currentFileData,
];
}

if (array_key_exists($uploadedFile, $imageHiddenStates)
&& $currentFileData['disabled'] != $imageHiddenStates[$uploadedFile]
) {
$imagesForChangeVisibility[] = [
'disabled' => $imageHiddenStates[$uploadedFile],
'imageData' => $currentFileData,
];
}
} else {
Expand All @@ -1724,9 +1738,11 @@ protected function _saveProducts()
}
$mediaGallery[$storeId][$rowSku][$uploadedFile] = [
'attribute_id' => $this->getMediaGalleryAttributeId(),
'label' => isset($rowLabels[$column][$columnImageKey]) ? $rowLabels[$column][$columnImageKey] : '',
'label' => isset($rowLabels[$column][$columnImageKey])
? $rowLabels[$column][$columnImageKey] : '',
'position' => ++$position,
'disabled' => isset($disabledImages[$columnImage]) ? '1' : '0',
'disabled' => isset($imageHiddenStates[$columnImage])
? $imageHiddenStates[$columnImage] : '0',
'value' => $uploadedFile,
];
}
Expand Down Expand Up @@ -1847,6 +1863,8 @@ protected function _saveProducts()
$mediaGallery
)->_saveProductAttributes(
$attributes
)->updateMediaGalleryVisibility(
$imagesForChangeVisibility
)->updateMediaGalleryLabels(
$labelsForUpdate
);
Expand All @@ -1861,6 +1879,34 @@ protected function _saveProducts()
}

/**
* Prepare array with image states (visible or hidden from product page)
*
* @param array $rowData
* @return array
*/
private function getImagesHiddenStates(array $rowData): array
{
$statesArray = [];
$mappingArray = [
'_media_is_disabled' => '1',
];

foreach ($mappingArray as $key => $value) {
if (isset($rowData[$key]) && strlen(trim($rowData[$key]))) {
$items = explode($this->getMultipleValueSeparator(), $rowData[$key]);

foreach ($items as $item) {
$statesArray[$item] = $value;
}
}
}

return $statesArray;
}

/**
* Resolve valid category ids from provided row data.
*
* @param array $rowData
* @return array
*/
Expand Down Expand Up @@ -2205,7 +2251,7 @@ public function getEntityTypeCode()
* Returns array of new products data with SKU as key. All SKU keys are in lowercase for avoiding creation of
* new products with the same SKU in different letter cases.
*
* @var string $sku
* @param string $sku
* @return array
*/
public function getNewSku($sku = null)
Expand Down Expand Up @@ -2716,7 +2762,7 @@ protected function getProductUrlSuffix($storeId = null)
protected function getUrlKey($rowData)
{
if (!empty($rowData[self::URL_KEY])) {
return strtolower($rowData[self::URL_KEY]);
return $this->productUrl->formatUrlKey($rowData[self::URL_KEY]);
}

if (!empty($rowData[self::COL_NAME])) {
Expand Down Expand Up @@ -2782,6 +2828,21 @@ private function updateMediaGalleryLabels(array $labels)
$this->mediaProcessor->updateMediaGalleryLabels($labels);
}

/**
* Update 'disabled' field for media gallery entity
*
* @param array $images
* @return $this
*/
private function updateMediaGalleryVisibility(array $images): Product
{
if (!empty($images)) {
$this->mediaProcessor->updateMediaGalleryVisibility($images);
}

return $this;
}

/**
* Parse values from multiple attributes fields
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function __construct(
/**
* Save product media gallery.
*
* @param $mediaGalleryData
* @param array $mediaGalleryData
* @return void
*/
public function saveMediaGallery(array $mediaGalleryData)
Expand Down Expand Up @@ -152,14 +152,37 @@ public function saveMediaGallery(array $mediaGalleryData)
* @return void
*/
public function updateMediaGalleryLabels(array $labels)
{
$this->updateMediaGalleryField($labels, 'label');
}

/**
* Update 'disabled' field for media gallery entity
*
* @param array $images
* @return void
*/
public function updateMediaGalleryVisibility(array $images)
{
$this->updateMediaGalleryField($images, 'disabled');
}

/**
* Update value for requested field in media gallery entities
*
* @param array $data
* @param string $field
* @return void
*/
private function updateMediaGalleryField(array $data, string $field)
{
$insertData = [];
foreach ($labels as $label) {
$imageData = $label['imageData'];
foreach ($data as $datum) {
$imageData = $datum['imageData'];

if ($imageData['label'] === null) {
if ($imageData[$field] === null) {
$insertData[] = [
'label' => $label['label'],
$field => $datum[$field],
$this->getProductEntityLinkField() => $imageData[$this->getProductEntityLinkField()],
'value_id' => $imageData['value_id'],
'store_id' => Store::DEFAULT_STORE_ID,
Expand All @@ -168,7 +191,7 @@ public function updateMediaGalleryLabels(array $labels)
$this->connection->update(
$this->mediaGalleryValueTableName,
[
'label' => $label['label'],
$field => $datum[$field],
],
[
$this->getProductEntityLinkField() . ' = ?' => $imageData[$this->getProductEntityLinkField()],
Expand Down Expand Up @@ -224,6 +247,7 @@ public function getExistingImages(array $bunch)
),
[
'label' => 'mgv.label',
'disabled' => 'mgv.disabled',
]
)->joinInner(
['pe' => $this->productEntityTableName],
Expand Down Expand Up @@ -263,7 +287,7 @@ private function initMediaGalleryResources()
/**
* Save media gallery data per store.
*
* @param $storeId
* @param int $storeId
* @param array $mediaGalleryData
* @param array $newMediaValues
* @param array $valueToProductId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ class Option extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
*/
private $optionTypeTitles;

/**
* @var array
*/
private $lastOptionTitle;

/**
* @param \Magento\ImportExport\Model\ResourceModel\Import\Data $importData
* @param ResourceConnection $resource
Expand Down Expand Up @@ -1119,6 +1124,8 @@ protected function _getMultiRowFormat($rowData)
}

/**
* Process option row.
*
* @param string $name
* @param array $optionRow
* @return array
Expand Down Expand Up @@ -1185,6 +1192,7 @@ private function addFileOptions($result, $optionRow)

/**
* Import data rows.
*
* Additional store view data (option titles) will be sought in store view specified import file rows
*
* @return boolean
Expand Down Expand Up @@ -1212,7 +1220,6 @@ protected function _importData()
$parentCount = [];
$childCount = [];
$optionsToRemove = [];

foreach ($bunch as $rowNumber => $rowData) {
if (isset($optionId, $valueId) && empty($rowData[Product::COL_STORE_VIEW_CODE])) {
$nextOptionId = $optionId;
Expand Down Expand Up @@ -1257,9 +1264,17 @@ protected function _importData()
$childCount
);
$this->_collectOptionTitle($combinedData, $prevOptionId, $titles);
$this->checkOptionTitles(
$options,
$titles,
$combinedData,
$prevOptionId,
$optionId,
$products,
$prices
);
}
}

// Remove all existing options if import behaviour is APPEND
// in other case remove options for products with empty "custom_options" row only
if ($this->getBehavior() != \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) {
Expand All @@ -1268,21 +1283,80 @@ protected function _importData()
// Remove options for products with empty "custom_options" row
$this->_deleteEntities($optionsToRemove);
}

// Save prepared custom options data
if ($this->_isReadyForSaving($options, $titles, $typeValues)) {
$types = [
'values' => $typeValues,
'prices' => $typePrices,
'titles' => $typeTitles
];
$this->setLastOptionTitle($titles);
$this->savePreparedCustomOptions($products, $options, $titles, $prices, $types);
}
}

return true;
}

/**
* Check options titles.
*
* If products were split up between bunches,
* this function will add needed option for option titles.
*
* @param array $options
* @param array $titles
* @param array $combinedData
* @param int $prevOptionId
* @param int $optionId
* @param array $products
* @param array $prices
* @return void
*/
private function checkOptionTitles(
array &$options,
array &$titles,
array $combinedData,
int &$prevOptionId,
int &$optionId,
array $products,
array $prices
) {
$titlesCount = count($titles);
if ($titlesCount > 0 && count($options) !== $titlesCount) {
$combinedData[Product::COL_STORE_VIEW_CODE] = '';
$optionId--;
$option = $this->_collectOptionMainData(
$combinedData,
$prevOptionId,
$optionId,
$products,
$prices
);
if ($option) {
$options[] = $option;
}
}
}

/**
* Setting last Custom Option Title
* to use it later in _collectOptionTitle
* to set correct title for default store view.
*
* @param array $titles
* @return void
*/
private function setLastOptionTitle(array &$titles)
{
if (count($titles) > 0) {
end($titles);
$key = key($titles);
$this->lastOptionTitle[$key] = $titles[$key];
}
}


/**
* Load data of existed products
*
Expand Down Expand Up @@ -1413,8 +1487,12 @@ protected function _collectOptionTitle(array $rowData, $prevOptionId, array &$ti
$defaultStoreId = Store::DEFAULT_STORE_ID;
if (!empty($rowData[self::COLUMN_TITLE])) {
if (!isset($titles[$prevOptionId][$defaultStoreId])) {
// ensure default title is set
$titles[$prevOptionId][$defaultStoreId] = $rowData[self::COLUMN_TITLE];
if (isset($this->lastOptionTitle[$prevOptionId])) {
$titles[$prevOptionId] = $this->lastOptionTitle[$prevOptionId];
unset($this->lastOptionTitle);
} else {
$titles[$prevOptionId][$defaultStoreId] = $rowData[self::COLUMN_TITLE];
}
}
$titles[$prevOptionId][$this->_rowStoreId] = $rowData[self::COLUMN_TITLE];
}
Expand Down
Loading

0 comments on commit 37f5661

Please sign in to comment.