Subversion Repositories Sites.obs-saisons.fr

Rev

Rev 255 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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