33namespace Yajra \DataTables \Jobs ;
44
55use Carbon \Carbon ;
6+ use Illuminate \Auth \Events \Login ;
67use Illuminate \Bus \Batchable ;
78use Illuminate \Bus \Queueable ;
89use Illuminate \Contracts \Queue \ShouldBeUnique ;
1516use Illuminate \Support \Arr ;
1617use Illuminate \Support \Collection ;
1718use Illuminate \Support \Facades \Auth ;
19+ use Illuminate \Support \Facades \Event ;
1820use Illuminate \Support \Facades \Storage ;
1921use Illuminate \Support \Str ;
2022use OpenSpout \Common \Helper \CellTypeHelper ;
@@ -73,10 +75,12 @@ public function __construct(array $dataTable, array $request, $user, string $she
7375 * @throws \OpenSpout\Common\Exception\IOException
7476 * @throws \OpenSpout\Common\Exception\UnsupportedTypeException
7577 * @throws \OpenSpout\Writer\Exception\WriterNotOpenedException
78+ * @throws \OpenSpout\Writer\Exception\InvalidSheetNameException
7679 */
7780 public function handle ()
7881 {
7982 if ($ this ->user ) {
83+ Event::forget (Login::class);
8084 Auth::loginUsingId ($ this ->user );
8185 }
8286
@@ -90,23 +94,19 @@ public function handle()
9094 $ dataTable = app ()->call ([$ oTable , 'dataTable ' ], compact ('query ' ));
9195 $ dataTable ->skipPaging ();
9296
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 ' ));
9898
9999 $ type = Str::startsWith ($ exportType , Type::CSV ) ? Type::CSV : Type::XLSX ;
100100 $ filename = $ this ->batchId .'. ' .$ type ;
101101
102- $ path = Storage::disk ($ disk )->path ($ filename );
102+ $ path = Storage::disk ($ this -> getDisk () )->path ($ filename );
103103
104104 $ writer = WriterEntityFactory::createWriter ($ type );
105105 $ writer ->openToFile ($ path );
106106
107107 if ($ writer instanceof XLSXWriter) {
108108 $ sheet = $ writer ->getCurrentSheet ();
109- $ sheet ->setName (substr ($ this ->sheetName ,0 , 31 ));
109+ $ sheet ->setName (substr ($ this ->sheetName , 0 , 31 ));
110110 }
111111
112112 $ columns = $ this ->getExportableColumns ($ oTable );
@@ -116,39 +116,40 @@ public function handle()
116116 )
117117 );
118118
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 ));
123121 $ query = $ dataTable ->getFilteredQuery ()->lazy ($ chunkSize );
124122 } else {
125123 $ query = $ dataTable ->getFilteredQuery ()->cursor ();
126124 }
127125
128126 foreach ($ query as $ row ) {
129127 $ 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 ) {
131140 $ property = $ column ->data ;
132141
133142 /* Handles orthogonal data */
134143 if (is_array ($ property )) {
135144 $ property = $ property ['_ ' ] ?? $ column ->name ;
136145 }
137146
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 ] ?? '' ;
144148
145149 if (is_array ($ value )) {
146150 $ value = json_encode ($ value );
147151 }
148152
149- /** @var string $defaultDateFormat */
150- $ defaultDateFormat = config ('datatables-export.default_date_format ' , 'yyyy-mm-dd ' );
151-
152153 switch (true ) {
153154 case $ this ->wantsText ($ column ):
154155 $ cellValue = strval ($ value );
@@ -180,6 +181,14 @@ public function handle()
180181 $ writer ->close ();
181182 }
182183
184+ /**
185+ * @return string
186+ */
187+ protected function getDisk (): string
188+ {
189+ return strval (config ('datatables-export.disk ' , 'local ' ));
190+ }
191+
183192 /**
184193 * @param \Yajra\DataTables\Services\DataTable $dataTable
185194 * @return \Illuminate\Support\Collection<array-key, Column>
@@ -192,25 +201,24 @@ protected function getExportableColumns(DataTable $dataTable): Collection
192201 }
193202
194203 /**
195- * @param \Yajra\DataTables\Html\Column $column
196204 * @return bool
197205 */
198- protected function wantsText ( Column $ column ): bool
206+ protected function usesLazyMethod ( ): bool
199207 {
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 ' ;
205209 }
206210
207211 /**
208212 * @param \Yajra\DataTables\Html\Column $column
209213 * @return bool
210214 */
211- protected function wantsNumeric (Column $ column ): bool
215+ protected function wantsText (Column $ column ): bool
212216 {
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 ' , ['@ ' ]));
214222 }
215223
216224 /**
@@ -229,6 +237,15 @@ protected function wantsDateFormat(Column $column): bool
229237 return in_array ($ column ['exportFormat ' ], $ formats );
230238 }
231239
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+
232249 /**
233250 * @param int|bool|string|null $value
234251 * @return bool
0 commit comments