@@ -176,7 +176,7 @@ public function exportAction()
176
176
/** @var Resultset $with */
177
177
$ find = $ this ->getFind ();
178
178
$ with = $ model ::with ($ this ->getWith () ? : [], $ find ? : []);
179
-
179
+
180
180
/**
181
181
* Expose the list
182
182
* @var int $key
@@ -186,10 +186,10 @@ public function exportAction()
186
186
foreach ($ with as $ key => $ item ) {
187
187
$ list [$ key ] = $ item ->expose ($ this ->getExportExpose ());
188
188
}
189
-
189
+
190
190
$ list = is_array ($ list ) ? array_values (array_filter ($ list )) : $ list ;
191
191
$ this ->flatternArrayForCsv ($ list );
192
-
192
+
193
193
if ($ contentType === 'json ' ) {
194
194
// $this->response->setJsonContent($list);
195
195
$ this ->response ->setContent (json_encode ($ list , JSON_PRETTY_PRINT , 2048 ));
@@ -272,22 +272,98 @@ public function exportAction()
272
272
// Something went wrong
273
273
throw new \Exception ('Failed to export ` ' . $ this ->getModelClassName () . '` using content-type ` ' . $ contentType . '` ' , 400 );
274
274
}
275
-
275
+
276
276
/**
277
277
* @param array|null $array
278
278
*
279
279
* @return array|null
280
280
*/
281
281
public function flatternArrayForCsv (?array &$ list = null ) {
282
-
282
+
283
283
foreach ($ list as $ listKey => $ listValue ) {
284
284
foreach ($ listValue as $ column => $ value ) {
285
285
if (is_array ($ value ) || is_object ($ value )) {
286
- $ list [$ listKey ][$ column ] = gettype ($ value );
286
+ $ list [$ listKey ][$ column ] = $ this ->arrayFlatten ($ value );
287
+ if (is_array ($ list [$ listKey ][$ column ])) {
288
+ foreach ($ list [$ listKey ][$ column ] as $ childKey => $ childValue ) {
289
+ $ list [$ listKey ][$ childKey ] = $ childValue ;
290
+ unset ($ list [$ listKey ][$ column ]);
291
+ }
292
+ }
287
293
}
288
294
}
289
295
}
290
-
296
+
297
+ $ this ->formatColumnText ($ list );
298
+ return $ this ->mergeColumns ($ list );
299
+ }
300
+
301
+ /**
302
+ * @param array|null $array
303
+ *
304
+ * @return array|null
305
+ */
306
+ function arrayFlatten (?array $ array ) {
307
+ $ return = array ();
308
+ foreach ($ array as $ key => $ value ) {
309
+ if (is_array ($ value )) {
310
+ $ return = array_merge ($ return , $ this ->arrayFlatten ($ value ));
311
+ }
312
+ else {
313
+ $ return [$ key ] = $ value ;
314
+ }
315
+ }
316
+ return $ return ;
317
+ }
318
+
319
+ /**
320
+ * @param array|null $list
321
+ *
322
+ * @return array|null
323
+ */
324
+ public function mergeColumns (?array &$ list ) {
325
+ $ columnToMergeList = $ this ->getExportMergeColum ();
326
+ if (!$ columnToMergeList || empty ($ columnToMergeList )) {
327
+ return $ list ;
328
+ }
329
+
330
+ $ columnList = [];
331
+ foreach ($ list as $ listKey => $ listValue ) {
332
+ foreach ($ columnToMergeList as $ columnToMerge ) {
333
+ foreach ($ columnToMerge ['columns ' ] as $ column ) {
334
+ if (isset ($ listValue [$ column ])) {
335
+ $ columnList [$ listKey ][] = $ listValue [$ column ];
336
+ unset($ list [$ listKey ][$ column ]);
337
+ }
338
+ }
339
+ $ list [$ listKey ][$ columnToMerge ['name ' ]] = implode (' ' , $ columnList [$ listKey ] ?? []);
340
+ }
341
+ }
342
+
343
+ return $ list ;
344
+ }
345
+
346
+ /**
347
+ * @param array|null $list
348
+ *
349
+ * @return array|null
350
+ */
351
+ public function formatColumnText (?array &$ list ) {
352
+ foreach ($ list as $ listKey => $ listValue ) {
353
+ $ formatArray = $ this ->getExportFormatFieldText ($ listValue );
354
+ if ($ formatArray ) {
355
+ foreach ($ formatArray as $ formatKey => $ formatValue ) {
356
+ if (isset ($ formatValue ['text ' ])) {
357
+ $ list [$ listKey ][$ formatKey ] = $ formatValue ['text ' ];
358
+ if ($ formatValue ['rename ' ]) {
359
+ $ list [$ listKey ][$ formatValue ['rename ' ]] = $ formatValue ['text ' ];
360
+ unset($ list [$ listKey ][$ formatKey ]);
361
+ }
362
+ }
363
+ }
364
+ }
365
+ }
366
+
291
367
return $ list ;
292
368
}
293
369
0 commit comments