|
10 | 10 | use Magento\Framework\Data\Collection;
|
11 | 11 |
|
12 | 12 | /**
|
13 |
| - * Class AttributeGridCollection |
| 13 | + * Association of attributes for grid |
14 | 14 | */
|
15 | 15 | class AttributeGridCollection extends Collection
|
16 | 16 | {
|
@@ -49,45 +49,41 @@ public function getSize(): int
|
49 | 49 | */
|
50 | 50 | public function addFieldToFilter($field, $condition)
|
51 | 51 | {
|
52 |
| - $value = (string)$condition['like']; |
53 |
| - $value = trim(trim($value, "'"), "%"); |
54 |
| - foreach ($this->getItems() as $item) { |
55 |
| - if (stripos($item->getData($field), $value) === false) { |
56 |
| - $this->removeItemByKey($item->getId()); |
57 |
| - } |
| 52 | + if (isset($condition['like'])) { |
| 53 | + $value = trim((string)$condition['like'], "'%"); |
| 54 | + $this->addFilter($field, $value); |
58 | 55 | }
|
59 | 56 |
|
60 | 57 | return $this;
|
61 | 58 | }
|
62 | 59 |
|
63 | 60 | /**
|
64 |
| - * Add select order |
| 61 | + * @inheritDoc |
65 | 62 | *
|
66 |
| - * @param string $field |
67 |
| - * @param string $direction |
| 63 | + * @param false $printQuery |
| 64 | + * @param false $logQuery |
68 | 65 | * @return $this
|
69 | 66 | */
|
70 |
| - public function setOrder($field, $direction = self::SORT_ORDER_DESC) |
| 67 | + public function load($printQuery = false, $logQuery = false) |
71 | 68 | {
|
72 |
| - $this->_orderField = $field; |
73 |
| - uasort($this->_items, [$this, 'compareAttributes']); |
74 |
| - |
75 |
| - if ($direction == self::SORT_ORDER_DESC) { |
76 |
| - $this->_items = array_reverse($this->_items, true); |
| 69 | + if (!$this->getFlag('isFilter') && !empty($this->_filters)) { |
| 70 | + foreach ($this->_filters as $filter) { |
| 71 | + foreach ($this->_items as $item) { |
| 72 | + $field = $item->getData($filter->getData('field')) ?? ''; |
| 73 | + if (stripos($field, $filter->getData('value')) === false) { |
| 74 | + $this->removeItemByKey($item->getId()); |
| 75 | + } |
| 76 | + } |
| 77 | + $this->setFlag('isFilter', true); |
| 78 | + } |
77 | 79 | }
|
78 | 80 |
|
79 |
| - return $this; |
80 |
| - } |
| 81 | + $sortOrder = $this->_orders['attribute_code']; |
| 82 | + uasort($this->_items, function ($a, $b) use ($sortOrder) { |
| 83 | + $cmp = strnatcmp($a->getData('attribute_code'), $b->getData('attribute_code')); |
| 84 | + return $sortOrder === self::SORT_ORDER_ASC ? $cmp : -$cmp; |
| 85 | + }); |
81 | 86 |
|
82 |
| - /** |
83 |
| - * Compare two collection items |
84 |
| - * |
85 |
| - * @param \Magento\Framework\DataObject $a |
86 |
| - * @param \Magento\Framework\DataObject $b |
87 |
| - * @return int |
88 |
| - */ |
89 |
| - public function compareAttributes(\Magento\Framework\DataObject $a, \Magento\Framework\DataObject $b) |
90 |
| - { |
91 |
| - return strnatcmp($a->getData($this->_orderField), $b->getData($this->_orderField)); |
| 87 | + return $this; |
92 | 88 | }
|
93 | 89 | }
|
0 commit comments