KoolReport's Forum

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

ColumnMeta calculate with function #1700

Closed Ron opened this topic on on Nov 5, 2020 - 8 comments

Ron commented on Nov 5, 2020

Hi,

This is the code I have in one of my reports. it represents a column of hours of a timetable for students. currently, the values of the columns are hard coded but I generated an array that holds the hours for this columns

->pipe(new \koolreport\processes\ColumnMeta(array(
    'hour_num'=>array('label'=>lang('tts.hour_num')),
    '1' => array("label" => lang('tts.day_1')),
    '2' => array("label" => lang('tts.day_2')),
    '3' => array("label" => lang('tts.day_3')),
    '4' => array("label" => lang('tts.day_4')),
    '5' => array("label" => lang('tts.day_5')),
    '6' => array("label" => lang('tts.day_6')),
)))

I want to do the same code but it will return the column values from an array stored in the $_SESSION for example:

->pipe(new \koolreport\processes\ColumnMeta(array(
     "{value}" => function() {
         return  '1' => array("label" => lang('tts.day_1'));
        etc...
    }       
)))

will appreciate your help

Sebastian Morales commented on Nov 6, 2020

Dear Ron, Map process would be pretty good for your case:

    ->pipe(new \koolreport\processes\Map(array(
        "{meta}" => function($meta) {
            $colMetas = $meta["columns"];
            foreach ($colMetas as $colKey => $colMeta) {
                if (is_numeric($colKey)) $colMetas[$colKey]['label'] = lang("tts.day_$colKey"); //for columns '1', '2', etc
                else $colMetas[$colKey]['label'] = lang("tts.$colKey"); //for column 'hour_num'
            }
            $meta["columns"] =$colMetas;
            return $meta;
        };
    )))
Ron commented on Nov 11, 2020

Thank you Sebastian.

Another quick question. each class can have a certain day-off meaning not learning in that day. Currently when the report is generated is does not show this column. for example lets say the a certain class has Tuesday as day off son now the report will show columns of Sunday, Monday, Wednesday, Thursday and Friday. it will skip from drawing an empty column of Tuesday. Can I force the report to do so even if I do not have data for Tuesday.

this is the report setup function

function setup()
    {
        if ( $this->params['class_id'] != 0 ) {
            $this->classList[] = array('id'=>$this->params['class_id'], 'class_name'=>'');
        } else {
            $this->classList = $this->params['class_list'];
        }

        $this->src("tts")
        ->query('CALL getClassSchedule(:year, :class_id)')
        ->params(array(
            ':year' => $this->params['year'],
            ':class_id' => $this->params['class_id']
        ))
        ->saveTo($node1);

        foreach ($this->classList as $class) {
            $node1->pipe( new Filter( array (
                array("class_id", "=", $class['id'])
            )))
            ->pipe(new \koolreport\processes\Map(array(
                "{value}" => function($row) {
                    $row['class_name'] =
                        $row['profession_name'].'<br>'.
                        ( $this->params['show_teacher'] != null ? $row['teacher_name'] != '' ? $row['teacher_name'] : lang('tts.no_class') : null ).'<br>'.
                        ( $this->params['show_group_name'] != null ? $row['name'] : null ).'<br>'.
                        ( $this->params['show_room'] != null ? $row['room_name'] : null );
                    if ( $this->params['class_id'] != 0 ) {
                        $this->classList[0]['name'] = $row['class_name'];
                    }
                    return $row;
                }
            )))
            ->pipe(new Cube(array(
                'row' => 'hour_num',
                'column' => 'day',
                'max' => 'class_name'
            )))
            ->pipe(new \koolreport\processes\Map(array(
                "{value}" => function($row) {
                    foreach ($row as $k => $v)
                        if ($v == null) $row[$k] = "";
                    return $row;
                }
            )))
            ->pipe(new \koolreport\processes\RemoveColumn(array(
                '{{all}}'
            )))
            ->pipe(new \koolreport\processes\Map(array(
                "{meta}" => function($meta) {
                    $colMetas = $meta["columns"];
                    foreach ($colMetas as $colKey => $colMeta) {
                        if (is_numeric($colKey)) $colMetas[$colKey]['label'] = lang("tts.day_$colKey"); //for columns '1', '2', etc
                        else $colMetas[$colKey]['label'] = lang("tts.$colKey"); //for column 'hour_num'
                    }
                    $meta["columns"] =$colMetas;
                    return $meta;
                }
            )))
            ->pipe($this->dataStore("DS_" . $class['id']));
        }
    }
Sebastian Morales commented on Nov 12, 2020

Ron, just before your Cube process, try to add empty rows for every weekday like this;

    ->pipe(new \koolreport\processes\Map(array(
        "{end}" => function($count, $mapState) {
            $emptyRows = [];
            $days = [1, 2, 3, 4, 5, 6]; //use day name if it's your case
            foreach ($days as $day) {
                $emptyRows[] = ['day' => $day];
            } 
            return $emptyRows;
        }
    )))
    //pipe Cube here
Ron commented on Nov 12, 2020

Thank you Sebastian, it works. but now the last row I get empty row with first cell showing {{others}} what is it? How can I remove it?

Sebastian Morales commented on Nov 12, 2020

Since your row field is "hour_num" and the empty rows have no null data for "hour_num" the Cube process assigns the null "hour_num" values to "{{others}}". Just change the empty rows a bit:

    $emptyRows[] = ['day' => $day, 'hour_num' => $someExistedValueOfHourNum];
Ron commented on Nov 12, 2020

Thank you Sebastian as always you make things happen :)

Can I change the width of a column in a table?

Sebastian Morales commented on Nov 13, 2020

Which table widget do you use, Ron?

Ron commented on Nov 14, 2020

Thanks Sebastian I already managed to fix it using the documentation.

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