Subversion Repositories Sites.obs-saisons.fr

Rev

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

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