I call my query and filter it inside dashboard dataSource function for a specific graph widget. But if I filter it and the result is 0 rows, Koolreport will explod on an "Undefined index" situation and the entire panel will not work.
I'm thinking in a solution where I add a custom process after the filter so I can check and if there are no rows, I can add a dummy row.
But I can't figure out how to get it working or if this solution will ever work.
Here is my code to add some context:
protected function dataSource()
{
$filter = [['val_acima_lr', '>', '0']];
$view = substr($this->name, -1);
switch ($view) {
case 'B':
case 'C':
$filter[] = ['ano', '=', substr($this->params['selectedRow'][0], 0, 4)];
break;
}
return $this->dashboard()->getDataG('queryAvb')
->run()
->process(\koolreport\processes\Filter::process(
$filter
)
->pipe(new \koolreport\processes\Custom(function($row){
if (count($row) < 1) {
$row = [[
'dt' => '2000-01-01 00:00:00',
'ano' => '2000',
'val_avb' => 0,
'val_acima_lr' => 1
]];
}
return $row;
}))
)
->process(\koolreport\processes\Group::process([
'by' => $view == 'A'? 'ano' : 'dt',
'sum' => ['val_avb', 'val_acima_lr']
]));
}
protected function fields()
{
return [
DateTime::create('dt')
->label(Lang::t('Date'))
->displayFormat(Lang::t(substr($this->name, -1) == 'A'? 'Y' : 'Y-m')),
Currency::create('val_acima_lr')
->label(Lang::t('Above'))
->symbol()
->decimals(2)
->BRL(),
Currency::create('val_avb')
->label(Lang::t('Endorsed'))
->symbol()
->decimals(2)
->BRL(),
];
}
This ->pipe with a Custom process is my attempt to create the dummy row, but it is not working.
Without this pipe in place, I will get the "Undefined index: dt" error. With the pipe, I will get an eternal loading animation.
What am I doing wrong?