コード例 #1
0
ファイル: AbstractEntity.php プロジェクト: kid17/magento2
 /**
  * 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;
 }
コード例 #2
0
 /**
  * Add not skipped attributes to select
  *
  * @param AbstractCollection $collection
  * @return AbstractCollection
  */
 protected function _addAttributesToCollection(AbstractCollection $collection)
 {
     $attributeCodes = $this->_getExportAttributeCodes();
     $collection->addAttributeToSelect($attributeCodes);
     return $collection;
 }