db-functions.php 41.2 KB
Newer Older
Gabriel Moreau's avatar
Gabriel Moreau committed
1
<?php if (!$web_page) exit() ?>
2

Gabriel Moreau's avatar
Gabriel Moreau committed
3
<?php
4
require_once('base-functions.php');
5
6
require_once('connect.php');

Gabriel Moreau's avatar
Gabriel Moreau committed
7
// ---------------------------------------------------------------------
8

9
10
11
12
define('GESTEX_DB_VERSION', 4);

// ---------------------------------------------------------------------

Gabriel Moreau's avatar
Gabriel Moreau committed
13
// connexion au serveur mySQL
14

15
function connect_db_minimal() {
16
17
18
19
20
21
22
23
	try{
		$pdo = new PDO('mysql:host='.GESTEX_DB_SERVER.'; dbname='.GESTEX_DB_DATABASE, GESTEX_DB_USER, GESTEX_DB_PASSWORD);
	}
	catch(PDOException $exception){
		error_log('Connection error: '.$exception->getMessage());
		echo $exception->getMessage();
		return false;
	}
24

25
26
27
28
29
30
31
32
33
34
35
36
	return $pdo;
}

// ---------------------------------------------------------------------

function connect_db() {
	if ($pdo = connect_db_minimal()) {
		try{
			$datasheet_version = get_version_by_name($pdo, 'datasheet');
			if ($datasheet_version < GESTEX_DB_VERSION) {
				error_log('Database version error: update the database schema');
				echo "Mettre à jour le schéma de la base de données";
37
				return false;
38
39
40
41
42
43
			}
		}
		catch(PDOException $exception){
			error_log('Database version error: '.$exception->getMessage());
			echo $exception->getMessage();
			return false;
44
45
46
		}
	}

47
48
49
	return $pdo;
}

Gabriel Moreau's avatar
Gabriel Moreau committed
50
// ---------------------------------------------------------------------
51

52
53
54
55
56
57
58
59
60
61
function connect_db_or_alert() {
	if ($pdo = connect_db())
		return $pdo;

	include_once('include/alert-db.php');
	exit();
}

// ---------------------------------------------------------------------

62
63
64
65
66
67
function query_db($statement) {
	$result   = mysql_query($statement) or die("<pre>\n\nCan't perform query: " . mysql_error() . " \n\n$statement\n\n</pre>");
	$num_rows = numrows_db($result);
	return array($result, $num_rows);
}

Gabriel Moreau's avatar
Gabriel Moreau committed
68
// ---------------------------------------------------------------------
69
70
71
72
73

function numrows_db($result) {
	return @mysql_num_rows($result);
}

Gabriel Moreau's avatar
Gabriel Moreau committed
74
// ---------------------------------------------------------------------
75
76
77
78
79
80
81
82

function result_db($result,$i=-1) {
	if ($i >= 0) {
		@mysql_data_seek($result,$i);
	}
	return mysql_fetch_array($result);
}

Gabriel Moreau's avatar
Gabriel Moreau committed
83
// ---------------------------------------------------------------------
84
85
86
87
88

function last_id_db() {
	return mysql_insert_id();
}

Gabriel Moreau's avatar
Gabriel Moreau committed
89
// ---------------------------------------------------------------------
90
91
92
93

function check_val_in_db($pdo, $table, $col, $value) {
	//teste l'existence de $value dans le champ $col de la table $table
	//echo "check in:".$table.":".$col." for ".$value."<br />";
94
95
96
	//$sql = 'SELECT * FROM ? WHERE ? = ?;';
	$stmt = $pdo->prepare("SELECT * FROM $table WHERE $col = '$value'");
	$stmt->execute();
97
98
99
100
101
102
103
104
105
106
	$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

	///echo "check_val:".numrows_db($reponse)."<br />";
	//renvoie 0 si non trouve
	//renvoie le nbre d'occurences autrement
	if (count($result) > 0)
		return true;
	return false;
}

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
// ---------------------------------------------------------------------
// Category
// ---------------------------------------------------------------------

function get_category_by_id($pdo, $id) {
	$sql = 'SELECT id, nom FROM categorie WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
	$category_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $category_fetch[0];
}

// ---------------------------------------------------------------------

function get_category_listshort($pdo) {
	$sql = 'SELECT id, nom FROM categorie ORDER BY nom;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute();
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch;
}

// ---------------------------------------------------------------------

function check_category_by_name($pdo, $name) {
	$sql = 'SELECT COUNT(*) as count FROM categorie WHERE nom = ?';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($name));
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch[0]['count'] > 0 ? true : false;
}

// ---------------------------------------------------------------------

function set_category_new($pdo, $name) {
	$sql = 'INSERT INTO categorie (nom) VALUE (?);';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($name));
	return $pdo->lastInsertId();
}

// ---------------------------------------------------------------------

Gabriel Moreau's avatar
Gabriel Moreau committed
150
function set_category_update($pdo, $category_id, $name) {
151
152
	$sql = 'UPDATE categorie SET nom = ? WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
153
	$stmt->execute(array($name, $category_id));
154
155
156
157
158
159
160
}

// ---------------------------------------------------------------------

function del_category_by_id($pdo, $id) {
	$sql = 'DELETE LOW_PRIORITY FROM categorie WHERE id = ? LIMIT 1';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
161
162
	$iostat = $stmt->execute(array($id));
	return $iostat;
163
164
}

165
166
// ---------------------------------------------------------------------
// Datasheet
Gabriel Moreau's avatar
Gabriel Moreau committed
167
// ---------------------------------------------------------------------
168

