-
Notifications
You must be signed in to change notification settings - Fork 21
/
student.php
131 lines (114 loc) · 4.24 KB
/
student.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
<?php
// Autoload PSR-4
spl_autoload_register();
// Imports
use \Classes\Webforce3\Config\Config;
use \Classes\Webforce3\Friendliness;
use \Classes\Webforce3\DB\Student;
use \Classes\Webforce3\DB\City;
use \Classes\Webforce3\DB\Session;
use \Classes\Webforce3\Helpers\SelectHelper;
// Get the config object
$conf = Config::getInstance();
$studentId = isset($_GET['stu_id']) ? intval($_GET['stu_id']) : 0;
$studentObject = new Student();
// Récupère la liste de sympathie pour la vue
$friendlinessList = Friendliness::getListForSelect();
// Récupère la liste complète des students en DB
$studentList = Student::getAllForSelect();
// Récupère la liste complète des cities en DB
$citiesList = City::getAllForSelect();
// Récupère la liste complète des sessions en DB
$sessionsList = Session::getAllForSelect();
// Si modification d'un student, on charge les données pour le formulaire
if ($studentId > 0) {
$studentObject = Student::get($studentId);
}
// Si lien suppression
if (isset($_GET['delete']) && intval($_GET['delete']) > 0) {
if (Student::deleteById(intval($_GET['delete']))) {
header('Location: student.php?success='.urlencode('Suppression effectuée'));
exit;
}
}
// Si formulaire soumis
if (!empty($_POST)) {
$studentId = isset($_POST['id']) ? intval($_POST['id']) : 0;
$sessionId = isset($_POST['ses_id']) ? intval($_POST['ses_id']) : 0;
$cityId = isset($_POST['cit_id']) ? intval($_POST['cit_id']) : 0;
$studentBirthdate = isset($_POST['stu_birthdate']) ? date('Y-m-d', strtotime($_POST['stu_birthdate'])) : 0;
$studentFriendliness = isset($_POST['stu_friendliness']) ? intval($_POST['stu_friendliness']) : 0;
$studentLastName = isset($_POST['stu_lname']) ? trim($_POST['stu_lname']) : '';
$studentFirstName = isset($_POST['stu_fname']) ? trim($_POST['stu_fname']) : '';
$studentEmail = isset($_POST['stu_email']) ? trim($_POST['stu_email']) : '';
if (strlen($studentBirthdate) < 10) {
$conf->addError('Birthdate non correcte');
}
if (!array_key_exists($studentFriendliness, $friendlinessList)) {
$conf->addError('Sympathie non valide');
}
if (!array_key_exists($cityId, $citiesList)) {
$conf->addError('Ville non valide');
}
if (!array_key_exists($sessionId, $sessionsList)) {
$conf->addError('Session de formation non valide');
}
if (empty($studentEmail) || filter_var($studentEmail, FILTER_VALIDATE_EMAIL) === false) {
$conf->addError('Email non valide');
}
if (empty($studentLastName)) {
$conf->addError('Veuillez renseigner le nom');
}
if (empty($studentFirstName)) {
$conf->addError('Veuillez renseigner le prénom');
}
// je remplis l'objet qui est lu pour les inputs du formulaire, ou pour l'ajout en DB
$studentObject = new Student(
$studentId,
new Session($sessionId),
new City($cityId),
$studentLastName,
$studentFirstName,
$studentEmail,
$studentBirthdate,
$studentFriendliness
);
// Si tout est ok
if (!$conf->haveError()) {
if ($studentObject->saveDB()) {
header('Location: student.php?success='.urlencode('Ajout/Modification effectuée').'&stu_id='.$studentObject->getId());
exit;
}
else {
$conf->addError('Erreur dans l\'ajout ou la modification');
}
}
}
// Instancie le générateur de menu déroulant pour la sympathie
$selectFriendliness = new SelectHelper($friendlinessList, $studentObject->getFriendliness(), array(
'name' => 'stu_friendliness',
'id' => 'stu_friendliness',
'class' => 'form-control',
));
// Instancie le générateur de menu déroulant pour la liste des étudiants
$selectStudents = new SelectHelper($studentList, $studentId, array(
'name' => 'stu_id',
'id' => 'stu_id',
'class' => 'form-control',
));
// Instancie le générateur de menu déroulant pour les trainings
$selectSessions = new SelectHelper($sessionsList, $studentObject->getSession()->getId(), array(
'name' => 'ses_id',
'id' => 'ses_id',
'class' => 'form-control',
));
// Instancie le générateur de menu déroulant pour les cities
$selectCities = new SelectHelper($citiesList, $studentObject->getCity()->getId(), array(
'name' => 'cit_id',
'id' => 'cit_id',
'class' => 'form-control',
));
// Views - toutes les variables seront automatiquement disponibles dans les vues
require $conf->getViewsDir().'header.php';
require $conf->getViewsDir().'student.php';
require $conf->getViewsDir().'footer.php';