Skip to content

Commit

Permalink
Merge pull request #725 from bobvandevijver/excel
Browse files Browse the repository at this point in the history
Excel export of the list view
  • Loading branch information
sescandell committed Feb 23, 2014
2 parents f517240 + f590fc7 commit 8f6ed27
Show file tree
Hide file tree
Showing 28 changed files with 297 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Builder/Admin/ExcelBuilder.php
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;
}
}
16 changes: 16 additions & 0 deletions Builder/Admin/ExcelBuilderAction.php
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';
}
}
14 changes: 14 additions & 0 deletions Builder/Doctrine/ExcelBuilderAction.php
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
{
}
14 changes: 14 additions & 0 deletions Builder/DoctrineODM/ExcelBuilderAction.php
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
{
}
14 changes: 14 additions & 0 deletions Builder/Propel/ExcelBuilderAction.php
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
{
}
24 changes: 24 additions & 0 deletions Generator/Action/Generic/ExcelAction.php
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');
}
}
1 change: 1 addition & 0 deletions Generator/BundleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BundleGenerator extends BaseBundleGenerator
'filters',
'row'
)),
'Excel' => array('views' => array()),
'Edit' => array('views' => array(
'index',
'form',
Expand Down
6 changes: 6 additions & 0 deletions Generator/DoctrineGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Admingenerator\GeneratorBundle\Builder\Doctrine\NestedListBuilderTemplate;
use Admingenerator\GeneratorBundle\Builder\Doctrine\FiltersBuilderType;

use Admingenerator\GeneratorBundle\Builder\Doctrine\ExcelBuilderAction;

use Admingenerator\GeneratorBundle\Builder\Doctrine\EditBuilderAction;
use Admingenerator\GeneratorBundle\Builder\Doctrine\EditBuilderTemplate;
use Admingenerator\GeneratorBundle\Builder\Doctrine\EditBuilderType;
Expand Down Expand Up @@ -98,6 +100,10 @@ public function build()
$generator->addBuilder(new ShowBuilderTemplate());
}

if (array_key_exists('excel', $builders)) {
$generator->addBuilder(new ExcelBuilderAction());
}

if (array_key_exists('actions', $builders)) {
$generator->addBuilder(new ActionsBuilderAction());
$generator->addBuilder(new ActionsBuilderTemplate());
Expand Down
6 changes: 6 additions & 0 deletions Generator/DoctrineODMGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Admingenerator\GeneratorBundle\Builder\DoctrineODM\ListBuilderTemplate;
use Admingenerator\GeneratorBundle\Builder\DoctrineODM\FiltersBuilderType;

use Admingenerator\GeneratorBundle\Builder\DoctrineODM\ExcelBuilderAction;

use Admingenerator\GeneratorBundle\Builder\DoctrineODM\EditBuilderAction;
use Admingenerator\GeneratorBundle\Builder\DoctrineODM\EditBuilderTemplate;
use Admingenerator\GeneratorBundle\Builder\DoctrineODM\EditBuilderType;
Expand Down Expand Up @@ -90,6 +92,10 @@ public function build()
$generator->addBuilder(new ShowBuilderTemplate());
}

if (array_key_exists('excel', $builders)) {
$generator->addBuilder(new ExcelBuilderAction());
}

if (array_key_exists('actions', $builders)) {
$generator->addBuilder(new ActionsBuilderAction());
$generator->addBuilder(new ActionsBuilderTemplate());
Expand Down
6 changes: 6 additions & 0 deletions Generator/PropelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Admingenerator\GeneratorBundle\Builder\Propel\NestedListBuilderTemplate;
use Admingenerator\GeneratorBundle\Builder\Propel\FiltersBuilderType;

use Admingenerator\GeneratorBundle\Builder\Propel\ExcelBuilderAction;

use Admingenerator\GeneratorBundle\Builder\Propel\EditBuilderAction;
use Admingenerator\GeneratorBundle\Builder\Propel\EditBuilderTemplate;
use Admingenerator\GeneratorBundle\Builder\Propel\EditBuilderType;
Expand Down Expand Up @@ -98,6 +100,10 @@ public function build()
$generator->addBuilder(new ShowBuilderTemplate());
}

if (array_key_exists('excel', $builders)) {
$generator->addBuilder(new ExcelBuilderAction());
}

if (array_key_exists('actions', $builders)) {
$generator->addBuilder(new ActionsBuilderAction());
$generator->addBuilder(new ActionsBuilderTemplate());
Expand Down
17 changes: 17 additions & 0 deletions Resources/doc/builders/excel-builder.md
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.
2 changes: 2 additions & 0 deletions Resources/doc/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ List of supported features for each model manager:
1. [Actions builder][builders-1]
2. [List builder][builders-2]
3. [Edit builder][builders-3]
4. [Excel builder][builders-4]

[builders-1]: https://github.com/symfony2admingenerator/AdmingeneratorGeneratorBundle/blob/master/Resources/doc/builders/actions-builder.md
[builders-2]: https://github.com/symfony2admingenerator/AdmingeneratorGeneratorBundle/blob/master/Resources/doc/builders/list-builder.md
[builders-3]: https://github.com/symfony2admingenerator/AdmingeneratorGeneratorBundle/blob/master/Resources/doc/builders/edit-builder.md
[builders-4]: https://github.com/symfony2admingenerator/AdmingeneratorGeneratorBundle/blob/master/Resources/doc/builders/excel-builder.md

[go up][go-up]

Expand Down
4 changes: 4 additions & 0 deletions Resources/skeleton/bundle/generator.yml.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ builders:
object_actions:
edit: ~
delete: ~
excel:
params: ~
filename: ~
filetype: ~
filters:
params:
display: ~
Expand Down
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 %}
}
7 changes: 7 additions & 0 deletions Resources/templates/Doctrine/ExcelBuilderAction.php.twig
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 %}
7 changes: 7 additions & 0 deletions Resources/templates/DoctrineODM/ExcelBuilderAction.php.twig
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 %}
7 changes: 7 additions & 0 deletions Resources/templates/Propel/ExcelBuilderAction.php.twig
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 %}
1 change: 1 addition & 0 deletions Resources/translations/Admingenerator.ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ profile:
action.generic:
list: 一覧
new: 新規作成
excel: Excel へのエクスポート
save: 保存
save-and-add: 保存して追加
save-and-list: 保存して戻る
Expand Down
1 change: 1 addition & 0 deletions Resources/translations/Admingenerator.nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ profile:
action.generic:
list: Lijst
new: Nieuw
excel: Exporteren naar Excel
save: Opslaan
save-and-add: Opslaan en naar toevoegen
save-and-list: Opslaan en naar lijst
Expand Down
1 change: 1 addition & 0 deletions Resources/translations/Admingenerator.pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ profile:
action.generic:
list: Lista
new: Nowy
excel: Eksport do Excela
save: Zapisz
save-and-add: Zapisz i Nowy
save-and-list: Zapisz i Lista
Expand Down
1 change: 1 addition & 0 deletions Resources/translations/Admingenerator.pt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ profile:
action.generic:
list: Listar
new: Novo
excel: Exportar a Excel
save: Salvar
save-and-add: Salvar e Novo
save-and-list: Salvar e Listar
Expand Down
Loading

0 comments on commit 8f6ed27

Please sign in to comment.