/
index.php
242 lines (186 loc) · 6.16 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* Routes file
*
* @author Riccardo Brambilla aka ribrain <riccardobra@gmail.com> <Riccardo.Brambilla@nttdata.com>
* @copyright Sunchemical
* @version 1.0
*/
error_reporting(E_ALL);
ini_set( "display_errors", "On" );
// # Give 3 minutes to allow process completion
@ set_time_limit( 180 );
// # Grant some extra memory for charts creation
@ ini_set( "memory_limit", "256M" );
// # Setting charset to iso
@ header( "Content-Type: text/html; charset=utf-8" );
// # Setting locale for dates, coins etc
@ setlocale ( LC_ALL, "it_IT" );
// # Setting timezone
@ date_default_timezone_set( "Europe/Rome" );
// # Mssql charset and required settings
@ ini_set( "mssql.charset", "UTF-8" );
@ ini_set( "mssql.textlimit", "2147483647" );
@ ini_set( "mssql.textsize", "2147483647" );
// # Import needed Namespaces
use Sunchemical\Suncharts\Core\PropertyManager;
use Sunchemical\Suncharts\Dao\DAOConnection;
use Sunchemical\Suncharts\Dao\DAO;
use Sunchemical\Suncharts\Charts\ChartsBuilder;
use Sunchemical\Suncharts\Pdf\SunchemicalPdf;
use Sunchemical\Suncharts\Response\ServiceResponse;
use Sunchemical\Suncharts\Utils\Utils;
use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Processor\IntrospectionProcessor;
// # Commons Required
require_once "vendor/autoload.php";
// # Create a log channel
$log = new Logger( 'SunCharts-Sunchemical' );
$log->pushHandler( new RotatingFileHandler( 'logs/errors', 1, Logger::ERROR ) );
$log->pushProcessor( new IntrospectionProcessor( Logger::ERROR ) );
// # Get instance and load props
$PM = new PropertyManager();
// # DAOConnection instance
$DAOConn = new DAOConnection( $PM );
// # Mailer instance
$Mailer = new PHPMailer();
// # ChartsBuilder instance
$CB = new ChartsBuilder( $PM, $log );
// # Initializing router
$router = new AltoRouter();
// # This app is deployed in production as a subfolder and not a virtualhost
// # so we need to set a basepath for prod environment
if( $PM->getproperty( 'env' ) == 'prod' ) {
$router->setBasePath( '/suncharts/' );
}
// # $Params default
$defaults = array(
'y' => date( 'Y' ),
'm' => date( 'm'),
'oTypeID' => 1,
'oID' => 'All',
'techID' => 'All',
'oVendorID' => 'All',
'countryID' => 'IT'
);
// # Config interface
$router->map( 'GET', '/', function() {
// # Shows the page
require_once "config.html";
});
// # Cron based call ( all params are set to defaults )
$router->map( 'GET', 'worker/default', function() use ( $PM, $DAOConn, $log, $CB, $defaults ) {
// # Unlink latest charts files
$CB->clearPast();
// # Response
$SR = new ServiceResponse();
try {
// # Connect to db
$DAO = new DAO( $DAOConn, $log );
// # re
$RSByoID = $DAO->getLabWorkloadByoID( $defaults[ 'y' ], $defaults[ 'oTypeID' ], $defaults[ 'oID' ], $defaults[ 'countryID' ] );
$RSByTechnician = $DAO->getLabWorkloadByTechnician( $defaults[ 'y' ], $defaults[ 'm' ], $defaults[ 'countryID' ] );
$RSByMonth = $DAO->getLabWorkloadByMonth( $defaults[ 'y' ], $defaults[ 'm' ], $defaults[ 'countryID' ] );
// # Build chart
$CB->buildChartByCustomer( $RSByoID );
$CB->buildChartByTechnician( $RSByTechnician );
$CB->buildChartByMonth( $RSByMonth );
// # OK
$SR->status = ServiceResponse::STATUS_OK;
} catch( Exception $e ) {
// # No way
$log->addError( $e->getMessage() );
}
});
// # Forces report sending, passes all required parameters
$router->map( 'POST', 'manual-push', function() {
if( isset( $_POST[ "recipients" ] ) && !empty( $_POST[ "recipients" ] ) ) {
file_put_contents( "config/mail/recipients", $_POST[ "recipients" ] );
}
// # Unlink latest charts files
$CB->clearPast();
// # Response
$SR = new ServiceResponse();
try {
// # Connect to db
$DAO = new DAO( $DAOConn, $log );
// # re
$RSByoID = $DAO->getLabWorkloadByoID( $_POST[ 'customer_y' ], $_POST[ 'customer_oTypeID' ], $_POST[ 'customer_oID' ], $_POST[ 'customer_countryID' ] );
$RSByTechnician = $DAO->getLabWorkloadByTechnician( $_POST[ 'technician_y' ], $_POST[ 'technician_technicianID' ], $_POST[ 'technician_countryID' ] );
$RSByMonth = $DAO->getLabWorkloadByMonth( $_POST[ 'month_y' ], $_POST[ 'month_m' ], $_POST[ 'month_countryID' ] );
// # Build chart
$CB->buildChartByCustomer( $RSByoID );
$CB->buildChartByTechnician( $RSByTechnician );
$CB->buildChartByMonth( $RSByMonth );
// # OK
$SR->status = ServiceResponse::STATUS_OK;
echo json_encode( $SR );
} catch( Exception $e ) {
// # No way
$log->addError( $e->getMessage() );
}
});
// # Mail sender route
$router->map( 'GET', 'sender', function() use ( $PM, $log ) {
// # Mail sender instance
$MS = new MailSender();
$SR = $MS->send();
// # Response
echo json_encode( $SR );
});
// # CONFIG Page ajax ROUTES
// # Get recipients API
$router->map( 'GET', 'recipients', function() {
echo json_encode( file_get_contents( "config/mail/recipients" ) );
});
// # Get technicians list
$router->map( 'GET', 'technicians', function() use ( $DAOConn, $log ) {
try {
// # Connect to db
$DAO = new DAO( $DAOConn, $log, array() );
echo json_encode( $DAO->getTechnicians() );
} catch( Exception $e ) {
// # No way
$log->addError( $e->getMessage() );
echo json_encode( null );
return;
}
});
// # Get oid customer list
$router->map( 'GET', 'oidCustomer', function() use ( $DAOConn, $log ) {
try {
// # Connect to db
$DAO = new DAO( $DAOConn, $log, array() );
echo json_encode( $DAO->getOIDCustomer() );
} catch( Exception $e ) {
// # No way
$log->addError( $e->getMessage() );
echo json_encode( null );
return;
}
});
// # Get oid vendor list
$router->map( 'GET', 'oidVendor', function() use ( $DAOConn, $log ) {
try {
// # Connect to db
$DAO = new DAO( $DAOConn, $log, array() );
echo json_encode( $DAO->getOIDVendor() );
} catch( Exception $e ) {
// # No way
$log->addError( $e->getMessage() );
echo json_encode( null );
return;
}
});
// // # Route matching
$match = $router->match();
// # Call closure or throw 404 status
if( !$match || !is_callable( $match[ 'target' ] ) ) {
// # No route was matched
$log->addError( "Unable to find route!" );
return false;
}
// // # Invoking callback
call_user_func_array( $match[ 'target' ], $match[ 'params' ] );
?>