-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
829 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
//Step 1: Load KoolReport | ||
require_once "../../../../koolreport/autoload.php"; | ||
|
||
//Step 2: Creating Report class | ||
class MyReport extends \koolreport\KoolReport | ||
{ | ||
use \koolreport\inputs\Bindable; | ||
use \koolreport\inputs\POSTBinding; | ||
|
||
protected function defaultParamValues() | ||
{ | ||
return array( | ||
"years"=>array(2003), | ||
"customerNames"=>array(), | ||
"productLines"=>array(), | ||
); | ||
} | ||
|
||
protected function bindParamsToInputs() | ||
{ | ||
return array( | ||
"years", | ||
"customerNames", | ||
"productLines" | ||
); | ||
} | ||
|
||
protected function settings() | ||
{ | ||
$config = include "../../../config.php"; | ||
return array( | ||
"dataSources"=>$config | ||
); | ||
} | ||
protected function setup() | ||
{ | ||
$query_params = array(); | ||
if($this->params["years"]!=array()) | ||
{ | ||
$query_params[":years"] = $this->params["years"]; | ||
} | ||
if($this->params["customerNames"]!=array()) | ||
{ | ||
$query_params[":customerNames"] = $this->params["customerNames"]; | ||
} | ||
if($this->params["productLines"]!=array()) | ||
{ | ||
$query_params[":productLines"] = $this->params["productLines"]; | ||
} | ||
|
||
$this->src('automaker')->query(" | ||
select | ||
customerName, | ||
productLine, | ||
YEAR(orderDate) as year, | ||
sum(quantityOrdered*priceEach) as amount | ||
from orders | ||
join customers | ||
on | ||
customers.customerNumber = orders.customerNumber | ||
join orderdetails | ||
on orders.orderNumber = orderdetails.orderNumber | ||
join products | ||
on products.productCode = orderdetails.productCode | ||
where 1=1 | ||
".(($this->params["years"]!=array())?"and YEAR(orderDate) in (:years)":"")." | ||
".(($this->params["customerNames"]!=array())?"and customerName in (:customerNames)":"")." | ||
".(($this->params["productLines"]!=array())?"and productLine in (:productLines)":"")." | ||
GROUP BY year, productLine, customerName | ||
")->params($query_params) | ||
->pipe($this->dataStore("orders")); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
reports/advanced/multiple_data_filters/MyReport.view.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
use \koolreport\widgets\koolphp\Table; | ||
use \koolreport\inputs\Select2; | ||
?> | ||
<div class="report-content"> | ||
<div class="text-center"> | ||
<h1>Multiple Data Filters</h1> | ||
<p class="lead"> | ||
The example demonstrate how to build dynamic reports with multiple data filters | ||
</p> | ||
</div> | ||
|
||
<form method="post"> | ||
<div class="row"> | ||
<div class="col-md-6"> | ||
<div class="form-group"> | ||
<b>Select Years</b> | ||
<?php | ||
Select2::create(array( | ||
"multiple"=>true, | ||
"name"=>"years", | ||
"dataSource"=>$this->src("automaker")->query(" | ||
select YEAR(orderDate) as year | ||
from orders | ||
group by year | ||
"), | ||
"attributes"=>array( | ||
"class"=>"form-control" | ||
) | ||
)); | ||
?> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<b>Select Product Lines</b> | ||
<?php | ||
Select2::create(array( | ||
"multiple"=>true, | ||
"name"=>"productLines", | ||
"dataSource"=>$this->src("automaker")->query(" | ||
select productLine | ||
from orders | ||
join orderdetails on orders.orderNumber = orderdetails.orderNumber | ||
join products on products.productCode = orderdetails.productCode | ||
".(($this->params["years"]!=array())?"":"where YEAR(orderDate) in (:years")." | ||
group by productLine | ||
")->params( | ||
$this->params["years"]!=array()? | ||
array(":years"=>$this->params["years"]): | ||
array() | ||
), | ||
"attributes"=>array( | ||
"class"=>"form-control" | ||
) | ||
)); | ||
?> | ||
</div> | ||
<div class="form-group"> | ||
<b>Select Customers</b> | ||
<?php | ||
Select2::create(array( | ||
"multiple"=>true, | ||
"name"=>"customerNames", | ||
"dataSource"=>$this->src("automaker")->query(" | ||
select customerName | ||
from orders | ||
join customers on customers.customerNumber = orders.customerNumber | ||
".(($this->params["years"]!=array())?"":"where YEAR(orderDate) in (:years")." | ||
group by customerName | ||
")->params( | ||
$this->params["years"]!=array()? | ||
array(":years"=>$this->params["years"]): | ||
array() | ||
), | ||
"attributes"=>array( | ||
"class"=>"form-control" | ||
) | ||
)); | ||
?> | ||
</div> | ||
<div class="form-group"> | ||
<button class="btn btn-primary">Submit</button> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
</form> | ||
<?php | ||
Table::create(array( | ||
"dataSource"=>$this->dataStore("orders"), | ||
"columns"=>array( | ||
"customerName", | ||
"productLine", | ||
"amount"=>array("prefix"=>"$"), | ||
"year"=>array("format"=>false) | ||
), | ||
"grouping"=>array( | ||
"year", | ||
"productLine" | ||
), | ||
"paging"=>array( | ||
"pageSize"=>25 | ||
), | ||
"cssClass"=>array( | ||
"table"=>"table-bordered" | ||
) | ||
)); | ||
?> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
//Step 1: Load KoolReport | ||
require_once "../../../../koolreport/autoload.php"; | ||
|
||
//Step 2: Creating Report class | ||
class MyReport extends \koolreport\KoolReport | ||
{ | ||
use \koolreport\inputs\Bindable; | ||
use \koolreport\inputs\POSTBinding; | ||
|
||
protected function defaultParamValues() | ||
{ | ||
return array( | ||
"years"=>array(2003), | ||
"customerNames"=>array(), | ||
"productLines"=>array(), | ||
); | ||
} | ||
|
||
protected function bindParamsToInputs() | ||
{ | ||
return array( | ||
"years", | ||
"customerNames", | ||
"productLines" | ||
); | ||
} | ||
|
||
protected function settings() | ||
{ | ||
return array( | ||
"dataSources"=>array( | ||
"automaker"=>array( | ||
"connectionString"=>"mysql:host=localhost;dbname=automaker", | ||
"username"=>"root", | ||
"password"=>"", | ||
"charset"=>"utf8" | ||
), | ||
) | ||
); | ||
} | ||
protected function setup() | ||
{ | ||
$query_params = array(); | ||
if($this->params["years"]!=array()) | ||
{ | ||
$query_params[":years"] = $this->params["years"]; | ||
} | ||
if($this->params["customerNames"]!=array()) | ||
{ | ||
$query_params[":customerNames"] = $this->params["customerNames"]; | ||
} | ||
if($this->params["productLines"]!=array()) | ||
{ | ||
$query_params[":productLines"] = $this->params["productLines"]; | ||
} | ||
|
||
$this->src('automaker')->query(" | ||
select | ||
customerName, | ||
productLine, | ||
YEAR(orderDate) as year, | ||
sum(quantityOrdered*priceEach) as amount | ||
from orders | ||
join customers | ||
on | ||
customers.customerNumber = orders.customerNumber | ||
join orderdetails | ||
on orders.orderNumber = orderdetails.orderNumber | ||
join products | ||
on products.productCode = orderdetails.productCode | ||
where 1=1 | ||
".(($this->params["years"]!=array())?"and YEAR(orderDate) in (:years)":"")." | ||
".(($this->params["customerNames"]!=array())?"and customerName in (:customerNames)":"")." | ||
".(($this->params["productLines"]!=array())?"and productLine in (:productLines)":"")." | ||
GROUP BY year, productLine, customerName | ||
")->params($query_params) | ||
->pipe($this->dataStore("orders")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
The example demonstrate how to build a dynamic reports with multiple data filters. In this example, the data selection can be filter by `year`, `productLine` and `customerName`. Those data filters can be multi-selected meaning that you can select more than 1 years, more than 1 product lines or more than 1 customer to view. | ||
|
||
The example use `Select2` from [inputs package](https://www.koolreport.com/packages/inputs) to construct the parameters selection. | ||
|
||
We use the [Row Group] feature of Table for better data visualization. This grouping features support unlimited levels of grouping. There is very limited data table in market supporting this features. This feature is totally free with KoolReport. | ||
|
||
__Code Explanation__: | ||
|
||
The report use `\koolreport\inputs\Bindable` and `\koolreport\inputs\POSTBinding` services which will allows data binding between the report parameters and inputs controls. So the selection from select2 controls like years,productLines and customerNames will be bound to the corresponding report params. | ||
|
||
We use the `defaultParamValues()` methods in the report to define default starting selection of users. As you can see from the code, we pre-select the year `2003`. You may add different year, or set preselected for customerNames or productLines | ||
|
||
We use the `bindParamsToInputs()` to bind the name of report parameters to the name of the input controls. To keep things simple, the report parameters and name of input controls are the same: | ||
|
||
``` | ||
protected function bindParamsToInputs() | ||
{ | ||
return array( | ||
"years", | ||
"customerNames", | ||
"productLines" | ||
); | ||
} | ||
``` | ||
|
||
It is equivalent to | ||
|
||
``` | ||
protected function bindParamsToInputs() | ||
{ | ||
return array( | ||
"years"=>"years", | ||
"customerNames"=>"customerNames", | ||
"productLines"=>"productLines" | ||
); | ||
} | ||
``` | ||
|
||
In `setup()` function, base on the selection of users we add custom condition to the SQL query. Below code means that do not add the year condition if user does not select year. | ||
|
||
``` | ||
".(($this->params["years"]!=array())?"and YEAR(orderDate) in (:years)":"")." | ||
``` | ||
|
||
And also, we have this line of code: | ||
|
||
``` | ||
$query_params = array(); | ||
if($this->params["years"]!=array()) | ||
{ | ||
$query_params[":years"] = $this->params["years"]; | ||
} | ||
``` | ||
|
||
meaning that if user select year, then we add the year to variable `$query_params` to be used as parameters for sql query. | ||
|
||
In the view, we have some advance code for `Select2` widget: | ||
|
||
``` | ||
<?php | ||
Select2::create(array( | ||
"multiple"=>true, | ||
"name"=>"years", | ||
"dataSource"=>$this->src("automaker")->query(" | ||
select YEAR(orderDate) as year | ||
from orders | ||
group by year | ||
"), | ||
"attributes"=>array( | ||
"class"=>"form-control" | ||
) | ||
)); | ||
?> | ||
``` | ||
|
||
As you may notice, in `"dataSource"` of Select2 we use directly SQL command to query the available year. | ||
|
||
So as user selects years, productLines and customerNames, we will execute and store result to `"orders"` dataStore later be visualized in `Table`. | ||
|
||
__Enjoy the example!__ |
Oops, something went wrong.