Gabriel Moreau's avatar
Gabriel Moreau committed
169
function get_datasheet_basepath() {
170
171
172
173
174
	return './data/datasheet';
}

// ---------------------------------------------------------------------

175
176
177
178
179
180
181
182
183
184
185
186
function get_datasheet_all_by_id($pdo, $id) {
	$sql = 'SELECT * FROM datasheet WHERE id = ?;' ;
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
}

// ---------------------------------------------------------------------

Gabriel Moreau's avatar
Gabriel Moreau committed
187
function get_datasheet_listall_by_equipment($pdo, $equipment_id) {
188
189
	$sql = 'SELECT * FROM datasheet WHERE id_equipment = ?;' ;
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
190
	$stmt->execute(array($equipment_id));
191
192
193
194
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch;
}

Gabriel Moreau's avatar
Gabriel Moreau committed
195
// ---------------------------------------------------------------------
196

Estéban Ristich's avatar
Estéban Ristich committed
197
198
199
200
201
/**
 * Récupère le nombre de notice pour un équipement donné
 * 
 * @return int
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
202
function get_datasheet_count_by_equipment($pdo, $equipment_id) {
203
204
	$sql = 'SELECT COUNT(*) as count FROM datasheet WHERE id_equipment = ?;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
205
	$stmt->execute(array($equipment_id));
206
207
208
209
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch[0]['count'];
}

210
211
// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
212
213
214
/**
 * Ajoute ou met à jour une notice
 */
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
function set_datasheet_new($pdo, $equipment_id, $file_field_name) {
	$datasheet_filename_upload = $_FILES[$file_field_name]['name'];
	$datasheet_tmp_file        = $_FILES[$file_field_name]['tmp_name'];
	$datasheet_io_error        = $_FILES[$file_field_name]['error'];

	$file_upload_errors = array(
		0 => 'There is no error, the file uploaded with success',
		1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
		2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
		3 => 'The uploaded file was only partially uploaded',
		4 => 'No file was uploaded',
		6 => 'Missing a temporary folder',
		7 => 'Failed to write file to disk.',
		8 => 'A PHP extension stopped the file upload.',
	);
	if (!is_uploaded_file($datasheet_tmp_file) or $datasheet_io_error > 0) {
		error_log('Error: not uploaded datasheet file - '.$datasheet_filename_upload.' - '.$file_upload_errors[$datasheet_io_error]);
232
		return false;
233
234
235
236
237
238
	}

	if (!preg_match('/\.pdf$/i', $datasheet_filename_upload)) {
		error_log('Error: datasheet file not a pdf - '.$datasheet_filename_upload);
		return false;
	}
239
240
241
242
243

	$new_datasheet_path = './data/datasheet';
	if (!is_dir($new_datasheet_path))
		mkdir($new_datasheet_path, 0755);

Gabriel Moreau's avatar
Gabriel Moreau committed
244
	$datasheet_filename_no_ext = pathinfo($datasheet_filename_upload, PATHINFO_FILENAME);
Gabriel Moreau's avatar
Gabriel Moreau committed
245
	$datasheet_filename_kebab = string_to_filename_kebab($datasheet_filename_no_ext).'.pdf';
246
247
248

	$sql1 = 'INSERT INTO datasheet (description, id_equipment) VALUES (?, ?);';
	$stmt1 = $pdo->prepare($sql1);
Gabriel Moreau's avatar
Gabriel Moreau committed
249
	$stmt1->execute(array($datasheet_filename_no_ext, $equipment_id));
Gabriel Moreau's avatar
Gabriel Moreau committed
250
	$datasheet_id = $pdo->lastInsertId();
251

Gabriel Moreau's avatar
Gabriel Moreau committed
252
	$sub_path = $datasheet_id.'-'.random_string(8);
253
254
	$sql2 = 'UPDATE datasheet SET pathname = ? WHERE id = ?;';
	$stmt2 = $pdo->prepare($sql2);
Gabriel Moreau's avatar
Gabriel Moreau committed
255
	$stmt2->execute(array($sub_path.'/'.$datasheet_filename_kebab, $datasheet_id));
256
257
258
259

	$new_dir = $new_datasheet_path.'/'.$sub_path;
	if (!is_dir($new_dir))
		mkdir($new_dir, 0755);
260

Gabriel Moreau's avatar
Gabriel Moreau committed
261
262
	$iostat = move_uploaded_file($datasheet_tmp_file, $new_dir.'/'.$datasheet_filename_kebab);
	if (!$iostat) {
263
		error_log('Error: not move datasheet file '.$datasheet_filename_upload.' to '.$datasheet_filename_kebab);
Gabriel Moreau's avatar
Gabriel Moreau committed
264
		del_datasheet_by_id($pdo, $datasheet_id);
265
266
		return false;
	}
267

Gabriel Moreau's avatar
Gabriel Moreau committed
268
	return $datasheet_id;
269
270
}

271
272
// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
273
274
275
276
/**
 * Supprime la notice via son ID ainsi que son fichier et son dossier 
 * sur le disque s'ils éxistent
 */
