Events
Overview #
Dashboard's events provide you a simple way to listen to what happened within your application. Each objects like Application, Dashboard and Widget provide events to which you can register and provide reaction on those events.
There are two type of events: pre-event and post-event. If you register for pre-event, you will get notification before that event actually happens. Normally the pre-event name will ended with -ing
like onRendering
or onExporting
. On the other hand, if you register for post-event, you will get notification after that event happens. The name of pre-events are normally ended with -ed
, for example onRendered
or onExported
.
Usage #
Application #
Name | parameter | description |
---|---|---|
onCreated | After object is created | |
onInit | After properties of object are set and object is fully established | |
onRunning | Before application is running | |
onRun | After the application is run successfully | |
onRendering | Before the view of application is rendered | |
onRendered | ["view"=>$view] | After the view of application is rendered |
onEnding | Before application is responding to client | |
onEnd | After the application responded to client and finish its life-cycle | |
onError | $exception | When error happens |
onPropChanged | ["name"=>$name, "oldValue"=>$oldValue, "newValue"=>$newValue] | After a property is changed its value |
Example:
<?php
use \koolreport\dashboard\Application;
class App extends Application
{
protected function onCreated()
{
$this->title("Dashboard");
}
protected function onError($exception)
{
Log::error("Something happens".$exception->getMessage());
}
protected function onRendering()
{
if(/*something wrong*/) {
return false; // Not allowed to render
}
return true; // Allow to render
}
}
Page #
Name | parameter | description |
---|---|---|
onCreated | After object is created | |
onInit | After properties of object are set and object is fully established | |
onHandling | Before page is handling request | |
onHandled | After page handled request | |
onRendering | Before the view of page is rendered | |
onRendered | ["view"=>$view] | After the view of page is rendered |
<?php
use \koolreport\dashboard\pages\Main;
class MyPage extends Main
{
protected function onCreated()
{
$this->loginRequired(true);
}
protected function onRendering()
{
if(/*something wrong*/) {
return false; // Not allowed to render
}
return true; // Allow to render
}
}
Dashboard #
Name | parameter | description |
---|---|---|
onCreated | After object is created | |
onInit | After properties of object are set and object is fully established | |
onStateInitiating | Dashboard's state is prepared to initiate | |
onStateInitiated | Dashboard's state has been initiated | |
onParamsInitiating | Dashboard's params are prepared to initiate | |
onParamsInitiated | Dashboard's params have been initiated | |
onWidgetGathering | Dashboard's widgets is prepared to gather | |
onWidgetGathered | Dashboard's widgets has been gathered | |
onWidgetsInitiating | Dashboard's widgets are prepared to initate | |
onWidgetsInitiatied | Dashboard's widgets have been initated | |
onRendering | Before the view of dashboard is rendered | |
onRendered | ["view"=>$view] | After the view of dashboard is rendered |
onError | $exception | When error happens inside dashboard |
onPropChanged | ["name"=>$name, "oldValue"=>$oldValue, "newValue"=>$newValue] | After a property is changed its value |
onAppLinked | After widget is linked to app |
Example:
<?php
use \koolreport\dashboard\Dashboard;
class MyDashboard extends Dashboard
{
protected function onCreated()
{
//Anything you set here can be changed by outside settings
$this->title("My Dashboard");
}
protected function onInit()
{
//Anything you set here will be the last before dashboard is going to be rendered.
$this->title("My Dashboard");
}
protected function onError($exception)
{
//Error happens inside dashboard
Log::error("Something went wrong");
}
}
Widget #
Name | parameter | description |
---|---|---|
onCreated | After widget is created | |
onInit | After properties of widget are set and widget is fully established | |
onRendering | Before the view of widget is rendered | |
onRendered | ["view"=>$view] | After the view of Widget is rendered |
onError | $exception | When error happens inside Widget |
onExporting | ["type"=>type] | Before widget is exported |
onExported | ["type"=>type, "code"=>$code, "name"=>$name, "path"=>$path] | Before widget is exported |
onDataReady | ["data"=>$data,"fields"=>$fields] | After data is queried successfully and available ready to be rendered. This event is available inside widget that has data source. |
onPropChanged | ["name"=>$name, "oldValue"=>$oldValue, "newValue"=>$newValue] | After a property is changed its value |
onAppLinked | After widget is linked to app | |
onDashboardLinked | After widget is linked to dashboard |
Example:
<?php
use \koolreport\dashboard\widgets\Table;
class MyTable extends Table
{
protected function onCreated()
{
$this->pageSize(10);
}
protected function onDataReady($result)
{
if($result["data"]->count()==0) {
$this->hidden(true);
}
}
protected function onRendering()
{
if (/*something wrong*/) {
return false;
}
return true;
}
}
Register events #
In common, all entities which use trait TEvent will have ability to subscribe listeners to an event using registerEvent()
function
->registerEvent(_string_ $eventName, _function_ $anonymousFucntion);
Example:
class MyWidget extends Widget
{
protected function onCreated()
{
$this->registerEvent("Init",function(){
// Do something on Init
});
}
}
Since registerEvent()
has public scope so you can register event from outside:
class MyDashboard extends Dashboard
{
protected function content()
{
return [
MyWidget::create()
->registerEvent("Init",function (){
//Do something
})
];
}
}
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.