By using the filter_is_current_active_user, it is possible to guarantee that when multiple users access the same report, each logged-in user can only view their respective records. This tutorial demonstrates how to implement this feature in Smart Report Engine by filtering the report’s data source with a where clause based on a specified column and the value of a session key found in the logged-in user’s PHP session. When a user logs in, their user ID is typically stored in their PHP session. In this example, we will filter an “Orders” Report by the “employee_Id” column in the “Orders” table. We will assume that each employee’s ID is stored in their PHP session key “user_id”, with a value of 9 in this example. However, in a real-life scenario, the user ID should be dynamic and based on each logged-in user.

use SRE\Engine\CustomEngine;
use SRE\Engine\ReportOptions;
require_once "sre_bootstrap.php";
$report = new ReportOptions(SRE_PRIVATE_REPORT);
$report->select_tables(array("orders"))
        ->security_init("some_login_page_url","some_logout_page_url")
        ->security_check_session_saved_user_key("user_id")
        ->filter_is_current_active_user("orders", "employee_id", "user_id")
        ->set_grouping(array("customer"))
        ->select_all_fields();

$engine = new CustomEngine($report);
$report_path = $engine->create_report();
if ($report_path) {
    echo "Report created successfully! To visit your report please  click here  ";
}

Report Options

The ReportOptions Class instance named “$report” is first used to define the report options in the above code. The following report options are specified in this example:

  1. Setting the Report as a “Private_Report”
    The report is defined as a private report by passing the “SRE_PRIVATE_REPORT” flag to the constructor of the ReportOptions Class.

  2. Setting two MYSQL tables as the data source of the report
    An array of the two selected tables “items” and “items_sales” is passed to the “select_tables” method to specify the data source for the report.

  3. Setting the login and logout pages
    To integrate with the login system, the login page, logout page, and session name are passed to the “security_init” method.

  4. Checking the “user_id” Key Before granting access to the private report
    the “security_check_session_saved_user_key” method is used to verify the presence of a “user_id” key in the session.

  5. Filtering the report with the currently active user
    To filter the report by the currently logged-in user, the “Orders” table, “employee_id” filter-by column, and “user_id” PHP session key are passed to the “filter_is_current_active_report” method . The “employee_id” column typically contains the user IDs as foreign keys.

  6. Selecting all fields
    In this example, all columns of the selected table are chosen using the “select_all_fields” method.

After defining all the report options, an instance of type ReportOptions is passed to the constructor of the CustomEngine class to create a new CustomEngine object. The “create_report” method is then called on this object to generate the report and return its URL.

Summery

To summarize, using Smart Report Engine, it is possible to create private reports that can be filtered by the currently logged-in user so that each user can only view their respective records.