Skip to content

Commit 55b4f94

Browse files
Merge pull request #264 from ksn135/dev-excel
Multi-report Excel reporting
2 parents fab86a7 + 7634d30 commit 55b4f94

21 files changed

+387
-24
lines changed

Builder/Admin/ExcelBuilder.php

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Admingenerator\GeneratorBundle\Builder\Admin;
44

5+
use Admingenerator\GeneratorBundle\Generator\Column;
6+
57
/**
68
* This builder generates php for list actions
79
*
@@ -11,6 +13,11 @@
1113
*/
1214
class ExcelBuilder extends ListBuilder
1315
{
16+
/**
17+
* @var array
18+
*/
19+
protected $export = null;
20+
1421
/**
1522
* (non-PHPdoc)
1623
* @see Admingenerator\GeneratorBundle\Builder.BaseBuilder::getYamlKey()
@@ -20,24 +27,78 @@ public function getYamlKey()
2027
return 'excel';
2128
}
2229

23-
public function getFileName(){
30+
public function getFileName($key = null){
2431
if(null === ($filename = $this->getVariable('filename'))){
25-
$filename = 'admin_export_'. str_replace(' ', '_', strtolower($this->getGenerator()->getFromYaml('builders.list.params.title')));
32+
$filename = 'admin_export_'. str_replace(' ', '_', strtolower($this->getGenerator()->getFromYaml('builders.list.params.title'))). '.xlsx';
2633
}
27-
return $filename;
34+
return $this->getExportParamsForKey($key, 'filename', $filename);
2835
}
2936

30-
public function getFileType(){
37+
public function getFileType($key = null){
3138
if(null === ($filetype = $this->getVariable('filetype'))){
3239
$filetype = 'Excel2007';
3340
}
34-
return $filetype;
41+
return $this->getExportParamsForKey($key, 'filetype', $filetype);
3542
}
3643

37-
public function getDateTimeFormat(){
44+
public function getDateTimeFormat($key = null){
3845
if(null === ($dateTimeFormat = $this->getVariable('datetime_format'))){
3946
$dateTimeFormat = 'Y-m-d H:i:s';
4047
}
41-
return $dateTimeFormat;
48+
return $this->getExportParamsForKey($key, 'datetime_format', $dateTimeFormat);
49+
}
50+
51+
/**
52+
* Return a list of columns from excel.export
53+
*
54+
* @return array
55+
*/
56+
public function getExport()
57+
{
58+
if (null === $this->export) {
59+
$this->export = array();
60+
$this->fillExport();
61+
}
62+
63+
return $this->export;
4264
}
43-
}
65+
66+
protected function fillExport()
67+
{
68+
$export = $this->getVariable('export',[]);
69+
if (!count($export)) return [];
70+
71+
foreach ($export as $keyName => $columns ) {
72+
$params = [];
73+
$this->export[$keyName] = [];
74+
if (isset($columns['display'])) {
75+
$params = isset($columns['fields']) ? $columns['fields'] : [];
76+
$columns = $columns['display'];
77+
}
78+
foreach ($columns as $columnName) {
79+
$column = $this->createColumn($columnName, false);
80+
$this->setUserColumnConfiguration($column);
81+
$this->setUserExcelColumnConfiguration($column, $params);
82+
$this->export[$keyName][$columnName] = $column;
83+
}
84+
}
85+
}
86+
87+
protected function setUserExcelColumnConfiguration(Column $column, array $optionsFields)
88+
{
89+
if (!count($optionsFields)) return;
90+
91+
$options = is_array($optionsFields) && array_key_exists($column->getName(), $optionsFields) ?
92+
$optionsFields[$column->getName()] : array();
93+
94+
foreach ($options as $option => $value) {
95+
$column->setProperty($option, $value);
96+
}
97+
}
98+
99+
public function getExportCredentials($key = null)
100+
{
101+
return $this->getExportParamsForKey($key, 'credentials', null);
102+
}
103+
104+
}

Builder/Admin/ListBuilder.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Admingenerator\GeneratorBundle\Generator\Column;
66
use Admingenerator\GeneratorBundle\Generator\Action;
7+
use Admingenerator\GeneratorBundle\Generator\Action\Generic\ExcelAction;
78