277
function del_datasheet_by_id($pdo, $id) {
278
279
280
281
282
283
284
	$datasheet_selected = get_datasheet_all_by_id($pdo, $id);

	$datasheet_basepath = get_datasheet_basepath();
	$datasheet_pathname = $datasheet_selected['pathname'];
	$datasheet_dirname  = pathinfo($datasheet_pathname, PATHINFO_DIRNAME);

	if (is_file($datasheet_basepath.'/'.$datasheet_pathname))
Gabriel Moreau's avatar
Gabriel Moreau committed
285
		$iostat = unlink($datasheet_basepath.'/'.$datasheet_pathname);
286
287

	if (!empty($datasheet_dirname) and is_dir($datasheet_basepath.'/'.$datasheet_dirname))
Gabriel Moreau's avatar
Gabriel Moreau committed
288
		$iostat = rmdir($datasheet_basepath.'/'.$datasheet_dirname);
289
290
291

	$sql = 'DELETE LOW_PRIORITY FROM datasheet WHERE id = ? LIMIT 1;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
292
293
	$iostat = $stmt->execute(array($id));
	return $iostat;
294
295
}

296
297
// ---------------------------------------------------------------------
// Equipment
Gabriel Moreau's avatar
Gabriel Moreau committed
298
// ---------------------------------------------------------------------
299

Estéban Ristich's avatar
Estéban Ristich committed
300
301
302
303
304
/**
 * Recupère l'ID et le nom d'un équipement par son ID
 * 
 * @return false|array Le contenu d'un équipement directement
 */
305
function get_equipment_by_id($pdo, $id) {
306
307
308
	$sql = 'SELECT id, nom FROM Listing WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
309
310
311
312
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
313
314
}

Gabriel Moreau's avatar
Gabriel Moreau committed
315
// ---------------------------------------------------------------------
316

Estéban Ristich's avatar
Estéban Ristich committed
317
318
319
320
321
/**
 * Récupère tout le contenu d'un équipement par son ID
 * 
 * @return false|array Le contenu d'un équipement directement
 */
322
function get_equipment_all_by_id($pdo, $id) {
323
324
325
	$sql = 'SELECT * FROM Listing WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
326
327
328
329
330
331
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
}

Gabriel Moreau's avatar
Gabriel Moreau committed
332
// ---------------------------------------------------------------------
333

Estéban Ristich's avatar
Estéban Ristich committed
334
335
336
337
338
339
/**
 * Recupere tout le contenu d'un équipement ainsi que son nom de categorie
 * rangé par nom d'équipement et nom d'équipe
 * 
 * @return array
 */
