KoolReport's Forum

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

Combination of Multiple Select and export to pdf #2236

Open Augustine Arthur opened this topic on on Jul 28, 2021 - 2 comments

Augustine Arthur commented on Jul 28, 2021

I have implemented multiple select. It is working fine. I have also included export to pdf. That also works perfectly. Now what I want to achieve is to print the selected parameters eg (Year and Region) in the Pdf title. Please I need assistance. Just for clarity I have included my report file, report view file and the reportpdf view file as well as the generated Pdf. Thank you.

MyReport file: <?php //Step 1: Load KoolReport require_once "../load.koolreport.php";

use \koolreport\clients\Bootstrap; use \koolreport\processes\ColumnMeta; use \koolreport\processes\Transpose; use \koolreport\processes\ColumnRename; use \koolreport\core\src\core\Utility; //Step 2: Creating Report class class MyReport extends \koolreport\KoolReport

{

use \koolreport\export\Exportable;
use \koolreport\inputs\Bindable;
use \koolreport\inputs\POSTBinding;

use \koolreport\clients\Bootstrap;


protected function defaultParamValues()
{
    return array(
        "Years"=>array(),
        "Region"=>array(),
    
    );
}
 protected function bindParamsToInputs()
{
    return array(
        "Years",
        "Region",
     
    );
}
protected function settings()
{
    return array(
        "dataSources"=>array(
            "data"=>array(
                "connectionString"=>"mysql:host=localhost;dbname=mlnrgh",
                "username"=>"root",
                "password"=>"",
                "charset"=>"utf8"
            ),
        )
    );
}


protected function setup()
{
	$query_params = array();
    if($this->params["Years"]!=array())
    {
        $query_params[":Years"] = $this->params["Years"];
    }
    if($this->params["Region"]!=array())
    {
        $query_params[":Region"] = $this->params["Region"];
$this->src('data')->query("
        select
            
            sum(Mining_leases) as Mining,
			sum(ProspLicense) as Prospecting,
			sum(Reconn_license) as Reconnaissance,
			sum(Quarry_licenses) as Quarry,
			sum(ClayKaolinLic) as 'Clay/Kaolin',
			sum(Salt_licenses) as Salt,
			sum(Mica_licenses) as Mica,
			sum(SandWinLic) as 'Sand Winning',
			sum(SmallScaDia) 'Small Scale Diamond',
			sum(SmallScaGold) 'Small Scale Gold',
			YEAR(DateAppRecvd) as 'Year'
			
          
        from mineral_rights_licenses
		where 1=1
        ".(($this->params["Years"]!=array())?"and YEAR(DateAppRecvd) in (:Years)":"")."
        ".(($this->params["Region"]!=array())?"and Region in (:Region)":"")."
         
        GROUP BY Year, Region
    ")->params($query_params)
		
   // ->saveTo($source);

    //Save orginal data
  // $source->pipe($this->dataStore("origin"));
    
    //Pipe through process to get result
 ->pipe(new Transpose())
/*  ->pipe(new ColumnRename(array(
"c0"=>"Mineral Rights",
"c1"=>"Number of Applications Received",

))) */

  ->pipe($this->dataStore("result")); 

}

}

}

MyReport.view.php

<?php

use \koolreport\widgets\koolphp\Table;
use \koolreport\inputs\Select2;
use \koolreport\widgets\google;
use \koolreport\widgets\google\PieChart;
use \koolreport\clients\bootstrap;
use \koolreport\widgets\google\ColumnChart;
use \koolreport\widgets\google\BarChart;

?> <div class="report-content">

<div class="text-center">
    <h1>Minerals Management</h1>
    <p class="lead">Mineral Rights / Licenses</p>
</div>
<form method="post">
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <b>Select Years</b>
                <?php 
                Select2::create(array(
                   // "multiple"=>true,
                    "name"=>"Years",
                    "dataSource"=>$this->src("data")->query("
                        select YEAR(DateAppRecvd) as Year
                        from mineral_rights_licenses
                        group by Year
                    "),
                    "attributes"=>array(
                        "class"=>"form-control"
						//"size"=>1
                    )
                ));
                ?>
            </div>    

                               
            <div class="form-group">
                <b>Select Region</b>
                <?php 
                Select2::create(array(
                   // "multiple"=>true,
                    "name"=>"Region",
                    "dataSource"=>$this->src("data")->query("
                        select Region
                        from mineral_rights_licenses
                        group by Region
                    ")->params(
                        $this->params["Years"]!=array()?
                        array(":Years"=>$this->params["Years"]):
                        array()
                    ),
                    "attributes"=>array(
                        "class"=>"form-control"
						//"size"=>1
                    )
                ));
                ?>                
            </div>  
            <div class="form-group">
                <button class="btn btn-primary">Submit</button>
				<button formaction="exportLicenses.php" class="btn btn-primary">Download PDF</button>
				
			
            </div>    
        </div>
    </div>
    
</form>

<?php
 Table::create(array(
 
    "dataSource"=>$this->dataStore("result"),
	
    "columns"=>array(
        "c0"=>array(
            "label"=>"Mineral Rights",
            /* "type"=>"datetime",
            "format"=>"Y-n",
            "displayFormat"=>"F, Y", */
        ),
        "c1"=>array(
            "label"=>"Number of Applications Received",
            "type"=>"number",
           
			),
			
			
    
    ),
	"cssClass"=>array(
        "table"=>"table-bordered table-striped table-hover",
)

) )

?>

<i class="fa fa-arrow-down" style="font-size:24px;"></i> <pre style="font-weight:bold"><code>

</code></pre> <i class="fa fa-arrow-down" style="font-size:24px;"></i>

<div style="margin-top:20px;">
    <div class="row">
        
		<div class="col-md-8">
        <?php 
     BarChart::create(array(
    "dataStore"=>$this->dataStore('result'),
	"options"=>array(
            'title' => 'Barchart Showing No. of Applications Received',
            'isStacked' => true
            ),		
    "columns"=>array(
        "c0"=>array(
            "label"=>"Mineral Rights",
            "type"=>"string",
           
        ),
        "c1"=>array(
            "label"=>"Number of Applications Received",
            "type"=>"number",
           
        )
    ),
    "width"=>"100%",
));
        ?>
    </div>
        <div class="col-md-8">
        <?php
        PieChart::create(array(
            "dataSource"=>$this->dataStore("result"),
			"options"=>array(
            'title' => 'Piechart Showing Revenue by Regions',
            ),
			 "columns"=>array(
            "c0"=>array(
                "label"=>"Mineral Rights"
            ),
			"c1"=>array(
                "type"=>"number",
               
                "emphasis"=>true
            ),
        ),
        
           
            "options"=>array(
                "legend"=>array(
                    "position"=>"right"
                ),
               
            ) 
        ));
        ?>
        </div>
    </div>


</div>

</div>

MyReportPdf.view.php

<?php

use \koolreport\widgets\koolphp\Table;
use \koolreport\inputs\Select2;
use \koolreport\widgets\google;
use \koolreport\widgets\google\PieChart;
use \koolreport\clients\bootstrap;
use \koolreport\widgets\google\ColumnChart;
use \koolreport\widgets\google\BarChart;

?> <html>

<body style="margin:0.5in 1in 0.5in 1in">
    <link rel="stylesheet" href="../../../assets/bs3/bootstrap.min.css" />
    <link rel="stylesheet" href="../../../assets/bs3/bootstrap-theme.min.css" />   
    <div class="page-header" style="text-align:right"><i></i></div>
    <div class="page-footer" style="text-align:right">{pageNum}</div>
    <div class="text-center">
    <div class="report-content">
<?php
$salesYear = isset($_POST['salesYear']) ? 
        $_POST['salesYear'] : array(2017, 2018, 2019, 2020, 2021, 2022);
    $salesYearStr = implode(", ", $salesYear);
	
    
?>	

Minerals Management

Mineral Rights / Licenses

$this->dataStore("result"), "columns"=>array( "c0"=>array( "label"=>"Mineral Rights", /* "type"=>"datetime", "format"=>"Y-n", "displayFormat"=>"F, Y", */ ), "c1"=>array( "label"=>"Number of Applications Received", "type"=>"number", ), ), "cssClass"=>array( "table"=>"table-bordered table-striped table-hover", ) ) ) ?>
$this->dataStore('result'), "options"=>array( 'title' => 'Barchart Showing No. of Applications Received', 'isStacked' => true ), "columns"=>array( "c0"=>array( "label"=>"Mineral Rights", "type"=>"string", ), "c1"=>array( "label"=>"Number of Applications Received", "type"=>"number", // "prefix"=>"$", ) ), "width"=>"100%", )); ?>
$this->dataStore("result"), "options"=>array( 'title' => 'Piechart Showing Revenue by Regions', ), "columns"=>array( "c0"=>array( "label"=>"Mineral Rights" ), "c1"=>array( "type"=>"number", "emphasis"=>true ), ), "options"=>array( "legend"=>array( "position"=>"right" ), ) )); ?>

