31 |
aurelien |
1 |
<?php
|
|
|
2 |
|
92 |
aurelien |
3 |
class OdsStation extends OdsTriple {
|
31 |
aurelien |
4 |
|
|
|
5 |
/**
|
|
|
6 |
* Méthode appelée avec une requête de type GET.
|
|
|
7 |
*
|
|
|
8 |
*/
|
|
|
9 |
function getElement($param = array()) {
|
|
|
10 |
|
|
|
11 |
if(isset($param[0])) {
|
|
|
12 |
$id_participant = $param[0];
|
|
|
13 |
} else {
|
|
|
14 |
return;
|
|
|
15 |
}
|
|
|
16 |
|
|
|
17 |
if($param[1] == "*") {
|
|
|
18 |
|
|
|
19 |
$info = $this->obtenirListeStationPourParticipant($id_participant);
|
|
|
20 |
|
|
|
21 |
} else if(is_numeric($param[1])) {
|
|
|
22 |
|
|
|
23 |
$id_station = $param[1];
|
|
|
24 |
|
|
|
25 |
$info = $this->obtenirInformationsStation($id_station);
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
// Envoi sur la sortie standard
|
|
|
29 |
$this->envoyer($info);
|
|
|
30 |
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Méthode appelée pour ajouter un élément.
|
|
|
35 |
*/
|
|
|
36 |
public function createElement($params) {
|
|
|
37 |
|
105 |
aurelien |
38 |
$elements_requis = array('id_participant','station_nom', 'station_commune', 'station_lat', 'station_lon','station_milieu','station_alt');
|
31 |
aurelien |
39 |
$erreurs = array();
|
|
|
40 |
|
|
|
41 |
foreach($elements_requis as $requis) {
|
|
|
42 |
if(!isset($params[$requis])) {
|
|
|
43 |
//$erreurs[$requis] = 'erreur ';
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
if(!empty($erreurs)) {
|
|
|
48 |
$this->envoyer($erreurs);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
$id_participant = $params['id_participant'];
|
|
|
52 |
|
|
|
53 |
$requete_creation_station = 'INSERT INTO ods_stations '.
|
|
|
54 |
'(os_ce_participant, os_nom, os_ce_commune, os_latitude, os_longitude, os_altitude, os_ce_environnement, os_commentaire) '.
|
|
|
55 |
'VALUES ('.
|
33 |
aurelien |
56 |
$this->proteger($id_participant).','.
|
|
|
57 |
$this->proteger($params['station_nom']).','.
|
|
|
58 |
$this->proteger($params['station_commune']).','.
|
|
|
59 |
$this->proteger($params['station_lat']).','.
|
|
|
60 |
$this->proteger($params['station_lon']).','.
|
|
|
61 |
$this->proteger($params['station_alt']).','.
|
|
|
62 |
$this->proteger($params['station_milieu']).','.
|
|
|
63 |
$this->proteger($params['station_description']).')';
|
31 |
aurelien |
64 |
|
|
|
65 |
$creation_station = $this->executerRequeteSimple($requete_creation_station);
|
|
|
66 |
|
|
|
67 |
if(!$creation_station) {
|
46 |
aurelien |
68 |
$retour['erreurs'] = 'erreur d\'insertion';
|
|
|
69 |
} else {
|
|
|
70 |
$retour['id_nouvelle_station'] = $this->renvoyerDernierIdInsere();
|
31 |
aurelien |
71 |
}
|
|
|
72 |
|
46 |
aurelien |
73 |
$this->envoyer($retour);
|
31 |
aurelien |
74 |
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Méthode appelée pour mettre à jour un élément
|
|
|
79 |
*/
|
|
|
80 |
public function updateElement($uid, $params) {
|
|
|
81 |
|
105 |
aurelien |
82 |
$elements_requis = array('id_participant','station_nom', 'station_commune', 'station_lat', 'station_lon','station_milieu','station_alt');
|
|
|
83 |
$erreurs = array();
|
|
|
84 |
|
|
|
85 |
foreach($elements_requis as $requis) {
|
|
|
86 |
if(!isset($params[$requis])) {
|
|
|
87 |
$erreurs[$requis] = 'erreur ';
|
|
|
88 |
}
|
31 |
aurelien |
89 |
}
|
|
|
90 |
|
105 |
aurelien |
91 |
if(!empty($erreurs)) {
|
|
|
92 |
$this->envoyer($erreurs);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
$id_participant = $params['id_participant'];
|
|
|
96 |
|
|
|
97 |
if(!isset($uid[0])) {
|
|
|
98 |
return;
|
31 |
aurelien |
99 |
} else {
|
105 |
aurelien |
100 |
$id_station = $uid[0];;
|
31 |
aurelien |
101 |
}
|
|
|
102 |
|
|
|
103 |
$requete_modification_station = 'UPDATE ods_stations '.
|
|
|
104 |
'SET '.
|
33 |
aurelien |
105 |
'os_nom ='.$this->proteger($params['station_nom']).','.
|
67 |
aurelien |
106 |
'os_ce_commune ='.$this->proteger($params['station_commune']).','.
|
33 |
aurelien |
107 |
'os_latitude ='.$this->proteger($params['station_lat']).','.
|
|
|
108 |
'os_longitude ='.$this->proteger($params['station_lon']).','.
|
|
|
109 |
'os_altitude ='.$this->proteger($params['station_alt']).','.
|
|
|
110 |
'os_ce_environnement ='.$this->proteger($params['station_milieu']).','.
|
|
|
111 |
'os_commentaire ='.$this->proteger($params['station_description']).' '.
|
105 |
aurelien |
112 |
'WHERE os_ce_participant = '.$this->proteger($id_participant).' '.
|
33 |
aurelien |
113 |
'AND os_id_station = '.$this->proteger($id_station);
|
31 |
aurelien |
114 |
|
105 |
aurelien |
115 |
$modification_station = $this->executerRequeteSimple($requete_modification_station);
|
31 |
aurelien |
116 |
|
105 |
aurelien |
117 |
$retour = array();
|
|
|
118 |
|
31 |
aurelien |
119 |
if(!$modification_station) {
|
105 |
aurelien |
120 |
$retour['erreurs'] = 'Erreur lors de la modification de la station';
|
|
|
121 |
} else {
|
|
|
122 |
$retour['reponse'] = 'OK';
|
31 |
aurelien |
123 |
}
|
105 |
aurelien |
124 |
|
|
|
125 |
$this->envoyer($retour);
|
31 |
aurelien |
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Méthode appelée pour supprimer un élément
|
|
|
130 |
*/
|
|
|
131 |
public function deleteElement($uid) {
|
|
|
132 |
|
|
|
133 |
// Pour le moment, pas de suppression des stations
|
|
|
134 |
return ;
|
|
|
135 |
|
|
|
136 |
if(!isset($uid[0])) {
|
|
|
137 |
$id_participant = $uid[0];
|
|
|
138 |
} else {
|
|
|
139 |
return;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
if(!isset($uid[1])) {
|
|
|
143 |
$id_station = $uid[1];
|
|
|
144 |
} else {
|
|
|
145 |
return;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
$requete_suppression_station = 'DELETE FROM ods_stations '.
|
33 |
aurelien |
149 |
'WHERE os_ce_particant = '.$this->proteger($id_participant).' '.
|
|
|
150 |
'AND os_id_station = '.$this->proteger($id_station);
|
31 |
aurelien |
151 |
|
|
|
152 |
// TODO : supprimer également tout ce qui est associé à la station (observations, etc...)
|
|
|
153 |
$suppression_station = $this->executerRequeteSimple($requete_suppression_station);
|
|
|
154 |
|
|
|
155 |
if(!$suppression_station) {
|
|
|
156 |
// TODO: comment gère t'on les erreurs ?
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
$this->envoyer();
|
|
|
160 |
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
// +---------------------------------------------------------------------------------------------------------------+
|
|
|
165 |
// METHODES D'ACCES A LA BASE DE DONNEES
|
|
|
166 |
private function obtenirListeStationPourParticipant($id_participant) {
|
|
|
167 |
|
33 |
aurelien |
168 |
$requete_liste_station = 'SELECT * FROM ods_stations WHERE os_ce_participant = '.$this->proteger($id_participant);
|
31 |
aurelien |
169 |
|
|
|
170 |
$liste_station = $this->executerRequete($requete_liste_station);
|
|
|
171 |
|
|
|
172 |
$liste_station_formatees = array();
|
|
|
173 |
|
|
|
174 |
foreach($liste_station as $indice => $station) {
|
|
|
175 |
|
|
|
176 |
$station_champs_formates = $this->formaterChampsStationPourEnvoi($station);
|
|
|
177 |
$liste_station_formatees[$station['os_id_station']] = $station_champs_formates;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
return $liste_station_formatees;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
private function obtenirInformationsStation($id_station) {
|
|
|
184 |
|
33 |
aurelien |
185 |
$requete_infos_station = 'SELECT * FROM ods_stations WHERE os_id_station = '.$this->proteger($id_station);
|
31 |
aurelien |
186 |
|
|
|
187 |
$infos_station = $this->executerRequete($requete_infos_station);
|
|
|
188 |
|
|
|
189 |
$infos_station_formatees = array();
|
|
|
190 |
|
|
|
191 |
if(!empty($infos_station)) {
|
|
|
192 |
$infos_station = $infos_station[0];
|
|
|
193 |
$infos_station_formatees = $this->formaterChampsStationPourEnvoi($infos_station);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
return $infos_station_formatees;
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
private function formaterChampsStationPourEnvoi($station) {
|
|
|
200 |
|
|
|
201 |
$station_champs_formates = array(
|
|
|
202 |
'id' => $station['os_id_station'],
|
|
|
203 |
'nom' => $station['os_nom'],
|
|
|
204 |
'id_commune' => $station['os_ce_commune'],
|
67 |
aurelien |
205 |
'commune' => $this->obtenirInformationsCommuneParCodeInsee($station['os_ce_commune']),
|
92 |
aurelien |
206 |
'id_milieu' => $station['os_ce_environnement'],
|
|
|
207 |
'milieu' => $this->obtenirInformationsMilieuParId($station['os_ce_environnement']),
|
31 |
aurelien |
208 |
'latitude' => $station['os_latitude'],
|
|
|
209 |
'longitude' => $station['os_longitude'],
|
|
|
210 |
'altitude' => $station['os_altitude'],
|
|
|
211 |
'description' => $station['os_commentaire']
|
|
|
212 |
);
|
|
|
213 |
|
|
|
214 |
return $station_champs_formates;
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
private function obtenirInformationsCommuneParId($id_commune) {
|
|
|
218 |
|
67 |
aurelien |
219 |
//$requete_infos_commune = 'SELECT * FROM ods_communes WHERE oc_id_commune = '.$this->proteger($id_commune);
|
|
|
220 |
//$infos_commune = $this->executerRequete($requete_infos_commune);
|
31 |
aurelien |
221 |
|
105 |
aurelien |
222 |
//TODO: en attendant de stocker les ids
|
|
|
223 |
return $id_commune;
|
67 |
aurelien |
224 |
|
105 |
aurelien |
225 |
//return $infos_commune;
|
31 |
aurelien |
226 |
}
|
|
|
227 |
|
|
|
228 |
private function obtenirInformationsCommuneParCodeInsee($code_insee_commune) {
|
|
|
229 |
|
67 |
aurelien |
230 |
//$requete_infos_commune = 'SELECT * FROM ods_communes WHERE oc_code_insee = '.$this->proteger($code_insee_commune);
|
|
|
231 |
//$infos_commune = $this->executerRequete($requete_infos_commune);
|
31 |
aurelien |
232 |
|
105 |
aurelien |
233 |
//TODO: en attendant de stocker les ids
|
|
|
234 |
return $code_insee_commune;
|
67 |
aurelien |
235 |
|
105 |
aurelien |
236 |
//return $infos_commune;
|
31 |
aurelien |
237 |
}
|
|
|
238 |
|
92 |
aurelien |
239 |
private function obtenirInformationsMilieuParId($id_milieu) {
|
31 |
aurelien |
240 |
|
92 |
aurelien |
241 |
$informations_milieu = $this->obtenirValeurTripleParId($id_milieu);
|
31 |
aurelien |
242 |
return $informations_milieu;
|
|
|
243 |
|
|
|
244 |
}
|
|
|
245 |
}
|
|
|
246 |
?>
|