Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

II-89 generate url if duplicate. added checkbox #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Serialize\SerializerInterface;

/**
* Class Data
Expand All @@ -26,18 +27,28 @@ class Data extends AbstractHelper
*/
protected $sourceFactory;

/**
* Json Serializer
*
* @var SerializerInterface
*/
public $serializer;

/**
* Data Helper constructor
*
* @param Context $context
* @param Factory $sourceFactory
* @param SerializerInterface $serializer
*/
public function __construct(
Context $context,
Factory $sourceFactory
Factory $sourceFactory,
SerializerInterface $serializer
) {
$this->coreConfig = $context->getScopeConfig();
$this->sourceFactory = $sourceFactory;
$this->serializer = $serializer;
parent::__construct($context);
}

Expand Down
133 changes: 119 additions & 14 deletions Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Firebear\ImportExport\Model\Import;

use Firebear\ImportExport\Api\UrlKeyManagerInterface;
use Magento\Catalog\Model\Product\Attribute\Backend\Sku;
use Magento\CatalogImportExport\Model\Import\Product\TaxClassProcessor;
use Magento\Framework\Stdlib\DateTime;
use Magento\CatalogImportExport\Model\Import\Product as MagentoProduct;
Expand Down Expand Up @@ -390,20 +391,6 @@ protected function saveProducts()

$rowSku = $rowData[self::COL_SKU];

$storeIds = $this->storeManager->getStore()->getId();
$urlKey = isset($rowData[self::URL_KEY])
? $this->productUrl->formatUrlKey($rowData[self::URL_KEY])
: $this->productUrl->formatUrlKey($rowData[self::COL_NAME]);
$isDuplicate = $this->isDuplicateUrlKey($urlKey, $rowSku, $storeIds);
if ($isDuplicate || $this->urlKeyManager->isUrlKeyExist($rowSku, $urlKey)) {
$urlKey = $this->productUrl->formatUrlKey(
$rowData[self::COL_NAME] . '-' . $rowData[self::COL_SKU]
);
}
$rowData[self::URL_KEY] = $urlKey;
$this->urlKeyManager->addUrlKeys($rowSku, $urlKey);
$this->urlKeys = [];

if (!$rowSku) {
$this->getErrorAggregator()->addRowToSkip($rowNum);
continue;
Expand Down Expand Up @@ -817,6 +804,11 @@ private function customFieldsMapping($rowData)
$rowData = $this->_parseAdditionalAttributes($rowData);

$rowData = $this->setStockUseConfigFieldsValues($rowData);
if (isset($this->_parameters['generate_url'])
&& $this->_parameters['generate_url'] == 1
) {
$rowData = $this->generateUrlKey($rowData);
}
if (array_key_exists('status', $rowData)
&& $rowData['status'] != \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
) {
Expand Down Expand Up @@ -1185,4 +1177,117 @@ private function getProductEntityLinkField()
}
return $this->productEntityLinkField;
}

/**
* Validate data rows and save bunches to DB.
*
* @return $this
*/
protected function _saveValidatedBunches()
{
$source = $this->_getSource();
$currentDataSize = 0;
$bunchRows = [];
$startNewBunch = false;
$nextRowBackup = [];
$maxDataSize = $this->_resourceHelper->getMaxDataSize();
$bunchSize = $this->_importExportData->getBunchSize();
$skuSet = [];

$source->rewind();
$this->_dataSourceModel->cleanBunches();

while ($source->valid() || $bunchRows) {
if ($startNewBunch || !$source->valid()) {
$this->_dataSourceModel->saveBunch($this->getEntityTypeCode(), $this->getBehavior(), $bunchRows);

$bunchRows = $nextRowBackup;
$currentDataSize = strlen($this->getSerializer()->serialize($bunchRows));
$startNewBunch = false;
$nextRowBackup = [];
}
if ($source->valid()) {
try {
$rowData = $source->current();
if (array_key_exists('sku', $rowData)) {
$skuSet[$rowData['sku']] = true;
}
} catch (\InvalidArgumentException $e) {
$this->addRowError($e->getMessage(), $this->_processedRowsCount);
$this->_processedRowsCount++;
$source->next();
continue;
}

$rowData = $this->customFieldsMapping($rowData);

$this->_processedRowsCount++;

if ($this->validateRow($rowData, $source->key())) {
// add row to bunch for save
$rowData = $this->_prepareRowForDb($rowData);
$rowSize = strlen($this->jsonHelper->jsonEncode($rowData));

$isBunchSizeExceeded = $bunchSize > 0 && count($bunchRows) >= $bunchSize;

if ($currentDataSize + $rowSize >= $maxDataSize || $isBunchSizeExceeded) {
$startNewBunch = true;
$nextRowBackup = [$source->key() => $rowData];
} else {
$bunchRows[$source->key()] = $rowData;
$currentDataSize += $rowSize;
}
}
$source->next();
}
}
$this->checkUrlKeyDuplicates();
$this->getOptionEntity()->validateAmbiguousData();
$this->_processedEntitiesCount = (count($skuSet)) ? : $this->_processedRowsCount;

return $this;
}

