3
3
namespace Yajra \DataTables \Jobs ;
4
4
5
5
use Carbon \Carbon ;
6
+ use Illuminate \Auth \Events \Login ;
6
7
use Illuminate \Bus \Batchable ;
7
8
use Illuminate \Bus \Queueable ;
8
9
use Illuminate \Contracts \Queue \ShouldBeUnique ;
15
16
use Illuminate \Support \Arr ;
16
17
use Illuminate \Support \Collection ;
17
18
use Illuminate \Support \Facades \Auth ;
19
+ use Illuminate \Support \Facades \Event ;
18
20
use Illuminate \Support \Facades \Storage ;
19
21
use Illuminate \Support \Str ;
20
22
use OpenSpout \Common \Helper \CellTypeHelper ;
@@ -73,10 +75,12 @@ public function __construct(array $dataTable, array $request, $user, string $she
73
75
* @throws \OpenSpout\Common\Exception\IOException
74
76
* @throws \OpenSpout\Common\Exception\UnsupportedTypeException
75
77
* @throws \OpenSpout\Writer\Exception\WriterNotOpenedException
78
+ * @throws \OpenSpout\Writer\Exception\InvalidSheetNameException
76
79
*/
77
80
public function handle ()
78
81
{
79
82
if ($ this ->user ) {
83
+ Event::forget (Login::class);
80
84
Auth::loginUsingId ($ this ->user );
81
85
}
82
86
@@ -90,23 +94,19 @@ public function handle()
90
94
$ dataTable = app ()->call ([$ oTable , 'dataTable ' ], compact ('query ' ));
91
95
$ dataTable ->skipPaging ();
92
96
93
- /** @var string $exportType */
94
- $ exportType = request ('exportType ' );
95
-
96
- /** @var string $disk */
97
- $ disk = config ('datatables-export.disk ' , 'local ' );
97
+ $ exportType = strval (request ('exportType ' ));
98
98
99
99
$ type = Str::startsWith ($ exportType , Type::CSV ) ? Type::CSV : Type::XLSX ;
100
100
$ filename = $ this ->batchId .'. ' .$ type ;
101
101
102
- $ path = Storage::disk ($ disk )->path ($ filename );
102
+ $ path = Storage::disk ($ this -> getDisk () )->path ($ filename );
103
103
104
104
$ writer = WriterEntityFactory::createWriter ($ type );
105
105
$ writer ->openToFile ($ path );
106
106
107
107
if ($ writer instanceof XLSXWriter) {
108
108
$ sheet = $ writer ->getCurrentSheet ();
109
- $ sheet ->setName (substr ($ this ->sheetName ,0 , 31 ));
109
+ $ sheet ->setName (substr ($ this ->sheetName , 0 , 31 ));
110
110
}
111
111
112
112
$ columns = $ this ->getExportableColumns ($ oTable );
@@ -116,39 +116,40 @@ public function handle()
116
116
)
117
117
);
118
118
119
- if (config ('datatables-export.method ' , 'lazy ' ) === 'lazy ' ) {
120
- /** @var int $chunkSize */
121
- $ chunkSize = config ('datatables-export.chunk ' , 1000 );
122
-
119
+ if ($ this ->usesLazyMethod ()) {
120
+ $ chunkSize = intval (config ('datatables-export.chunk ' , 1000 ));
123
121
$ query = $ dataTable ->getFilteredQuery ()->lazy ($ chunkSize );
124
122
} else {
125
123
$ query = $ dataTable ->getFilteredQuery ()->cursor ();
126
124
}
127
125
128
126
foreach ($ query as $ row ) {
129
127
$ cells = [];
130
- $ columns ->map (function (Column $ column ) use ($ row , &$ cells ) {
128
+
129
+ if (! $ row instanceof Model) {
130
+ $ row = $ row instanceof Arrayable ? $ row ->toArray () : (array ) $ row ;
131
+ }
132
+
133
+ if ($ this ->usesLazyMethod () && is_array ($ row )) {
134
+ $ row = Arr::flatten ($ row );
135
+ }
136
+
137
+ $ defaultDateFormat = strval (config ('datatables-export.default_date_format ' , 'yyyy-mm-dd ' ));
138
+
139
+ $ columns ->map (function (Column $ column ) use ($ row , &$ cells , $ defaultDateFormat ) {
131
140
$ property = $ column ->data ;
132
141
133
142
/* Handles orthogonal data */
134
143
if (is_array ($ property )) {
135
144
$ property = $ property ['_ ' ] ?? $ column ->name ;
136
145
}
137
146
138
- if (! $ row instanceof Model) {
139
- $ row = $ row instanceof Arrayable ? $ row ->toArray () : (array ) $ row ;
140
- }
141
-
142
- /** @var array|bool|int|string|null $value */
143
- $ value = Arr::get ($ row , $ property , '' );
147
+ $ value = $ row [$ property ] ?? '' ;
144
148
145
149
if (is_array ($ value )) {
146
150
$ value = json_encode ($ value );
147
151
}
148
152
149
- /** @var string $defaultDateFormat */
150
- $ defaultDateFormat = config ('datatables-export.default_date_format ' , 'yyyy-mm-dd ' );
151
-
152
153
switch (true ) {
153
154
case $ this ->wantsText ($ column ):
154
155
$ cellValue = strval ($ value );
@@ -180,6 +181,14 @@ public function handle()
180
181
$ writer ->close ();
181
182
}
182
183
184
+ /**
185
+ * @return string
186
+ */
187
+ protected function getDisk (): string
188
+ {
189
+ return strval (config ('datatables-export.disk ' , 'local ' ));
190
+ }
191
+
183
192
/**
184
193
* @param \Yajra\DataTables\Services\DataTable $dataTable
185
194
* @return \Illuminate\Support\Collection<array-key, Column>
@@ -192,25 +201,24 @@ protected function getExportableColumns(DataTable $dataTable): Collection
192
201
}
193
202
194
203
/**
195
- * @param \Yajra\DataTables\Html\Column $column
196
204
* @return bool
197
205
*/
198
- protected function wantsText ( Column $ column ): bool
206
+ protected function usesLazyMethod ( ): bool
199
207
{
200
- if (! isset ($ column ['exportFormat ' ])) {
201
- return false ;
202
- }
203
-
204
- return in_array ($ column ['exportFormat ' ], (array ) config ('datatables-export.text_formats ' , ['@ ' ]));
208
+ return config ('datatables-export.method ' , 'lazy ' ) === 'lazy ' ;
205
209
}
206
210
207
211
/**
208
212
* @param \Yajra\DataTables\Html\Column $column
209
213
* @return bool
210
214
*/
211
- protected function wantsNumeric (Column $ column ): bool
215
+ protected function wantsText (Column $ column ): bool
212
216
{
213
- return Str::contains ($ column ->exportFormat , ['0 ' , '# ' ]);
217
+ if (! isset ($ column ['exportFormat ' ])) {
218
+ return false ;
219
+ }
220
+
221
+ return in_array ($ column ['exportFormat ' ], (array ) config ('datatables-export.text_formats ' , ['@ ' ]));
214
222
}
215
223
216
224
/**
@@ -229,6 +237,15 @@ protected function wantsDateFormat(Column $column): bool
229
237
return in_array ($ column ['exportFormat ' ], $ formats );
230
238
}
231
239
240
+ /**
241
+ * @param \Yajra\DataTables\Html\Column $column
242
+ * @return bool
243
+ */
244
+ protected function wantsNumeric (Column $ column ): bool
245
+ {
246
+ return Str::contains ($ column ->exportFormat , ['0 ' , '# ' ]);
247
+ }
248
+
232
249
/**
233
250
* @param int|bool|string|null $value
234
251
* @return bool
0 commit comments