diff --git a/app/code/Magento/ImportExport/Block/Adminhtml/Export/Filter.php b/app/code/Magento/ImportExport/Block/Adminhtml/Export/Filter.php index 94dc0ee7493b..dc928b4c7942 100644 --- a/app/code/Magento/ImportExport/Block/Adminhtml/Export/Filter.php +++ b/app/code/Magento/ImportExport/Block/Adminhtml/Export/Filter.php @@ -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']) { @@ -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( @@ -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; diff --git a/app/code/Magento/ImportExport/Model/Export.php b/app/code/Magento/ImportExport/Model/Export.php index 9b0139ff911c..695df18fd103 100644 --- a/app/code/Magento/ImportExport/Model/Export.php +++ b/app/code/Magento/ImportExport/Model/Export.php @@ -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'; @@ -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 @@ -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.') + ); } /** diff --git a/app/code/Magento/ImportExport/Model/Export/Entity/AbstractEav.php b/app/code/Magento/ImportExport/Model/Export/Entity/AbstractEav.php index 5ade67bbf894..df556e961d2c 100644 --- a/app/code/Magento/ImportExport/Model/Export/Entity/AbstractEav.php +++ b/app/code/Magento/ImportExport/Model/Export/Entity/AbstractEav.php @@ -152,7 +152,6 @@ 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( @@ -160,6 +159,18 @@ public function filterEntityCollection(AbstractCollection $collection) ['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( diff --git a/app/code/Magento/ImportExport/Model/Export/Entity/AbstractEntity.php b/app/code/Magento/ImportExport/Model/Export/Entity/AbstractEntity.php index 5cac5e46ffaf..d9e3879b4be1 100644 --- a/app/code/Magento/ImportExport/Model/Export/Entity/AbstractEntity.php +++ b/app/code/Magento/ImportExport/Model/Export/Entity/AbstractEntity.php @@ -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]}%"]);