Skip to content

Commit f39a345

Browse files
authored
Merge pull request #3 from babacarfallb/dev
add fonctionalite to export excel
2 parents 4ef3dae + bd04bf4 commit f39a345

File tree

2 files changed

+105
-8
lines changed

2 files changed

+105
-8
lines changed

src/Mvc/Controller/Model.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,28 @@ protected function getListExpose()
151151
*/
152152
protected function getExportExpose()
153153
{
154-
return $this->getExportExpose();
154+
return $this->getExpose();
155+
}
156+
157+
/**
158+
* Get columns merge definition for export
159+
*
160+
* @return null|array
161+
*/
162+
public function getExportMergeColum ()
163+
{
164+
return null;
165+
}
166+
167+
/**
168+
* Get columns format field text definition for export
169+
*
170+
* @param array|null $params
171+
*
172+
* @return null|array
173+
*/
174+
public function getExportFormatFieldText (?array $params = null) {
175+
return null;
155176
}
156177

157178
/**

src/Mvc/Controller/Rest.php

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public function exportAction()
176176
/** @var Resultset $with */
177177
$find = $this->getFind();
178178
$with = $model::with($this->getWith() ? : [], $find ? : []);
179-
179+
180180
/**
181181
* Expose the list
182182
* @var int $key
@@ -186,10 +186,10 @@ public function exportAction()
186186
foreach ($with as $key => $item) {
187187
$list[$key] = $item->expose($this->getExportExpose());
188188
}
189-
189+
190190
$list = is_array($list) ? array_values(array_filter($list)) : $list;
191191
$this->flatternArrayForCsv($list);
192-
192+
193193
if ($contentType === 'json') {
194194
// $this->response->setJsonContent($list);
195195
$this->response->setContent(json_encode($list, JSON_PRETTY_PRINT, 2048));
@@ -272,22 +272,98 @@ public function exportAction()
272272
// Something went wrong
273273
throw new \Exception('Failed to export `' . $this->getModelClassName() . '` using content-type `' . $contentType . '`', 400);
274274
}
275-
275+
276276
/**
277277
* @param array|null $array
278278
*
279279
* @return array|null
280280
*/
281281
public function flatternArrayForCsv(?array &$list = null) {
282-
282+
283283
foreach ($list as $listKey => $listValue) {
284284
foreach ($listValue as $column => $value) {
285285
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+
}
287293
}
288294
}
289295
}
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+
291367
return $list;
292368
}
293369

0 commit comments

Comments
 (0)