</html>

Sebastian Morales commented on Jul 29, 2021

You could access the Years and Region in your exportLicenses.php file and use them like this:

//exportLicenses.php
$years = $_POST["Years"];
if (is_array($years)) $years = implode("-", $years);
$region = $_POST["Region"];
$pdfTitle = $region . $years . ".pdf";
...
$report->run()
->export(..)
-pdf(...)
->toBrowser($pdfTitle);

Let us know if this is what you want. Tks,

Augustine Arthur commented on Jul 29, 2021

Thank you. I have implemented you code and it has worked perfectly. What I did was to include it the exportLicenses.php and also included in the MyReportPdf.view.php file. I noticed that if I include it only in the export file it only takes the parameters as the pdf file name. It is when I include the file in the MyReportPdf.view.php and echo the variable $pdfTitle in a div tag that I get the selection appearing in the title. Please this are two amended files: MyReportPdf.view.php <?php

use \koolreport\widgets\koolphp\Table;
use \koolreport\inputs\Select2;
use \koolreport\widgets\google;
use \koolreport\widgets\google\PieChart;
use \koolreport\clients\bootstrap;
use \koolreport\widgets\google\ColumnChart;
use \koolreport\widgets\google\BarChart;

?> <html>

<body style="margin:0.5in 1in 0.5in 1in">
    <link rel="stylesheet" href="../../../assets/bs3/bootstrap.min.css" />
    <link rel="stylesheet" href="../../../assets/bs3/bootstrap-theme.min.css" />   
    <div class="page-header" style="text-align:right"><i></i></div>
    <div class="page-footer" style="text-align:right">{pageNum}</div>
    <div class="text-center">
    <div class="report-content">

