1414 */
1515class AttributeGridCollection extends Collection
1616{
17+ private const FILTERED_FLAG_NAME = 'agc_filtered ' ;
1718
1819 /**
1920 * Adding item to collection
@@ -32,8 +33,6 @@ public function setItems(array $items): self
3233
3334 /**
3435 * @inheritDoc
35- *
36- * @return int
3736 */
3837 public function getSize (): int
3938 {
@@ -42,10 +41,6 @@ public function getSize(): int
4241
4342 /**
4443 * @inheritDoc
45- *
46- * @param string $field
47- * @param array|int|string $condition
48- * @return $this
4944 */
5045 public function addFieldToFilter ($ field , $ condition )
5146 {
@@ -59,28 +54,48 @@ public function addFieldToFilter($field, $condition)
5954
6055 /**
6156 * @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
6268 *
63- * @param false $printQuery
64- * @param false $logQuery
6569 * @return $this
6670 */
67- public function load ( $ printQuery = false , $ logQuery = false )
71+ private function filterCollection ( )
6872 {
69- if (!$ this ->getFlag (' isFilter ' ) && !empty ($ this ->_filters )) {
73+ if (!$ this ->getFlag (self :: FILTERED_FLAG_NAME ) && !empty ($ this ->_filters )) {
7074 foreach ($ this ->_filters as $ filter ) {
7175 foreach ($ this ->_items as $ item ) {
7276 $ field = $ item ->getData ($ filter ->getData ('field ' )) ?? '' ;
7377 if (stripos ($ field , $ filter ->getData ('value ' )) === false ) {
7478 $ this ->removeItemByKey ($ item ->getId ());
7579 }
7680 }
77- $ this ->setFlag ('isFilter ' , true );
7881 }
82+ $ this ->setFlag (self ::FILTERED_FLAG_NAME , true );
7983 }
8084
85+ return $ this ;
86+ }
87+
88+ /**
89+ * Sort collection by attribute code
90+ *
91+ * @return $this
92+ */
93+ private function sortCollectionByAttributeCode ()
94+ {
8195 $ sortOrder = $ this ->_orders ['attribute_code ' ];
8296 uasort ($ this ->_items , function ($ a , $ b ) use ($ sortOrder ) {
8397 $ cmp = strnatcmp ($ a ->getData ('attribute_code ' ), $ b ->getData ('attribute_code ' ));
98+
8499 return $ sortOrder === self ::SORT_ORDER_ASC ? $ cmp : -$ cmp ;
85100 });
86101
0 commit comments