3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Ui \Model \Export ;
7
8
9
+ use DateTime ;
10
+ use DateTimeZone ;
11
+ use Exception ;
8
12
use Magento \Framework \Api \Search \DocumentInterface ;
13
+ use Magento \Framework \Data \OptionSourceInterface ;
14
+ use Magento \Framework \Exception \LocalizedException ;
15
+ use Magento \Framework \Locale \ResolverInterface ;
16
+ use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
9
17
use Magento \Framework \View \Element \UiComponentInterface ;
10
18
use Magento \Ui \Component \Filters ;
11
19
use Magento \Ui \Component \Filters \Type \Select ;
12
20
use Magento \Ui \Component \Listing \Columns ;
21
+ use Magento \Ui \Component \Listing \Columns \Column ;
13
22
use Magento \Ui \Component \MassAction \Filter ;
14
- use Magento \Framework \Locale \ResolverInterface ;
15
- use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
16
23
17
24
/**
18
- * Metadata Provider
25
+ * Metadata Provider for grid listing export.
26
+ *
19
27
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20
28
*/
21
29
class MetadataProvider
@@ -76,7 +84,7 @@ public function __construct(
76
84
*
77
85
* @param UiComponentInterface $component
78
86
* @return UiComponentInterface
79
- * @throws \ Exception
87
+ * @throws Exception
80
88
*/
81
89
protected function getColumnsComponent (UiComponentInterface $ component )
82
90
{
@@ -85,14 +93,16 @@ protected function getColumnsComponent(UiComponentInterface $component)
85
93
return $ childComponent ;
86
94
}
87
95
}
88
- throw new \ Exception ('No columns found ' ); // @codingStandardsIgnoreLine
96
+ throw new Exception ('No columns found ' ); // @codingStandardsIgnoreLine
89
97
}
90
98
91
99
/**
92
100
* Returns columns list
93
101
*
94
102
* @param UiComponentInterface $component
103
+ *
95
104
* @return UiComponentInterface[]
105
+ * @throws Exception
96
106
*/
97
107
protected function getColumns (UiComponentInterface $ component )
98
108
{
@@ -111,7 +121,9 @@ protected function getColumns(UiComponentInterface $component)
111
121
* Retrieve Headers row array for Export
112
122
*
113
123
* @param UiComponentInterface $component
124
+ *
114
125
* @return string[]
126
+ * @throws Exception
115
127
*/
116
128
public function getHeaders (UiComponentInterface $ component )
117
129
{
@@ -127,7 +139,9 @@ public function getHeaders(UiComponentInterface $component)
127
139
* Returns DB fields list
128
140
*
129
141
* @param UiComponentInterface $component
142
+ *
130
143
* @return array
144
+ * @throws Exception
131
145
*/
132
146
public function getFields (UiComponentInterface $ component )
133
147
{
@@ -184,34 +198,73 @@ protected function getComplexLabel($list, $label, &$output)
184
198
}
185
199
186
200
/**
187
- * Returns array of Select options
201
+ * Prepare array of options.
202
+ *
203
+ * @param array $options
188
204
*
189
- * @param Select $filter
190
205
* @return array
191
206
*/
192
- protected function getFilterOptions ( Select $ filter )
207
+ protected function getOptionsArray ( array $ options ): array
193
208
{
194
- $ options = [];
195
- foreach ($ filter -> getData ( ' config/ options' ) as $ option ) {
209
+ $ preparedOptions = [];
210
+ foreach ($ options as $ option ) {
196
211
if (!is_array ($ option ['value ' ])) {
197
- $ options [$ option ['value ' ]] = $ option ['label ' ];
212
+ $ preparedOptions [$ option ['value ' ]] = $ option ['label ' ];
198
213
} else {
199
214
$ this ->getComplexLabel (
200
215
$ option ['value ' ],
201
216
$ option ['label ' ],
202
- $ options
217
+ $ preparedOptions
203
218
);
204
219
}
205
220
}
206
- return $ options ;
221
+
222
+ return $ preparedOptions ;
207
223
}
208
224
209
225
/**
210
226
* Returns Filters with options
211
227
*
212
228
* @return array
229
+ * @throws LocalizedException
213
230
*/
214
231
public function getOptions ()
232
+ {
233
+ return array_merge (
234
+ $ this ->getColumnOptions (),
235
+ $ this ->getFilterOptions ()
236
+ );
237
+ }
238
+
239
+ /**
240
+ * Get options from columns.
241
+ *
242
+ * @return array
243
+ * @throws LocalizedException
244
+ * @throws Exception
245
+ */
246
+ protected function getColumnOptions ()
247
+ {
248
+ $ options = [];
249
+ $ component = $ this ->filter ->getComponent ();
250
+ /** @var Column $columnComponent */
251
+ foreach ($ this ->getColumns ($ component ) as $ columnComponent ) {
252
+ /** @var OptionSourceInterface $options */
253
+ if ($ optionSource = $ columnComponent ->getData ('options ' )) {
254
+ $ options [$ columnComponent ->getName ()] = $ this ->getOptionsArray ($ optionSource ->toOptionArray ());
255
+ }
256
+ }
257
+
258
+ return $ options ;
259
+ }
260
+
261
+ /**
262
+ * Get options from column filters.
263
+ *
264
+ * @return array
265
+ * @throws LocalizedException
266
+ */
267
+ protected function getFilterOptions ()
215
268
{
216
269
$ options = [];
217
270
$ component = $ this ->filter ->getComponent ();
@@ -221,7 +274,7 @@ public function getOptions()
221
274
if ($ child instanceof Filters) {
222
275
foreach ($ child ->getChildComponents () as $ filter ) {
223
276
if ($ filter instanceof Select) {
224
- $ options [$ filter ->getName ()] = $ this ->getFilterOptions ($ filter );
277
+ $ options [$ filter ->getName ()] = $ this ->getOptionsArray ($ filter-> getData ( ' config/options ' ) );
225
278
}
226
279
}
227
280
}
@@ -232,9 +285,10 @@ public function getOptions()
232
285
/**
233
286
* Convert document date(UTC) fields to default scope specified
234
287
*
235
- * @param \Magento\Framework\Api\Search\ DocumentInterface $document
288
+ * @param DocumentInterface $document
236
289
* @param string $componentName
237
290
* @return void
291
+ * @throws Exception
238
292
*/
239
293
public function convertDate ($ document , $ componentName )
240
294
{
@@ -247,7 +301,7 @@ public function convertDate($document, $componentName)
247
301
continue ;
248
302
}
249
303
$ convertedDate = $ this ->localeDate ->date (
250
- new \ DateTime ($ fieldValue , new \ DateTimeZone ('UTC ' )),
304
+ new DateTime ($ fieldValue , new DateTimeZone ('UTC ' )),
251
305
$ this ->locale ,
252
306
true
253
307
);
0 commit comments