Skip to content

Commit 45030a3

Browse files
author
Stanislav Idolov
authored
MAGETWO-87017: [MAGETWO-1556] Export: Unable to Filter Data by Attribute With Input … #98
2 parents 63e5713 + ffdede8 commit 45030a3

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

app/code/Magento/ImportExport/Block/Adminhtml/Export/Filter.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected function _getMultiSelectHtmlWithValue(Attribute $attribute, $value)
142142
if ($attribute->getFilterOptions()) {
143143
$options = $attribute->getFilterOptions();
144144
} else {
145-
$options = $attribute->getSource()->getAllOptions(false);
145+
$options = $attribute->getSource()->getAllOptions();
146146

147147
foreach ($options as $key => $optionParams) {
148148
if ('' === $optionParams['value']) {
@@ -151,12 +151,13 @@ protected function _getMultiSelectHtmlWithValue(Attribute $attribute, $value)
151151
}
152152
}
153153
}
154+
154155
if ($size = count($options)) {
155156
$arguments = [
156157
'name' => $this->getFilterElementName($attribute->getAttributeCode()) . '[]',
157158
'id' => $this->getFilterElementId($attribute->getAttributeCode()),
158159
'class' => 'multiselect multiselect-export-filter',
159-
'extra_params' => 'multiple="multiple" size="' . ($size > 5 ? 5 : ($size < 2 ? 2 : $size)),
160+
'extra_params' => 'multiple="multiple" size="' . ($size > 5 ? 5 : ($size < 2 ? 2 : $size)) . '"',
160161
];
161162
/** @var $selectBlock \Magento\Framework\View\Element\Html\Select */
162163
$selectBlock = $this->_layout->createBlock(
@@ -364,6 +365,9 @@ public function decorateFilter($value, Attribute $row, \Magento\Framework\DataOb
364365
case \Magento\ImportExport\Model\Export::FILTER_TYPE_SELECT:
365366
$cell = $this->_getSelectHtmlWithValue($row, $value);
366367
break;
368+
case \Magento\ImportExport\Model\Export::FILTER_TYPE_MULTISELECT:
369+
$cell = $this->_getMultiSelectHtmlWithValue($row, $value);
370+
break;
367371
case \Magento\ImportExport\Model\Export::FILTER_TYPE_INPUT:
368372
$cell = $this->_getInputHtmlWithValue($row, $value);
369373
break;

app/code/Magento/ImportExport/Model/Export.php

+26-12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class Export extends \Magento\ImportExport\Model\AbstractModel
3030
*/
3131
const FILTER_TYPE_SELECT = 'select';
3232

33+
const FILTER_TYPE_MULTISELECT = 'multiselect';
34+
3335
const FILTER_TYPE_INPUT = 'input';
3436

3537
const FILTER_TYPE_DATE = 'date';
@@ -65,6 +67,17 @@ class Export extends \Magento\ImportExport\Model\AbstractModel
6567
*/
6668
protected $_exportAdapterFac;
6769

70+
/**
71+
* @var array
72+
*/
73+
private static $backendTypeToFilterMapper = [
74+
'datetime' => self::FILTER_TYPE_DATE,
75+
'decimal' => self::FILTER_TYPE_NUMBER,
76+
'int' => self::FILTER_TYPE_NUMBER,
77+
'varchar' => self::FILTER_TYPE_INPUT,
78+
'text' => self::FILTER_TYPE_INPUT
79+
];
80+
6881
/**
6982
* @param \Psr\Log\LoggerInterface $logger
7083
* @param \Magento\Framework\Filesystem $filesystem
@@ -215,20 +228,21 @@ public function filterAttributeCollection(\Magento\Framework\Data\Collection $co
215228
public static function getAttributeFilterType(\Magento\Eav\Model\Entity\Attribute $attribute)
216229
{
217230
if ($attribute->usesSource() || $attribute->getFilterOptions()) {
218-
return self::FILTER_TYPE_SELECT;
219-
} elseif ('datetime' == $attribute->getBackendType()) {
220-
return self::FILTER_TYPE_DATE;
221-
} elseif ('decimal' == $attribute->getBackendType() || 'int' == $attribute->getBackendType()) {
222-
return self::FILTER_TYPE_NUMBER;
223-
} elseif ('varchar' == $attribute->getBackendType() || 'text' == $attribute->getBackendType()) {
224-
return self::FILTER_TYPE_INPUT;
225-
} elseif ($attribute->isStatic()) {
231+
return 'multiselect' == $attribute->getFrontendInput() ?
232+
self::FILTER_TYPE_MULTISELECT : self::FILTER_TYPE_SELECT;
233+
}
234+
235+
if (isset(self::$backendTypeToFilterMapper[$attribute->getBackendType()])) {
236+
return self::$backendTypeToFilterMapper[$attribute->getBackendType()];
237+
}
238+
239+
if ($attribute->isStatic()) {
226240
return self::getStaticAttributeFilterType($attribute);
227-
} else {
228-
throw new \Magento\Framework\Exception\LocalizedException(
229-
__('We can\'t determine the attribute filter type.')
230-
);
231241
}
242+
243+
throw new \Magento\Framework\Exception\LocalizedException(
244+
__('We can\'t determine the attribute filter type.')
245+
);
232246
}
233247

234248
/**

app/code/Magento/ImportExport/Model/Export/Entity/AbstractEav.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,25 @@ public function filterEntityCollection(AbstractCollection $collection)
152152
// filter applying
153153
if (isset($exportFilter[$attributeCode])) {
154154
$attributeFilterType = Export::getAttributeFilterType($attribute);
155-
156155
if (Export::FILTER_TYPE_SELECT == $attributeFilterType) {
157156
if (is_scalar($exportFilter[$attributeCode]) && trim($exportFilter[$attributeCode])) {
158157
$collection->addAttributeToFilter(
159158
$attributeCode,
160159
['eq' => $exportFilter[$attributeCode]]
161160
);
162161
}
162+
} elseif (Export::FILTER_TYPE_MULTISELECT == $attributeFilterType) {
163+
if (is_array($exportFilter[$attributeCode])) {
164+
array_filter($exportFilter[$attributeCode]);
165+
if (!empty($exportFilter[$attributeCode])) {
166+
foreach ($exportFilter[$attributeCode] as $val) {
167+
$collection->addAttributeToFilter(
168+
$attributeCode,
169+
['finset' => $val]
170+
);
171+
}
172+
}
173+
}
163174
} elseif (Export::FILTER_TYPE_INPUT == $attributeFilterType) {
164175
if (is_scalar($exportFilter[$attributeCode]) && trim($exportFilter[$attributeCode])) {
165176
$collection->addAttributeToFilter(

app/code/Magento/ImportExport/Model/Export/Entity/AbstractEntity.php

+12
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,18 @@ protected function _prepareEntityCollection(\Magento\Eav\Model\Entity\Collection
277277
if (is_scalar($exportFilter[$attrCode]) && trim($exportFilter[$attrCode])) {
278278
$collection->addAttributeToFilter($attrCode, ['eq' => $exportFilter[$attrCode]]);
279279
}
280+
} elseif (\Magento\ImportExport\Model\Export::FILTER_TYPE_MULTISELECT == $attrFilterType) {
281+
if (is_array($exportFilter[$attrCode])) {
282+
array_filter($exportFilter[$attrCode]);
283+
if (!empty($exportFilter[$attrCode])) {
284+
foreach ($exportFilter[$attrCode] as $val) {
285+
$collection->addAttributeToFilter(
286+
$attrCode,
287+
['finset' => $val]
288+
);
289+
}
290+
}
291+
}
280292
} elseif (\Magento\ImportExport\Model\Export::FILTER_TYPE_INPUT == $attrFilterType) {
281293
if (is_scalar($exportFilter[$attrCode]) && trim($exportFilter[$attrCode])) {
282294
$collection->addAttributeToFilter($attrCode, ['like' => "%{$exportFilter[$attrCode]}%"]);

0 commit comments

Comments
 (0)