Subversion Repositories Sites.obs-saisons.fr

Rev

Rev 297 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 297 Rev 314
1
<?php
1
<?php
2
 
2
 
3
class OdsStation extends OdsTriple {
3
class OdsStation extends OdsTriple {
4
 
4
 
5
	/**
5
	/**
6
     * Méthode appelée avec une requête de type GET.
6
     * Méthode appelée avec une requête de type GET.
7
     *
7
     *
8
     */
8
     */
9
    function getElement($param = array()) {
9
    function getElement($param = array()) {
10
    	
10
    	
11
    	if(isset($param[0]) && isset($param[1])) {
11
    	if(isset($param[0]) && isset($param[1])) {
12
    		$id_participant = $param[0];
12
    		$id_participant = $param[0];
13
    	} else {
13
    	} else {
14
    		return;
14
    		return;
15
    	}
15
    	}
16
       
16
       
17
		if($param[1] == "*") { 
17
		if($param[1] == "*") { 
18
			$info = $this->obtenirListeStationPourParticipant($id_participant);
18
			$info = $this->obtenirListeStationPourParticipant($id_participant);
19
		} else if(is_numeric($param[1])) {
19
		} else if(is_numeric($param[1])) {
20
			$id_station = $param[1];
20
			$id_station = $param[1];
21
			$info = $this->obtenirInformationsStation($id_station);
21
			$info = $this->obtenirInformationsStation($id_station);
22
		}
22
		}
23
       
23
       
24
        $this->envoyer($info);
24
        $this->envoyer($info);
25
    }
25
    }
26
   
26
   
27
    /**
27
    /**
28
     * Méthode appelée pour ajouter un élément.
28
     * Méthode appelée pour ajouter un élément.
29
     */
29
     */
30
    public function createElement($params) {
30
    public function createElement($params) {
31
    	
31
    	
32
    	$elements_requis = array('id_participant','station_nom', 'station_commune', 'station_lat', 'station_lon','station_milieu','station_alt');
32
    	$elements_requis = array('id_participant','station_nom', 'station_commune', 'station_lat', 'station_lon','station_milieu','station_alt');
33
    	$erreurs = array();
33
    	$erreurs = array();
34
        
34
        
35
    	foreach($elements_requis as $requis) {
35
    	foreach($elements_requis as $requis) {
36
    		if(!isset($params[$requis])) {
36
    		if(!isset($params[$requis])) {
37
    			//$erreurs[$requis] = 'erreur ';
37
    			//$erreurs[$requis] = 'erreur ';
38
    		}
38
    		}
39
    	}
39
    	}
40
    	
40
    	
41
    	if(!empty($erreurs)) {
41
    	if(!empty($erreurs)) {
42
    		$this->envoyer($erreurs);
42
    		$this->envoyer($erreurs);
43
    	}
43
    	}
44
    	
44
    	
45
    	$id_participant = $params['id_participant'];
45
    	$id_participant = $params['id_participant'];
46
    	$commune = $this->traiterParametreCommune($params);
46
    	$commune = $this->traiterParametreCommune($params);
47
    	
47
    	
48
    	// description n'est pas obligatoire, et également pour le moment inutilisé
48
    	// description n'est pas obligatoire, et également pour le moment inutilisé
49
    	$params['station_description'] = isset($params['station_description']) ? $params['station_description'] : '';
49
    	$params['station_description'] = isset($params['station_description']) ? $params['station_description'] : '';
50
    	
50
    	
51
    	$requete_creation_station = 'INSERT INTO ods_stations '.
51
    	$requete_creation_station = 'INSERT INTO ods_stations '.
52
					'(os_ce_participant, os_nom, os_ce_commune, os_latitude, os_longitude, os_altitude, os_ce_environnement, os_commentaire) '.
52
					'(os_ce_participant, os_nom, os_ce_commune, os_latitude, os_longitude, os_altitude, os_ce_environnement, os_commentaire) '.
53
					'VALUES ('.
53
					'VALUES ('.
54
							$this->proteger($id_participant).','.
54
							$this->proteger($id_participant).','.
55
							$this->proteger($params['station_nom']).','.
55
							$this->proteger($params['station_nom']).','.
56
							$this->proteger($commune).','.
56
							$this->proteger($commune).','.
57
							$this->proteger($params['station_lat']).','.
57
							$this->proteger($params['station_lat']).','.
58
							$this->proteger($params['station_lon']).','.
58
							$this->proteger($params['station_lon']).','.
59
							$this->proteger($params['station_alt']).','.
59
							$this->proteger($params['station_alt']).','.
60
							$this->proteger($params['station_milieu']).','.
60
							$this->proteger($params['station_milieu']).','.
61
							$this->proteger($params['station_description']).')';		
61
							$this->proteger($params['station_description']).')';		
62
							
62
							
63
		$creation_station = $this->executerRequeteSimple($requete_creation_station);
63
		$creation_station = $this->executerRequeteSimple($requete_creation_station);
64
		
64
		
65
		if(!$creation_station) {
65
		if(!$creation_station) {
66
    		$retour['erreurs'] = 'erreur d\'insertion';
66
    		$retour['erreurs'] = 'erreur d\'insertion';
67
    	} else {
67
    	} else {
68
    		$retour['id_nouvelle_station'] = $this->renvoyerDernierIdInsere();
68
    		$retour['id_nouvelle_station'] = $this->renvoyerDernierIdInsere();
69
    	}
69
    	}
70
    	
70
    	
71
    	$this->envoyer($retour);
71
    	$this->envoyer($retour);
72
    	
72
    	
73
    }
73
    }
74
   
74
   
75
    /**
75
    /**
76
     * Méthode appelée pour mettre à jour un élément
76
     * Méthode appelée pour mettre à jour un élément
77
     */
77
     */
78
    public function updateElement($uid, $params)    {
78
    public function updateElement($uid, $params)    {
79
    	
79
    	
80
	    $elements_requis = array('id_participant','station_nom', 'station_commune', 'station_lat', 'station_lon','station_milieu','station_alt');
80
	    $elements_requis = array('id_participant','station_nom', 'station_commune', 'station_lat', 'station_lon','station_milieu','station_alt');
81
    	$erreurs = array();
81
    	$erreurs = array();
82
        
82
        
83
    	foreach($elements_requis as $requis) {
83
    	foreach($elements_requis as $requis) {
84
    		if(!isset($params[$requis])) {
84
    		if(!isset($params[$requis])) {
85
    			$erreurs[$requis] = 'erreur ';
85
    			$erreurs[$requis] = 'erreur ';
86
    		}
86
    		}
87
    	}
87
    	}
88
    	
88
    	
89
    	if(!empty($erreurs)) {
89
    	if(!empty($erreurs)) {
90
    		$this->envoyer($erreurs);
90
    		$this->envoyer($erreurs);
91
    	}
91
    	}
92
    	
92
    	
93
    	$id_participant = $params['id_participant'];
93
    	$id_participant = $params['id_participant'];
94
    	
94
    	
95
    	if(!isset($uid[0])) {
95
    	if(!isset($uid[0])) {
96
    		return;
96
    		return;
97
    	} else {
97
    	} else {
98
    		$id_station = $uid[0];
98
    		$id_station = $uid[0];
99
    	}
99
    	}
100
    	
100
    	
101
    	$commune = $this->traiterParametreCommune($params);
101
    	$commune = $this->traiterParametreCommune($params);
-
 
102
    	$params['station_description'] = empty($params['station_description']) ? '' : $params['station_description'];
102
    	
103
    	
103
        $requete_modification_station = 'UPDATE ods_stations '.
104
        $requete_modification_station = 'UPDATE ods_stations '.
104
			'SET '.
105
			'SET '.
105
			'os_nom ='.$this->proteger($params['station_nom']).','.
106
			'os_nom ='.$this->proteger($params['station_nom']).','.
106
			'os_ce_commune ='.$this->proteger($commune).','.
107
			'os_ce_commune ='.$this->proteger($commune).','.
107
			'os_latitude ='.$this->proteger($params['station_lat']).','.
108
			'os_latitude ='.$this->proteger($params['station_lat']).','.
108
			'os_longitude ='.$this->proteger($params['station_lon']).','.
109
			'os_longitude ='.$this->proteger($params['station_lon']).','.
109
			'os_altitude ='.$this->proteger($params['station_alt']).','.
110
			'os_altitude ='.$this->proteger($params['station_alt']).','.
110
			'os_ce_environnement ='.$this->proteger($params['station_milieu']).','.
111
			'os_ce_environnement ='.$this->proteger($params['station_milieu']).','.
111
			'os_commentaire ='.$this->proteger($params['station_description']).' '.
112
			'os_commentaire ='.$this->proteger($params['station_description']).' '.
112
			'WHERE os_ce_participant = '.$this->proteger($id_participant).' '.
113
			'WHERE os_ce_participant = '.$this->proteger($id_participant).' '.
113
        	'AND os_id_station = '.$this->proteger($id_station);
114
        	'AND os_id_station = '.$this->proteger($id_station);
114
							
115
							
115
		$modification_station = $this->executerRequeteSimple($requete_modification_station);
116
		$modification_station = $this->executerRequeteSimple($requete_modification_station);
116
		
117
		
117
		$retour = array();
118
		$retour = array();
118
		
119
		
119
		if(!$modification_station) {
120
		if(!$modification_station) {
120
    		$retour['erreurs'] = 'Erreur lors de la modification de la station';
121
    		$retour['erreurs'] = 'Erreur lors de la modification de la station';
121
    	} else {
122
    	} else {
122
    		$retour['reponse'] = 'OK';
123
    		$retour['reponse'] = 'OK';
123
    	}
124
    	}
124
 
125
 
125
    	$this->envoyer($retour);
126
    	$this->envoyer($retour);
126
    }
127
    }
127
    
128
    
128
    private function traiterParametreCommune($params) { 
129
    private function traiterParametreCommune($params) { 
129
    	// Si on a pas de code INSEE on mettra la valeur brute que 
130
    	// Si on a pas de code INSEE on mettra la valeur brute que 
130
    	// l'utilisateur a saisi afin de ne pas perdre d'infos
131
    	// l'utilisateur a saisi afin de ne pas perdre d'infos
131
    	$commune = $params['station_commune'];
132
    	$commune = $params['station_commune'];
132
    	// Si un code INSEE semble présent
133
    	// Si un code INSEE semble présent
133
    	if(isset($params['station_code_insee']) && trim($params['station_code_insee']) != '') {
134
    	if(isset($params['station_code_insee']) && trim($params['station_code_insee']) != '') {
134
    		// Si c'est le cas affectation directe
135
    		// Si c'est le cas affectation directe
135
    		if($this->estUnCodeInsee($params)) {
136
    		if($this->estUnCodeInsee($params)) {
136
    			$commune = $params['station_code_insee'];
137
    			$commune = $params['station_code_insee'];
137
    			// Cas du code saisi sans le 0 de gauche
138
    			// Cas du code saisi sans le 0 de gauche
138
    			$commune = (strlen($commune) == 4) ? '0'.$commune : $commune;
139
    			$commune = (strlen($commune) == 4) ? '0'.$commune : $commune;
139
    		} elseif($this->estUnCommunePlusDepartement($params)) {
140
    		} elseif($this->estUnCommunePlusDepartement($params)) {
140
    			// Cas du département sur un ou deux chiffres + une commune
141
    			// Cas du département sur un ou deux chiffres + une commune
141
    			// tentative de retrouve le code INSEE complet
142
    			// tentative de retrouve le code INSEE complet
142
    			$params['station_code_insee'] = (strlen($params['station_code_insee']) == 1) ? '0'.$params['station_code_insee'] : $params['station_code_insee'];
143
    			$params['station_code_insee'] = (strlen($params['station_code_insee']) == 1) ? '0'.$params['station_code_insee'] : $params['station_code_insee'];
143
    			$infos = $this->obtenirInformationsCommuneParNomEtDepartement($params['station_commune'], $params['station_code_insee']);
144
    			$infos = $this->obtenirInformationsCommuneParNomEtDepartement($params['station_commune'], $params['station_code_insee']);
144
    			$commune = ($infos != null) ? $infos['oc_code_insee'] : $commune;
145
    			$commune = ($infos != null) ? $infos['oc_code_insee'] : $commune;
145
    			//TODO: quoi faire si on a 3 chiffres par exemple ou plus de 5 chiffres ?
146
    			//TODO: quoi faire si on a 3 chiffres par exemple ou plus de 5 chiffres ?
146
    		}
147
    		}
147
    	}
148
    	}
148
 
149
 
149
    	return $commune;
150
    	return $commune;
150
    }
151
    }
151
    
152
    
152
    private function estUnCodeInsee($params) {
153
    private function estUnCodeInsee($params) {
153
    	return  is_numeric($params['station_code_insee']) &&
154
    	return  is_numeric($params['station_code_insee']) &&
154
    			(strlen($params['station_code_insee']) == 5 ||
155
    			(strlen($params['station_code_insee']) == 5 ||
155
    			strlen($params['station_code_insee']) == 4);
156
    			strlen($params['station_code_insee']) == 4);
156
    }
157
    }
157
    
158
    
158
    private function estUnCommunePlusDepartement($params) {
159
    private function estUnCommunePlusDepartement($params) {
159
    	return  is_numeric($params['station_code_insee']) &&
160
    	return  is_numeric($params['station_code_insee']) &&
160
    			(strlen($params['station_code_insee']) == 2 || 
161
    			(strlen($params['station_code_insee']) == 2 || 
161
    			strlen($params['station_code_insee']) == 1) &&
162
    			strlen($params['station_code_insee']) == 1) &&
162
    			isset($params['station_commune']) &&
163
    			isset($params['station_commune']) &&
163
    			trim($params['station_commune']) != "";
164
    			trim($params['station_commune']) != "";
164
    }
165
    }
165
   
166
   
166
    /**
167
    /**
167
     * Méthode appelée pour supprimer un élément
168
     * Méthode appelée pour supprimer un élément
168
     */
169
     */
169
    public function deleteElement($uid) {
170
    public function deleteElement($uid) {
170
    	    	
171
    	    	
171
    	if(isset($uid[0])) {
172
    	if(isset($uid[0])) {
172
    		$id_participant = $uid[0];
173
    		$id_participant = $uid[0];
173
    	} else {
174
    	} else {
174
    		return;
175
    		return;
175
    	}
176
    	}
176
    	
177
    	
177
    	if(isset($uid[1])) {
178
    	if(isset($uid[1])) {
178
    		$id_station = $uid[1];
179
    		$id_station = $uid[1];
179
    	} else {
180
    	} else {
180
    		return;
181
    		return;
181
    	}
182
    	}
182
    	    	
183
    	    	
183
    	$requete_suppression_station = 'DELETE FROM ods_stations '.
184
    	$requete_suppression_station = 'DELETE FROM ods_stations '.
184
    	'WHERE os_ce_participant = '.$this->proteger($id_participant).' '.
185
    	'WHERE os_ce_participant = '.$this->proteger($id_participant).' '.
185
        	'AND os_id_station = '.$this->proteger($id_station);
186
        	'AND os_id_station = '.$this->proteger($id_station);
186
    	    	
187
    	    	
187
    	$suppression_station = $this->executerRequeteSimple($requete_suppression_station);
188
    	$suppression_station = $this->executerRequeteSimple($requete_suppression_station);
188
    	
189
    	
189
    	if(!$suppression_station) {
190
    	if(!$suppression_station) {
190
    		// TODO: comment gère t'on les erreurs ?
191
    		// TODO: comment gère t'on les erreurs ?
191
    	}
192
    	}
192
    	
193
    	
193
    	$this->envoyer();
194
    	$this->envoyer();
194
    	
195
    	
195
    }
196
    }
196
    
197
    
197
	private function obtenirListeStationPourParticipant($id_participant) {
198
	private function obtenirListeStationPourParticipant($id_participant) {
198
	    	
199
	    	
199
    	$requete_liste_station = 'SELECT * FROM ods_stations WHERE os_ce_participant = '.$this->proteger($id_participant);
200
    	$requete_liste_station = 'SELECT * FROM ods_stations WHERE os_ce_participant = '.$this->proteger($id_participant);
200
 
201
 
201
    	$liste_station = $this->executerRequete($requete_liste_station);
202
    	$liste_station = $this->executerRequete($requete_liste_station);
202
    	
203
    	
203
    	$liste_station_formatees = array();
204
    	$liste_station_formatees = array();
204
    	 	
205
    	 	
205
    	foreach($liste_station as $indice => $station) {
206
    	foreach($liste_station as $indice => $station) {
206
	    		
207
	    		
207
    		$station_champs_formates = $this->formaterChampsStationPourEnvoi($station);
208
    		$station_champs_formates = $this->formaterChampsStationPourEnvoi($station);
208
		    $liste_station_formatees[$station['os_id_station']] = $station_champs_formates;
209
		    $liste_station_formatees[$station['os_id_station']] = $station_champs_formates;
209
    	}
210
    	}
210
    	
211
    	
211
    	return $liste_station_formatees;
212
    	return $liste_station_formatees;
212
	}
213
	}
213
    
214
    
214
    private function obtenirInformationsStation($id_station) {
215
    private function obtenirInformationsStation($id_station) {
215
    	
216
    	
216
    	$requete_infos_station = 'SELECT * FROM ods_stations WHERE os_id_station = '.$this->proteger($id_station);
217
    	$requete_infos_station = 'SELECT * FROM ods_stations WHERE os_id_station = '.$this->proteger($id_station);
217
 
218
 
218
    	$infos_station = $this->executerRequete($requete_infos_station);
219
    	$infos_station = $this->executerRequete($requete_infos_station);
219
    	
220
    	
220
    	$infos_station_formatees = array();
221
    	$infos_station_formatees = array();
221
    	
222
    	
222
    	if(!empty($infos_station)) {
223
    	if(!empty($infos_station)) {
223
    		$infos_station = $infos_station[0];
224
    		$infos_station = $infos_station[0];
224
    		$infos_station_formatees = $this->formaterChampsStationPourEnvoi($infos_station);
225
    		$infos_station_formatees = $this->formaterChampsStationPourEnvoi($infos_station);
225
    	}
226
    	}
226
    	
227
    	
227
    	return $infos_station_formatees;
228
    	return $infos_station_formatees;
228
    }
229
    }
229
    
230
    
230
    private function formaterChampsStationPourEnvoi($station) {
231
    private function formaterChampsStationPourEnvoi($station) {
231
    	
232
    	
232
    	if(is_numeric($station['os_ce_commune'])) {
233
    	if(is_numeric($station['os_ce_commune'])) {
233
    		$commune = $this->obtenirInformationsCommuneParCodeInsee($station['os_ce_commune']);
234
    		$commune = $this->obtenirInformationsCommuneParCodeInsee($station['os_ce_commune']);
234
    	} else {
235
    	} else {
235
    		$commune = $station['os_ce_commune'];
236
    		$commune = $station['os_ce_commune'];
236
    	}
237
    	}
237
    	
238
    	
238
    	$station_champs_formates = array(
239
    	$station_champs_formates = array(
239
		    	'id' => $station['os_id_station'],
240
		    	'id' => $station['os_id_station'],
240
		    	'nom' => $station['os_nom'],
241
		    	'nom' => $station['os_nom'],
241
				'code_insee' => $station['os_ce_commune'],
242
				'code_insee' => $station['os_ce_commune'],
242
    			'commune' => $commune,
243
    			'commune' => $commune,
243
				'id_milieu' => $station['os_ce_environnement'],
244
				'id_milieu' => $station['os_ce_environnement'],
244
    			'milieu' => $this->obtenirInformationsMilieuParId($station['os_ce_environnement']),
245
    			'milieu' => $this->obtenirInformationsMilieuParId($station['os_ce_environnement']),
245
				'latitude' => $station['os_latitude'],
246
				'latitude' => $station['os_latitude'],
246
				'longitude' => $station['os_longitude'],
247
				'longitude' => $station['os_longitude'],
247
    			'altitude' => $station['os_altitude'],
248
    			'altitude' => $station['os_altitude'],
248
				'description' => $station['os_commentaire']
249
				'description' => $station['os_commentaire']
249
		    );
250
		    );
250
		    
251
		    
251
		return $station_champs_formates;
252
		return $station_champs_formates;
252
    }
253
    }
253
    
254
    
254
	private function obtenirInformationsCommuneParCodeInsee($code_insee_commune) {
255
	private function obtenirInformationsCommuneParCodeInsee($code_insee_commune) {
255
    	
256
    	
256
    	$requete_infos_commune = 'SELECT * FROM ods_communes WHERE oc_code_insee = '.$this->proteger($code_insee_commune);
257
    	$requete_infos_commune = 'SELECT * FROM ods_communes WHERE oc_code_insee = '.$this->proteger($code_insee_commune);
257
    	$infos_commune = $this->executerRequete($requete_infos_commune);
258
    	$infos_commune = $this->executerRequete($requete_infos_commune);
258
    	
259
    	
259
		return $infos_commune[0]['oc_nom'];
260
		return $infos_commune[0]['oc_nom'];
260
    }
261
    }
261
    
262
    
262
    private function obtenirInformationsCommuneParNomEtDepartement($nom, $departement) {
263
    private function obtenirInformationsCommuneParNomEtDepartement($nom, $departement) {
263
    	// un remplacement par des underscores permet de s'affranchir des problèmes de saisie
264
    	// un remplacement par des underscores permet de s'affranchir des problèmes de saisie
264
    	// des communes avec des tirets espaces etc...
265
    	// des communes avec des tirets espaces etc...
265
    	$nom = str_replace(array("'","-",'"',' ') ,'_', $nom);
266
    	$nom = str_replace(array("'","-",'"',' ') ,'_', $nom);
266
    	$requete_infos_commune = 'SELECT oc_nom, oc_code_insee FROM ods_communes '.
267
    	$requete_infos_commune = 'SELECT oc_nom, oc_code_insee FROM ods_communes '.
267
    							 'WHERE oc_code_insee LIKE '.$this->proteger($departement.'%').' AND '.
268
    							 'WHERE oc_code_insee LIKE '.$this->proteger($departement.'%').' AND '.
268
    							 'oc_nom LIKE '.$this->proteger($nom);
269
    							 'oc_nom LIKE '.$this->proteger($nom);
269
    	
270
    	
270
    	$infos_commune = $this->executerRequete($requete_infos_commune);
271
    	$infos_commune = $this->executerRequete($requete_infos_commune);
271
    	$retour = null;
272
    	$retour = null;
272
    	if($infos_commune && isset($infos_commune[0])) {
273
    	if($infos_commune && isset($infos_commune[0])) {
273
    		$retour = $infos_commune[0];
274
    		$retour = $infos_commune[0];
274
    	}
275
    	}
275
 
276
 
276
    	return $retour;
277
    	return $retour;
277
    }
278
    }
278
    
279
    
279
    
280
    
280
    private function obtenirInformationsMilieuParId($id_milieu) {
281
    private function obtenirInformationsMilieuParId($id_milieu) {
281
    	
282
    	
282
    	$informations_milieu = $this->obtenirValeurTripleParId($id_milieu);
283
    	$informations_milieu = $this->obtenirValeurTripleParId($id_milieu);
283
    	return $informations_milieu;
284
    	return $informations_milieu;
284
    	
285
    	
285
    }
286
    }
286
}
287
}
287
?>
288
?>