340
function get_equipment_listall($pdo) {
341
342
	// $sql = 'SELECT * FROM Listing ORDER BY categorie, nom;';
	$sql = 'SELECT DISTINCT e.*, c.nom AS category_name FROM Listing AS e INNER JOIN categorie AS c ON e.categorie = c.id ORDER BY c.nom, e.nom;';
343
344
345
346
347
348
349
350
	$stmt = $pdo->prepare($sql);
	$stmt->execute();
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch;
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
351
/**
Estéban Ristich's avatar
Estéban Ristich committed
352
353
 * Recupere tout le contenu des équipements ainsi que leurs nom d'équipe via 
 * l'ID d'équipe, rangé par nom d'équipement et nom d'équipe
Estéban Ristich's avatar
Estéban Ristich committed
354
355
356
 * 
 * @return array
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
357
function get_equipment_listall_by_team($pdo, $team_id) {
358
359
	//$sql = 'SELECT * FROM Listing WHERE equipe = ? ORDER BY categorie, nom;';
	$sql = 'SELECT DISTINCT e.*, c.nom AS category_name FROM Listing AS e INNER JOIN categorie AS c ON e.categorie = c.id WHERE e.equipe = ? ORDER BY c.nom, e.nom;';
360
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
361
	$stmt->execute(array($team_id));
362
363
364
365
366
367
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch;
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
368
369
370
371
372
373
/**
 * Recupere le nombre d'équipement empruntable avec la même équipe via 
 * l'ID de l'équipe
 * 
 * @return int
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
374
function get_equipment_count_loanable_by_team($pdo, $team_id) {
375
376
	$sql = 'SELECT COUNT(*) as count FROM Listing AS e WHERE e.loanable = 1 and e.equipe = ?;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
377
	$stmt->execute(array($team_id));
378
379
380
381
382
383
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch[0]['count'];
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
384
385
386
387
388
389
/**
 * Recupere tout le contenu des equipements ayant la même 
 * categorie, rangé par nom
 * 
 * @return array
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
390
function get_equipment_listall_by_category($pdo, $category_id) {
391
	$sql = 'SELECT * FROM Listing WHERE categorie = ? ORDER BY nom;';
392
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
393
	$stmt->execute(array($category_id));
394
395
396
397
398
399
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch;
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
400
401
402
403
404
405
/**
 * Recupere l'ID et le nom de tout les equipements rangé par
 * catégorie et nom
 * 
 * @return array
 */
406
function get_equipment_listshort($pdo) {
407
	$sql = 'SELECT id, nom FROM Listing ORDER BY categorie, nom;';
408
409
	$stmt = $pdo->prepare($sql);
	$stmt->execute();
410
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
411
	return $result_fetch;
412
413
}

Gabriel Moreau's avatar
Gabriel Moreau committed
414
// ---------------------------------------------------------------------
415

Estéban Ristich's avatar
Estéban Ristich committed
416
417
418
419
420
421
/**
 * Récupere l'ID d'un équipement depuis la table pret, 
 * de par l'ID du pret
 * 
 * @return ID de l'equipement
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
422
function get_equipment_by_loan_id($pdo, $loan_id) {
423
424
	$sql = 'SELECT nom FROM pret WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
425
	$stmt->execute(array($loan_id));
426
427
428
429
430
431
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch[0]["nom"];
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
432
433
434
435
436
437
/**
 * Créer un équipement
 * 
 * @return array|string Renvoie une chaine si echec sinon 
 * un tableau avec l'ID et un msg d'erreur eventuellement
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
438
function set_equipment_new($pdo, $categorie, $nom, $modele, $feature, $equipe, $fourn, $achat, $tech, $reparation, $accessoires, $inventaire, $notice, $barcode, $loanable) {
Gabriel Moreau's avatar
Gabriel Moreau committed
439
	$sql = 'INSERT INTO Listing (categorie, nom, modele, gamme, equipe, fournisseur, achat, responsable, reparation, accessoires, inventaire, notice, barcode, loanable)';
440
	$sql .=            ' VALUES (?,         ?,   ?,      ?,     ?,      ?,           ?,     ?,           ?,          ?,           ?,          ?,      ?,       ?);';
441
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
442
	$iostat = $stmt->execute(array($categorie, $nom, $modele, $feature, $equipe, $fourn, $achat, $tech, $reparation, $accessoires, $inventaire, $notice, $barcode, $loanable));
443
	$err_msg = '';
Gabriel Moreau's avatar
Gabriel Moreau committed
444
	if (!$iostat)
Gabriel Moreau's avatar
Gabriel Moreau committed
445
		$err_msg = $stmt->errorInfo()[2];
446
	return array($pdo->lastInsertId(), $err_msg);
447
448
449
450
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
451
452
453
454
455
456
/**
 * Met à jour un équipement par son ID
 *
 * @return string Renvoie une chaine vide
 * si réussite sinon une chaine d'erreurs
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
457
function set_equipment_update($pdo, $equipment_id, $categorie, $nom, $modele, $feature, $equipe, $fourn, $achat, $tech, $reparation, $accessoires, $inventaire, $notice, $barcode, $loanable) {
458
459
	$sql = 'UPDATE Listing SET categorie = ?, nom = ?, modele = ?, gamme = ?, equipe = ?, fournisseur = ?, achat = ?, responsable = ?, reparation = ?, accessoires = ?, inventaire = ?, notice = ?, barcode = ?, loanable = ? WHERE id = ?;)';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
460
	$iostat = $stmt->execute(array($categorie, $nom, $modele, $feature, $equipe, $fourn, $achat, $tech, $reparation, $accessoires, $inventaire, $notice, $barcode, $loanable, $equipment_id));
461
	$err_msg = '';
Gabriel Moreau's avatar
Gabriel Moreau committed
462
	if (!$iostat)
463
464
465
466
467
468
		$err_msg = $stmt->errorInfo()[2];
	return $err_msg;
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
469
/**
Estéban Ristich's avatar
Estéban Ristich committed
470
 * Supprime un seul équipement par son ID
Estéban Ristich's avatar
Estéban Ristich committed
471
 */
472
function del_equipment_by_id($pdo, $id) {
473
474
	$sql = 'DELETE LOW_PRIORITY FROM Listing WHERE id = ? LIMIT 1;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
475
476
	$iostat = $stmt->execute(array($id));
	return $iostat;
477
478
479
480
}

// ---------------------------------------------------------------------
// Loan
481
482
// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
483
484
485
486
487
488
/**
 * Récupère tout le contenu d'un pret par son ID
 * 
 * @return array|false S'il n'est pas "false", le 
 * retour ne sera qu'un seul objet
 */
489
490
491
492
493
494
495
496
497
498
function get_loan_all_by_id($pdo, $id) {
	$sql = 'SELECT * FROM pret WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
}

499
500
// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
501
502
503
504
505
/**
 * Récupere tout les prets liés à un équipement qui ne sont pas retourné
 * 
 * @deprecated Remplacé par la fonction get_loan_active_listall_by_equipment()
 */
506
function get_loans_by_equipment($pdo, $equipment_id) {
507
	$sql = 'SELECT * FROM pret WHERE nom = ? AND NOT status = ?;';
508
	$stmt = $pdo->prepare($sql);
509
	$stmt->execute(array($equipment_id, STATUS_LOAN_RETURNED));
510
511
512
513
514
515
516
517
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch;
	return false;
}

// ---------------------------------------------------------------------

518
519
520
521
/**
* Récupere tout les prets liés à un équipement qui ne sont pas retourné,
* rangé dans un certaine ordre de priorité.
*/
Gabriel Moreau's avatar
Gabriel Moreau committed
522
function get_loan_active_listall_by_equipment($pdo, $equipment_id) {
523
	$sql = 'SELECT * FROM pret WHERE nom = ? AND status != ? ORDER BY status DESC, emprunt ASC, retour ASC;';
524
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
525
	$stmt->execute(array($equipment_id, STATUS_LOAN_RETURNED));
526
527
528
529
530
531
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch;
	return false;
}

532
533
// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
534
535
536
537
538
539
/**
 * Recupère l'ID et le status d'un pret via l'ID d'équipement
 * 
 * @return false|array Le contenu du pret directement
 * @deprecated
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
540
function get_loan_short_by_id_equipment($pdo, $equipment_id) {
Gabriel Moreau's avatar
Gabriel Moreau committed
541
	// recupere l'appareil via l'id qui est mis dans un champs texte (nom) !
Estéban Ristich's avatar
Estéban Ristich committed
542
	$sql = 'SELECT id, status FROM pret WHERE nom = ?;';
Gabriel Moreau's avatar
Gabriel Moreau committed
543
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
544
	$stmt->execute(array($equipment_id));
Gabriel Moreau's avatar
Gabriel Moreau committed
545
546
547
548
549
550
551
552
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
553
554
555
556
557
/**
 * Recupère tout le contenu d'un pret via l'ID d'équipement
 * 
 * @return false|array Le contenu du pret directement
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
558
function get_loan_all_by_id_equipment($pdo, $equipment_id) {
559
560
	// recupere l'appareil via l'id qui est mis dans un champs texte (nom) !
	$sql = 'SELECT * FROM pret WHERE nom = ?;';
Gabriel Moreau's avatar
Gabriel Moreau committed
561
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
562
	$stmt->execute(array($equipment_id));
Gabriel Moreau's avatar
Gabriel Moreau committed
563
564
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
565
		return $result_fetch;
Gabriel Moreau's avatar
Gabriel Moreau committed
566
567
568
569
570
	return false;
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
571
572
573
/**
 * @todo effectuer la documentation de la jointure
 */
574
function get_loan_listall($pdo) {
575
	//$sql = 'SELECT * FROM pret;';
576
	$sql = 'SELECT DISTINCT l.*, e.nom AS equipment_name FROM pret AS l INNER JOIN Listing AS e ON l.nom = e.id WHERE status = ? ORDER BY l.retour DESC, l.emprunt DESC, e.nom;';
577
	$stmt = $pdo->prepare($sql);
578
	$stmt->execute(array(STATUS_LOAN_BORROWED));
579
580
581
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch;
}
Gabriel Moreau's avatar
Gabriel Moreau committed
582
583
// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
584
585
586
/**
 * @todo effectuer la documentation de la jointure
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
587
function get_loan_listall_by_team($pdo, $team_id) {
588
	$sql = 'SELECT DISTINCT l.*, e.nom AS equipment_name FROM pret AS l INNER JOIN Listing AS e ON l.nom = e.id WHERE l.equipe = ? AND status = ? ORDER BY l.retour DESC, l.emprunt DESC, e.nom;';
Gabriel Moreau's avatar
Gabriel Moreau committed
589
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
590
	$stmt->execute(array($team_id, STATUS_LOAN_BORROWED));
Gabriel Moreau's avatar
Gabriel Moreau committed
591
592
593
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch;
}
594
595
596

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
597
598
599
600
601
/**
 * Récupère le nombre de pret d'une équipe correspondante
 * 
 * @return int
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
602
function get_loan_count_by_team($pdo, $team_id) {
603
604
	$sql = 'SELECT COUNT(*) as count FROM pret AS l INNER JOIN Listing AS e ON l.nom = e.id WHERE e.equipe = ?;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
605
	$stmt->execute(array($team_id));
606
607
608
609
610
611
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch[0]['count'];
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
612
613
614
615
616
617
618
/**
 * Récupère tout le contenu des prets d'un emprunteur en utilisant
 * la directive "RLIKE" pour détécter l'utilisateur dans le champ
 * commentaire des prets
 * 
 * @return array 
 */
619
620
621
622
623
624
625
626
627
628
function get_loan_find($pdo, $find) {
	$sql = 'SELECT * FROM pret WHERE commentaire RLIKE ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($find));
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch;
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
629
630
631
632
633
634
/**
 * Récupère tout le contenu des prets qui possède le même ID d'équipement
 * et étant actuellement en emprunt
 * 
 * @return false|array
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
635
function get_loans_by_equipment_and_borrowed($pdo, $equipment_id) {
636
	$sql = 'SELECT * FROM pret WHERE nom = ? AND status = ?;';
637
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
638
	$stmt->execute(array($equipment_id, STATUS_LOAN_BORROWED));
639
640
641
642
643
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch;
	return false;
}
644

645
646
// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
647
648
/**
 * Récupère tout le contenu des prets étant dans l'intervalle d'emprunt
Estéban Ristich's avatar
Estéban Ristich committed
649
 * des dates d'un équipement spécifier. Renverra "false" s'il n'y en a pas
Estéban Ristich's avatar
Estéban Ristich committed
650
651
652
 * 
 * @return false|array
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
653
function get_loans_interval_by_id($pdo, $equipment_id, $from, $to) {
654
	$sql = 'SELECT * FROM pret WHERE ((`emprunt` <= ? AND `retour` >= ?) AND `nom` = ?) OR ((`emprunt` <= ? AND `retour` >= ?) AND `nom` = ?) OR ((`emprunt` >= ? AND `retour` <= ?) AND `nom` = ?);';
655
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
656
	$stmt->execute(array($from, $from, $equipment_id, $to, $to, $equipment_id, $from, $to, $equipment_id));
Estéban Ristich's avatar
Fix bug    
Estéban Ristich committed
657
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
658
659
660
	if (count($result_fetch) > 0)
		return $result_fetch;
	return false;
661
662
663
664
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
665
666
667
668
669
670
671
/**
 * Récupère tout le contenu des prets étant dans l'intervalle d'emprunt
 * des dates d'un équipement spécifier excepter un emprunt en particulier.
 * Renverra "false" s'il n'y en a pas
 * 
 * @return false|array
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
672
function get_loans_interval_by_id_except_loan($pdo, $equipment_id, $from, $to, $except_id) {
673
674
	$sql = 'SELECT * FROM pret WHERE (((`emprunt` <= ? AND `retour` >= ?) AND `nom` = ?) OR ((`emprunt` <= ? AND `retour` >= ?) AND `nom` = ?) OR ((`emprunt` >= ? AND `retour` <= ?) AND `nom` = ?)) AND NOT id = ?;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
675
	$stmt->execute(array($from, $from, $equipment_id, $to, $to, $equipment_id, $from, $to, $equipment_id, $except_id));
676
677
678
679
680
681
682
683
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch;
	return false;
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
684
685
686
687
688
/**
 * Récupère le status du pret via son ID
 * 
 * @return string 
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
689
function get_loan_status_by_id($pdo, $loan_id) {
690
691
	$sql = 'SELECT status FROM pret WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
692
	$stmt->execute(array($loan_id));
693
694
695
696
697
698
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch[0]["status"];
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
699
/**
700
 * Récupère tout le contenu du dernier prêt retourné
Estéban Ristich's avatar
Estéban Ristich committed
701
 * 
702
 * @todo Faire en sorte de retourner qu'un seul objet directement
Estéban Ristich's avatar
Estéban Ristich committed
703
704
 * @return false|array
 */
705
function get_loan_all_last_returned($pdo, $equipment_id) {
706
	$sql = 'SELECT * FROM pret WHERE nom = ? AND status = ? ORDER BY retour DESC LIMIT 1;';
707
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
708
	$stmt->execute(array($equipment_id, STATUS_LOAN_RETURNED));
709
710
711
712
713
714
715
716
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch;
	return false;
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
717
718
719
720
721
722
/**
 * Ajoute un nouveau pret défini comme étant actuellement en emprunt
 * 
 * @deprecated
 * @return int
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
723
function set_loan_borrowed_new($pdo, $equipment_id, $team_id, $date_begin, $date_end, $comment) {
724
	$sql = 'INSERT INTO pret (nom, equipe, emprunt, retour, commentaire, status) VALUES (?, ?, ?, ?, ?, ?);';
725
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
726
	$stmt->execute(array($equipment_id, $team_id, $date_begin, $date_end, $comment, STATUS_LOAN_BORROWED));
727
728
729
730
731
	return $pdo->lastInsertId();
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
732
733
734
735
736
737
/**
 * Ajoute un nouveau pret défini comme étant en réservation
 * 
 * @deprecated
 * @return int
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
738
function set_loan_reserved_new($pdo, $equipment_id, $team_id, $date_begin, $date_end, $comment) {
739
	$sql = 'INSERT INTO pret (nom, equipe, emprunt, retour, commentaire, status) VALUES (?, ?, ?, ?, ?, ?);';
740
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
741
	$stmt->execute(array($equipment_id, $team_id, $date_begin, $date_end, $comment, STATUS_LOAN_RESERVED));
742
	return $pdo->lastInsertId();
743
744
745
746
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
747
748
749
750
751
752
/**
 * Met à jour un emprunt (en spécifiant son ID) comme étant emprunter et bloque également
 * sa date d'emprunt au jour même de son appel
 * 
 * @deprecated
 */
753
function set_loan_update_to_borrowed($pdo, $loan_id) {
754
	$sql = 'UPDATE pret SET status = ?, emprunt = CURRENT_DATE WHERE id = ?;';
Gabriel Moreau's avatar
Gabriel Moreau committed
755
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
756
	$stmt->execute(array(STATUS_LOAN_BORROWED, $loan_id));
Gabriel Moreau's avatar
Gabriel Moreau committed
757
758
}

Gabriel Moreau's avatar
Gabriel Moreau committed
759
760
// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
761
762
763
/**
 * Met à jour le pret en spécifiant son ID sans modifier par défaut le status
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
764
function set_loan_update($pdo, $loan_id, $equipment_id, $team_id, $date_begin, $date_end, $comment) {
Gabriel Moreau's avatar
Gabriel Moreau committed
765
766
	$sql = 'UPDATE pret SET nom = ?, equipe = ?, emprunt = ?, retour = ?, commentaire = ? WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
767
	$stmt->execute(array($equipment_id, $team_id, $date_begin, $date_end, $comment, $loan_id));
Gabriel Moreau's avatar
Gabriel Moreau committed
768
769
}

Gabriel Moreau's avatar
Gabriel Moreau committed
770
771
// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
772
773
774
/**
 * Supprime un seul pret via son ID
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
775
function del_loan_by_id($pdo, $loan_id) {
Gabriel Moreau's avatar
Gabriel Moreau committed
776
777
	$sql = 'DELETE LOW_PRIORITY FROM pret WHERE id = ? LIMIT 1;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
778
	$iostat = $stmt->execute(array($loan_id));
Gabriel Moreau's avatar
Gabriel Moreau committed
779
	return $iostat;
Gabriel Moreau's avatar
Gabriel Moreau committed
780
781
}

782
783
// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
784
785
786
787
/**
 * Met à jour un pret (en spécifiant son ID) comme étant retourné et
 * bloque sa date de retour au jour de son appel
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
788
function set_loan_to_returned($pdo, $loan_id) {
789
	$sql = 'UPDATE LOW_PRIORITY pret SET status = ?, retour = CURRENT_DATE WHERE id = ?;';
790
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
791
	$iostat = $stmt->execute(array(STATUS_LOAN_RETURNED, $loan_id));
792
793
794
	return $iostat;
}

795

796
797
// ---------------------------------------------------------------------
// Supplier
Gabriel Moreau's avatar
Gabriel Moreau committed
798
799
// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
800
801
802
803
804
/**
 * Récupère l'ID et le nom d'un fournisseur via son ID
 * 
 * @return false|array Retourne directement le fournisseur
 */
Estéban Ristich's avatar
Estéban Ristich committed
805
function get_supplier_short_by_id($pdo, $id) {
806
	$sql = 'SELECT id, nom FROM fournisseurs WHERE id = ?;';
807
808
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
809
810
811
812
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
813
814
}

Gabriel Moreau's avatar
Gabriel Moreau committed
815
// ---------------------------------------------------------------------
816

Estéban Ristich's avatar
Estéban Ristich committed
817
818
819
820
821
/**
 * Récupère tout le contenu d'un seul fournisseur via son ID
 * 
 * @return false|array Retourne directement le fournisseur
 */
822
823
824
825
826
827
828
829
830
831
832
833
function get_supplier_all_by_id($pdo, $id) {
	$sql = 'SELECT * FROM fournisseurs WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
834
835
836
837
838
/**
 * Récupère l'ID et le nom de tout les fournisseurs trier par nom croissant
 * 
 * @return array
 */
839
function get_supplier_listshort($pdo) {
840
	$sql = 'SELECT id, nom FROM fournisseurs ORDER BY nom;';
841
	$stmt = $pdo->prepare($sql);
842
	$stmt->execute();
843
	$supplier_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
844
	return $supplier_fetch;
845
846
}

Gabriel Moreau's avatar
Gabriel Moreau committed
847
// ---------------------------------------------------------------------
848

Estéban Ristich's avatar
Estéban Ristich committed
849
850
851
852
853
/**
 * Récupère tout le contenu de tout les fournisseurs trier par nom croissant
 * 
 * @return array
 */
854
function get_supplier_listall($pdo) {
855
	$sql = 'SELECT * FROM fournisseurs ORDER BY nom;';
856
	$stmt = $pdo->prepare($sql);
857
	$stmt->execute();
858
	$supplier_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
859
	return $supplier_fetch;
860
861
}

Gabriel Moreau's avatar
Gabriel Moreau committed
862
// ---------------------------------------------------------------------
863

Estéban Ristich's avatar
Estéban Ristich committed
864
865
866
867
868
869
870
871
/**
 * Retourne la liste de tout les fournisseurs si la valeurs $find est "true"
 * ou une chaîne vide. Sinon elle récuperera tout le contenu des fournisseurs
 * où le chaine demandé a le plus de similitude avec le nom ou la description
 * du fournisseur
 * 
 * @return array
 */
872
873
874
875
function get_supplier_find($pdo, $find='') {
	if (empty($find) or ($find === true))
		return get_supplier_listall($pdo);
	$sql = 'SELECT * FROM fournisseurs WHERE nom RLIKE ? OR descr RLIKE ?;';
876
	$stmt = $pdo->prepare($sql);
877
	$stmt->execute(array($find, $find));
878
	$supplier_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
879
	return $supplier_fetch;
880
881
}

882
883
// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
884
885
886
887
888
/**
 * Ajoute un nouveau fournisseur
 * 
 * @return array Avec chaine d'erreur au deuxième index
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
889
890
891
function set_supplier_new($pdo, $name, $address, $tel, $fax, $email, $www, $contact, $description) {
	$sql = 'INSERT INTO fournisseurs (nom, adresse, mail, www, tel, fax, contact, descr) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
892
	$iostat = $stmt->execute(array($name, $address, $tel, $fax, $email, $www, $contact, $description));
Gabriel Moreau's avatar
Gabriel Moreau committed
893
	$err_msg = '';
Gabriel Moreau's avatar
Gabriel Moreau committed
894
	if (!$iostat)
Gabriel Moreau's avatar
Gabriel Moreau committed
895
896
897
898
899
900
		$err_msg = $stmt->errorInfo()[2];
	return array($pdo->lastInsertId(), $err_msg);
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
901
902
903
904
905
/**
 * Met à jour un fournisseur via son ID
 * 
 * @return array Avec chaine d'erreur au deuxième index
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
906
function set_supplier_update($pdo, $supplier_id, $name, $address, $tel, $fax, $email, $www, $contact, $description) {
907
908
	$sql = 'UPDATE LOW_PRIORITY fournisseurs  SET nom = ?, adresse = ?, tel = ?, fax = ?, mail = ?, www = ?, contact = ?, descr = ? WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
909
	$iostat = $stmt->execute(array($name, $address, $tel, $fax, $email, $www, $contact, $description, $supplier_id));
910
	$err_msg = '';
Gabriel Moreau's avatar
Gabriel Moreau committed
911
	if (!$iostat)
912
913
914
915
		$err_msg = $stmt->errorInfo()[2];
	return $err_msg;
}

916
// ---------------------------------------------------------------------
Gabriel Moreau's avatar
Gabriel Moreau committed
917

Estéban Ristich's avatar
Estéban Ristich committed
918
919
920
/**
 * Supprime un fournisseur via son ID
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
921
922
923
function del_supplier_by_id($pdo, $id) {
	$sql = 'DELETE LOW_PRIORITY FROM fournisseurs WHERE id = ? LIMIT 1;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
924
925
	$iostat = $stmt->execute(array($id));
	return $iostat;
Gabriel Moreau's avatar
Gabriel Moreau committed
926
927
928
}

// ---------------------------------------------------------------------
929
// Team
Gabriel Moreau's avatar
Gabriel Moreau committed
930
// ---------------------------------------------------------------------
931

Estéban Ristich's avatar
Estéban Ristich committed
932
933
934
935
936
/**
 * Récupère l'ID et le nom d'une seul équipe via son ID
 * 
 * @return array Retourne directement l'équipe
 */
937
938
function get_team_by_id($pdo, $id) {
	$sql = 'SELECT id, nom FROM equipe WHERE id = ?;';
939
940
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
941
942
943
944
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
945
946
}

Gabriel Moreau's avatar
Gabriel Moreau committed
947
// ---------------------------------------------------------------------
948

Estéban Ristich's avatar
Estéban Ristich committed
949
950
951
952
953
/**
 * Récupère tout le contenu d'une seul équipe via son ID
 * 
 * @return array Retourne directement l'équipe
 */
954
955
956
957
958
959
960
961
962
963
964
965
function get_team_all_by_id($pdo, $id) {
	$sql = 'SELECT * FROM equipe WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($id));
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if (count($result_fetch) > 0)
		return $result_fetch[0];
	return false;
}

// ---------------------------------------------------------------------

Estéban Ristich's avatar
Estéban Ristich committed
966
967
968
969
970
/**
 * Récupère l'ID et le nom de toutes les équipes par ordre de nom croissant
 * 
 * @return array
 */
971
function get_team_listshort($pdo) {
972
	$sql = 'SELECT id, nom FROM equipe ORDER BY nom;';
973
	$stmt = $pdo->prepare($sql);
974
	$stmt->execute();
975
	$team_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
976
	return $team_fetch;
977
978
}

Gabriel Moreau's avatar
Gabriel Moreau committed
979
// ---------------------------------------------------------------------
980

Estéban Ristich's avatar
Estéban Ristich committed
981
982
983
984
985
/**
 * Récupère tout le contenu de toutes les équipe par ordre de nom croissant
 * 
 * @return array
 */
986
function get_team_listall($pdo) {
987
	$sql = 'SELECT * FROM equipe ORDER BY nom;';
988
	$stmt = $pdo->prepare($sql);
989
	$stmt->execute();
990
	$team_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
991
	return $team_fetch;
992
993
}

Gabriel Moreau's avatar
Gabriel Moreau committed
994
// ---------------------------------------------------------------------
995

Estéban Ristich's avatar
Estéban Ristich committed
996
997
998
999
1000
1001
/**
 * Récupère l'ID et le nom des équipe par ordre croissant de nom,
 * possèdant au moins un équipement
 * 
 * @return array
 */
1002
function get_team_with_appareil($pdo) {
1003
	$sql = 'SELECT DISTINCT equipe.id, equipe.nom FROM equipe INNER JOIN Listing ON equipe.id = Listing.equipe ORDER BY equipe.nom;';
1004
	$stmt = $pdo->prepare($sql);
1005
	$stmt->execute();
1006
	$team_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
1007
	return $team_fetch;
1008
1009
}

Gabriel Moreau's avatar
Gabriel Moreau committed
1010
1011
// ---------------------------------------------------------------------

1012
1013
1014
1015
1016
/**
 * Récupère le nombre d'équipe directement
 * 
 * @return int
 */
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
function get_team_count($pdo) {
	$sql = 'SELECT COUNT(*) as count FROM equipe;';
	$stmt = $pdo->prepare($sql);
	$stmt->execute();
	$result_fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
	return $result_fetch[0]['count'];
}

// ---------------------------------------------------------------------

1027
1028
1029
1030
1031
/**
 * Ajoute une nouvelle équipe
 * 
 * @return array Avec potentiellement une chaine d'erreur
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
1032
1033
1034
function set_team_new($pdo, $name, $description, $account, $manager) {
	$sql = 'INSERT INTO equipe (nom, descr, compte, chef) VALUES (?,  ?, ?, ?);';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
1035
	$iostat = $stmt->execute(array($name, $description, $account, $manager));
Gabriel Moreau's avatar
Gabriel Moreau committed
1036
	$err_msg = '';
Gabriel Moreau's avatar
Gabriel Moreau committed
1037
	if (!$iostat)
Gabriel Moreau's avatar
Gabriel Moreau committed
1038
1039
		$err_msg = $stmt->errorInfo()[2];
	return array($pdo->lastInsertId(), $err_msg);
Gabriel Moreau's avatar
Gabriel Moreau committed
1040
1041
}

Gabriel Moreau's avatar
Gabriel Moreau committed
1042
1043
// ---------------------------------------------------------------------

1044
1045
1046
1047
1048
/**
 * Met à jour une équipe via son ID
 * 
 * @return array Avec potentiellement une chaine d'erreur
 */
Gabriel Moreau's avatar
Gabriel Moreau committed
1049
function set_team_update($pdo, $team_id, $name, $description, $account, $manager) {
Gabriel Moreau's avatar
Gabriel Moreau committed
1050
1051
	$sql = 'UPDATE LOW_PRIORITY equipe SET nom = ?, descr = ?, compte = ?, chef = ? WHERE id = ?;';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
1052
	$iostat = $stmt->execute(array($name, $description, $account, $manager, $team_id));
Gabriel Moreau's avatar
Gabriel Moreau committed
1053
	$err_msg = '';
Gabriel Moreau's avatar
Gabriel Moreau committed
1054
	if (!$iostat)
Gabriel Moreau's avatar
Gabriel Moreau committed
1055
1056
1057
1058
		$err_msg = $stmt->errorInfo()[2];
	return $err_msg;
}

1059
1060
// ---------------------------------------------------------------------

1061
1062
1063
/**
 * Supprime une équipe via son ID
 */
1064
1065
1066
function del_team_by_id($pdo, $id) {
	$sql = 'DELETE LOW_PRIORITY FROM equipe WHERE id = ? LIMIT 1';
	$stmt = $pdo->prepare($sql);
Gabriel Moreau's avatar
Gabriel Moreau committed
1067
1068
	$iostat = $stmt->execute(array($id));
	return $iostat;
1069
1070
}

1071
1072
// ---------------------------------------------------------------------
// User
Gabriel Moreau's avatar
Gabriel Moreau committed
1073
// ---------------------------------------------------------------------
1074