-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.demande.php
497 lines (403 loc) · 15.4 KB
/
class.demande.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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
error_reporting(E_ALL);
/**
* class.demande.php
*
* @author Jean-Francois Levesque, <[email protected]>
* @author Philippe Gref-Viau, <[email protected]>
*/
/*
* Definition des constantes pour la base de données.
*
* ATTENTION : Lorsqu'il y a ajout ou suppression de constantes,
* il faut modifier les méthodes SET correspondantes.
*/
define('DEMANDE_TYPE_COMPTANT', 1);
define('DEMANDE_TYPE_CHEQUE', 2);
define('DEMANDE_TYPE_MANDAT', 3);
define('DEMANDE_TYPE_INTERAC', 4);
define('DEMANDE_TYPE_VISA', 5);
define('DEMANDE_TYPE_MC', 6);
define('DEMANDE_PERMIS_C', 0);
define('DEMANDE_GROUPE_AEP', 0);
define('DEMANDE_STATUS_ATTENTE', 0);
define('DEMANDE_STATUS_REFUSE', 1);
define('DEMANDE_STATUS_ACCEPTE', 2);
define('DEMANDE_STATUS_PAYE', 3);
define('DEMANDE_STATUS_PREUVEOK', 4);
define('DEMANDE_STATUS_ANNULE', 5);
define('DEMANDE_STATUS_IMPRIME', 6);
define('DEMANDE_STATUSETUDIANT_PLEIN', 1);
define('DEMANDE_STATUSETUDIANT_PARTIEL',2);
if (0 > version_compare(PHP_VERSION, '5')) {
die('This file was generated for PHP 5');
}
require_once('class.config.php');
require_once('class.database.php');
require_once('class.log.php');
require_once('class.validation.php');
require_once('class.util.php');
require_once('class.file.php');
require_once('class.car.php');
require_once('class.demandStatus.php');
require_once('class.tripInfo.php');
require_once('class.userData.php');
/**
* Short description of class demande
*
* @access public
* @author Jean-Francois Levesque, <[email protected]>
*/
class demande
{
// Constants
const MATRICULE_DB_FIELD = "matricule";
const PAYMENT_METHOD_DB_FIELD = "payment";
const CREATION_DATE_DB_FIELD = "dateCreation";
const MODIF_DATE_DB_FIELD = "dateLastModif";
const STATUS_DB_FIELD = "status";
const STATUS_DETAILS_DB_FIELD = "statusDetails";
const CARPOOLING_DB_FIELD = "carpooling";
const CARPOOLING_OTHERS_DB_FIELD = "carpoolingPool";
const DETAILS_DB_FIELD = "carpoolingDetails";
const DRIVING_LICENSE_DB_FIELD = "drivingLicenseURL";
const PROOF_OF_RESIDENCE_DB_FIELD = "residentialProofURL";
const CAR1_DB_FIELD = "car1";
const CAR2_DB_FIELD = "car2";
const TRIP_DB_FIELD = "tripInfo";
const PAYMENT_METHOD_FORM_FIELD = "form_payment";
const CREATION_DATE_FORM_FIELD = "dateCreation";
const MODIF_DATE_FORM_FIELD = "dateLastModif";
const STATUS_FORM_FIELD = "demandStatus";
const CARPOOLING_FORM_FIELD = "carpooling";
const CARPOOLING_OTHERS_FORM_FIELD = "carpoolingWithOthers";
const DETAILS_FORM_FIELD = "carpoolingDetails";
const DRIVING_LICENSE_FORM_FIELD = "drivingLicense";
const PROOF_OF_RESIDENCE_FORM_FIELD = "residenceProof";
const DRIVING_LICENSE_DIRECTORY = "proofs/license/";
const PROOF_OF_RESIDENCE_DIRECTORY = "proofs/residential/";
// Attributes
private $userData = null;
private $paymentMethod = 0;
private $creationDate = 0;
private $modificationDate = 0;
private $carpooling = false;
private $carpoolingOthers = false;
private $details = "";
private $drivingLicense;
private $proofOfResidence;
private $status = null;
private $matricule = null;
private $car1 = null;
private $car2 = null;
private $tripInfo = null;
private $isDataFromPost = false;
private $hasExistingDemandInDatabase = false;
private $objLog = null;
private $database = null;
// --- OPERATIONS ---
public function __construct()
{
$a = func_get_args();
$i = func_num_args();
if(method_exists($this, $f='__construct'.$i)) {
call_user_func_array(array($this, $f), $a);
} else {
$this->init();
$this->status = new demandStatus();
}
}
public function __construct1($matricule) {
$this->matricule = $matricule;
$this->init();
$this->status = new demandStatus($matricule);
$this->updateDataFromDB();
}
public function init() {
$this->objLog = log::instance();
$this->database = database::instance();
$this->car1 = new car(1);
$this->car2 = new car(2);
$this->userData = new userData($this->matricule);
$this->userData->getUserData($_SESSION['usager']);
}
public function updateDataFromDB() {
$this->getDataGromDB($this->matricule);
}
public function getDataGromDB($matricule)
{
$results = $this->database->requete("SELECT * from st_demande WHERE matricule = '$matricule'");
$this->drivingLicense = new demandTableFile();
$this->proofOfResidence = new demandTableFile();
$this->status->loadStatusData($matricule);
if (mysql_num_rows($results) != 1)
{
$this->hasExistingDemandInDatabase = false;
return false;
}
$this->hasExistingDemandInDatabase = true;
$resultsArray = mysql_fetch_array($results);
$this->paymentMethod = $resultsArray[demande::PAYMENT_METHOD_DB_FIELD];
$this->creationDate = $resultsArray[demande::CREATION_DATE_DB_FIELD];
$this->modificationDate = $resultsArray[demande::MODIF_DATE_DB_FIELD];
$this->carpooling = $resultsArray[demande::CARPOOLING_DB_FIELD];
$this->carpoolingOthers = $resultsArray[demande::CARPOOLING_OTHERS_DB_FIELD];
$this->details = $resultsArray[demande::DETAILS_DB_FIELD];
$this->drivingLicense->loadFromServer($matricule, demande::DRIVING_LICENSE_DB_FIELD);
$this->proofOfResidence->loadFromServer($matricule, demande::PROOF_OF_RESIDENCE_DB_FIELD);
$this->car1->getDataFromDB($resultsArray[demande::CAR1_DB_FIELD], $matricule);
$this->car2->getDataFromDB($resultsArray[demande::CAR2_DB_FIELD], $matricule);
$this->tripInfo = new tripInfo();
$this->tripInfo->getTripInfoFromDb($resultsArray[demande::TRIP_DB_FIELD]);
$this->isDataFromPost = false;
return true;
}
public function getDataFromPost($postData,$matricule)
{
$this->paymentMethod = util::getParam($postData, demande::PAYMENT_METHOD_FORM_FIELD);
$this->carpooling = util::getParam($postData, demande::CARPOOLING_FORM_FIELD);
$this->carpoolingOthers = util::getParam($postData, demande::CARPOOLING_OTHERS_FORM_FIELD);
$this->details = util::getParam($postData, demande::DETAILS_FORM_FIELD);
$this->drivingLicense = new demandTableFile();
$this->proofOfResidence = new demandTableFile();
$this->drivingLicense->loadFromPost(demande::DRIVING_LICENSE_FORM_FIELD,
util::$ALLOWED_EXTENSIONS,
demande::DRIVING_LICENSE_DIRECTORY,
demande::DRIVING_LICENSE_DB_FIELD,
$matricule);
$this->proofOfResidence->loadFromPost(demande::PROOF_OF_RESIDENCE_FORM_FIELD,
util::$ALLOWED_EXTENSIONS,
demande::PROOF_OF_RESIDENCE_DIRECTORY,
demande::PROOF_OF_RESIDENCE_DB_FIELD,
$matricule);
$this->car1 = new car(1);
$this->car2 = new car(2);
$result = $this->database->requete("SELECT * FROM st_demande WHERE matricule = '".$matricule."'");
if(mysql_num_rows($result) == 0)
{
$this->car1->getDataFromPost($postData);
$this->car2->getDataFromPost($postData);
}
else
{
$resultsArray = mysql_fetch_array($result);
$this->car1->getDataFromPost($postData, $resultsArray[demande::CAR1_DB_FIELD]);
$this->car2->getDataFromPost($postData, $resultsArray[demande::CAR2_DB_FIELD]);
}
$this->isDataFromPost = true;
}
public function saveToDatabase($matricule)
{
try
{
$this->database->beginTransaction();
$result = $this->database->requete("SELECT * FROM st_demande WHERE matricule = '".$matricule."'");
$hasDemandInDatabase = (mysql_num_rows($result) != 0);
$resultsArray = mysql_fetch_array($result);
// Saving first car
$firstCarId = $resultsArray[demande::CAR1_DB_FIELD];
$doesFirstCarExistInDatabse = isset($firstCarId);
$this->car1->saveToDatabase($firstCarId, $doesFirstCarExistInDatabse);
// Saving second car
$secondCarId = $resultsArray[demande::CAR2_DB_FIELD];
$doesSecondCarExistInDatabse = isset($secondCarId);
// Trip info
$this->tripInfo = new tripInfo($this->userData->getAddress(), $this->userData->getCity(), $this->userData->getZipCode());
$this->tripInfo->computeValues();
$this->tripInfo->saveToDatabase($resultsArray[demande::TRIP_DB_FIELD]);
$save_car2 = false;
if($this->car2->hasValuesInAtLeastOneField())
{
$this->car2->saveToDatabase($secondCarId, $doesSecondCarExistInDatabse);
$save_car2 = true;
}
$query = "";
if (!$hasDemandInDatabase)
{
$this->creationDate = date("Y-m-d");
$this->modificationDate = date("Y-m-d");
$this->drivingLicense->saveToServer($resultsArray[demande::DRIVING_LICENSE_DB_FIELD]);
$this->proofOfResidence->saveToServer($resultsArray[demande::PROOF_OF_RESIDENCE_DB_FIELD]);
if($save_car2) {
$car2_field = ",".demande::CAR2_DB_FIELD;
$car2_value = "','".util::cleanup($this->car2->getId());
}
$query = "INSERT INTO st_demande
(".demande::MATRICULE_DB_FIELD.",
".demande::CREATION_DATE_DB_FIELD.",
".demande::MODIF_DATE_DB_FIELD.",
".demande::PAYMENT_METHOD_DB_FIELD.",
".demande::CARPOOLING_DB_FIELD.",
".demande::DETAILS_DB_FIELD.",
".demande::CARPOOLING_OTHERS_DB_FIELD.",
".demande::DRIVING_LICENSE_DB_FIELD.",
".demande::PROOF_OF_RESIDENCE_DB_FIELD.",
".demande::TRIP_DB_FIELD.",
".demande::CAR1_DB_FIELD
.$car2_field.")
VALUES
('".util::cleanup($matricule)."',
'".util::cleanup($this->creationDate)."',
'".util::cleanup($this->modificationDate)."',
'".util::cleanup($this->paymentMethod)."',
'".util::cleanup($this->carpooling)."',
'".util::cleanup($this->details)."',
'".util::cleanup($this->carpoolingOthers)."',
'".util::cleanup($this->drivingLicense->getOutputLocation())."',
'".util::cleanup($this->proofOfResidence->getOutputLocation())."',
'".util::cleanup($this->tripInfo->getId())."',
'".util::cleanup($this->car1->getId())
.$car2_value."')";
}
else
{
$this->modificationDate = date("Y-m-d");
// Server and database URLs for this file may differ at this point, in which case we update the DB
$licenseHasChangedOnServer = $this->drivingLicense->saveToServer($resultsArray[demande::DRIVING_LICENSE_DB_FIELD]);
$licenseColumnString = $licenseHasChangedOnServer ? demande::DRIVING_LICENSE_DB_FIELD." = " : "";
$licenseValuesString = $licenseHasChangedOnServer ? "'".util::cleanup($this->drivingLicense->getOutputLocation())."', " : "";
// Server and database URLs for this file may differ at this point, in which case we update the DB
$proofOfResidenceHasChangedOnServer = $this->proofOfResidence->saveToServer($resultsArray[demande::PROOF_OF_RESIDENCE_DB_FIELD]);
$proofOfResidenceColumnString = $proofOfResidenceHasChangedOnServer ? demande::PROOF_OF_RESIDENCE_DB_FIELD." = " : "";
$proofOfResidenceValuesString = $proofOfResidenceHasChangedOnServer ? "'".util::cleanup($this->proofOfResidence->getOutputLocation())."', " : "";
if($save_car2) {
$car2_field = ",".demande::CAR2_DB_FIELD;
$car2_value = " = '".util::cleanup($this->car2->getId())."'";
}
$query = "UPDATE st_demande
SET
".demande::MODIF_DATE_DB_FIELD." = '".util::cleanup($this->modificationDate)."',
".demande::PAYMENT_METHOD_DB_FIELD." = '".util::cleanup($this->paymentMethod)."',
".$licenseColumnString.$licenseValuesString."
".$proofOfResidenceColumnString.$proofOfResidenceValuesString."
".demande::CARPOOLING_DB_FIELD." = '".util::cleanup($this->carpooling)."',
".demande::DETAILS_DB_FIELD." = '".util::cleanup($this->details)."',
".demande::CARPOOLING_OTHERS_DB_FIELD." = '".util::cleanup($this->carpoolingOthers)."',
".demande::CAR1_DB_FIELD." = '".util::cleanup($this->car1->getId())."'
".$car2_field.$car2_value."
WHERE
matricule = '".$matricule."'";
}
$this->database->requete($query);
$this->database->commitTransaction();
}
catch(Exception $e)
{
$this->database->abortTransaction();
return false;
}
return true;
}
public function refreshStatus()
{
$this->status->loadStatusData($this->matricule);
}
public function cancelDemand()
{
$this->status->cancel();
}
public function reactivateDemand()
{
$this->status->reactivate();
}
public function getDetails()
{
return $this->details;
}
public function getStatus()
{
return $this->status;
}
public function getPaymentMethod()
{
return $this->paymentMethod;
}
public function getPaymentMethodString() {
switch($this->paymentMethod) {
case DEMANDE_TYPE_COMPTANT: return "Comptant";
case DEMANDE_TYPE_CHEQUE: return "Cheque";
case DEMANDE_TYPE_MANDAT: return "Mandat";
case DEMANDE_TYPE_INTERAC: return "Interac";
case DEMANDE_TYPE_VISA: return "Visa";
case DEMANDE_TYPE_MC: return "Mastercard";
default: return "null";
}
}
public function getModificationDate() {
return $this->modificationDate;
}
public function getCreationDate() {
return $this->creationDate;
}
public function getLicense()
{
return $this->drivingLicense;
}
public function getResidenceProof()
{
return $this->proofOfResidence;
}
public function isCarpooling()
{
return $this->carpooling;
}
public function isCarpoolingOthers()
{
return $this->carpoolingOthers;
}
public function getFirstCar()
{
return $this->car1;
}
public function getSecondCar()
{
return $this->car2;
}
public function getUserData()
{
return $this->userData;
}
public function getTripInfo()
{
return $this->tripInfo;
}
public function hasExistingDemandInDB()
{
return $this->hasExistingDemandInDatabase;
}
public function isValid()
{
$isValid = true;
$isValid &= !($this->hasLicenseError());
$isValid &= !($this->hasPaymentError());
$isValid &= !($this->hasResidenceError());
$isValid &= $this->car1->isValid();
$isValid &= $this->car2->isValid();
return $isValid;
}
public function getPaymentErrorMessage()
{
return ($this->hasPaymentError() && $this->isDataFromPost) ? "Veuillez choisir une méthode de paiement": "" ;
}
public function getLicenseErrorMessage()
{
return $this->hasLicenseError() && $this->isDataFromPost ? $this->drivingLicense->getErrorMessage() : "" ;
}
public function getResidenceErrorMessage()
{
return $this->hasResidenceError() && $this->isDataFromPost ? $this->proofOfResidence->getErrorMessage() : "" ;
}
private function hasPaymentError()
{
return ($this->paymentMethod < 1 || $this->paymentMethod > 6);
}
private function hasLicenseError()
{
return (!isset($this->drivingLicense) || !$this->drivingLicense->isValid() ) ;
}
private function hasResidenceError()
{
return (!isset($this->proofOfResidence) || !$this->proofOfResidence->isValid() );
}
} /* end of class demande */
?>