Minerals Management

Mineral Rights / Licenses for

$this->dataStore("result"), "columns"=>array( "c0"=>array( "label"=>"Mineral Rights", ), "c1"=>array( "label"=>"Number of Applications Received", "type"=>"number", ), ), "cssClass"=>array( "table"=>"table-bordered table-striped table-hover", ) ) ) ?>
$this->dataStore('result'), "options"=>array( 'title' => 'Barchart Showing No. of Applications Received', 'isStacked' => true ), "columns"=>array( "c0"=>array( "label"=>"Mineral Rights", "type"=>"string", ), "c1"=>array( "label"=>"Number of Applications Received", "type"=>"number", // "prefix"=>"$", ) ), "width"=>"100%", )); ?>
$this->dataStore("result"), "options"=>array( 'title' => 'Piechart Showing Number of Applications Received', ), "columns"=>array( "c0"=>array( "label"=>"Mineral Rights" ), "c1"=>array( "type"=>"number", "emphasis"=>true ), ), "options"=>array( "legend"=>array( "position"=>"right" ), "width"=>'100%', ) )); ?>

</html>

exportLicenses.php

<?php require_once "MyReport.php"; $report = new MyReport; $years = $_POST["Years"]; if (is_array($years)) $years = implode(",", $years); $region = $_POST["Region"]; $pdfTitle = $region . $years . ".pdf";

$report->run() ->export('MyReportPdf') ->pdf(array(

"format"=>"A4",
"orientation"=>"portrait",
"zoom"=>2

)) ->toBrowser($pdfTitle);

This is a snapshot of the generated pdf.

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
solved

None