-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #725 from bobvandevijver/excel
Excel export of the list view
- Loading branch information
Showing
28 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Admingenerator\GeneratorBundle\Builder\Admin; | ||
|
||
use Admingenerator\GeneratorBundle\Generator\Column; | ||
use Admingenerator\GeneratorBundle\Generator\Action; | ||
|
||
/** | ||
* This builder generates php for list actions | ||
* | ||
* @author cedric Lombardot | ||
* @author Piotr Gołębiewski <[email protected]> | ||
* @author Bob van de Vijver | ||
*/ | ||
class ExcelBuilder extends ListBuilder | ||
{ | ||
/** | ||
* (non-PHPdoc) | ||
* @see Admingenerator\GeneratorBundle\Builder.BaseBuilder::getYamlKey() | ||
*/ | ||
public function getYamlKey() | ||
{ | ||
return 'excel'; | ||
} | ||
|
||
public function getFileName(){ | ||
if(null === ($filename =$this->getVariable('filename'))){ | ||
$filename = 'admin_export_'. str_replace(' ', '_', strtolower($this->getGenerator()->getFromYaml('builders.list.params.title'))); | ||
} | ||
return $filename; | ||
} | ||
|
||
public function getFileType(){ | ||
if(null === ($filetype =$this->getVariable('filetype'))){ | ||
$filetype = 'Excel2007'; | ||
} | ||
return $filetype; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Admingenerator\GeneratorBundle\Builder\Admin; | ||
|
||
/** | ||
* This builder generates php for lists actions | ||
* @author cedric Lombardot | ||
* @author Bob van de Vijver | ||
*/ | ||
class ExcelBuilderAction extends ExcelBuilder | ||
{ | ||
public function getOutputName() | ||
{ | ||
return $this->getGenerator()->getGeneratedControllerFolder().'/ExcelController.php'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Admingenerator\GeneratorBundle\Builder\Doctrine; | ||
|
||
use Admingenerator\GeneratorBundle\Builder\Admin\ExcelBuilderAction as AdminExcelBuilderAction; | ||
|
||
/** | ||
* This builder generate php for Excel actions in doctrine | ||
* @author cedric Lombardot | ||
* @author Bob van de Vijver | ||
*/ | ||
class ExcelBuilderAction extends AdminExcelBuilderAction | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Admingenerator\GeneratorBundle\Builder\DoctrineODM; | ||
|
||
use Admingenerator\GeneratorBundle\Builder\Admin\ExcelBuilderAction as AdminExcelBuilderAction; | ||
|
||
/** | ||
* This builder generate php for Excel actions in doctrine | ||
* @author cedric Lombardot | ||
* @author Bob van de Vijver | ||
*/ | ||
class ExcelBuilderAction extends AdminExcelBuilderAction | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Admingenerator\GeneratorBundle\Builder\Propel; | ||
|
||
use Admingenerator\GeneratorBundle\Builder\Admin\ExcelBuilderAction as AdminExcelBuilderAction; | ||
|
||
/** | ||
* This builder generate php for Excel actions in Propel | ||
* @author cedric Lombardot | ||
* @author Bob van de Vijver | ||
*/ | ||
class ExcelBuilderAction extends AdminExcelBuilderAction | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Admingenerator\GeneratorBundle\Generator\Action\Generic; | ||
|
||
use Admingenerator\GeneratorBundle\Builder\Admin\BaseBuilder; | ||
use Admingenerator\GeneratorBundle\Generator\Action; | ||
|
||
/** | ||
* This class describes generic Excel action | ||
* @author cedric Lombardot | ||
* @author Piotr Gołębiewski <[email protected]> | ||
* @author Bob van de Vijver | ||
*/ | ||
class ExcelAction extends Action | ||
{ | ||
public function __construct($name, BaseBuilder $builder) | ||
{ | ||
parent::__construct($name, 'generic'); | ||
|
||
$this->setClass('btn-primary'); | ||
$this->setIcon('icon-white icon-print'); | ||
$this->setLabel('action.generic.excel'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Excel builder | ||
--------------------------------------- | ||
|
||
[go back to Table of contents][back-to-index] | ||
|
||
[back-to-index]: https://github.com/symfony2admingenerator/AdmingeneratorGeneratorBundle/blob/master/Resources/doc/documentation.md#4-generator | ||
|
||
### 1. Usage | ||
The Excel builder provides an Excel export of the list view. The export uses the data that is visible on the screen (and the next pages) and thus uses the filters and scopes selected. | ||
|
||
To use the Excel export make sure you have installes the recommended dependency `liuggio/excelbundle` and enabled it in your AppKernel.php (`new Liuggio\ExcelBundle\LiuggioExcelBundle()`). Without this bundle enabled the ExcelAction will not work. | ||
|
||
### 2. Options | ||
|
||
* `display`: The same as in list/new/show/edit, selects the columns to export | ||
* `filename`: Specify the export filename. When null 'admin_export_{list title}' is used | ||
* `filetype`: Default Excel2007. See the [excelbundle documention](https://github.com/liuggio/excelbundle#not-only-excel5) for the possible options. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
Resources/templates/CommonAdmin/ExcelAction/ExcelBuilderAction.php.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
namespace Admingenerated\{{ namespace_prefix }}{{ bundle_name }}\{{ builder.generator.GeneratedControllerFolder }}; | ||
{{- block('security_use') }} | ||
{{- block('csrf_protection_use') }} | ||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; | ||
/** | ||
* @author Bob van de Vijver | ||
*/ | ||
class ExcelController extends ListController | ||
{ | ||
/** | ||
* Generates the Excel object and send a streamed response | ||
* @return \Symfony\Component\HttpFoundation\StreamedResponse | ||
*/ | ||
public function excelAction() | ||
{ | ||
// Create the PHPExcel object with some standard values | ||
try { | ||
$phpexcel = $this->get('phpexcel'); | ||
} catch (ServiceNotFoundException $e){ | ||
throw new \Exception('You will need to enable the PHPExcel bundle for this function to work.', null, $e); | ||
} | ||
$phpExcelObject = $phpexcel->createPHPExcelObject(); | ||
$this->createExcelObject($phpExcelObject); | ||
$sheet = $phpExcelObject->setActiveSheetIndex(0); | ||
// Create the first bold row in the Excel spreadsheet | ||
$this->createExcelHeader($sheet); | ||
// Print the data | ||
$this->createExcelData($sheet); | ||
// Create the Writer, Response and add header | ||
$writer = $phpexcel->createWriter($phpExcelObject, '{{ builder.filetype }}'); | ||
$response = $phpexcel->createStreamedResponse($writer); | ||
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8'); | ||
$response->headers->set('Content-Disposition', 'attachment;filename={{ builder.filename }}.xlsx'); | ||
return $response; | ||
} | ||
/** | ||
* Override this method to add your own creator, title and more to the Excel spreadsheet | ||
*/ | ||
protected function createExcelObject(\PHPExcel $phpExcelObject){ | ||
$phpExcelObject->getProperties()->setCreator("AdminGeneratorBundle") | ||
->setTitle('AdminGenerator Excel Export') | ||
->setSubject("AdminGenerator Excel Export") | ||
->setDescription("AdminGenerator Excel export"); | ||
} | ||
/** | ||
* Fill the Excel speadsheet with the headers | ||
*/ | ||
protected function createExcelheader(\PhpExcel_Worksheet $sheet){ | ||
{% set column = 0 %} | ||
{% for builder_column in builder.columns %} | ||
$sheet->setCellValueByColumnAndRow({{ column }}, 1, "{{ builder_column.label }}", true); | ||
$columnLetter = \PHPExcel_Cell::stringFromColumnIndex({{ column }}); | ||
$sheet->getStyle($columnLetter . '1')->getFont()->setBold(true); | ||
$sheet->getColumnDimension($columnLetter)->setAutoSize(true); | ||
{% set column = column + 1 %} | ||
{% endfor %} | ||
} | ||
/** | ||
* Fills the Excel spreadsheet with data | ||
*/ | ||
protected function createExcelData(\PhpExcel_Worksheet $sheet){ | ||
$row = 2; | ||
$results = $this->getResults(); | ||
foreach($results as $rowData){ | ||
{% set column = 0 %} | ||
{% for builder_column in builder.columns %} | ||
$sheet->setCellValueByColumnAndRow({{ column }}, $row, $rowData->get{{ builder_column.getter|capitalize }}()); | ||
{% set column = column + 1 %} | ||
{% endfor %} | ||
foreach($rowData as $columnData){ | ||
$sheet->setCellValueByColumnAndRow($column, $row, $columnData); | ||
$column++; | ||
} | ||
$row++; | ||
} | ||
} | ||
{% block getResults -%} | ||
protected function getResults() | ||
{ | ||
// ORM JOB | ||
} | ||
{% endblock %} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{% extends '../CommonAdmin/ExcelAction/ExcelBuilderAction.php.twig' %} | ||
|
||
{% block getResults %} | ||
protected function getResults(){ | ||
return $this->getQuery()->getResult(); | ||
} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{% extends '../CommonAdmin/ExcelAction/ExcelBuilderAction.php.twig' %} | ||
|
||
{% block getResults %} | ||
protected function getResults(){ | ||
return $this->getQuery()->getResult(); | ||
} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{% extends '../CommonAdmin/ExcelAction/ExcelBuilderAction.php.twig' %} | ||
|
||
{% block getResults %} | ||
protected function getResults(){ | ||
return $this->getQuery()->find(); | ||
} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.