mesd/jasper-report-bundle

Jasper Reports 套件

安装次数: 3,914

依赖者: 1

建议者: 0

安全: 0

星级: 3

关注者: 15

分支: 4

类型:symfony-bundle

v1.0.12 2018-02-16 18:35 UTC

README

Jasper Report Bundle 作为 MESD Jasper Client 库的包装层,旨在简化与 Symfony 应用程序的集成。该套件还包含一些额外的功能,利用了一些 Symfon'y 的特性,并为添加 Jasper Report 支持提供额外工具。此外,该报告套件设计为与可选的报告查看器套件兼容,后者提供了处理常见报告套件的大部分视图和控制器。

### 功能

  • 从 Jasper 服务器获取报告列表的能力
  • 报告执行,报告输出保存到本地目录以供以后检索
  • 输入控制选项管理,可根据用户轻松锁定特定输入控制的选项
  • 将报告输入转换为 Symfony 表单
  • 保存和检索过去的报告

### 待办事项列表

  • 添加对 Jasper Server Pro 的支持,目前仅支持社区版
  • 添加更多工具以帮助管理报告存储

安装

通过 Composer 将包添加到项目中

$ composer require mesd/jasper-report-bundle "~1.0"

添加这些包后,执行 composer update 以安装您 vendor 目录中的包。与所有其他套件一样,报告套件需要在应用程序的应用程序内核中注册。

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Mesd\Jasper\ReportBundle\MesdJasperReportBundle()
    )
} 

### 配置报告历史记录问题控制台命令

app/console doctrine:schema:update --env = [whereever] --dump-sql

...进行审查,然后...

app/console doctrine:schema:update --env = [whereever] --force

将历史表添加到您的模式中。如果您有多个实体管理器,可能需要将映射添加到 doctrine orm 配置中。

重要:将 /report-store(或配置中 cache_dir 字段中指定的其他父目录)添加到 .gitignore,以避免将大量报告添加到存储库中。

### 连接到 Jasper Report 服务器

#config.yml
mesd_jasper_report:
    connection:
        username: %jasper_username%
        password: %jasper_password%
        host: %jasper_host%
        port: %jasper_port%

链接到套件的路由文件

MESDJasperReportBundle:
    resource: "@MesdJasperReportBundle/Resources/config/routing.yml"
    prefix: /reportbundle

报告套件使用一些内部路由来处理报告资产(图像、图表和图形)的处理以及导出存储在报告存储中的报告。由于 Symfony 的自动加载器不自动处理路由,因此您必须导入套件的 routing.yml 文件才能使其正常工作。为此,请将以下内容添加到应用程序目录中的 app/config/routing.yml 中。

### 设置配置选项

### 创建选项处理器

<?php
 
namespace My\Bundle\Somewhere;
 
use Mesd\Jasper\ReportBundle\Interfaces\AbstractOptionsHandler;
use Mesd\Jasper\ReportBundle\InputControl\Option;
 
class MyOptionsHandler extends AbstractOptionsHandler
{
    public function registerFunctions() {
        return array(
            'someInputControlId' => 'foo'
        );
    }
 
    public function foo() {
        return array(
            new Option('1', 'I want door number 1'),
            new Option('2', 'I really want door number 2'),
            new Option('3', 'Just give me what ever is behind door number 3')
        );
    }
 
}

一旦创建了这个类,就需要在服务配置文件中将它注册为一个服务,如下所示

parameters:
    my_options_handler: My\Bundle\Somewhere\MyOptionsHandler
 
services:
    my_options_handler:
        class: %my_options_handler%

###从报告服务器获取资源列表 要从报告服务器获取资源列表,请使用客户端服务方法getResourceList返回一个资源对象数组。如果启用了报告安全,这将仅返回当前用户有权查看的资源。资源列表

//Get a list of resources in the default directory
$resources = $this->container->get('mesd.jasper.report.client')->getResourceList();
 
//Get a list of resources from a particular directory
$resources = $this->container->get('mesd.jasper.report.client')->getResourceList('reports/super-secret-reports/');
 
//Get all the resources below a particular directory (note that this can be a slow process)
$resources = $this->container->get('mesd.jasper.report.client')->getResourceList('reports/', true);

###获取输入控件 要构建一个新的报告,我们首先需要获取报告的输入控件并将其显示出来。这可以通过捆绑实现,该捆绑将从报告的输入控件中构建一个Symfony表单,并将在输入选项源设置为“自定义”或“回退”模式时使用选项处理器。以下是一个示例

//Display the report form
public function formAction()
{
    $form = $this->container->get('mesd.jasper.report.client')
        ->buildReportInputForm(
            'report/sales/quarterly_report',
            'route_to_next_action',
            array('routeParameters' => array('key' => 'value')
        )
    );
    
    
 
    //Display and such
    ...
}

###运行报告 一旦填写了输入控件,需要将其返回给报告,然后我们可以运行报告。当通过报告构建器的runReport方法运行报告时,报告将存储到报告存储目录中(以所有请求的格式存储,默认格式为html、pdf和xls),并将记录条目插入到数据库中。每个报告实例都有一个请求ID,作为其唯一标识符。以下是一个示例

