/** * Apply filter to collection and add not skipped attributes to select. * * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _prepareEntityCollection(\Magento\Eav\Model\Entity\Collection\AbstractCollection $collection) { if (!isset($this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP]) || !is_array($this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP])) { $exportFilter = []; } else { $exportFilter = $this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP]; } $exportAttrCodes = $this->_getExportAttrCodes(); foreach ($this->filterAttributeCollection($this->getAttributeCollection()) as $attribute) { $attrCode = $attribute->getAttributeCode(); // filter applying if (isset($exportFilter[$attrCode])) { $attrFilterType = \Magento\ImportExport\Model\Export::getAttributeFilterType($attribute); if (\Magento\ImportExport\Model\Export::FILTER_TYPE_SELECT == $attrFilterType) { if (is_scalar($exportFilter[$attrCode]) && trim($exportFilter[$attrCode])) { $collection->addAttributeToFilter($attrCode, ['eq' => $exportFilter[$attrCode]]); } } elseif (\Magento\ImportExport\Model\Export::FILTER_TYPE_INPUT == $attrFilterType) { if (is_scalar($exportFilter[$attrCode]) && trim($exportFilter[$attrCode])) { $collection->addAttributeToFilter($attrCode, ['like' => "%{$exportFilter[$attrCode]}%"]); } } elseif (\Magento\ImportExport\Model\Export::FILTER_TYPE_DATE == $attrFilterType) { if (is_array($exportFilter[$attrCode]) && count($exportFilter[$attrCode]) == 2) { $from = array_shift($exportFilter[$attrCode]); $to = array_shift($exportFilter[$attrCode]); if (is_scalar($from) && !empty($from)) { $date = (new \DateTime($from))->format('m/d/Y'); $collection->addAttributeToFilter($attrCode, ['from' => $date, 'date' => true]); } if (is_scalar($to) && !empty($to)) { $date = (new \DateTime($to))->format('m/d/Y'); $collection->addAttributeToFilter($attrCode, ['to' => $date, 'date' => true]); } } } elseif (\Magento\ImportExport\Model\Export::FILTER_TYPE_NUMBER == $attrFilterType) { if (is_array($exportFilter[$attrCode]) && count($exportFilter[$attrCode]) == 2) { $from = array_shift($exportFilter[$attrCode]); $to = array_shift($exportFilter[$attrCode]); if (is_numeric($from)) { $collection->addAttributeToFilter($attrCode, ['from' => $from]); } if (is_numeric($to)) { $collection->addAttributeToFilter($attrCode, ['to' => $to]); } } } } if (in_array($attrCode, $exportAttrCodes)) { $collection->addAttributeToSelect($attrCode); } } return $collection; }
/** * Create filter fields for 'Filter' column. * * @param mixed $value * @param Attribute $row * @param \Magento\Framework\DataObject $column * @param boolean $isExport * @return string * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function decorateFilter($value, Attribute $row, \Magento\Framework\DataObject $column, $isExport) { $value = null; $values = $column->getValues(); if (is_array($values) && isset($values[$row->getAttributeCode()])) { $value = $values[$row->getAttributeCode()]; } $code = $row->getAttributeCode(); if (isset($this->_filterTypeByAttrCode[$code])) { $filterType = $this->_filterTypeByAttrCode[$code]; } else { $filterType = \Magento\ImportExport\Model\Export::getAttributeFilterType($row); } switch ($filterType) { case \Magento\ImportExport\Model\Export::FILTER_TYPE_SELECT: $cell = $this->_getSelectHtmlWithValue($row, $value); break; case \Magento\ImportExport\Model\Export::FILTER_TYPE_INPUT: $cell = $this->_getInputHtmlWithValue($row, $value); break; case \Magento\ImportExport\Model\Export::FILTER_TYPE_DATE: $cell = $this->_getDateFromToHtmlWithValue($row, $value); break; case \Magento\ImportExport\Model\Export::FILTER_TYPE_NUMBER: $cell = $this->_getNumberFromToHtmlWithValue($row, $value); break; default: $cell = __('Unknown attribute filter type'); } return $cell; }
/** * Apply filter to collection * * @param AbstractCollection $collection * @return AbstractCollection * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function filterEntityCollection(AbstractCollection $collection) { if (!isset($this->_parameters[Export::FILTER_ELEMENT_GROUP]) || !is_array($this->_parameters[Export::FILTER_ELEMENT_GROUP])) { $exportFilter = []; } else { $exportFilter = $this->_parameters[Export::FILTER_ELEMENT_GROUP]; } /** @var $attribute AbstractAttribute */ foreach ($this->filterAttributeCollection($this->getAttributeCollection()) as $attribute) { $attributeCode = $attribute->getAttributeCode(); // 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_INPUT == $attributeFilterType) { if (is_scalar($exportFilter[$attributeCode]) && trim($exportFilter[$attributeCode])) { $collection->addAttributeToFilter($attributeCode, ['like' => "%{$exportFilter[$attributeCode]}%"]); } } elseif (Export::FILTER_TYPE_DATE == $attributeFilterType) { if (is_array($exportFilter[$attributeCode]) && count($exportFilter[$attributeCode]) == 2) { $from = array_shift($exportFilter[$attributeCode]); $to = array_shift($exportFilter[$attributeCode]); if (is_scalar($from) && !empty($from)) { $date = (new \DateTime($from))->format('m/d/Y'); $collection->addAttributeToFilter($attributeCode, ['from' => $date, 'date' => true]); } if (is_scalar($to) && !empty($to)) { $date = (new \DateTime($to))->format('m/d/Y'); $collection->addAttributeToFilter($attributeCode, ['to' => $date, 'date' => true]); } } } elseif (Export::FILTER_TYPE_NUMBER == $attributeFilterType) { if (is_array($exportFilter[$attributeCode]) && count($exportFilter[$attributeCode]) == 2) { $from = array_shift($exportFilter[$attributeCode]); $to = array_shift($exportFilter[$attributeCode]); if (is_numeric($from)) { $collection->addAttributeToFilter($attributeCode, ['from' => $from]); } if (is_numeric($to)) { $collection->addAttributeToFilter($attributeCode, ['to' => $to]); } } } } } return $collection; }
/** * Test method '_getEntityAdapter' in case when entity is invalid * * @expectedException \Magento\Framework\Exception\LocalizedException * @covers \Magento\ImportExport\Model\Export::_getEntityAdapter */ public function testGetEntityAdapterWithInvalidEntity() { $this->_model->setData(['entity' => 'test']); $this->_model->getEntityAttributeCollection(); }