Subversion Repositories Sites.obs-saisons.fr

Rev

Rev 105 | Rev 194 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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