-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntroCarnetVol_tools.php
More file actions
483 lines (452 loc) · 17.7 KB
/
IntroCarnetVol_tools.php
File metadata and controls
483 lines (452 loc) · 17.7 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php
require_once 'incident.class.php' ;
require_once "dbi.php" ;
function GetCompteurHour($Compteur) {
$aPos=strpos($Compteur,".");
if(!$aPos) {
return NULL;
}
return substr($Compteur,0,$aPos);
}
function GetCompteurMinute($Compteur) {
$aPos=strpos($Compteur,".");
if(!$aPos) {
return NULL;
}
return substr($Compteur,$aPos+1,strlen($Compteur)-$aPos-1);
}
function GetDayTime($Date, $Time) {
return $Date." ".$Time;
}
function CheckVar($var) {
if(!isset($var)) {
$var='NULL';
}
else if(empty($var)) {
$var='NULL';
}
return $var;
}
// Compute the remark associated to the CP
function GetFullRemarks( $theFraisCP, $thePAX, $theRemarque, $theFraisDC) {
$aRemark="";
if($theFraisCP != "") {
$aRemark = $theFraisCP;
if($thePAX == -1) {
$aRemark = $aRemark . " Ferry";
}
else if($thePAX == -2) {
$aRemark = $aRemark . " Club";
}
else if($thePAX == -3) {
$aRemark = $aRemark . " Initiation";
}
else if($thePAX == -4) {
$aRemark = $aRemark . " Vol IF";
}
else if($thePAX == -5) {
$aRemark = $aRemark . " Vol membre";
}
else if($thePAX == -6) {
$aRemark = $aRemark . " Vol D.H.F.";
}
else if($thePAX == -7) {
$aRemark = $aRemark . " Vol PR";
}
else if($thePAX == -8) {
$aRemark = $aRemark . " Mécano";
}
else {
$aRemark = $aRemark . " PAX ".strval($thePAX);
}
}
if($theRemarque != "") {
if($aRemark != "") {
$aRemark = $aRemark." ";
}
$aRemark = $aRemark.$theRemarque;
}
if($theFraisDC != "DC") {
if($aRemark != "") {
$aRemark = $aRemark." ";
}
$aRemark = $aRemark.$theFraisDC;
}
return $aRemark;
}
// Add an incident in the Aircraft Technical Log
function AddATLIncident($theLogId, $thePlane, $theSeverity, $theRemark) {
global $mysqli_link, $table_incident_history, $userId ;
//if(1) return 76;
//print("AddATLIncident:Started: theLogId=$theLogId, thePlane=$thePlane, theSeverity=$theSeverity, theRemark=$theRemark<br>");
if($theLogId==0) return false;
if(!CheckIncidentCoherency($theLogId)){
// The incident table is corrected!
print("<p style=\"color: red;\"><b>ERROR:AddATLIncident($theLogId): Incident table corrected.</b></p><br>");
//return false;
}
$incidentID=GetATLIncidentID($theLogId);
if($incidentID == 0) {
if($theSeverity != "nothing") {
// No incident associated to the logid $$ severity = hazard or nohazard
$incident = new Incident() ;
$incident->logId = $theLogId;
$incident->plane = $thePlane ;
$incident->severity = $theSeverity ;
$incident->save() ;
$event = new IncidentEvent() ;
$event->incident = $incident ;
$event->status = 'opened' ;
$event->text = $theRemark ;
$event->save() ;
return $incident->id;
}
else {
return 0;
}
}
else {
// An incident is already associated to the logid then edit the incident
$incident = new Incident() ;
$incident->getByLogId($theLogId) ;
$aPreviousPlane=$incident->plane;
$aPreviousSeverity=$incident->severity;
$aPreviousRemark=$incident->lastText;
if($aPreviousPlane==$thePlane && $aPreviousSeverity==$theSeverity && $aPreviousRemark==$theRemark) {
// nothing changed for the ATL. Nothing to do.
//print("AddATLIncident: Same ATL nothing to do!<br>");
return 0;
}
if($theSeverity == "nothing") {
//print("AddATLIncident: The pilot removes its ATL Log -> Close the Event!<br>");
// The pilot removes its ATL Log -> Close the Event
$event = new IncidentEvent() ;
$event->incident = $incident ;
$event->status = "closed";
$event->text = "Event removed by the pilot. Nothing more to declare!";
$event->save() ;
return $incident->id;;
}
if($aPreviousRemark==$theRemark) {
// Same remark but severity or plane changed ! No new event
//print("AddATLIncident: Same remark but serverity or plane changed!<br>");
$incident->plane = $thePlane ;
$incident->severity = $theSeverity ;
$incident->save();
return $incident->id;;
}
else {
//print("AddATLIncident: the remark has changed!<br>");
// the remark has changed. Create a new event with the new remark.
// We are not allowed to update the event. Only create a new one.
$incident->plane = $thePlane ;
$incident->severity = $theSeverity ;
$incident->save();
$event = new IncidentEvent() ;
$event->incident = $incident ;
$event->status = "opened";
$event->text = trim(web2db($theRemark));
$event->save() ;
return $incident->id;;
}
}
return 0;
}
// Remove all oil quantity associated to a BookId
function RemoveAllHuileQuantityBooking($bookingid) {
global $mysqli_link, $table_logbook ;
$result = mysqli_query($mysqli_link, "select l_id
from $table_logbook where l_booking = $bookingid")
or die("Impossible de lire les entrees pour reservation $bookingid: " . mysqli_error($mysqli_link)) ;
while ($row = mysqli_fetch_array($result)) {
$logid=$row['l_id'];
RemoveHuileQuantity($logid);
}
}
//Remove Entry in the Topup table
function RemoveHuileQuantity($logid)
{
global $mysqli_link, $table_planes_topup ;
$sql = "DELETE FROM $table_planes_topup WHERE pt_log_id = $logid AND pt_item='oil'";
mysqli_query($mysqli_link, $sql)
or journalise($userId, "F", "Cannot delete topup $logid: " . mysqli_error($mysqli_link)) ;
}
// Retieve the oil quantity assocated to a log
function GethuileQuantity($logid)
{
//print("GethuileQuantity($logid) <br>");
global $mysqli_link, $table_planes_topup ;
$sql = "SELECT pt_quantity FROM $table_planes_topup WHERE pt_log_id = $logid AND pt_item='oil'";
//print("sql=$sql<br>");
//if(1) return 3.0;
$result = mysqli_query($mysqli_link, $sql)
or journalise($userId, "F", "Cannot retrieve topup by log id ($logid): " . mysqli_error($mysqli_link)) ;
$row = mysqli_fetch_array($result) ;
$quantity=0.0;
if ($row) {
$quantity=$row['pt_quantity'];
}
//print("GethuileQuantity(Quantity=$quantity <br>");
return $quantity;
}
// Manage the oil quantity stored in the table $table_planes_topup = 'rapcs_planes_topup' ;
function ManageHuileQuantity($logid, $planeId, $huileQuantity, $pilotId, $startDayTime, $engineStartHour,$engineStartMin)
{
global $userId, $mysqli_link, $table_planes_topup ;
//print("ManageHuileQuantity started ($logid, $planeId, $huileQuantity, $pilotId, $startDayTime, $engineStartHour,$engineStartMin<br>)");
$sql = "SELECT * FROM $table_planes_topup WHERE pt_log_id = $logid AND pt_item='oil'";
//print("sql=$sql<br>");
$result = mysqli_query($mysqli_link, $sql)
or journalise($userId, "F", "Cannot retrieve topup by log id ($logid): " . mysqli_error($mysqli_link)) ;
$row = mysqli_fetch_array($result) ;
$ptLogID=0;
if ($row) {
$ptLogID=$row['pt_log_id'];
}
if($huileQuantity==0.0) {
if($ptLogID==0) {
//print("ManageHuileQuantity: Nothing to do<br>");
// Nothing to do
return 0;
}
else {
// Delete the entry in the table
//print("ManageHuileQuantity: Delete entry<br>");
RemoveHuileQuantity($logid);
return 0;
}
}
if($ptLogID==0) {
// Create a new entry in the topup
//print("ManageHuileQuantity: Create new entry<br>");
$sql="insert into $table_planes_topup(pt_plane, pt_date, pt_item, pt_quantity, pt_who, pt_log_id, pt_index_hour, pt_index_minute)
values ('$planeId', '$startDayTime', 'oil', $huileQuantity, $pilotId, $logid, $engineStartHour,$engineStartMin);";
//print("sql=$sql<br>");
mysqli_query($mysqli_link, $sql)
or journalise($userId, "F", "Impossible d'ajouter dans le TOPUP: " . mysqli_error($mysqli_link)) ;
$ptLogID= $logid ;
//print("ManageHuileQuantity:ptLogID=$ptLogID");
}
else {
// Replace in the Topup table
//print("ManageHuileQuantity: Replace entry<br>");
$sql="UPDATE $table_planes_topup
SET pt_plane='$planeId', pt_date='$startDayTime',
pt_quantity=$huileQuantity, pt_who=$pilotId, pt_index_hour=$engineStartHour, pt_index_minute=$engineStartMin
WHERE pt_log_id = $logid AND pt_item='oil'";
mysqli_query($mysqli_link, $sql)
or journalise($userId, "F", "Impossible de mettre à jour la table TOPUP $sql: " . mysqli_error($mysqli_link)) ;
}
return $ptLogID;
}
// Check if the two table rapcs_incident and rapcs_incident_history are coherent
function CheckIncidentCoherency($theLogid)
{
global $userId, $mysqli_link, $table_incident, $table_incident_history ;
$sql = "SELECT * FROM $table_incident WHERE i_log = $theLogid";
$result = mysqli_query($mysqli_link, $sql)
or journalise($userId, "F", "Cannot retrieve incident by log id ($theLogid): " . mysqli_error($mysqli_link)) ;
$row = mysqli_fetch_array($result) ;
// TODO: check if multiple incidents for same logid...
if ($row) {
$id=$row['i_id'];
$sql = "SELECT * FROM $table_incident_history WHERE ih_incident = $id";
$result = mysqli_query($mysqli_link, $sql)
or journalise($userId, "F", "Cannot retrieve incident history by id ($id): " . mysqli_error($mysqli_link)) ;
$row = mysqli_fetch_array($result) ;
if(!$row) {
// Incoherent: no history associated to an incident
print("<p style=\"color: red;\"><b>ERROR:CheckIncidentCoherency($theLogid): No history associated to incident id=$id. Deleting this entry.</b></p><br>");
journalise($userId, "W", "Incoherent incident found for log id $theLogid: no history associated to incident id=$id. Deleting this entry.") ;
$sql = "DELETE FROM $table_incident WHERE i_log = $theLogid";
mysqli_query($mysqli_link, $sql)
or journalise($userId, "F", "Cannot delete incident #$theLogid: " . mysqli_error($mysqli_link)) ;
return false;
}
}
return true;
}
// Retrieve the IncidentId associated to the logID
// Returns 0 if no incident associated to the loggID
function GetATLIncidentID($theLogid) {
//print("GetATLIncidentID($theLogid)<br>");
$incident = new Incident() ;
$incident->getByLogId($theLogid) ;
if($incident!=NULL) {
//print("GetATLIncidentID($theLogid) incident.id=$incident->id<br>");
if($incident->id==NULL) {
return 0;
}
return $incident->id;
}
//print("GetATLIncidentID($theLogid) return 0<br>");
return 0;
}
// Retrieve the severity associated to an incidentId
function GetATLIncidentSeverity($theIncidentId) {
$incident = new Incident() ;
$incident->getById($theIncidentId) ;
if($incident!=NULL) {
if($incident->lastStatus=="closed") {
return "nothing";
}
return $incident->severity;
}
return "select";
}
// Retrieve the description associated to an incidentId
function GetATLIncidentDescription($theIncidentId) {
$incident = new Incident() ;
$incident->getById($theIncidentId) ;
if($incident!=NULL) {
if($incident->lastStatus=="closed") {
return "";
}
return $incident->lastText;
}
return "";
}
// Returns true is the incident is closed
function IsATLIncidentClosed($theIncidentId) {
$incident = new Incident() ;
$incident->getById($theIncidentId) ;
if($incident!=NULL) {
$status=$incident->lastStatus;
if($status=="closed") {
return true;
}
}
return false;
}
// Retrieve the description associated of all open incidend for all planes
// JSON: '{"ATL": [{"plane": "OO-ALD", "logs":["log11","log12"]},{"plane": "OO-JRB", "logs":["log21","log22","log23"]}]}'
function GetJSONIncidentByPlanes() {
$planes = array('OO-ALD', 'OO-ALE', 'OO-APV', 'OO-FUN', 'OO-JRB', 'OO-FMX', 'OO-SPQ', 'PH-AML') ;
$jsonString='{"ATL": [';
//loop on planes
$planeCount=0;
foreach($planes as $plane) {
//$plane="OO-JRB";
$incidents = new Incidents($plane, ['opened', 'inprogressnoaog', 'inprogressaog', 'camonoaog', 'camoaog']) ;
$incidentCount=0;
foreach($incidents as $incident) {
$incidentCount++;
if($incidentCount==1) {
$planeCount++;
if($planeCount>1) {
$jsonString.=',';
}
$jsonString.='{"plane": "'.$plane.'", "logs": [';
}
else {
$jsonString.=',';
}
$jsonString.='"'.CleanATLLog($incident->firstText).'"';
}
if($incidentCount>0) {
$jsonString.=']}';
}
}
//end plane
$jsonString.=']}';
return $jsonString;
//return '{"ATL": [{"plane": "OO-ALD", "logs":["log11","log12"]},{"plane": "OO-JRB", "logs":["log21","log22","log23"]}]}';
}
// Remove all characters incompatible with javascript
function CleanATLLog($logText) {
$text=str_replace("'"," ",$logText);
$text=str_replace('"'," ",$text);
return $text;
}
// Send a mail for an HAZARD incident
function SentIncidentMail($theATLId, $thePlane, $theSeverity, $theRemark)
{
global $userId,$fleetEmail,$userFullName;
//print("SentIncidentMail($theATLId, $thePlane, $theSeverity, $theRemark)");
$severity=strtoupper($theSeverity);
if($severity=="HAZARD") {
$replyto="patrick.reginster@gmail.com";
//$mailto="patrick.reginster@gmail.com";
$mailto=$fleetEmail.",fis@spa-aviation.be";
$from_mail="fleet@spa-aviation.be";
$subject="Nouvel ATL report numero $theATLId de type HAZARD pour ".$thePlane;
$remark= remove_accents($theRemark);
$message="Un nouvel ATL report numéro $theATLId de type HAZARD a été introduit par ".$userFullName.
" pour l'avion ".$thePlane."<br>Description du problème: ".$remark."<br>Il faut peut être bloquer l'avion.<br><br>Mail généré automatiquement lors de l'introduction du vol ou via la page TechLog";
$headers="";
if($from_mail != "") {
$headers .= "From: ".$from_mail."\r\n";
}
//$headers .= "Cc: ".$replyto."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//echo("mailto=$mailto subject=$subject message=$message headers=$headers<br/>");
if(smtp_mail($mailto, $subject, $message, $headers)) {
return true;
}
else {
echo("mail SentIncidentMail() send ... ERROR!<br/>");
}
}
return false;
}
function remove_accents($text) {
$from = explode(" ",""
." À Á Â Ã Ä Å Ç È É Ê Ë Ì Í Î Ï Ñ Ò Ó Ô Õ Ö Ø Ù Ú Û Ü Ý à á â"
." ã ä å ç è é ê ë ì í î ï ñ ò ó ô õ ö ø ù ú û ü ý ÿ Ā ā Ă ă Ą"
." ą Ć ć Ĉ ĉ Ċ ċ Č č Ď ď Đ đ Ē ē Ĕ ĕ Ė ė Ę ę Ě ě Ĝ ĝ Ğ ğ Ġ ġ Ģ");
$to = explode(" ",""
." A A A A A A C E E E E I I I I N O O O O O O U U U U Y a a a"
." a a a c e e e e i i i i n o o o o o o u u u u y y A a A a A"
." a C c C c C c C c D d D d E e E e E e E e E e G g G g G g G");
return str_replace( $from, $to, $text);
}
// Check if a DTO flight is already associated to the logbook
function HasDTOFlight($theLogId) {
global $userId;
global $mysqli_link, $table_dto_flight;
//print("PRE HasDTOFlight: theLogId=$theLogId<br>");
if(!isset($theLogId)) {
//print("PRE1 HasDTOFlight: theLogId=$theLogId<br>");
return true;
}
$result = mysqli_query($mysqli_link, "SELECT *
FROM $table_dto_flight
WHERE df_flight_log = $theLogId")
or journalise($userId, "F", "Cannot read from $table_dto_flight for df_flight_log $theLogId: " . mysqli_error($mysqli_link)) ;
$row = mysqli_fetch_array($result) ;
if (! $row) {
//print("PRE2 HasDTOFlight: theLogId=$theLogId<br>");
return false ;
}
//print("PRE3 HasDTOFlight: theLogId=$theLogId<br>");
return true;
}
// Returns true is the thePilotID is a student
function IsStudent($thePilotID) {
global $mysqli_link, $table_user_usergroup_map,$joomla_student_group;
$studentResult=mysqli_query($mysqli_link,"SELECT user_id FROM $table_user_usergroup_map WHERE user_id = '$thePilotID' and group_id='$joomla_student_group';") or die("Impossible de retrouver le user_id dans table_user_usergroup_map: " . mysqli_error($mysqli_link)) ;
if ($studentResult->num_rows != 0) {
return true;
}
return false;
}
// Is a segment flight already introduced?
function IsSegmentAlreadyIntroduced($planeId,$startDayTime,$pilotId)
{
global $userId;
global $mysqli_link, $table_logbook;
//print("IsSegmentAlreadyIntroduced:start $planeId , $startDayTime , $pilotId<br>");
$result = mysqli_query($mysqli_link, "SELECT l_plane
FROM $table_logbook
WHERE l_plane = '$planeId' AND l_start = '$startDayTime' AND l_pilot= $pilotId")
or journalise($userId, "F", "Cannot read from $table_logbook for l_start = $startDayTime: " . mysqli_error($mysqli_link)) ;
$row = mysqli_fetch_array($result) ;
if (! $row) {
// print("IsSegmentAlreadyIntroduced n existe pas<br>");
return false ;
}
//print("IsSegmentAlreadyIntroduced existe<br>");
return true;
}
?>