//Run the report and get the output
public function processAction(Request $request, $key) {
    //Note that key will be 'value' here (just in case you need something like this)
     
    //Rebuild the form
    $form = $this->container->get('mesd.jasper.report.client')->buildReportInputForm('report/sales/quarterly_report');
  
    //Handle the request
    $form->handleRequest($request);
  
    //Check if the form is valid
    if ($form->isValid()) {
        //Create the report builder
        $rb = $this->container->get('mesd.jasper.report.client')->createReportBuilder('report/sales/quarterly_report');
        $rb->setInputParametersArray($form->getData());
 
 
        //Run the report and get the request id (NOTE: this instance of the report will now be saved to the report store and a record will be saved to the db)
        $requestId = $rb->runReport();
 
    }
}

有了报告的请求ID,它可以在任何时候从报告存储中加载,无论是立即显示还是稍后参考。

###从报告存储获取报告 要从报告存储中获取报告,只需调用加载服务,并提供请求ID以及显示HTML报告时要检索的页面号

//Get page 1 of the report
 
//Call the loader service to setup the client library's report loader
$reportLoader = $this->container->get('mesd.jasper.report.loader')->getReportLoader();
$report = $reportLoader->getCachedReport($requestId, 'html', array('page' => 1));
 
//Display
$report->getOutput();

您可以通过将html更改为pdf或xls来以其他格式获取报告,尽管非html格式不接受页面选项。

###基于角色的报告安全 报告安全Yaml文件 通过创建报告安全yaml并在配置设置中设置选项,客户端服务将自动根据用户的角色限制用户可以查看的报告。这些角色可以设置在目录或报告实例上。

reports: #the reports directory
    _roles:     # By default only a user with report_user or admin can view the resource in the reports directory
        - ROLE_REPORT_USER
        - ROLE_ADMIN
    project_a: #reports/project_a
        report_cake:    #reports/project_a/report_cake
            _roles: #only admins can see this report
                - ROLE_ADMIN

Jasper Report Bundle包括一个命令(mesd_jasper_report:security::generate-yaml),可以帮助您生成基本的报告安全文件。运行以下命令以获取更多信息:app/console mesd_jasper_report:security::generate-yaml --help

###配置设置 报告捆绑具有多种配置设置。以下是一个可能的选项列表,它们的默认值及其功能。默认值用方括号[]表示

mesd_jasper_report:
    default_folder: [/reports] #The folder on the jasper server to use as a default
    connection:
        username: [please_change] #Username of the account on the jasper server to use
        password: [please_change] #Password for the above user
        host: [please_change] #The Hostname of the jasper server
        port: [8080] #The port number that the jasper server is listening to
    folder_cache: #Settings for the cached resource list
        use_cache: [true] #Whether to cache resource lists
        cache_dir: [../app/cache/jasper_resource_list/] #Directory to store the resource list cache
        cache_timeout: [30] #Amount of time before the resource list cache expires
    report_cache: #Settings for the report store
        use_cache: [true] Whether to save reports in the report store by default
        cache_dir: [../report-store/reports/] #Report store directory
    display:
        default_export_route: [MesdJasperReportBundle_export_cached_report] #The route that handle report assets by default
    report_loader:
        default_page: [1] #The page to load by default if none given
        default_attach_asset_url: [true] #Whether to handle report assets by default
        default_asset_route: [MesdJasperReportBundle_render_cached_asset] #The route that handle report assets by default
    report_history:
        entity_manager: [default] #The entity manager that will handle report execution records
    options_handler: *REQUIRED* #Service name of the application specific options handler
    default_input_options_source: [Fallback] #Where to get the input options from by default (Fallback, Jasper, or Custom)
    report_security:
        use_security: [true] #Whether to use the security service or not
        max_level_set_at_default: [true] #Prevent any folder higher than the default from being viewed
        security_file: [/config/report_security.yml] #The location of the report security yml file relative to app
        default_roles: #Default roles to attach to the security yml when using the generator
            [ -ROLE_USER
              -ROLE_ADMIN
              -ROLE_SUPERADMIN ]

###根据用户显示输入选项 最常见的目的是覆盖输入选项并拥有选项处理器服务,以提供一种限制选项给用户的方法,这样用户就只能运行他们有权查看数据的报告。以下是一个示例,限制用户只能查看他们部门的报告。

<?php
 
namespace My\Bundle\Somewhere;
 
use Mesd\Jasper\ReportBundle\Interfaces\AbstractOptionsHandler;
use Mesd\Jasper\ReportBundle\InputControl\Option;
 
class MyOptionsHandler extends AbstractOptionsHandler
{
    //The Symfony SecurityContext object
    private $securityContext;
 
    //Override the constructor to allow the injection of the security context
    public function __construct($securityContext) {
        $this->securityContext = $securityContext;
         
        //Call the parent constructor (this is important)
        parent::__construct();
    }
     
    //Register the functions
    protected function registerFunctions(){
        return array(
            'Deptmartment' => 'getOptionsForDept'
        );
    }
     
    //Get the options for the department selector
    public function getOptionsForDept() {
        $options = array();
        foreach($this->securityContext->getUser()->getDepartments() as $dept) {
            $options[] = new Option($dept->getId(), $dept->getName());
        }
        return $options;
    }
}

###使用删除损坏的报告历史记录命令 随着时间的推移,数据库中的报告历史记录可能会指向存储中不再保存的报告。app/console mesd_jasper_report:history:delete_broken_records命令将删除所有这些不再指向存储中报告的记录。添加--dry-run命令以查看要尝试删除的记录,然后运行不带该命令以删除它们。

API文档

生成的文档存储在捆绑的docs目录下。

许可

本项目采用MIT许可证。有关更多信息,请参阅LICENSE.md文件。