89
/**
910
* This builder generates php for list actions
@@ -14,6 +15,11 @@
1415
*/
1516
class ListBuilder extends BaseBuilder
1617
{
18+
/**
19+
* @var array
20+
*/
21+
protected $excelActions = null;
22+
1723
/**
1824
* @var array
1925
*/
@@ -223,4 +229,46 @@ protected function findBatchActions()
223229
$this->addBatchAction($action);
224230
}
225231
}
232+
233+
/**
234+
* Return a list of actions from excel.export
235+
*
236+
* @return array
237+
*/
238+
public function getExcelActions()
239+
{
240+
if (null === $this->excelActions) {
241+
$this->excelActions = array();
242+
$this->fillExportActions();
243+
}
244+
245+
return $this->excelActions;
246+
}
247+
248+
protected function fillExportActions()
249+
{
250+
$export = $this->getGenerator()->getFromYaml('builders.excel.params.export', []);
251+
if (!count($export)) return;
252+
253+
foreach ($export as $keyName => $params ) {
254+
if (!isset($params['show_button']) || (isset($params['show_button']) && filter_var($params['show_button'], FILTER_VALIDATE_BOOLEAN))) {
255+
$action = new ExcelAction($keyName, $this);
256+
$action->setCredentials($this->getExportParamsForKey($keyName, 'credentials', 'AdmingenAllowed'));
257+
$action->setClass($this->getExportParamsForKey($keyName, 'class', 'btn-info'));
258+
$action->setIcon($this->getExportParamsForKey($keyName, 'icon', 'fa-file-excel-o'));
259+
$action->setLabel($this->getExportParamsForKey($keyName, 'label', 'action.generic.excel'));
260+
$this->excelActions[$keyName] = $action;
261+
}
262+
}
263+
}
264+
265+
public function getExportParamsForKey($key, $name, $default)
266+
{
267+
if (!$key) return $default;
268+
269+
$export = $this->getGenerator()->getFromYaml('builders.excel.params.export', []);
270+
if (!count($export) || !isset($export[$key]) || !count($export[$key]) || !isset($export[$key][$name])) return $default;
271+
272+
return $export[$key][$name];
273+
}
226274
}

Resources/doc/admin/builder-excel.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,69 @@ builders:
3030
filetype: ~
3131
datetime_format: ~
3232
fields: ~
33+
export: ~
3334
```
3435

36+
#### Export
37+
38+
This key allows to export several excel files in different formats.
39+
40+
```yaml
41+
builders:
42+
excel:
43+
params:
44+
export:
45+
full:
46+
credentials: 'hasRole("ROLE_A")'
47+
show_button: false
48+
icon: fa-files-o
49+
label: Full report
50+
filename: full-report.xlsx
51+
filetype: Excel2007
52+
datetime_format: Y-m-d H:i:s
53+
display:
54+
- id
55+
- title
56+
- code
57+
- guid
58+
- note
59+
60+
short:
61+
credentials: 'hasRole("ROLE_B")'
62+
show_button: true
63+
icon: fa-files-o
64+
label: Show report
65+
filename: Short-repot.xls
66+
filetype: Excel5
67+
datetime_format: d.m.Y
68+
fields:
69+
title:
70+
label: Product name
71+
display:
72+
- id
73+
- code
74+
- title
75+
76+
```
77+
78+
You can customize everything includes columns, format, filename, title and even credentials.
79+
Also you can setup autogeneration of export buttons on list template via parameter `show_button`.
80+
It also auto-generates routes for each export key (if key is not found use defaults – `display`):
81+
82+
* /excel
83+
* /excel/full
84+
* /excel/short
85+
86+
#### Show button
87+
88+
`show_button` __default__: `true` __type__: `boolean`
89+
90+
```yaml
91+
show_button: true
92+
```
93+
94+
This will display button for each key (report) registered under `export` parameters key.
95+
3596
#### Display
3697

3798
`display` __default__: `~` __type__: `array`

0 commit comments

Comments
 (0)