14
14
*/
15
15
class AttributeGridCollection extends Collection
16
16
{
17
+ private const FILTERED_FLAG_NAME = 'agc_filtered ' ;
17
18
18
19
/**
19
20
* Adding item to collection
@@ -32,8 +33,6 @@ public function setItems(array $items): self
32
33
33
34
/**
34
35
* @inheritDoc
35
- *
36
- * @return int
37
36
*/
38
37
public function getSize (): int
39
38
{
@@ -42,10 +41,6 @@ public function getSize(): int
42
41
43
42
/**
44
43
* @inheritDoc
45
- *
46
- * @param string $field
47
- * @param array|int|string $condition
48
- * @return $this
49
44
*/
50
45
public function addFieldToFilter ($ field , $ condition )
51
46
{
@@ -59,28 +54,48 @@ public function addFieldToFilter($field, $condition)
59
54
60
55
/**
61
56
* @inheritDoc
57
+ */
58
+ public function load ($ printQuery = false , $ logQuery = false )
59
+ {
60
+ $ this ->filterCollection ();
61
+ $ this ->sortCollectionByAttributeCode ();
62
+
63
+ return $ this ;
64
+ }
65
+
66
+ /**
67
+ * Add filters to collection
62
68
*
63
- * @param false $printQuery
64
- * @param false $logQuery
65
69
* @return $this
66
70
*/
67
- public function load ( $ printQuery = false , $ logQuery = false )
71
+ private function filterCollection ( )
68
72
{
69
- if (!$ this ->getFlag (' isFilter ' ) && !empty ($ this ->_filters )) {
73
+ if (!$ this ->getFlag (self :: FILTERED_FLAG_NAME ) && !empty ($ this ->_filters )) {
70
74
foreach ($ this ->_filters as $ filter ) {
71
75
foreach ($ this ->_items as $ item ) {
72
76
$ field = $ item ->getData ($ filter ->getData ('field ' )) ?? '' ;
73
77
if (stripos ($ field , $ filter ->getData ('value ' )) === false ) {
74
78
$ this ->removeItemByKey ($ item ->getId ());
75
79
}
76
80
}
77
- $ this ->setFlag ('isFilter ' , true );
78
81
}
82
+ $ this ->setFlag (self ::FILTERED_FLAG_NAME , true );
79
83
}
80
84
85
+ return $ this ;
86
+ }
87
+
88
+ /**
89
+ * Sort collection by attribute code
90
+ *
91
+ * @return $this
92
+ */
93
+ private function sortCollectionByAttributeCode ()
94
+ {
81
95
$ sortOrder = $ this ->_orders ['attribute_code ' ];
82
96
uasort ($ this ->_items , function ($ a , $ b ) use ($ sortOrder ) {
83
97
$ cmp = strnatcmp ($ a ->getData ('attribute_code ' ), $ b ->getData ('attribute_code ' ));
98
+
84
99
return $ sortOrder === self ::SORT_ORDER_ASC ? $ cmp : -$ cmp ;
85
100
});
86
101
0 commit comments