Skip to content

Commit

Permalink
MAGETWO-87017: [MAGETWO-1556] Export: Unable to Filter Data by Attrib…
Browse files Browse the repository at this point in the history
…ute With Input … #98

 - Merge Pull Request magento-engcom/import-export-improvements#98 from php4umagento/import-export-improvements:2.3-develop
 - Merged commits:
   1. d8c59be
   2. 6f88d3f
   3. 3946034
   4. bd8930d
   5. 4f28878
   6. 3da3ae6
  • Loading branch information
magento-engcom-team committed Mar 29, 2018
2 parents 452710d + 3da3ae6 commit ffdede8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected function _getMultiSelectHtmlWithValue(Attribute $attribute, $value)
if ($attribute->getFilterOptions()) {
$options = $attribute->getFilterOptions();
} else {
$options = $attribute->getSource()->getAllOptions(false);
$options = $attribute->getSource()->getAllOptions();

foreach ($options as $key => $optionParams) {
if ('' === $optionParams['value']) {
Expand All @@ -151,12 +151,13 @@ protected function _getMultiSelectHtmlWithValue(Attribute $attribute, $value)
}
}
}

if ($size = count($options)) {
$arguments = [
'name' => $this->getFilterElementName($attribute->getAttributeCode()) . '[]',
'id' => $this->getFilterElementId($attribute->getAttributeCode()),
'class' => 'multiselect multiselect-export-filter',
'extra_params' => 'multiple="multiple" size="' . ($size > 5 ? 5 : ($size < 2 ? 2 : $size)),
'extra_params' => 'multiple="multiple" size="' . ($size > 5 ? 5 : ($size < 2 ? 2 : $size)) . '"',
];
/** @var $selectBlock \Magento\Framework\View\Element\Html\Select */
$selectBlock = $this->_layout->createBlock(
Expand Down Expand Up @@ -364,6 +365,9 @@ public function decorateFilter($value, Attribute $row, \Magento\Framework\DataOb
case \Magento\ImportExport\Model\Export::FILTER_TYPE_SELECT:
$cell = $this->_getSelectHtmlWithValue($row, $value);
break;
case \Magento\ImportExport\Model\Export::FILTER_TYPE_MULTISELECT:
$cell = $this->_getMultiSelectHtmlWithValue($row, $value);
break;
case \Magento\ImportExport\Model\Export::FILTER_TYPE_INPUT:
$cell = $this->_getInputHtmlWithValue($row, $value);
break;
Expand Down
38 changes: 26 additions & 12 deletions app/code/Magento/ImportExport/Model/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Export extends \Magento\ImportExport\Model\AbstractModel
*/
const FILTER_TYPE_SELECT = 'select';

const FILTER_TYPE_MULTISELECT = 'multiselect';

const FILTER_TYPE_INPUT = 'input';

const FILTER_TYPE_DATE = 'date';
Expand Down Expand Up @@ -65,6 +67,17 @@ class Export extends \Magento\ImportExport\Model\AbstractModel
*/
protected $_exportAdapterFac;

/**
* @var array
*/
private static $backendTypeToFilterMapper = [
'datetime' => self::FILTER_TYPE_DATE,
'decimal' => self::FILTER_TYPE_NUMBER,
'int' => self::FILTER_TYPE_NUMBER,
'varchar' => self::FILTER_TYPE_INPUT,
'text' => self::FILTER_TYPE_INPUT
];

/**
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Filesystem $filesystem
Expand Down Expand Up @@ -215,20 +228,21 @@ public function filterAttributeCollection(\Magento\Framework\Data\Collection $co
public static function getAttributeFilterType(\Magento\Eav\Model\Entity\Attribute $attribute)
{
if ($attribute->usesSource() || $attribute->getFilterOptions()) {
return self::FILTER_TYPE_SELECT;
} elseif ('datetime' == $attribute->getBackendType()) {
return self::FILTER_TYPE_DATE;
} elseif ('decimal' == $attribute->getBackendType() || 'int' == $attribute->getBackendType()) {
return self::FILTER_TYPE_NUMBER;
} elseif ('varchar' == $attribute->getBackendType() || 'text' == $attribute->getBackendType()) {
return self::FILTER_TYPE_INPUT;
} elseif ($attribute->isStatic()) {
return 'multiselect' == $attribute->getFrontendInput() ?
self::FILTER_TYPE_MULTISELECT : self::FILTER_TYPE_SELECT;
}

if (isset(self::$backendTypeToFilterMapper[$attribute->getBackendType()])) {
return self::$backendTypeToFilterMapper[$attribute->getBackendType()];
}

if ($attribute->isStatic()) {
return self::getStaticAttributeFilterType($attribute);
} else {
throw new \Magento\Framework\Exception\LocalizedException(
__('We can\'t determine the attribute filter type.')
);
}

throw new \Magento\Framework\Exception\LocalizedException(
__('We can\'t determine the attribute filter type.')
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,25 @@ public function filterEntityCollection(AbstractCollection $collection)
// filter applying
if (isset($exportFilter[$attributeCode])) {
$attributeFilterType = Export::getAttributeFilterType($attribute);

if (Export::FILTER_TYPE_SELECT == $attributeFilterType) {
if (is_scalar($exportFilter[$attributeCode]) && trim($exportFilter[$attributeCode])) {
$collection->addAttributeToFilter(
$attributeCode,
['eq' => $exportFilter[$attributeCode]]
);
}
} elseif (Export::FILTER_TYPE_MULTISELECT == $attributeFilterType) {
if (is_array($exportFilter[$attributeCode])) {
array_filter($exportFilter[$attributeCode]);
if (!empty($exportFilter[$attributeCode])) {
foreach ($exportFilter[$attributeCode] as $val) {
$collection->addAttributeToFilter(
$attributeCode,
['finset' => $val]
);
}
}
}
} elseif (Export::FILTER_TYPE_INPUT == $attributeFilterType) {
if (is_scalar($exportFilter[$attributeCode]) && trim($exportFilter[$attributeCode])) {
$collection->addAttributeToFilter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,18 @@ protected function _prepareEntityCollection(\Magento\Eav\Model\Entity\Collection
if (is_scalar($exportFilter[$attrCode]) && trim($exportFilter[$attrCode])) {
$collection->addAttributeToFilter($attrCode, ['eq' => $exportFilter[$attrCode]]);
}
} elseif (\Magento\ImportExport\Model\Export::FILTER_TYPE_MULTISELECT == $attrFilterType) {
if (is_array($exportFilter[$attrCode])) {
array_filter($exportFilter[$attrCode]);
if (!empty($exportFilter[$attrCode])) {
foreach ($exportFilter[$attrCode] as $val) {
$collection->addAttributeToFilter(
$attrCode,
['finset' => $val]
);
}
}
}
} elseif (\Magento\ImportExport\Model\Export::FILTER_TYPE_INPUT == $attrFilterType) {
if (is_scalar($exportFilter[$attrCode]) && trim($exportFilter[$attrCode])) {
$collection->addAttributeToFilter($attrCode, ['like' => "%{$exportFilter[$attrCode]}%"]);
Expand Down

0 comments on commit ffdede8

Please sign in to comment.