The example demonstrates usage of Custom
process. The Custom process take a function as argument. In this custom function, you can do anything with the data row before return it to data stream.
Note: If you don't return row in custom function, the process will dismiss that row from data stream. You can use this feature as a filtering process if you want to allow some rows and remove others base on your conditions.
<?php
require_once "MyReport.php";
$report = new MyReport;
$report->run()->render();
<?php
//Step 1: Load KoolReport
require_once "../../../load.koolreport.php";
use \koolreport\processes\ColumnMeta;
use \koolreport\processes\Custom;
//Step 2: Creating Report class
class MyReport extends \koolreport\KoolReport
{
protected function settings()
{
return array(
"dataSources"=>array(
"data"=>array(
"class"=>'\koolreport\datasources\ArrayDataSource',
"dataFormat"=>"table",
"data"=>array(
array("name","income"),
array("John",50000),
array("Marry",60000),
array("Peter",100000),
array("Donald",80000),
)
)
)
);
}
protected function setup()
{
//Prepare data
$this->src("data")
->pipe(new ColumnMeta(array(
"income"=>array(
"type"=>"number",
"prefix"=>"$"
)
)))
->saveTo($source);
//Save orginal data
$source->pipe($this->dataStore("origin"));
//Pipe through process to get result
$source->pipe(new Custom(function($row){
$row["income"] = $row["income"] + 1000;
return $row;
}))
->pipe($this->dataStore("result"));
}
}
<?php
use \koolreport\widgets\koolphp\Table;
?>
<div class="report-content">
<div class="text-center">
<h1>Custom Process</h1>
<p class="lead">This example shows the usage of Custom process</p>
</div>
<?php
Table::create(array(
"dataSource"=>$this->dataStore("origin"),
"cssClass"=>array(
"table"=>"table-bordered table-striped table-hover"
)
));
?>
<i class="fa fa-arrow-down" style="font-size:24px;"></i>
<pre style="font-weight:bold"><code>
->pipe(new Custom(function($row){
$row["income"] = $row["income"] + 1000;
return $row;
}))
</code></pre>
<i class="fa fa-arrow-down" style="font-size:24px;"></i>
<div style="margin-top:20px;">
<?php
Table::create(array(
"dataSource"=>$this->dataStore("result"),
"cssClass"=>array(
"table"=>"table-bordered table-striped table-hover"
)
));
?>
</div>
</div>