Subversion Repositories Sites.obs-saisons.fr

Rev

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

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