I'm trying to do an export as per examples in the forums. The report works and the cakephp view file is : report.php
In the Controller I have a class:
class emReportsController extends AppController
{
public function initialize(): void
{
parent::initialize();
}
public function export()
{
require_once "report.php";
$report = new report; __<---- it says this is Undefined__
$report->run();
$secretToken = 'xxxxxxxxx My token is here xxxxxxxxxxxx';
$type = isset($_GET['type']) ? $_GET['type'] : 'PDF';
$settings = [
"pageWaiting" => "networkidle2", //load, domcontentloaded, networkidle0, networkidle2
];
$pdfOptions = [
"format" => "A4",
'landscape' => false,
"noRepeatTableFooter" => true,
];
$report->cloudExport("report")
->chromeHeadlessio($secretToken)
->settings($settings)
->pdf($pdfOptions)
->toBrowser("MyReport.pdf");
}
public function report()
{
$year = date("Y");
$year_range = range('1990', $year);
$year_range = array_reverse(array_combine($year_range, $year_range), true);
$report_year = $year - 1;
if ($this->request->is('post')) {
$posted_data = $this->request->getData();
if ($posted_data['year_input']) {
$report_year = $posted_data['year_input'];
}
}
$this->loadComponent('Utilities');
//Look up Lists
$plant_list = $this->Utilities->look_up('EmLuPlants');
$species_list = $this->Utilities->look_up('EmLuProcessedSpecies');
$gender_list = $this->Utilities->look_up('EmLuGenders');
$education_list = $this->Utilities->look_up('EmLuEducationLevels');
$status_list = $this->Utilities->look_up('EmLuWorkStatus');
$km_traveled_list = $this->Utilities->look_up('EmLuKmTraveled');
//21 Total number of factory workers overall, by plant
$query = $this->fetchTable('EmPlants')->find();
$result = $query->select([
'total' => $query->func()->COUNT('DISTINCT em_profiles_id'),
'year' => 'year_worked',
'plant' => 'em_plants_id'
])->group(' year_worked,em_plants_id')->where(['year_worked =' => $report_year]);
$total_by_factory["dataSource"] = [];
foreach ($result as $key => $val) {
$total_by_factory["dataSource"][] = ["year" => $val['year_worked'], "plant" => $plant_list[$val['plant']], "total" => $val['total']];
}
//22 Total number of foreign workers overall, by plant
$query = $this->fetchTable('EmPlants')->find();
$result = $query->select([
'total' => $query->func()->COUNT('DISTINCT EmPlants.em_profiles_id'),
'statusid' => 'citizen_status_id',
'plant' => 'EmPlants.em_plants_id'
])->join([
"table" => "em_profiles",
"type" => "INNER",
"conditions" => "EmPlants.em_profiles_id= em_profiles.em_profiles_id"
])->where(['citizen_status_id =' => 4])->group('em_plants_id')->having(['total >=' => 1])->toArray();
$foreign_workers["dataSource"] = [];
foreach ($result as $key => $val) {
$foreign_workers["dataSource"][] = ["statusid" => $val['statusid'], "plant" => $plant_list[$val['plant']], "total" => $val['total']];
}
//23.1 Overall average age, by plant
$query = $this->fetchTable('EmVwAgeInPlant')->find();
$result = $query->select([
'age' => $query->func()->AVG('age'),
'plant' => 'plant_id'
])->group('plant_id')->toArray();
$avg_age_plant["dataSource"] = [];
foreach ($result as $key => $val) {
$avg_age_plant["dataSource"][] = ["plant" => $plant_list[$val['plant']], "age" => $val['age']];
}
//23.2 Overall average age by plant, by male
$query = $this->fetchTable('EmVwAgeInPlant')->find();
$result = $query->select([
'age' => $query->func()->AVG('age'),
'plant' => 'plant_id'
])->group('plant_id')->where(['gender_id =' => 1])->toArray();
$avg_age_plant_male["dataSource"] = [];
foreach ($result as $key => $val) {
$avg_age_plant_male["dataSource"][] = ["plant" => $plant_list[$val['plant']], "age" => $val['age']];
}
}
This is the report.php view --- This view works very well
<?php
$this->layout = "registryMain";
use koolreport\widgets\google\BarChart;
?>
<div class='shadow p-3 my-3'>
<h1 class="text-center"><?= __("Random Ratio Reports - All History") ?></h1>
<div class="d-flex justify-content-end">
<div class="col-1 border">
<?php
echo $this->Form->create(NULL, [
"class" => "form-horizontal needs-validation",
"id" => "report_year",
"novalidate" => true,
]);
echo $this->Form->select('year_input', $year_range, [
"id" => "year_input",
"class" => 'form-control multiselect-select2 nosearch',
]);
?>
</div>
<button type="submit" name="submit" class="btn btn-secondary">Submit</button>
</div>
<?php
echo $this->Form->end();
//21 Total number of factory workers overall, by plant
if (!empty($total_by_factory["dataSource"])) {
BarChart::create(array(
"title" => __("21 Total number of factory workers overall, by plant - year 2000"),
"dataSource" => $total_by_factory["dataSource"],
"columns" => array(
"plant",
"total" => array(
"label" => __("total"),
"type" => "number",
),
), "options" => array(
"isStacked" => true
)
));
} else {
echo "<br>", __("No Data Available for Total number of factory workers overall, by plant");
}
//22 Total number of foreign workers overall, by plant
if (!empty($foreign_workers["dataSource"])) {
BarChart::create(array(
"title" => __("22 Total number of foreign workers overall, by plant"),
"dataSource" => $foreign_workers["dataSource"],
"columns" => array(
"plant",
"total" => array(
"label" => __("total"),
"type" => "number",
),
), "options" => array(
"isStacked" => true
)
));
} else {
echo "<br>", __("No Data Available for Total number of foreign workers overall, by plant");
}