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 #

Nameparameterdescription
onCreatedAfter object is created
onInitAfter properties of object are set and object is fully established
onRunningBefore application is running
onRunAfter the application is run successfully
onRenderingBefore the view of application is rendered
onRendered["view"=>$view]After the view of application is rendered
onEndingBefore application is responding to client
onEndAfter the application responded to client and finish its life-cycle
onError$exceptionWhen 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
    }
}

Dashboard #

Nameparameterdescription
onCreatedAfter object is created
onInitAfter properties of object are set and object is fully established
onRenderingBefore the view of dashboard is rendered
onRendered["view"=>$view]After the view of dashboard is rendered
onError$exceptionWhen error happens inside dashboard
onPropChanged["name"=>$name, "oldValue"=>$oldValue, "newValue"=>$newValue]After a property is changed its value
onAppLinkedAfter 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 #

Nameparameterdescription
onCreatedAfter widget is created
onInitAfter properties of widget are set and widget is fully established
onRenderingBefore the view of widget is rendered
onRendered["view"=>$view]After the view of Widget is rendered
onError$exceptionWhen 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
onAppLinkedAfter widget is linked to app
onDashboardLinkedAfter 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 widgets()
    {
        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.