Skip to content

Commit 59c6ff7

Browse files
committed
Add more examples
1 parent c41e3c9 commit 59c6ff7

27 files changed

+829
-18
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "koolphp/koolreport-examples",
3-
"version":"1.0.0",
3+
"version":"1.1.0",
44
"description": "Examples for KoolReport Framework.",
55
"keywords": ["KoolReport php example", "php reporting framework","php reporting tools","data processing","data visualization","charts and graphs"],
66
"homepage": "https://www.koolreport.com",

reports.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"Combo Chart":"/reports/google_charts/combo_chart/",
6767
"Pareto Chart":"/reports/google_charts/pareto_chart/",
6868
"Sankey":"/reports/google_charts/sankey/",
69+
"TreeMap":"/reports/google_charts/treemap/",
6970
"Color Scheme":"/reports/google_charts/color_scheme/"
7071
}
7172
},
@@ -107,6 +108,9 @@
107108
"Table Sorting":"/reports/datagrid/datatables_sorting/",
108109
"Table Column Reorder":"/reports/datagrid/datatables_colreorder/",
109110
"Server Processing":"/reports/datagrid/server_processing/"
111+
},
112+
"<i class='fa fa-file-excel-o'></i>Excel":{
113+
"Excel export template":"/reports/excel/excel_template/"
110114
},
111115
"<i class='fa fa-file-pdf-o'></i>Export":{
112116
"PDF Exporting":"/reports/export/sakila_rental/"
@@ -149,7 +153,8 @@
149153
},
150154
"ADVANCED EXAMPLES":{
151155
"Advanced":{
152-
"Input &amp; Export":"/reports/advanced/input_and_export/"
156+
"Input &amp; Export":"/reports/advanced/input_and_export/",
157+
"Multiple Data Filters":"/reports/advanced/multiple_data_filters/"
153158
},
154159
"SubReport":{
155160
"Ajax Loading":"/reports/others/subreport_demo/"
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
//Step 1: Load KoolReport
3+
require_once "../../../../koolreport/autoload.php";
4+
5+
//Step 2: Creating Report class
6+
class MyReport extends \koolreport\KoolReport
7+
{
8+
use \koolreport\inputs\Bindable;
9+
use \koolreport\inputs\POSTBinding;
10+
11+
protected function defaultParamValues()
12+
{
13+
return array(
14+
"years"=>array(2003),
15+
"customerNames"=>array(),
16+
"productLines"=>array(),
17+
);
18+
}
19+
20+
protected function bindParamsToInputs()
21+
{
22+
return array(
23+
"years",
24+
"customerNames",
25+
"productLines"
26+
);
27+
}
28+
29+
protected function settings()
30+
{
31+
$config = include "../../../config.php";
32+
return array(
33+
"dataSources"=>$config
34+
);
35+
}
36+
protected function setup()
37+
{
38+
$query_params = array();
39+
if($this->params["years"]!=array())
40+
{
41+
$query_params[":years"] = $this->params["years"];
42+
}
43+
if($this->params["customerNames"]!=array())
44+
{
45+
$query_params[":customerNames"] = $this->params["customerNames"];
46+
}
47+
if($this->params["productLines"]!=array())
48+
{
49+
$query_params[":productLines"] = $this->params["productLines"];
50+
}
51+
52+
$this->src('automaker')->query("
53+
select
54+
customerName,
55+
productLine,
56+
YEAR(orderDate) as year,
57+
sum(quantityOrdered*priceEach) as amount
58+
from orders
59+
join customers
60+
on
61+
customers.customerNumber = orders.customerNumber
62+
join orderdetails
63+
on orders.orderNumber = orderdetails.orderNumber
64+
join products
65+
on products.productCode = orderdetails.productCode
66+
where 1=1
67+
".(($this->params["years"]!=array())?"and YEAR(orderDate) in (:years)":"")."
68+
".(($this->params["customerNames"]!=array())?"and customerName in (:customerNames)":"")."
69+
".(($this->params["productLines"]!=array())?"and productLine in (:productLines)":"")."
70+
GROUP BY year, productLine, customerName
71+
")->params($query_params)
72+
->pipe($this->dataStore("orders"));
73+
}
74+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
use \koolreport\widgets\koolphp\Table;
3+
use \koolreport\inputs\Select2;
4+
?>
5+
<div class="report-content">
6+
<div class="text-center">
7+
<h1>Multiple Data Filters</h1>
8+
<p class="lead">
9+
The example demonstrate how to build dynamic reports with multiple data filters
10+
</p>
11+
</div>
12+
13+
<form method="post">
14+
<div class="row">
15+
<div class="col-md-6">
16+
<div class="form-group">
17+
<b>Select Years</b>
18+
<?php
19+
Select2::create(array(
20+
"multiple"=>true,
21+
"name"=>"years",
22+
"dataSource"=>$this->src("automaker")->query("
23+
select YEAR(orderDate) as year
24+
from orders
25+
group by year
26+
"),
27+
"attributes"=>array(
28+
"class"=>"form-control"
29+
)
30+
));
31+
?>
32+
</div>
33+
34+
<div class="form-group">
35+
<b>Select Product Lines</b>
36+
<?php
37+
Select2::create(array(
38+
"multiple"=>true,
39+
"name"=>"productLines",
40+
"dataSource"=>$this->src("automaker")->query("
41+
select productLine
42+
from orders
43+
join orderdetails on orders.orderNumber = orderdetails.orderNumber
44+
join products on products.productCode = orderdetails.productCode
45+
".(($this->params["years"]!=array())?"":"where YEAR(orderDate) in (:years")."
46+
group by productLine
47+
")->params(
48+
$this->params["years"]!=array()?
49+
array(":years"=>$this->params["years"]):
50+
array()
51+
),
52+
"attributes"=>array(
53+
"class"=>"form-control"
54+
)
55+
));
56+
?>
57+
</div>
58+
<div class="form-group">
59+
<b>Select Customers</b>
60+
<?php
61+
Select2::create(array(
62+
"multiple"=>true,
63+
"name"=>"customerNames",
64+
"dataSource"=>$this->src("automaker")->query("
65+
select customerName
66+
from orders
67+
join customers on customers.customerNumber = orders.customerNumber
68+
".(($this->params["years"]!=array())?"":"where YEAR(orderDate) in (:years")."
69+
group by customerName
70+
")->params(
71+
$this->params["years"]!=array()?
72+
array(":years"=>$this->params["years"]):
73+
array()
74+
),
75+
"attributes"=>array(
76+
"class"=>"form-control"
77+
)
78+
));
79+
?>
80+
</div>
81+
<div class="form-group">
82+
<button class="btn btn-primary">Submit</button>
83+
</div>
84+
</div>
85+
</div>
86+
87+
</form>
88+
<?php
89+
Table::create(array(
90+
"dataSource"=>$this->dataStore("orders"),
91+
"columns"=>array(
92+
"customerName",
93+
"productLine",
94+
"amount"=>array("prefix"=>"$"),
95+
"year"=>array("format"=>false)
96+
),
97+
"grouping"=>array(
98+
"year",
99+
"productLine"
100+
),
101+
"paging"=>array(
102+
"pageSize"=>25
103+
),
104+
"cssClass"=>array(
105+
"table"=>"table-bordered"
106+
)
107+
));
108+
?>
109+
</div>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
//Step 1: Load KoolReport
3+
require_once "../../../../koolreport/autoload.php";
4+
5+
//Step 2: Creating Report class
6+
class MyReport extends \koolreport\KoolReport
7+
{
8+
use \koolreport\inputs\Bindable;
9+
use \koolreport\inputs\POSTBinding;
10+
11+
protected function defaultParamValues()
12+
{
13+
return array(
14+
"years"=>array(2003),
15+
"customerNames"=>array(),
16+
"productLines"=>array(),
17+
);
18+
}
19+
20+
protected function bindParamsToInputs()
21+
{
22+
return array(
23+
"years",
24+
"customerNames",
25+
"productLines"
26+
);
27+
}
28+
29+
protected function settings()
30+
{
31+
return array(
32+
"dataSources"=>array(
33+
"automaker"=>array(
34+
"connectionString"=>"mysql:host=localhost;dbname=automaker",
35+
"username"=>"root",
36+
"password"=>"",
37+
"charset"=>"utf8"
38+
),
39+
)
40+
);
41+
}
42+
protected function setup()
43+
{
44+
$query_params = array();
45+
if($this->params["years"]!=array())
46+
{
47+
$query_params[":years"] = $this->params["years"];
48+
}
49+
if($this->params["customerNames"]!=array())
50+
{
51+
$query_params[":customerNames"] = $this->params["customerNames"];
52+
}
53+
if($this->params["productLines"]!=array())
54+
{
55+
$query_params[":productLines"] = $this->params["productLines"];
56+
}
57+
58+
$this->src('automaker')->query("
59+
select
60+
customerName,
61+
productLine,
62+
YEAR(orderDate) as year,
63+
sum(quantityOrdered*priceEach) as amount
64+
from orders
65+
join customers
66+
on
67+
customers.customerNumber = orders.customerNumber
68+
join orderdetails
69+
on orders.orderNumber = orderdetails.orderNumber
70+
join products
71+
on products.productCode = orderdetails.productCode
72+
where 1=1
73+
".(($this->params["years"]!=array())?"and YEAR(orderDate) in (:years)":"")."
74+
".(($this->params["customerNames"]!=array())?"and customerName in (:customerNames)":"")."
75+
".(($this->params["productLines"]!=array())?"and productLine in (:productLines)":"")."
76+
GROUP BY year, productLine, customerName
77+
")->params($query_params)
78+
->pipe($this->dataStore("orders"));
79+
}
80+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
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.
2+
3+
The example use `Select2` from [inputs package](https://www.koolreport.com/packages/inputs) to construct the parameters selection.
4+
5+
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.
6+
7+
__Code Explanation__:
8+
9+
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.
10+
11+
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
12+
13+
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:
14+
15+
```
16+
protected function bindParamsToInputs()
17+
{
18+
return array(
19+
"years",
20+
"customerNames",
21+
"productLines"
22+
);
23+
}
24+
```
25+
26+
It is equivalent to
27+
28+
```
29+
protected function bindParamsToInputs()
30+
{
31+
return array(
32+
"years"=>"years",
33+
"customerNames"=>"customerNames",
34+
"productLines"=>"productLines"
35+
);
36+
}
37+
```
38+
39+
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.
40+
41+
```
42+
".(($this->params["years"]!=array())?"and YEAR(orderDate) in (:years)":"")."
43+
```
44+
45+
And also, we have this line of code:
46+
47+
```
48+
$query_params = array();
49+
if($this->params["years"]!=array())
50+
{
51+
$query_params[":years"] = $this->params["years"];
52+
}
53+
```
54+
55+
meaning that if user select year, then we add the year to variable `$query_params` to be used as parameters for sql query.
56+
57+
In the view, we have some advance code for `Select2` widget:
58+
59+
```
60+
<?php
61+
Select2::create(array(
62+
"multiple"=>true,
63+
"name"=>"years",
64+
"dataSource"=>$this->src("automaker")->query("
65+
select YEAR(orderDate) as year
66+
from orders
67+
group by year
68+
"),
69+
"attributes"=>array(
70+
"class"=>"form-control"
71+
)
72+
));
73+
?>
74+
```
75+
76+
As you may notice, in `"dataSource"` of Select2 we use directly SQL command to query the available year.
77+
78+
So as user selects years, productLines and customerNames, we will execute and store result to `"orders"` dataStore later be visualized in `Table`.
79+
80+
__Enjoy the example!__

0 commit comments

Comments
 (0)