/**
* @param $rowData
* @return mixed
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function generateUrlKey($rowData)
{
$sku = $this->getCorrectSkuAsPerLength($rowData);
$storeIds = $this->storeManager->getStore()->getId();
$urlKey = isset($rowData[self::URL_KEY])
? $this->productUrl->formatUrlKey($rowData[self::URL_KEY])
: $this->productUrl->formatUrlKey($rowData[self::COL_NAME]);
$isDuplicate = $this->isDuplicateUrlKey($urlKey, $sku, $storeIds);
if ($isDuplicate || $this->urlKeyManager->isUrlKeyExist($sku, $urlKey)) {
$urlKey = $this->productUrl->formatUrlKey(
$rowData[self::COL_NAME] . '-' . $rowData[self::COL_SKU]
);
}
$rowData[self::URL_KEY] = $urlKey;
$this->urlKeyManager->addUrlKeys($sku, $urlKey);

return $rowData;
}

/**
* @param array $rowData
*
* @return mixed
*/
public function getCorrectSkuAsPerLength(array $rowData)
{
return strlen($rowData[self::COL_SKU]) > Sku::SKU_MAX_LENGTH ?
substr($rowData[self::COL_SKU], 0, Sku::SKU_MAX_LENGTH) : $rowData[self::COL_SKU];
}

/**
* @return \Magento\Framework\Serialize\Serializer\Json|\Magento\Framework\Serialize\SerializerInterface
*/
private function getSerializer()
{
return $this->_helper->serializer;
}
}
12 changes: 12 additions & 0 deletions Plugin/Block/Import/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ public function beforeSetForm(\Magento\ImportExport\Block\Adminhtml\Import\Edit\
}
}

$fileFieldset = $form->getElement('basic_behavior_fieldset');
$fileFieldset->addField(
'base_behavior generate_url',
'checkbox',
[
'name' => 'generate_url',
'label' => __('Generate Unique Url if Duplicate'),
'title' => __('Generate Unique Url if Duplicate'),
'value' => 1,
]
);

return [$form];
}
}
15 changes: 14 additions & 1 deletion view/adminhtml/templates/import/form/before.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require(['jquery', 'Magento_Ui/js/modal/alert', 'prototype'], function(jQuery){
this.showBehavior(entity.val());
this.showImportSource(true);
this.showSampleFile(entity.val());
this.showGenerateUrlIfDuplicate(entity.val());
} else {
this.showBehavior(false);
this.showImportSource(false);
Expand Down Expand Up @@ -66,7 +67,7 @@ require(['jquery', 'Magento_Ui/js/modal/alert', 'prototype'], function(jQuery){
action: newActionUrl,
target: this.ifrElemName
}]);
},
};

/**
* Show upload source dropdown
Expand All @@ -83,6 +84,18 @@ require(['jquery', 'Magento_Ui/js/modal/alert', 'prototype'], function(jQuery){
}
jQuery('.source-fieldset').find('._required').removeClass('required-entry').prop('disabled', true);
};

/**
* Show Generate Unique Url if Duplicate
* @param entitySelected
*/
varienImport.showGenerateUrlIfDuplicate = function(entitySelected) {
if (entitySelected === 'catalog_product') {
jQuery('.generate_url').show();
} else {
jQuery('.generate_url').hide();
}
};
//]]>

});
Expand Down