Subversion Repositories Sites.obs-saisons.fr

Rev

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