KoolReport's Forum

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

Pass variable to report in loop from dynamic dashboard #2858

Closed Andre Van Der Walt opened this topic on on Oct 31, 2022 - 6 comments

Andre Van Der Walt commented on Oct 31, 2022

Good day,

Can you please assist?

We are trying to create a dynamic dashboard based on query output numbers.

The headings are already dynamic as we can pass them through with the header property.

The idea is to pass the variable to the various queries which will return the values for each sub panel in each panel.

Some values are hardcoded or commented out to illustrate an output.

DynamicWidgetBoard.php:

<?php

namespace ...;

use ...\AutoMaker;
use ...\DynamicWidgetOne;
use ...\DynamicWidgetTwo;
use ...\DynamicWidgetThree;

use \koolreport\dashboard\Dashboard;
use \koolreport\dashboard\containers\Row;
use \koolreport\dashboard\containers\Panel;
use \koolreport\dashboard\widgets\Text;
use koolreport\dashboard\containers\Tabs;

class DynamicWidgetBoard extends Dashboard
{

    private function getFarmData($region)
    {
        // Get Farm Data for the region
        $query = AutoMaker::rawSQL("SELECT 
                                                  1 FarmId, 'FarmOne' FarmName    
                                               WHERE
                                                   $region = $region
                                              
                                               UNION
                                               
                                               SELECT 
                                                  2 FarmId, 'FarmTwo' FarmName     
                                               WHERE
                                                   $region = $region
                                               
                                               UNION
                                               
                                               SELECT 
                                                  3 FarmId, 'FarmThree' FarmName   
                                               WHERE
                                                   $region = $region                                              
                                            ")
            ->run();
        $farmOutput = $query->toJson();
        return ($farmOutput);
    }

    protected function widgets()
    {
        $region = 2; //User input
        $dynamicRows = [];
        $dynamicPanel = [];
        $farmData[] = ($this->getFarmData($region));
        $panelCount = 3;//Count of farms in the region;

        for ($i = 0; $i < $panelCount; $i++) {
            $fId = json_decode($farmData[0], true)[$i]["FarmId"];

            $farmData[] = ($this->getFarmData($fId, $region));
            $farmName = ((json_decode($farmData[$i], true)[$i]["FarmName"]));

            $dynamicPanel[] =
                Panel::create()
                    ->header("<b>$farmName</b>")
                    ->width(1 / 2)
                    ->sub([
                        Panel::create()
                            ->header("<span style='font-weight: 700;'></i>  Pigs</span>")
                            ->cssClass("panel-card")
                            ->sub([
                                DynamicWidgetOne::create()
                            ]),
                        Panel::create()
                            ->header("<span style='font-weight: 700;'></i>  Cows</span>")
                            ->cssClass("panel-card")
                            ->sub([
                                DynamicWidgetTwo::create(),
                            ]),
                        Panel::create()
                            ->header("<span style='font-weight: 700;'></i>  Sheep</span>")
                            ->cssClass("panel-card")
                            ->sub([
                                DynamicWidgetThree::create(),
                            ])
                        ]);


        }

        for ($i = 0; $i < 1; $i++) {

            $dynamicRows[] = Row::create(
                $dynamicPanel,
                Text::create()->text("<br>")->asHtml(true)
            );
        }
        return [
            Text::create()->text("<h3>Farm Info</h3>")->asHtml(true),
            Text::create()->text("<br>")->asHtml(true),
            Tabs::create()
                ->addTab("Region 1",[
                    Row::create([
                        Panel::create()
                            ->width(1 / 1)
                            ->sub([
                                Panel::create()
                                    ->sub([
                                        Row::create([
                                            $dynamicRows,
                                        ]),
                                    ]),
                            ]),
                    ]),
                ]),

        ];
    }

}

DynamicDashboardOne:

<?php

namespace ...;

use ...\AutoMaker;


class DynamicWidgetOne extends \koolreport\dashboard\widgets\Text
{


    protected function onRendering()
    {
        //Received from DynamicWidgetBoard
        $farmId = 1;
//        $query = AutoMaker::table("someTable")
//            ->count("DISTINCT value")->alias("Total")
//            ->whereRaw("id = $farmId")
//            ->run();

        //Mimic query output
        $query = 0;
        if ($farmId == 1) {
            $query = 88;
        }
        elseif ($farmId == 2) {
            $query = 25;
        }
        elseif ($farmId == 3) {
            $query = 29;
        }

        $value = $query;//->get(0,"Total");
        $value = number_format($value);
        $this->text($value);
        return true;
    }

}

DynamicDashboardTwo:

<?php

namespace ...;

use ...\AutoMaker;


class DynamicWidgetTwo extends \koolreport\dashboard\widgets\Text
{

    protected function onCreated()
    {
        $this
            ->cssStyle("font-size: 1.5rem; color: #36a9e1; font-weight: 500;");
    }

    protected function onRendering()
    {
        //Received from DynamicWidgetBoard
        $farmId = 1;
//        $query = AutoMaker::table("someTable")
//            ->count("DISTINCT value")->alias("Total")
//            ->whereRaw("id = $farmId")
//            ->run();

        //Mimic query output
        $query = 0;
        if ($farmId == 1) {
            $query = 26;
        }
        elseif ($farmId == 2) {
            $query = 19;
        }
        elseif ($farmId == 3) {
            $query = 22;
        }

        $value = $query;//->get(0,"Total");
        $value = number_format($value);
        $this->text($value);
        return true;
    }

}

DynamicDashboardThree:

<?php

namespace ...;

use ...\AutoMaker;


class DynamicWidgetThree extends \koolreport\dashboard\widgets\Text
{
    

    protected function onRendering()
    {
        //Received from DynamicWidgetBoard
        $farmId = 1;
//        $query = AutoMaker::table("someTable")
//            ->count("DISTINCT value")->alias("Total")
//            ->whereRaw("id = $farmId")
//            ->run();

        //Mimic query output
        $query = 0;
        if ($farmId == 1) {
            $query = 5;
        }
        elseif ($farmId == 2) {
            $query = 10;
        }
        elseif ($farmId == 3) {
            $query = 8;
        }

        $value = $query;//->get(0,"Total");
        $value = number_format($value);
        $this->text($value);
        return true;
    }

}
Sebastian Morales commented on Nov 2, 2022

I think you can try to pass dynamic parameters to dashboard or widget like this:

//MyDashboard.php
...
DynamicWidget::create()->params([
    'farmId' => 1
])
...

//DynamicWidget.php
...
$farmId = isset($this->params()['farmId']) ? $this->params()['farmId'] : -1;
...

If you meant another question let us know. Tks,

KoolReport commented on Nov 2, 2022

Or by short-hand:

$farmId = $this->params("farmId");
if($farmId===null) {
    $farmId = -1;
}
Andre Van Der Walt commented on Nov 3, 2022

Hi Sebastian

Thank you for the reply.

I'm getting the following error.

*Message: Call undefined params() method Line: 23 File: ...TMagicMethod.php

#0: ...DynamicWidgetBoard.php Line 78 : __call(["params",[{"farmId":".1."}]])*

KoolReport commented on Nov 3, 2022

Could you please try to upgrade dashboard to latest version. Or you can try to add this into your class:

class DynamicWidgetThree extends \koolreport\dashboard\widgets\Text
{
    use \koolreport\dashboard\TParams; //To enable the params() method.
    ...
}
Andre Van Der Walt commented on Nov 4, 2022

That worked. Thank you very much!

KoolReport commented on Nov 4, 2022

That's great :D

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
help needed
solved

Dashboard