A standard KoolReport implementation consists of three main parts: a bootstrap file (index.php), a report definition class (SalesByCustomer.php), and a view file (SalesByCustomer.view.php).
1. The Bootstrap File (index.php)
This file initializes the report class, runs the data processing, and renders the view.
<?php
require_once "SalesByCustomer.php";
$salesByCustomer = new SalesByCustomer;
$salesByCustomer->run()->render();
2. The Report Class (SalesByCustomer.php)
Extend \koolreport\KoolReport to define your data sources and processing pipeline.
settings(): Define your dataSources. Each source requires a class (e.g., \koolreport\datasources\CSVDataSource) and configuration parameters (e.g., filePath).setup(): Define the data pipeline. Use $this->src('source_name') to select a source, then use ->pipe() to pass data through various processes (like Group, Sort, or Limit) before saving it to a dataStore using ->pipe($this->dataStore('store_name')).
3. The View File (SalesByCustomer.view.php)
Use widget classes to render the processed data. Widgets typically use ::create() and require a dataStore name that matches the one defined in the setup() method.
<?php
use \koolreport\widgets\koolphp\Table;
use \koolreport\widgets\google\BarChart;
?>
<?php
BarChart::create(array(
"dataStore" => $this->dataStore('sales_by_customer'),
"columns" => array(
"customerName" => array("label" => "Customer"),
"dollar_sales" => array("type" => "number", "label" => "Amount")
)
));
?>
<?php
require_once "../koolreport/autoload.php";
use \koolreport\processes\Group;
use \koolreport\processes\Sort;
use \koolreport\processes\Limit;
class SalesByCustomer extends \koolreport\KoolReport
{
protected function settings()
{
return array(
"dataSources"=>array(
"sales"=>array(
"class"=>'\koolreport\datasources\CSVDataSource',
"filePath"=>"orders.csv",
),
)
);
}
protected function setup()
{
$this->src('sales')
->pipe(new Group(array(
"by"=>"customerName",
"sum"=>"dollar_sales"
)))
->pipe(new Sort(array(
"dollar_sales"=>"desc"
)))
->pipe(new Limit(array(10)))
->pipe($this->dataStore('sales_by_customer'));
}
}