Subversion Repositories Sites.obs-saisons.fr

Rev

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

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