1. Using views to create file - krzysztofruszczynski/Krsc-Reports-PHPExcel-Framework GitHub Wiki

From v1.2.0, views and service for creating reports were implemented.

Service

Project is adapted to be used by Symfony and work with its service architecture. Therefore KrscReports\Service class is implemented. This class can be used to create any kind of report. Due to its configuration mechanisms, we are able to customize many features of report.

Example YAML configuration

KrscReports\Views\SingleTable:
    calls:
      - [setColumnTranslator, ['@KrscReports\ColumnTranslatorService']]
    shared: false

KrscReports\ColumnTranslatorService:
        arguments: ['@translator']

reporting.example:
    class: KrscReports\Service
    # arguments: array with columns, translator service instance (optional), translator domain
    arguments: ['%reporting.example.columns%', '@KrscReports\ColumnTranslatorService', reporting]
    calls:
       - [setFileName, ['%reporting.example.file_name%']]
       - [setReportView, ['@KrscReports\Views\SingleTable', '%reporting.example.options%']]

Explaining constructor for reporting.example

  • reporting.example.columns - array with column names, can be translate keys, example:
reporting.example.columns: [label.example.size, [label.example.firstElement, ', ', label.example.secondElement]]

Service can accept array elements, which are strings (translate key or text to be displayed) or arrays (all elements of array would be concatenated into column name).

  • KrscReports\ColumnTranslatorService - service for translating columns, which uses translator service from Symfony (optional, if not provided, translations are not made).

  • reporting - translator domain (optional, also when translator is provided).

Explaining calls for reporting.example

  • setFileName - method for setting file name; can be done in YAML or dynamically later in php (otherwise default name would be used)
  • setReportView - method for setting View (described in View paragraph); One view can be set for KrscReports\Service . Later can be accessed by calling getReportView() method of that class
    • reporting.example.options - with these parameter View can be configured, see more in subparagraph of view "Setting View Object properties"

Views

Every view have to extend KrscReports\Views\AbstractView class. Currently library implements:

  • KrscReports\Views\SingleTable - ordinary table with header
  • KrscReports\Views\SingleTableWithFilters - in addition to single table, it has table at the beginning with selected filters
  • KrscReports\Views\SingleTableWithGraphs - in addition to single table, it allows to create graph with source data from that table
  • KrscReports\Views\CompositeView - it can store theoretically unlimited number of other views. Each view can be configured separately before adding it to composite or later by using reference

You are able to write your own view in your code (also extending from KrscReports\Views\AbstractView class).

Setting View object properties

reporting.example.options:
    document_properties:
        LastModifiedBy: reporting.property.setLastModifiedBy
        Title: reporting.example.property.title
        Subject: reporting.example.property.subject
        Description: reporting.example.property.description
        Keywords: reporting.example.property.keywords
        Category: reporting.example.property.category
    filters: [label.example.filter1, label.example.filter2]
    translator_domain: messages
    column_sizes:
        0: 20
        1: 20
        2: 25
        3: 25
        4: 25
        5: 10
        6: 18
        7: 15

    
  • document_properties - properties of file (can be seen in Excel when properties are displayed)
  • filters - name of columns with filters (can be translated like normal columns)
  • translator_domain - translator domain for filters
  • column_sizes - sizes of columns; index start from 0 - value of each element is column width

Example of service usage inside Action of Symfony Controller


public function reportAction()
{
    $excelService = $this->get('reporting.example');
    $excelService->setData(array(
        0 => 
            array ( 0 => 'Column 1 Row 1', 1 => 'Column 2 Row 1', 2 => 'Column 3 Row 1', 3 => 'Column 4 Row 1', 4 => 'Column 5 Row 1', 5 => 'Column 6 Row 1', 6 => 'Column 7 Row 1', 7 => 'Column 8 Row 1')
        1 => 
            array ( 0 => 'Column 1 Row 2', 1 => 'Column 2 Row 2', 2 => 'Column 3 Row 2', 3 => 'Column 4 Row 2', 4 => 'Column 4 Row 2', 5 => 'Column 5 Row 2', 6 => 'Column 6 Row 2', 7 => 'Column 7 Row 2')
    ));
    $excelService->createReport();

    return new Response();
}