Subversion Repositories Sites.obs-saisons.fr

Rev

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

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