So I'm working on a new project in Codeigniter 4. Having never integrated KoolReport into it [or CI3] before, my first step was to search these forums, then KoolReport git, then the CodeIgniter forums. There was little information. Most information related to integrating with CI3 which is totally different as CI4 is not backword compatible. So after about 3 hours of frustrating work, I got it working. Hopefully this save someone some frustration.
I'll include snips from KoolReports integration steps from CI3 as well as some info found in these forums from [https://www.koolreport.com/forum/topics/1529](pargibay topic 1529). as well as a small bug [maybe] in the KoolReport Autoload.php.
Note : Composer was NOT used.
- I downloaded CI 4.04 and unzipped it so that I had a folder structure like :
app public system writeable
- I downloaded KoolReport 5.0.1 free and unzipped it. I navigated to koolreport/core/src and copied the src folder to CI4 app/Libraries folder then renamed src to koolreport. My app/Libraries now looks like
apps libraries koolreports clients core datastructures processes widgets KoolReport.php <-- file debug.view.php <-- file
- I downloaded the KoolReport codeigniter [1.8.0] plugin and unzipped it. I created a codeigniter folder in the koolreports folder above and placed all files in it. You really just need the two php files.
- This is where I used the info from [https://www.koolreport.com/forum/topics/1529](topic 1529). Note that the CI4 part of this just bridges to the default database. If you have more connections, just look at the CI3 part and modify the CI4 part accordingly. Full code is
<?php
namespace koolreport\codeigniter;
use \koolreport\core\Utility;
trait Friendship
{
public function __constructFriendship()
{
//assets folder
$assets = Utility::get($this->reportSettings, "assets");
if ($assets == null) {
$document_root = Utility::getDocumentRoot();
$script_folder = str_replace("\\", "/", realpath(dirname($_SERVER["SCRIPT_FILENAME"])));
$asset_path = $script_folder . "/assets";
$asset_url = Utility::strReplaceFirst($document_root, "", $script_folder) . "/assets";
if (!is_dir($asset_path . "/koolreport_assets")) {
if (!is_dir($asset_path)) {
mkdir($asset_path, 0755);
}
mkdir($asset_path . "/koolreport_assets", 0755);
}
$assets = array(
"url" => $asset_url . "/koolreport_assets",
"path" => $asset_path . "/koolreport_assets",
);
$this->reportSettings["assets"] = $assets;
}
//If codeigniter 4 then load default database <====
if(file_exists(APPPATH . 'Config/App.php')){
$db = \Config\Database::connect();
$dataSources = array(
"default"=>array(
"connectionString"=>"mysql:host=$db->hostname;dbname=$db->database",
"username"=>$db->username,
"password"=>$db->password,
"charset"=>$db->charset
),
);
$this->reportSettings["dataSources"] = $dataSources;
}
return;
////////////////////////////////////end CI4\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//If codeigniter 3 then load databases <====
if (!file_exists($file_path = APPPATH . 'config/' . ENVIRONMENT . '/database.php')
&& !file_exists($file_path = APPPATH . 'config/database.php')) {
return;
}
include $file_path;
$dbSources = array();
foreach ($db as $name => $dbconfig) {
$dbSources[$name] = array(
"class" => CIDataSource::class,
"name" => $name,
);
}
$dataSources = Utility::get($this->reportSettings, "dataSources", array());
$this->reportSettings["dataSources"] = array_merge($dbSources, $dataSources);
////////////////////////////////////end CI3\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
}
}
- I had forgotten about the KoolReport autoload.php so copied it from the core folder of the zip file and placed it in app/Libraries/koolreport. The same folder that contains KoolReport.php. This one took a while to get working as there is an assumption in the autoload.php. Maybe it's because I didn't use Composer. I had to remove the packages subfolder reference in line 39 as it doesn't exist. This is when stuff really started to work.
$filePath = $dir."/".str_replace("koolreport/", "/", $classname).".php";
- Now that we are fully configured, we have to set up one test report. For good segregation, create an app/Reports. This will be where you place all of your reports. Create a test report and view here. Don't be tempted to use your Views folder just yet.
testReport.php
<?php
namespace App\Reports;
require APPPATH."Libraries/koolreport/autoload.php";
class TestReport extends \koolreport\KoolReport
{
use \koolreport\codeigniter\Friendship;
function setup()
{
$this->src("default")
->query("SELECT * FROM users WHERE id in (14)")
->pipe($this->dataStore("users"));
}
}
testReport.view.php
<?php
use \koolreport\widgets\koolphp\Table;
Table::create(array(
"dataStore"=>$this->dataStore("users"),
"class"=>array(
"table"=>"table table-hover"
)
));
?>
Create a Reports Controller in app/Controllers. Note that we don't run $report->run()->render(); just $report->run();. We will render it in the CI4 view.
<?php namespace App\Controllers;
use CodeIgniter\Controller;
require APPPATH."Reports/TestReport.php";
class Reports extends BaseController
{
function __construct()
{
$this->session = session();
}
public function index()
{
}
public function testReport(){
$report = new \App\Reports\TestReport();
$report->run();
$data['report'] = $report;
echo view('page_shell/header');
echo view('page_shell/profile');
echo view('reports/showReport',$data);
echo view('page_shell/footer');
}
}
Create a showReport view in app/Views. It will just include the below code. This allows you to frame your Kool Report within the framework of your CI4 site. You only need this one view so it can be reused through all of your reports in app/Reports.
<?php $report->render(); ?>
That's it, you're done, just call your controller/method [Reports/testReport]. Enjoy. thanks to the CI and KR team for two fantastic projects.