KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Trying to get a basic widget working #192

Open Ben opened this topic on on Jan 12, 2018 - 5 comments

Ben commented on Jan 12, 2018

Hi, I'm attempting to create a basic widget from the documentation, my setup is as follows -

In the koolreports/widgets/mywidget folder

MyWidget.php

class MyWidget extends \koolreport\core\Widget
{
    protected $dataStore;
    public function onInit()
    {
        $this->dataStore = $this->params["dataStore"];
    }
    public function render()
    {
          $this->getAssetManager()->publish("my_widget_asset_folder");

        $this->template("MyWidget",array(
            "any_variable"=>$any_variable
        ));
    }
    static function create($params)
    {
        $widget = new MyWidget($params);
        $widget->render();
    }
}

MyWidget.tpl.php

echo $any_variable;

For the report itself

dataSource.php

require_once "../../../../koolreport/autoload.php";

use \koolreport\KoolReport;
use \koolreport\processes\Filter;
use \koolreport\processes\TimeBucket;
use \koolreport\processes\Group;
use \koolreport\processes\Limit;

class DataSource extends KoolReport
{
    public function settings()
    {
        return array(
            "dataSources"=>array(
                "celebrities_data"=>array(
                    "class"=>'\koolreport\datasources\ArrayDataSource',
                    "dataFormat"=>"associate",
                    "data"=>array(
                        array("customerName"=>"Johny Deep","dollar_sales"=>100),
                        array("customerName"=>"Angelina Jolie","dollar_sales"=>200),
                        array("customerName"=>"Brad Pitt","dollar_sales"=>200),
                        array("customerName"=>"Nocole Kidman","dollar_sales"=>100),
                    )
                ),
            )
        );
    }

    protected function setup()
    {
        $this->src('celebrities_data')
        ->pipe($this->dataStore('data'));
    }
}

dataSource.view.php

use \koolreport\widgets\mywidget\MyWidget;


MyWidget::create(array(
  "dataStore"=>$this->dataStore('data'),
      "any_variable"=>"value"
));

When I run the report I receive the following error -

"Warning: Declaration of MyWidget::create($params) should be compatible with koolreport\core\Widget::create($params, $return = false) in C:\xampp\htdocs\koolreport\koolreport\widgets\mywidget\MyWidget.php on line 3

Fatal error: Uncaught Error: Class 'koolreport\widgets\mywidget\MyWidget' not found in C:\xampp\htdocs\koolreport\examples\reports\basic\dataSource\dataSource.view.php:12 Stack trace: #0 C:\xampp\htdocs\koolreport\koolreport\KoolReport.php(303): include() #1 C:\xampp\htdocs\koolreport\examples\reports\basic\dataSource\index.php(18): koolreport\KoolReport->render() #2 {main} thrown in C:\xampp\htdocs\koolreport\examples\reports\basic\dataSource\dataSource.view.php on line 12"

My guess is that I am not calling the widget correctly, but I am not exactly sure. Just trying to get a basic widget passing data to work. Any help much appreciated.

KoolReport commented on Jan 12, 2018

Hi Ben,

You do this:

  1. Remove the static function create. Reason: In the new version of KoolReport, Widget has this function by default. That why you see the Warning.
  2. Add the namespace for your class on top of your file:
namespace koolreport\widgets\mywidget;

class MyWidget extends ...
Ben commented on Jan 12, 2018

Hi thanks for the quick reply. I just want to make sure I have this right. In dataSource.view.php, I'd have the following?

MyWidget(array(
  "dataStore"=>$this->dataStore('data'),
      "any_variable"=>"value"
));

and I'd put -> namespace koolreport\widgets\mywidget; in MyWidget.php correct?

KoolReport commented on Jan 12, 2018

That correct, you put the namespace there in MyWidget file

To use your widget, you can use create() function

\koolreport\widgets\mywidget\MyWidget::create(array(
    "dataStore"=>$this->dataStore("data"),
    "paramName"=>"paramValue"
));
Ben commented on Jan 12, 2018

Hey thanks a lot for your help, after some messing with it got it to display the value I was sending. The company I work for is really interested in buying the full Koolreport as it should fit our reporting needs, I just wanted to make sure I could pass it json and create custom form templates and html layouts/widgets, and it's looking good. Thanks much!

KoolReport commented on Jan 12, 2018

That's great that you make widget work. Please get the KoolReport Pro, you will not regret. We are adding more and more valuable packages to the Pro version and with Pro license you will get them for free.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None