-
Notifications
You must be signed in to change notification settings - Fork 21
/
Rendering a Report with SQL from Code on the Server-Side.php
93 lines (77 loc) · 3.3 KB
/
Rendering a Report with SQL from Code on the Server-Side.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
require_once '../vendor/autoload.php';
use Stimulsoft\Events\StiDataEventArgs;
use Stimulsoft\Report\Enums\StiEngineType;
use Stimulsoft\Report\StiReport;
// Changing the working directory one level up, this is necessary because the examples are in a subdirectory
chdir('../');
// Creating a report object
$report = new StiReport();
// Setting the server-side rendering mode
$report->engine = StiEngineType::ServerNodeJS;
// Defining viewer events before processing
// It is allowed to assign a PHP function, or the name of a JavaScript function, or a JavaScript function as a string
// Also it is possible to add several functions of different types using the append() method
$report->onBeginProcessData = function (StiDataEventArgs $args) {
// In this event, it is possible to handle the data request, and replace the connection parameters
// You can change the connection string
// This example uses the 'Northwind' SQL database, it is located in the 'Data' folder
// You need to import it and correct the connection string to your database
if ($args->connection == 'MyConnectionName')
$args->connectionString = 'Server=localhost; Database=northwind; UserId=root; Pwd=;';
// You can change the SQL query
if ($args->dataSource == 'MyDataSource')
$args->queryString = 'SELECT * FROM MyTable';
// You can change the SQL query parameters with the required values
// For example: SELECT * FROM @Parameter1 WHERE Id = @Parameter2 AND Date > @Parameter3
if ($args->dataSource == 'MyDataSourceWithParams') {
$args->parameters['Parameter1']->value = 'TableName';
$args->parameters['Parameter2']->value = 10;
$args->parameters['Parameter3']->value = '2019-01-20';
}
};
// Processing the request and, if successful, immediately printing the result
$report->process();
// Loading a report by URL
// This method loads a report file and stores it as a compressed string in an object
// The report will be loaded from this string into a JavaScript object when using Node.js
$report->loadFile('reports/SimpleListSQL.mrt');
// Building a report
// The report is not built using PHP code
// This method will prepare JavaScript code and pass it to Node.js, which will build a report and return the finished document
$result = $report->render();
if ($result) {
// After successfully building a report, the finished document can be saved as a string or to a file
$document = $report->saveDocument();
// $result = $report->saveDocument('reports/SimpleList.mdc');
$bytes = strlen($document);
$message = "The finished document takes $bytes bytes.";
}
else {
// If there is a build error, you can display the error text
$message = $report->nodejs->error;
}
use Stimulsoft\Export\Enums\StiExportFormat;
$message .= $report->exportDocument(StiExportFormat::Html);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
<title>Rendering a Report with SQL from Code on the Server-Side</title>
<style>
html, body {
font-family: sans-serif;
}
</style>
</head>
<body>
<h2>Rendering a Report with SQL from Code on the Server-Side</h2>
<hr>
<?php
// Displaying the result message
echo $message;
?>
</body>
</html>