11
11
use Magento \Framework \App \RequestInterface ;
12
12
use Magento \Framework \View \Element \UiComponentInterface ;
13
13
use Magento \Framework \Data \Collection \AbstractDb ;
14
+ use Magento \Framework \View \Element \UiComponent \DataProvider \DataProviderInterface ;
14
15
15
16
/**
16
17
* Class Filter
@@ -43,6 +44,11 @@ class Filter
43
44
*/
44
45
protected $ filterBuilder ;
45
46
47
+ /**
48
+ * @var DataProviderInterface
49
+ */
50
+ private $ dataProvider ;
51
+
46
52
/**
47
53
* @param UiComponentFactory $factory
48
54
* @param RequestInterface $request
@@ -74,23 +80,33 @@ public function getComponent()
74
80
}
75
81
76
82
/**
83
+ * Adds filters to collection using DataProvider filter results
84
+ *
77
85
* @param AbstractDb $collection
78
86
* @return AbstractDb
79
87
* @throws LocalizedException
80
88
*/
81
89
public function getCollection (AbstractDb $ collection )
82
90
{
83
- $ component = $ this ->getComponent ();
84
- $ this ->prepareComponent ($ component );
85
- $ dataProvider = $ component ->getContext ()->getDataProvider ();
86
- $ dataProvider ->setLimit (0 , false );
87
- $ ids = [];
88
- foreach ($ dataProvider ->getSearchResult ()->getItems () as $ document ) {
89
- $ ids [] = $ document ->getId ();
90
- }
91
+ $ selected = $ this ->request ->getParam (static ::SELECTED_PARAM );
92
+ $ excluded = $ this ->request ->getParam (static ::EXCLUDED_PARAM );
91
93
92
- $ collection ->addFieldToFilter ($ collection ->getIdFieldName (), ['in ' => $ ids ]);
93
- return $ this ->applySelection ($ collection );
94
+ $ isExcludedIdsValid = (is_array ($ excluded ) && !empty ($ excluded ));
95
+ $ isSelectedIdsValid = (is_array ($ selected ) && !empty ($ selected ));
96
+
97
+ if ('false ' !== $ excluded ) {
98
+ if (!$ isExcludedIdsValid && !$ isSelectedIdsValid ) {
99
+ throw new LocalizedException (__ ('Please select item(s). ' ));
100
+ }
101
+ }
102
+ $ idsArray = $ this ->getFilterIds ();
103
+ if (!empty ($ idsArray )) {
104
+ $ collection ->addFieldToFilter (
105
+ $ collection ->getIdFieldName (),
106
+ ['in ' => $ idsArray ]
107
+ );
108
+ }
109
+ return $ collection ;
94
110
}
95
111
96
112
/**
@@ -106,9 +122,7 @@ public function applySelectionOnTargetProvider()
106
122
if ('false ' === $ excluded ) {
107
123
return ;
108
124
}
109
- $ component = $ this ->getComponent ();
110
- $ this ->prepareComponent ($ component );
111
- $ dataProvider = $ component ->getContext ()->getDataProvider ();
125
+ $ dataProvider = $ this ->getDataProvider ();
112
126
try {
113
127
if (is_array ($ excluded ) && !empty ($ excluded )) {
114
128
$ this ->filterBuilder ->setConditionType ('nin ' )
@@ -127,6 +141,8 @@ public function applySelectionOnTargetProvider()
127
141
}
128
142
129
143
/**
144
+ * Applies selection to collection from POST parameters
145
+ *
130
146
* @param AbstractDb $collection
131
147
* @return AbstractDb
132
148
* @throws LocalizedException
@@ -169,7 +185,7 @@ public function prepareComponent(UiComponentInterface $component)
169
185
}
170
186
171
187
/**
172
- * Returns RefererUrl
188
+ * Returns Referrer Url
173
189
*
174
190
* @return string|null
175
191
*/
@@ -178,4 +194,33 @@ public function getComponentRefererUrl()
178
194
$ data = $ this ->getComponent ()->getContext ()->getDataProvider ()->getConfigData ();
179
195
return (isset ($ data ['referer_url ' ])) ? $ data ['referer_url ' ] : null ;
180
196
}
197
+
198
+ /**
199
+ * Get data provider
200
+ *
201
+ * @return DataProviderInterface
202
+ */
203
+ private function getDataProvider ()
204
+ {
205
+ if (!$ this ->dataProvider ) {
206
+ $ component = $ this ->getComponent ();
207
+ $ this ->prepareComponent ($ component );
208
+ $ this ->dataProvider = $ component ->getContext ()->getDataProvider ();
209
+ }
210
+ return $ this ->dataProvider ;
211
+ }
212
+
213
+ /**
214
+ * Get filter ids as array
215
+ *
216
+ * @return int[]
217
+ */
218
+ private function getFilterIds ()
219
+ {
220
+ $ this ->applySelectionOnTargetProvider ();
221
+ if ($ this ->getDataProvider ()->getSearchResult ()) {
222
+ return $ this ->getDataProvider ()->getSearchResult ()->getAllIds ();
223
+ }
224
+ return [];
225
+ }
181
226
}
0 commit comments