Before we begin, if you haven’t already done so, you might want to read The Primary Usage Example. This post will help you understand how to start using Smart Report Engine.

Column Types:

Smart Report Engine supports many column customization options, which allows you to change the appearance of any column in your report. You can select one of the following types:

  • Standard column:
    The values stored in your database column will be displayed as-is without any editing from any kind.
  • Image column:
    It’s an ideal choice if you are storing image names or paths in a textual data type column, and you want to display that column as images in your report.
  • Checkbox column:
    This type displays any Boolean data type column as checkboxes.
  • Country flag column:
    This type displays countries names along with their flags.
  • Star rating Column: 
    This type displays any numerical data as star-ratings.
  • Prefix text column:
    You can use this type to prepend a text to the cell value stored in your database. For example, you can use this type to add a currency such as “$” to the price stored in your database.
  • Suffix text column:
    You can use this type to append a text to the cell value stored in your database.
  • Link column:
    You can use this type to display URLs stored in your database as clickable links in your report.

Smart Report Engine supports many formatting methods to apply different customization options to any report column(s). For example, format_image_column, format_country_flag_column, and format_prifix_text_to_column

Report Options:

For the sake of this example, let’s assume we have an “items” table in a MYSQL DB. This table stores data about some items in a sunglasses store. In this example we want to:

  1. Show only the following columns:
    “id,” ”product_name,” “country,” “price,” “photo,” “rating,” “discontinued.”
  2. Customize the columns as follows:
    1. Display the data stored in the “country” column as country flags.
    2. Add a currency to the price column.
    3. Display the data stored in the “photo” column as clickable thumbnails.
    4. Display the data stored in the rating column as star-ratings
  3. Group the records by “Country.”

We will start by showing the code in a native PHP project and then in a Laravel project.

1- Case of a native PHP Project

In a native PHP Project, the code will be something like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use SRE\Engine\CustomEngine;
use SRE\Engine\ReportOptions;
require_once "../sre_bootstrap.php";
$report = new ReportOptions(SRE_PUBLIC_REPORT);
$report->select_tables(array("items"))
       ->select_fields(array("id", "country", "list_price", "product_name","photo","rating","discontinued"))
       ->set_grouping(array("country"))
       ->format_country_flag_column ("country")
       ->format_rating_column("rating")
       ->format_prefix_text_to_column("list_price", "$")
       ->format_check_box_column("discontinued")
       ->format_image_column("photo");
$engine = new CustomEngine($report);
$report_path = $engine->create_report();
if ($report_path) {
    echo "Report created successfully! To visit your report please <a href='" . $report_path . "' /> click here </a> ";
}

2- Case of Laravel Project

We will put the report generation logic in a service. It will be something like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace App\Services;
 
use Sre\SmartReportingEngine\src\Engine\Constants;
use Sre\SmartReportingEngine\src\Engine\CustomEngine;
use Sre\SmartReportingEngine\src\Engine\ReportOptions;
 
class ReportService {
 
    public function generatePublicReport() {
        $report = new ReportOptions(Constants::SRE_PUBLIC_REPORT);
        $report->select_tables(array("items"))
               ->select_fields(array("id", "country", "list_price", "product_name","photo","rating","discontinued"))
               ->set_grouping(array("country"))
               ->format_country_flag_column ("country")
               ->format_rating_column("rating")
               ->format_prefix_text_to_column("list_price", "$")
               ->format_check_box_column("discontinued")
               ->format_image_column("photo");
        $engine = new CustomEngine($report);
        $report_path = $engine->create_report();
        return $report_path;
    }
}

Step-by-step code explanation

In the above code, we start by defining the options we want for this report using the “$report” object, which is an instance of the “ReportOptions” Class.
Following are the report options defined in the example:

Setting the Report as a “Public_Report”

A public report can be accessed freely without any integration with a login system. To set this report as public, we pass the “SRE_PUBLIC_REPORT”  flag to the constructor of the “ReportOptions” class.

Setting a specific MYSQL table as the data source of the report.

In the above example, we want to select a table with the name “items” as the data source of the report. For doing this, we pass an array with only the “items” table to the “select_tables” method.

Selecting certain columns to show in the report.

In the above example, we want to show only specific columns in the report. So, we pass an array of the selected columns to the “select_fields” method.

Applying columns customization:

In the above example, we apply four formatting methods, which are :

  • The “format_country_flag_column” is applied to the “country” column.
  • The “format_prifix_text_to_column” is applied to the “list_price” column. Please note that we pass the column name (i.e., “list_price”) and the text we want to prepend to the formatting method.
  • The “format_check_box_column” is applied to the “discontinued” column.
  • The “format_image_column” is applied to the “Photo” column.
    – Please note that the default location of the actual images should be inside a directory with the name “images” inside your report’s directory.

Then after defining all the options for the report using an instance of type “ReportOptions,” We pass this instance to the constructor of the “CustomEngine” class. By doing this, we create a new instance of “CustomEngine”; Then, we use this new instance to call the “create_report” method to generate the report and return its URL.