Widget #

What is widget? #

Widget is the component of your report view. It takes the data from report's dataStore and represent them in meaningful way. Widget can be anything, text, tables or charts. All of them are derived from \koolreport\core\Widget class.

<?php
    use \koolreport\widgets\koolphp\Table;
?>
<h1>Sale Report</h1>
<?php 
    Table::create(array(
        "dataStore"=>$this->dataStore('sales'),
        "columns"=>array("product","sale_amount")
    ));
?>

Code explanation:

  1. In above code, we use the Table widget so we declare use \koolreport\widgets\koolphp\Table so that later on we only need to use Table to refer to its full name.
  2. We use Table::create() function to create Table widget. Other widgets are create with the same manner.
  3. The Table use "dataSource" pointing to "sales" data store where data is located. Many widgets can share one data store.
  4. In this Table, we display only two columns which are "product" and "sale_amount".

Widget DataSource #

"dataSource" property is a default property of almost all KoolReport widgets. The "dataSource" can receive DataStore object (as you may see above), DataSource, Process or even raw array.

From DataStore

<?php
//Use data store
Table::create(array(
    "dataSource"=>$this->dataStore("mydata"),
    ...
))
?>

Use DataSource

<?php
//Use DataSource
Table::create(array(
    "dataSource"=>$this->src("sakila")->query("select * from orders"),
    ...
))
?>

Use Process

<?php
//Use Process
Table::create(array(
    "dataSource"=>(
        $this->src("sakila")->query("select * from orders")
        ->pipe(new Filter(array(
            array("payment_date",">","2014-01-01")
        )))
    
    ),
    ...
))
?>

Use array

<?php
//Use associate array
Table::create(array(
    "dataSource"=>array(
        array("name"=>"Peter","age"=>35),
        array("name"=>"Karl","age"=>32),
    )
));
?>
<?php
//Use normal array
Table::create(array(
    "dataSource"=>array(
        array("name","age"),
        array("Peter",35),
        array("Karl",32),
    )
));
?>

Even from function: You may return any kinds of above from in your custom function

<?php
//Use function
Table::create(array(
    "dataSource"=>function(){
        return array(
            array("name","age"),
            array("Peter",35),
            array("Karl",32),
        );
    }
));
?>

Widget Rendering #

All of KoolReport widgets have a public static ::create(...) method which renders/outputs a widget's html and js/css together:

<?php
Table::create(array(
    ...
)); // output a widget content
?>

In case you want to get a widget's content instead of rendering it immediately, there are two ways:

<?php
$widgetContent = Table::create(array(
    ...
), true); // add a second parameter with `true` value
//or
$widgetContent = Table::html(array( // calling static ::html(...) method
    ...
)); // $widgetContent is a string
// ...
echo $widgetContent;
?>

There are some times you want a widget's content to be separated into html and js/css parts:

<?php
$widgetHtmlJs = Table::htmlJs(array( // calling static ::htmlJs(...) method
    ...
)); // $widgetHtmlJs is an array
$htmlPart = $widgetHtmlJs['html'];
$jsCssPart = $widgetHtmlJs['js'];
// ...
echo $htmlPart; // html part must always be echoed before js/css part
// ...
echo $jsCssPart; // jts/css part must always be echoed before html part
?>

Content Security Policy (CSP) compatible rendering #

Most KoolReport widgets require executing javascript functions after loading. In ajax request when the responsed functions are anonymous or not loaded before, CSP unsafe-eval must be required. Since version 6.6.0, most of all KoolReport widgets support a properted called jsonRender which allow them to be rendered using json data and preloaded javascript functions to avoid CSP unsafe-eval.

<?php
Table::create(array(
    ...
    "jsonRender" => true,
));
?>

Get started with KoolReport

KoolReport will help you to construct good php data report by gathering your data from multiple sources, transforming them into valuable insights, and finally visualizing them in stunning charts and graphs.