KoolReport's Forum

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

Dashboard: Drilldown CSRF token mismatch. #2350

Open Andre Van Der Walt opened this topic on on Sep 22, 2021 - 2 comments

Andre Van Der Walt commented on Sep 22, 2021

Hi,

Please assist, getting CSRF token mismatch error by clicking on the chart to drilldown. Here is the code :

class TestDrilldown extends DrillDown
{
    protected function levels()
    {
        return [
            Level::create()
                ->title("All Users")

                ->widget(
                    KWidget::create()
                        ->use(\koolreport\widgets\google\ColumnChart::class)
                        ->dataSource(function($params, $scope){

                            return AutoMaker::rawSQL
                            ("
                                SELECT
                                Agent
                                , SUM(Total) Total
                                FROM (
                                        ....
                                 ) A
                                GROUP BY Agent
                            ")->run();
                        })
                        ->columns([
                            "Agent"=>["type"=>"string"],
                            "Total"=>[
                                "type"=>"number"
                            ]
                        ])
                        ->settings([
                            "colorScheme"=>ColorList::random()
                        ])
                ),

            Level::create()
                ->title(function($params){
                    return "User ".$params["Agent"];
                })
                ->widget(
                    KWidget::create()
                        ->use(\koolreport\widgets\google\ColumnChart::class)
                        ->dataSource(function($params, $scope){

                            return AutoMaker::rawSQL
                            ("
                                SELECT
                                DateUpdated
                                , SUM(Total) Total
                                FROM (
                                        ....
                                 ) A
                                WHERE Total > 0 AND Agent = '".$params["Agent"]."'
                                GROUP BY DateUpdated
                            ")->run();
                        })
                        ->columns([
                            "DateUpdated"=>[
                                "type"=>"string",
                            ],
                            "Total"=>[
                                "type"=>"number",
                            ]
                        ])
                        ->settings([
                            "colorScheme"=>ColorList::random()
                        ])
                )
        ];
    }
}

Sebastian Morales commented on Sep 23, 2021

Pls override method beforeOnRendering() and add "_token" parameter to settings' global like this:

class TestDrilldown extends DrillDown
{
    ...
    protected function beforeOnRendering()
    {
        parent::beforeOnRendering();
        $settings = $this->_settings();
        $settings["global"]["_token"] = csrf_token();
        $this->settings($settings);
        return true;
    }

Let us know if this works for you or not. Tks,

Ankit Agarwal commented on May 25, 2023

facing same issue below is my code

<?php
    use \koolreport\drilldown\DrillDown;
    use \koolreport\processes\CopyColumn;
    use \koolreport\processes\DateTimeFormat;
    use \koolreport\widgets\google\ColumnChart;
    use Illuminate\Support\Facades\Session;
?>
<div class="report-content">
    <div class="text-center">
        <h1>Sale By Time</h1>
        <p class="lead">
            This example shows how to setup a drill down report to see sale report
            by time.
            <br/>
            Please click on the column of chart to go further down on details.
        </p>
    </div>

    <?php
    $l1query = "SELECT SalesMen.team, DATENAME(month, sales.docdate) AS Month, SUM(amount) AS Sale,salesmen.teamcolour as Colour
    FROM dbo.sales AS sales 
    INNER JOIN dbo.SalesMen AS SalesMen ON sales.salesman = SalesMen.id 
    left JOIN dbo.tbl_user_sexe ON dbo.tbl_user_sexe.s_exe = salesmen.id
    cross join universal.dbo.tbl_user 
    WHERE salesmen.enabled = 'yes' AND salesmen.team != 'NULL'
    GROUP BY salesmen.team, DATENAME(month, sales.docdate),salesmen.teamcolour";
    $l1result = DB::select(DB::raw($l1query));
    //dd($l1result);
    DrillDown::create(array(
        "name"=>"saleDrillDown",
        "title"=>"Sale Report",
        "levels"=>array(
            array(
                "title"=>"All Month",
                "content"=>function($params,$scope)
                {
                    ColumnChart::create(array(
                        "dataSource"=>DB::select(DB::raw("SELECT SalesMen.team, DATENAME(month, sales.docdate) AS Month, SUM(amount) AS Sale,salesmen.teamcolour as Colour
                        FROM dbo.sales AS sales 
                        INNER JOIN dbo.SalesMen AS SalesMen ON sales.salesman = SalesMen.id 
                        left JOIN dbo.tbl_user_sexe ON dbo.tbl_user_sexe.s_exe = salesmen.id
                        cross join universal.dbo.tbl_user 
                        WHERE salesmen.enabled = 'yes' AND salesmen.team != 'NULL'
                        GROUP BY salesmen.team, DATENAME(month, sales.docdate),salesmen.teamcolour")),
                        "columns"=>array(
                            "team"=>array(
                                "type"=>"string",
                                "label"=>"Team",
                            ),
                            
                            "Month"=>array(
                                "label"=>"Month",
                                "type"=>"number",
                            ),
                            "Sale"=>array(
                                "label"=>"Sale Amount",
                                "type"=>"number",
                            )
                        ),
                        "clientEvents"=>array(
                            "itemSelect"=>"function(params){
                                var token = '".Session::token()."';
                                saleDrillDown.next({Month:params.selectedRow[0]});
                            }",
                        )
                    ));
                }
            ),
            array(
                "title"=>function($params,$scope)
                {
                    return "Month ".$params["Month"];
                },
                "content"=>function($params,$scope)
                {
                    ColumnChart::create(array(
                        "dataSource"=>( DB::select(DB::raw("SELECT SalesMen.team, DATENAME(WEEK, sales.docdate) AS Week, SUM(amount) AS Sale,salesmen.teamcolour as Colour
                        FROM dbo.sales AS sales 
                        INNER JOIN dbo.SalesMen AS SalesMen ON sales.salesman = SalesMen.id 
                        left JOIN dbo.tbl_user_sexe ON dbo.tbl_user_sexe.s_exe = salesmen.id
                        cross join universal.dbo.tbl_user 
                        WHERE salesmen.enabled = 'yes' AND salesmen.team != 'NULL' AND DATENAME(month, sales.docdate)=:Month
                        GROUP BY salesmen.team, DATENAME(WEEK, sales.docdate),salesmen.teamcolour"))
                        ->params(array(
                                ":Month"=>$params["Month"]
                            ))
                        )
                        ,
                        "columns"=>array(
                            "team"=>array(
                                "type"=>"string",
                                "label"=>"Team",
                            ),
                            
                            "Week"=>array(
                                "label"=>"Week",
                                "type"=>"number",
                            ),
                            "Sale"=>array(
                                "label"=>"Sale Amount",
                                "type"=>"number",
                            )
                        ),
                        "clientEvents"=>array(
                            "itemSelect"=>"function(params){
                                var token = '".Session::token()."';
                                saleDrillDown.next({Week:params.selectedRow[0]});
                            }",
                        )
                    ));
                }        
            ),
            array(
                "title"=>function($params,$scope)
                {
                    return date('F', mktime(0, 0, 0, $params["Week"], 10));
                },
                "content"=>function($params,$scope)
                {
                    ColumnChart::create(array(
                        "dataSource"=>(DB::select(DB::raw("SELECT SalesMen.team, DATENAME(DAY, sales.docdate) AS Day, SUM(amount) AS Sale,salesmen.teamcolour as Colour
                        FROM dbo.sales AS sales 
                        INNER JOIN dbo.SalesMen AS SalesMen ON sales.salesman = SalesMen.id 
                        left JOIN dbo.tbl_user_sexe ON dbo.tbl_user_sexe.s_exe = salesmen.id
                        cross join universal.dbo.tbl_user 
                        WHERE salesmen.enabled = 'yes' AND salesmen.team != 'NULL' AND DATENAME(WEEK, sales.docdate)=:Week
                        GROUP BY salesmen.team, DATENAME(DAY, sales.docdate),salesmen.teamcolour"))
                            ->params(array(
                                ":year"=>$params["year"],
                                ":month"=>$params["month"],
                            ))
                        ),
                        "columns"=>array(
                            "team"=>array(
                                "type"=>"string",
                                "label"=>"Team",
                            ),
                            
                            "Day"=>array(
                                "label"=>"Day",
                                "type"=>"number",
                            ),
                            "Sale"=>array(
                                "label"=>"Sale Amount",
                                "type"=>"number",
                            )
                        ),
                    ));
                }                
            )
        ),
    ));
    ?> 
</div>

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

Dashboard