6
6
7
7
namespace Magento \Backend \Block \Widget \Grid ;
8
8
9
+ use Magento \Backend \Block \Template \Context ;
10
+ use Magento \Backend \Block \Widget ;
11
+ use Magento \Backend \Block \Widget \Button ;
9
12
use Magento \Framework \App \Filesystem \DirectoryList ;
13
+ use Magento \Framework \Convert \Excel ;
14
+ use Magento \Framework \Data \Collection ;
15
+ use Magento \Framework \Data \CollectionFactory ;
16
+ use Magento \Framework \DataObject ;
17
+ use Magento \Framework \Exception \FileSystemException ;
18
+ use Magento \Framework \Exception \LocalizedException ;
19
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
20
+ use Magento \Framework \Filesystem \File \WriteInterface as FileWriteInterface ;
10
21
11
22
/**
12
23
* Class Export for exporting grid data as CSV file or MS Excel 2003 XML Document file
16
27
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17
28
* @since 100.0.2
18
29
*/
19
- class Export extends \ Magento \ Backend \ Block \ Widget implements \ Magento \ Backend \ Block \ Widget \ Grid \ ExportInterface
30
+ class Export extends Widget implements ExportInterface
20
31
{
21
32
/**
22
33
* Grid export types
23
34
*
24
- * @var \Magento\Framework\ DataObject[]
35
+ * @var DataObject[]
25
36
*/
26
37
protected $ _exportTypes = [];
27
38
@@ -40,12 +51,12 @@ class Export extends \Magento\Backend\Block\Widget implements \Magento\Backend\B
40
51
protected $ _template = "Magento_Backend::widget/grid/export.phtml " ;
41
52
42
53
/**
43
- * @var \Magento\Framework\Data\ CollectionFactory
54
+ * @var CollectionFactory
44
55
*/
45
56
protected $ _collectionFactory ;
46
57
47
58
/**
48
- * @var \Magento\Framework\Filesystem\Directory\ WriteInterface
59
+ * @var WriteInterface
49
60
*/
50
61
protected $ _directory ;
51
62
@@ -57,13 +68,13 @@ class Export extends \Magento\Backend\Block\Widget implements \Magento\Backend\B
57
68
protected $ _path = 'export ' ;
58
69
59
70
/**
60
- * @param \Magento\Backend\Block\Template\ Context $context
61
- * @param \Magento\Framework\Data\ CollectionFactory $collectionFactory
71
+ * @param Context $context
72
+ * @param CollectionFactory $collectionFactory
62
73
* @param array $data
63
74
*/
64
75
public function __construct (
65
- \ Magento \ Backend \ Block \ Template \ Context $ context ,
66
- \ Magento \ Framework \ Data \ CollectionFactory $ collectionFactory ,
76
+ Context $ context ,
77
+ CollectionFactory $ collectionFactory ,
67
78
array $ data = []
68
79
) {
69
80
$ this ->_collectionFactory = $ collectionFactory ;
@@ -74,15 +85,15 @@ public function __construct(
74
85
* Internal constructor, that is called from real constructor
75
86
*
76
87
* @return void
77
- * @throws \Magento\Framework\Exception\ LocalizedException
88
+ * @throws LocalizedException
78
89
*/
79
90
protected function _construct ()
80
91
{
81
92
parent ::_construct ();
82
93
if ($ this ->hasData ('exportTypes ' )) {
83
94
foreach ($ this ->getData ('exportTypes ' ) as $ type ) {
84
95
if (!isset ($ type ['urlPath ' ]) || !isset ($ type ['label ' ])) {
85
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
96
+ throw new LocalizedException (
86
97
__ ('Invalid export type supplied for grid export block ' )
87
98
);
88
99
}
@@ -95,7 +106,7 @@ protected function _construct()
95
106
/**
96
107
* Retrieve grid columns
97
108
*
98
- * @return \Magento\Backend\Block\Widget\Grid\ Column[]
109
+ * @return Column[]
99
110
*/
100
111
protected function _getColumns ()
101
112
{
@@ -105,7 +116,7 @@ protected function _getColumns()
105
116
/**
106
117
* Retrieve totals
107
118
*
108
- * @return \Magento\Framework\ DataObject
119
+ * @return DataObject
109
120
*/
110
121
protected function _getTotals ()
111
122
{
@@ -126,7 +137,7 @@ public function getCountTotals()
126
137
/**
127
138
* Get collection object
128
139
*
129
- * @return \Magento\Framework\Data\ Collection
140
+ * @return Collection
130
141
*/
131
142
protected function _getCollection ()
132
143
{
@@ -136,7 +147,7 @@ protected function _getCollection()
136
147
/**
137
148
* Retrieve grid export types
138
149
*
139
- * @return \Magento\Framework\ DataObject[]|false
150
+ * @return DataObject[]|false
140
151
*/
141
152
public function getExportTypes ()
142
153
{
@@ -157,13 +168,14 @@ public function getId()
157
168
* Prepare export button
158
169
*
159
170
* @return $this
171
+ * @throws LocalizedException
160
172
*/
161
173
protected function _prepareLayout ()
162
174
{
163
175
$ this ->setChild (
164
176
'export_button ' ,
165
177
$ this ->getLayout ()->createBlock (
166
- \ Magento \ Backend \ Block \ Widget \ Button::class
178
+ Button::class
167
179
)->setData (
168
180
[
169
181
'label ' => __ ('Export ' ),
@@ -194,7 +206,7 @@ public function getExportButtonHtml()
194
206
*/
195
207
public function addExportType ($ url , $ label )
196
208
{
197
- $ this ->_exportTypes [] = new \ Magento \ Framework \ DataObject (
209
+ $ this ->_exportTypes [] = new DataObject (
198
210
['url ' => $ this ->getUrl ($ url , ['_current ' => true ]), 'label ' => $ label ]
199
211
);
200
212
return $ this ;
@@ -205,6 +217,7 @@ public function addExportType($url, $label)
205
217
*
206
218
* @param array $fileData
207
219
* @return string
220
+ * @throws FileSystemException
208
221
*/
209
222
protected function _getFileContainerContent (array $ fileData )
210
223
{
@@ -255,7 +268,7 @@ protected function _getExportTotals()
255
268
*/
256
269
public function _exportIterateCollection ($ callback , array $ args )
257
270
{
258
- /** @var $originalCollection \Magento\Framework\Data\ Collection */
271
+ /** @var $originalCollection Collection */
259
272
$ originalCollection = $ this ->getParentBlock ()->getPreparedCollection ();
260
273
$ count = null ;
261
274
$ page = 1 ;
@@ -291,13 +304,14 @@ public function _exportIterateCollection($callback, array $args)
291
304
/**
292
305
* Write item data to csv export file
293
306
*
294
- * @param \Magento\Framework\ DataObject $item
295
- * @param \Magento\Framework\Filesystem\File\WriteInterface $stream
307
+ * @param DataObject $item
308
+ * @param FileWriteInterface $stream
296
309
* @return void
310
+ * @throws FileSystemException
297
311
*/
298
312
protected function _exportCsvItem (
299
- \ Magento \ Framework \ DataObject $ item ,
300
- \ Magento \ Framework \ Filesystem \ File \ WriteInterface $ stream
313
+ DataObject $ item ,
314
+ FileWriteInterface $ stream
301
315
) {
302
316
$ row = [];
303
317
foreach ($ this ->_getColumns () as $ column ) {
@@ -364,7 +378,7 @@ public function getCsv()
364
378
$ data [] = '" ' . str_replace (
365
379
['" ' , '\\' ],
366
380
['"" ' , '\\\\' ],
367
- $ column ->getRowFieldExport ($ item )
381
+ $ column ->getRowFieldExport ($ item ) ?: ''
368
382
) . '" ' ;
369
383
}
370
384
}
@@ -378,7 +392,7 @@ public function getCsv()
378
392
$ data [] = '" ' . str_replace (
379
393
['" ' , '\\' ],
380
394
['"" ' , '\\\\' ],
381
- $ column ->getRowFieldExport ($ this ->_getTotals ())
395
+ $ column ->getRowFieldExport ($ this ->_getTotals ()) ?: ''
382
396
) . '" ' ;
383
397
}
384
398
}
@@ -418,10 +432,10 @@ public function getXml()
418
432
/**
419
433
* Get a row data of the particular columns
420
434
*
421
- * @param \Magento\Framework\ DataObject $data
435
+ * @param DataObject $data
422
436
* @return string[]
423
437
*/
424
- public function getRowRecord (\ Magento \ Framework \ DataObject $ data )
438
+ public function getRowRecord (DataObject $ data )
425
439
{
426
440
$ row = [];
427
441
foreach ($ this ->_getColumns () as $ column ) {
@@ -444,7 +458,7 @@ public function getExcelFile($sheetName = '')
444
458
{
445
459
$ collection = $ this ->_getPreparedCollection ();
446
460
447
- $ convert = new \ Magento \ Framework \ Convert \ Excel ($ collection ->getIterator (), [$ this , 'getRowRecord ' ]);
461
+ $ convert = new Excel ($ collection ->getIterator (), [$ this , 'getRowRecord ' ]);
448
462
449
463
$ name = hash ('sha256 ' , microtime ());
450
464
$ file = $ this ->_path . '/ ' . $ name . '.xml ' ;
@@ -507,30 +521,30 @@ public function getExcel()
507
521
$ data [] = $ row ;
508
522
}
509
523
510
- $ convert = new \ Magento \ Framework \ Convert \ Excel (new \ArrayIterator ($ data ));
524
+ $ convert = new Excel (new \ArrayIterator ($ data ));
511
525
return $ convert ->convert ('single_sheet ' );
512
526
}
513
527
514
528
/**
515
529
* Reformat base collection into collection without sub-collection in items
516
530
*
517
- * @param \Magento\Framework\Data\ Collection $baseCollection
518
- * @return \Magento\Framework\Data\ Collection
531
+ * @param Collection $baseCollection
532
+ * @return Collection
519
533
*/
520
- protected function _getRowCollection (\ Magento \ Framework \ Data \ Collection $ baseCollection = null )
534
+ protected function _getRowCollection (Collection $ baseCollection = null )
521
535
{
522
536
if (null === $ baseCollection ) {
523
537
$ baseCollection = $ this ->getParentBlock ()->getPreparedCollection ();
524
538
}
525
539
$ collection = $ this ->_collectionFactory ->create ();
526
540
527
- /** @var $item \Magento\Framework\ DataObject */
541
+ /** @var $item DataObject */
528
542
foreach ($ baseCollection as $ item ) {
529
543
if ($ item ->getIsEmpty ()) {
530
544
continue ;
531
545
}
532
546
if ($ item ->hasChildren () && count ($ item ->getChildren ()) > 0 ) {
533
- /** @var $subItem \Magento\Framework\ DataObject */
547
+ /** @var $subItem DataObject */
534
548
foreach ($ item ->getChildren () as $ subItem ) {
535
549
$ tmpItem = clone $ item ;
536
550
$ tmpItem ->unsChildren ();
@@ -548,11 +562,11 @@ protected function _getRowCollection(\Magento\Framework\Data\Collection $baseCol
548
562
/**
549
563
* Return prepared collection as row collection with additional conditions
550
564
*
551
- * @return \Magento\Framework\Data\ Collection
565
+ * @return Collection
552
566
*/
553
567
public function _getPreparedCollection ()
554
568
{
555
- /** @var $collection \Magento\Framework\Data\ Collection */
569
+ /** @var $collection Collection */
556
570
$ collection = $ this ->getParentBlock ()->getPreparedCollection ();
557
571
$ collection ->setPageSize (0 );
558
572
$ collection ->load ();
0 commit comments