5
5
*/
6
6
namespace Magento \Ui \Component \MassAction ;
7
7
8
+ use Magento \Framework \Data \Collection ;
8
9
use Magento \Framework \Api \FilterBuilder ;
9
10
use Magento \Framework \Exception \LocalizedException ;
10
11
use Magento \Framework \View \Element \UiComponentFactory ;
11
12
use Magento \Framework \App \RequestInterface ;
12
13
use Magento \Framework \View \Element \UiComponentInterface ;
13
14
use Magento \Framework \Data \Collection \AbstractDb ;
15
+ use Magento \Framework \View \Element \UiComponent \DataProvider \DataProviderInterface ;
14
16
15
17
/**
16
18
* Class Filter
@@ -43,6 +45,11 @@ class Filter
43
45
*/
44
46
protected $ filterBuilder ;
45
47
48
+ /**
49
+ * @var DataProviderInterface
50
+ */
51
+ private $ dataProvider ;
52
+
46
53
/**
47
54
* @param UiComponentFactory $factory
48
55
* @param RequestInterface $request
@@ -70,27 +77,39 @@ public function getComponent()
70
77
if (!isset ($ this ->components [$ namespace ])) {
71
78
$ this ->components [$ namespace ] = $ this ->factory ->create ($ namespace );
72
79
}
80
+
73
81
return $ this ->components [$ namespace ];
74
82
}
75
83
76
84
/**
85
+ * Adds filters to collection using DataProvider filter results
86
+ *
77
87
* @param AbstractDb $collection
78
88
* @return AbstractDb
79
89
* @throws LocalizedException
80
90
*/
81
91
public function getCollection (AbstractDb $ collection )
82
92
{
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 ();
93
+ $ selected = $ this ->request ->getParam (static ::SELECTED_PARAM );
94
+ $ excluded = $ this ->request ->getParam (static ::EXCLUDED_PARAM );
95
+
96
+ $ isExcludedIdsValid = (is_array ($ excluded ) && !empty ($ excluded ));
97
+ $ isSelectedIdsValid = (is_array ($ selected ) && !empty ($ selected ));
98
+
99
+ if ('false ' !== $ excluded ) {
100
+ if (!$ isExcludedIdsValid && !$ isSelectedIdsValid ) {
101
+ throw new LocalizedException (__ ('Please select item(s). ' ));
102
+ }
103
+ }
104
+ $ idsArray = $ this ->getFilterIds ();
105
+ if (!empty ($ idsArray )) {
106
+ $ collection ->addFieldToFilter (
107
+ $ collection ->getIdFieldName (),
108
+ ['in ' => $ idsArray ]
109
+ );
90
110
}
91
111
92
- $ collection ->addFieldToFilter ($ collection ->getIdFieldName (), ['in ' => $ ids ]);
93
- return $ this ->applySelection ($ collection );
112
+ return $ collection ;
94
113
}
95
114
96
115
/**
@@ -103,12 +122,12 @@ public function applySelectionOnTargetProvider()
103
122
{
104
123
$ selected = $ this ->request ->getParam (static ::SELECTED_PARAM );
105
124
$ excluded = $ this ->request ->getParam (static ::EXCLUDED_PARAM );
125
+
106
126
if ('false ' === $ excluded ) {
107
127
return ;
108
128
}
109
- $ component = $ this ->getComponent ();
110
- $ this ->prepareComponent ($ component );
111
- $ dataProvider = $ component ->getContext ()->getDataProvider ();
129
+
130
+ $ dataProvider = $ this ->getDataProvider ();
112
131
try {
113
132
if (is_array ($ excluded ) && !empty ($ excluded )) {
114
133
$ this ->filterBuilder ->setConditionType ('nin ' )
@@ -127,6 +146,8 @@ public function applySelectionOnTargetProvider()
127
146
}
128
147
129
148
/**
149
+ * Applies selection to collection from POST parameters
150
+ *
130
151
* @param AbstractDb $collection
131
152
* @return AbstractDb
132
153
* @throws LocalizedException
@@ -151,6 +172,7 @@ protected function applySelection(AbstractDb $collection)
151
172
} catch (\Exception $ e ) {
152
173
throw new LocalizedException (__ ($ e ->getMessage ()));
153
174
}
175
+
154
176
return $ collection ;
155
177
}
156
178
@@ -176,6 +198,51 @@ public function prepareComponent(UiComponentInterface $component)
176
198
public function getComponentRefererUrl ()
177
199
{
178
200
$ data = $ this ->getComponent ()->getContext ()->getDataProvider ()->getConfigData ();
201
+
179
202
return (isset ($ data ['referer_url ' ])) ? $ data ['referer_url ' ] : null ;
180
203
}
204
+
205
+ /**
206
+ * Get data provider
207
+ *
208
+ * @return DataProviderInterface
209
+ */
210
+ private function getDataProvider ()
211
+ {
212
+ if (!$ this ->dataProvider ) {
213
+ $ component = $ this ->getComponent ();
214
+ $ this ->prepareComponent ($ component );
215
+ $ this ->dataProvider = $ component ->getContext ()->getDataProvider ();
216
+ $ this ->dataProvider ->setLimit (0 , false );
217
+ }
218
+
219
+ return $ this ->dataProvider ;
220
+ }
221
+
222
+ /**
223
+ * Get filter ids as array
224
+ *
225
+ * @return int[]
226
+ */
227
+ private function getFilterIds ()
228
+ {
229
+ $ ids = [];
230
+ $ this ->applySelectionOnTargetProvider ();
231
+
232
+ /** @var \Magento\Framework\Api\Search\SearchResultInterface $searchResult */
233
+ $ searchResult = $ this ->getDataProvider ()->getSearchResult ();
234
+
235
+ if ($ searchResult ) {
236
+ if ($ searchResult instanceof Collection
237
+ || method_exists ($ searchResult , 'getAllIds ' )) {
238
+ $ ids = $ searchResult ->getAllIds ();
239
+ } else {
240
+ foreach ($ searchResult ->getItems () as $ document ) {
241
+ $ ids [] = $ document ->getId ();
242
+ }
243
+ }
244
+ }
245
+
246
+ return $ ids ;
247
+ }
181
248
}
0 commit comments