Subversion Repositories Sites.obs-saisons.fr

Rev

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

Rev 278 Rev 279
1
<?php
1
<?php
2
 
2
 
3
class OdsExport extends OdsTriple {
3
class OdsExport extends OdsTriple {
4
	
4
	
5
	const PREFIXE = 'get';
5
	const PREFIXE = 'get';
6
	const ABBR_LISTE_EVENEMENTS = 'evenement';
6
	const ABBR_LISTE_EVENEMENTS = 'evenement';
7
	const ABBR_LISTE_ESPECES = 'espece';
7
	const ABBR_LISTE_ESPECES = 'espece';
8
	const ABBR_LISTE_MILIEUX = 'environnement';
8
	const ABBR_LISTE_MILIEUX = 'environnement';
9
 
9
 
10
	private $type_mime = 'text/html';
10
	private $type_mime = 'text/html';
11
	
11
	
12
	/**
12
	/**
13
 	* Méthodes d'extractions d'informations
13
 	* Méthodes d'extractions d'informations
14
 	*/
14
 	*/
15
	
15
	
16
	/**
16
	/**
17
     * Méthode appelée avec une requête de type GET.
17
     * Méthode appelée avec une requête de type GET.
18
     *
18
     *
19
     */
19
     */
20
    function getElement($param = array()) {
20
    function getElement($param = array()) {
21
    	
21
    	
22
    	$type = $param[0];
22
    	$type = $param[0];
23
    	    	
23
    	    	
24
        if ($type == '*' || is_numeric($type)) {
24
        if ($type == '*' || is_numeric($type)) {
25
            $info = $this->getElementParDefaut($param);
25
            $info = $this->getElementParDefaut($param);
26
        } else {
26
        } else {
27
            $methode = self::PREFIXE.$type;
27
            $methode = self::PREFIXE.$type;
28
            if (method_exists($this, $methode)) {
28
            if (method_exists($this, $methode)) {
29
                array_shift($param);
29
                array_shift($param);
30
                $info = $this->$methode($param);
30
                $info = $this->$methode($param);
31
            } else {
31
            } else {
32
                $this->messages[] = "Le type d'information demandé '$type' n'est pas disponible.";
32
                $this->messages[] = "Le type d'information demandé '$type' n'est pas disponible.";
33
            }
33
            }
34
        }
34
        }
35
 
35
 
36
		$this->envoyer($info,$this->type_mime);
36
		$this->envoyer($info,$this->type_mime);
37
    }
37
    }
38
    
38
    
39
    public function getExportStationJson() {
39
    public function getExportStationJson() {
40
    
40
    
41
    	$donnees = $this->getExportStation();
41
    	$donnees = $this->getExportStation();
42
    	$donnees_formatees = $this->formaterListeStationPourExportJson($donnees);
42
    	$donnees_formatees = $this->formaterListeStationPourExportJson($donnees);
43
    
43
    
44
    	$this->type_mime = 'application/json';
44
    	$this->type_mime = 'application/json';
45
    
45
    
46
    	return $donnees_formatees;
46
    	return $donnees_formatees;
47
    }
47
    }
48
    
48
    
49
    public function getExportStation($start = null,$limit = null, $order_by = 'oo_date') {
49
    public function getExportStation($start = null,$limit = null, $order_by = 'oo_date') {
-
 
50
    
50
    
51
    	$id_demo = $this->config['appli']['id_participant_demo'];
51
    	$requete_selection_stations = 'SELECT * FROM ods_stations ';    
52
    	$requete_selection_stations = 'SELECT * FROM ods_stations WHERE os_ce_participant != '.$this->proteger($id_demo);    
52
    	$res_selection_stations = $this->executerRequete($requete_selection_stations);
53
    	$res_selection_stations = $this->executerRequete($requete_selection_stations);
53
    
54
    
54
    	return $res_selection_stations;
55
    	return $res_selection_stations;
55
    }
56
    }
56
    
57
    
57
    public function formaterListeStationPourExportJson($tableau_stations_infos) {
58
    public function formaterListeStationPourExportJson($tableau_stations_infos) {
58
    
59
    
59
    	$gestionnaire_especes = new OdsEspece($this->config);
60
    	$gestionnaire_especes = new OdsEspece($this->config);
60
    	$gestionnaire_communes = new OdsCommune($this->config);
61
    	$gestionnaire_communes = new OdsCommune($this->config);
61
    	$gestionnaire_utilisateurs = new OdsUtilisateur($this->config);
62
    	$gestionnaire_utilisateurs = new OdsUtilisateur($this->config);
62
    
63
    
63
    	$especes = $gestionnaire_especes->getToutesEspeces();
64
    	$especes = $gestionnaire_especes->getToutesEspeces();
64
    	$evenements = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_EVENEMENTS);
65
    	$evenements = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_EVENEMENTS);
65
    	$milieux = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_MILIEUX);
66
    	$milieux = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_MILIEUX);
66
    	$utilisateurs = $gestionnaire_utilisateurs->getListeUtilisateurAvecInfosComplementairesFormateeId();
67
    	$utilisateurs = $gestionnaire_utilisateurs->getListeUtilisateurAvecInfosComplementairesFormateeId();
67
    	$communes = $gestionnaire_communes->obtenirCommunesLieesAStationIndexeesCodeInsee();
68
    	$communes = $gestionnaire_communes->obtenirCommunesLieesAStationIndexeesCodeInsee();
68
  
69
  
69
    	$resultats_formates = array();
70
    	$resultats_formates = array();
70
    	$stats = array('total' => count($tableau_stations_infos));
71
    	$stats = array('total' => count($tableau_stations_infos));
71
    	
72
    	
72
    	foreach($tableau_stations_infos as $stations_infos) {    		
73
    	foreach($tableau_stations_infos as $stations_infos) {    		
73
    		$commune = isset($communes[$stations_infos['os_ce_commune']]['oc_nom']) ? $communes[$stations_infos['os_ce_commune']]['oc_nom'] : '';
74
    		$commune = isset($communes[$stations_infos['os_ce_commune']]['oc_nom']) ? $communes[$stations_infos['os_ce_commune']]['oc_nom'] : '';
74
    		
75
    		
75
    		$id_station = $stations_infos['os_id_station'];
76
    		$id_station = $stations_infos['os_id_station'];
76
    		$resultats_formates[$id_station]['nom'] = $stations_infos['os_nom'];
77
    		$resultats_formates[$id_station]['nom'] = $stations_infos['os_nom'];
77
    		$resultats_formates[$id_station]['code_commune'] = is_numeric($stations_infos['os_ce_commune']) ? substr($stations_infos['os_ce_commune'], 0, 2) : '';
78
    		$resultats_formates[$id_station]['code_commune'] = is_numeric($stations_infos['os_ce_commune']) ? substr($stations_infos['os_ce_commune'], 0, 2) : '';
78
    		$resultats_formates[$id_station]['nom_commune'] = $commune;
79
    		$resultats_formates[$id_station]['nom_commune'] = $commune;
79
    		$resultats_formates[$id_station]['latitude'] = $stations_infos['os_latitude'];
80
    		$resultats_formates[$id_station]['latitude'] = $stations_infos['os_latitude'];
80
    		$resultats_formates[$id_station]['longitude'] = $stations_infos['os_longitude'];
81
    		$resultats_formates[$id_station]['longitude'] = $stations_infos['os_longitude'];
81
    		$resultats_formates[$id_station]['altitude'] = $stations_infos['os_altitude'];
82
    		$resultats_formates[$id_station]['altitude'] = $stations_infos['os_altitude'];
82
    		$resultats_formates[$id_station]['type_participant'] = 'Particulier';
83
    		$resultats_formates[$id_station]['type_participant'] = 'Particulier';
83
    		
84
    		
84
    		if(isset($utilisateurs[$stations_infos['os_ce_participant']])) {
85
    		if(isset($utilisateurs[$stations_infos['os_ce_participant']])) {
85
    			$participant = $utilisateurs[$stations_infos['os_ce_participant']];
86
    			$participant = $utilisateurs[$stations_infos['os_ce_participant']];
86
    			$resultats_formates[$id_station]['participant']['id'] = $participant['uid'];
87
    			$resultats_formates[$id_station]['participant']['id'] = $participant['uid'];
87
    			$resultats_formates[$id_station]['participant']['nom'] = $participant['name'];
88
    			$resultats_formates[$id_station]['participant']['nom'] = $participant['name'];
88
    			$resultats_formates[$id_station]['type_participant'] = isset($participant['profile_type']) ? $participant['profile_type'] : 'Particulier';
89
    			$resultats_formates[$id_station]['type_participant'] = isset($participant['profile_type']) ? $participant['profile_type'] : 'Particulier';
89
    		} else {
90
    		} else {
90
    			$resultats_formates[$id_station]['participant']['id'] = null;
91
    			$resultats_formates[$id_station]['participant']['id'] = null;
91
    			$resultats_formates[$id_station]['participant']['nom'] = 'Anonyme';
92
    			$resultats_formates[$id_station]['participant']['nom'] = 'Anonyme';
92
    			$resultats_formates[$id_station]['type_participant'] = 'Particulier';
93
    			$resultats_formates[$id_station]['type_participant'] = 'Particulier';
93
    		}
94
    		}
94
    		if(isset($stats[$resultats_formates[$id_station]['type_participant']])) {
95
    		if(isset($stats[$resultats_formates[$id_station]['type_participant']])) {
95
    			$stats[$resultats_formates[$id_station]['type_participant']]++;
96
    			$stats[$resultats_formates[$id_station]['type_participant']]++;
96
    		} else {
97
    		} else {
97
    			$stats[$resultats_formates[$id_station]['type_participant']] = 1;
98
    			$stats[$resultats_formates[$id_station]['type_participant']] = 1;
98
    		}
99
    		}
99
    	}
100
    	}
100
    	$resultat = array('stats' => $stats, 'stations' => $resultats_formates);    	
101
    	$resultat = array('stats' => $stats, 'stations' => $resultats_formates);    	
101
    	return $resultat;
102
    	return $resultat;
102
    }
103
    }
103
	
104
	
104
	public function getExportObservation($start = null,$limit = null, $order_by = 'oo_date') {
105
	public function getExportObservation($start = null,$limit = null, $order_by = 'oo_date') {
105
						
106
						
106
		$requete_selection_observations = 'SELECT * FROM ods_observations '.
107
		$requete_selection_observations = 'SELECT * FROM ods_observations '.
107
									' LEFT JOIN ods_individus '. 
108
									' LEFT JOIN ods_individus '. 
108
									' ON oi_id_individu = oo_ce_individu'.
109
									' ON oi_id_individu = oo_ce_individu'.
109
									' LEFT JOIN ods_stations '.
110
									' LEFT JOIN ods_stations '.
110
									' ON oi_ce_station = os_id_station '.
111
									' ON oi_ce_station = os_id_station '.
111
									' LEFT JOIN ods_communes '.
112
									' LEFT JOIN ods_communes '.
112
									'ON os_ce_commune = oc_code_insee ';
113
									'ON os_ce_commune = oc_code_insee ';
113
		
114
		
114
		$requete_selection_observations .= $this->construireConditionRequete();
115
		$requete_selection_observations .= $this->construireConditionRequete();
115
		$requete_selection_observations .= ' ORDER BY oo_date DESC';
116
		$requete_selection_observations .= ' ORDER BY oo_date DESC';
116
		
117
		
117
		$res_selection_observations = $this->executerRequete($requete_selection_observations);
118
		$res_selection_observations = $this->executerRequete($requete_selection_observations);
118
				
119
				
119
		return $res_selection_observations;
120
		return $res_selection_observations;
120
	}
121
	}
121
	
122
	
122
	private function creerFiltreIdZoneGeo($valeurMasque) {
123
	private function creerFiltreIdZoneGeo($valeurMasque) {
123
		$masque = '';
124
		$masque = '';
124
		$dept = $valeurMasque;
125
		$dept = $valeurMasque;
125
		$dept = sprintf('%02s', $dept);
126
		$dept = sprintf('%02s', $dept);
126
		$dept = sprintf("%-'_5s", $dept);
127
		$dept = sprintf("%-'_5s", $dept);
127
		$masque = " oc_code_insee LIKE ".$this->proteger($dept);
128
		$masque = " oc_code_insee LIKE ".$this->proteger($dept);
128
		return $masque;
129
		return $masque;
129
	}
130
	}
130
	
131
	
131
	public function construireConditionRequete() {
132
	public function construireConditionRequete() {
132
		
133
		
133
		$condition = ' WHERE oo_date != "0000-00-00" AND DAY(oo_date) != "00" AND ';
134
		$condition = ' WHERE oo_date != "0000-00-00" AND DAY(oo_date) != "00" AND ';
134
		
135
		
135
		foreach($_GET as $cle => $valeur) {
136
		foreach($_GET as $cle => $valeur) {
136
			
137
			
137
			switch($cle) {
138
			switch($cle) {
138
			
139
			
139
				case 'type_espece':
140
				case 'type_espece':
140
					$condition .= 'oi_ce_espece IN (SELECT oe_id_espece FROM ods_especes WHERE oe_ce_type = '.$this->proteger($valeur).')';
141
					$condition .= 'oi_ce_espece IN (SELECT oe_id_espece FROM ods_especes WHERE oe_ce_type = '.$this->proteger($valeur).')';
141
				break;
142
				break;
142
				
143
				
143
				case 'annee':
144
				case 'annee':
144
					if($this->anneesMultiplesDemandees($valeur)) {
145
					if($this->anneesMultiplesDemandees($valeur)) {
145
						$valeur = rtrim($valeur,',');
146
						$valeur = rtrim($valeur,',');
146
						$condition .= 'YEAR(oo_date) IN ('.$valeur.')';						
147
						$condition .= 'YEAR(oo_date) IN ('.$valeur.')';						
147
					} else {
148
					} else {
148
						$condition .= 'YEAR(oo_date) ';
149
						$condition .= 'YEAR(oo_date) ';
149
						$condition .= ' = '.$this->proteger($valeur);
150
						$condition .= ' = '.$this->proteger($valeur);
150
					}
151
					}
151
				break;
152
				break;
152
				
153
				
153
				case 'mois':
154
				case 'mois':
154
					$condition .= 'MONTH(oo_date) <= '.$this->proteger($valeur);
155
					$condition .= 'MONTH(oo_date) <= '.$this->proteger($valeur);
155
				break;
156
				break;
156
				
157
				
157
				case 'espece':
158
				case 'espece':
158
					$condition .= 'oi_ce_espece IN ('.$valeur.') ';
159
					$condition .= 'oi_ce_espece IN ('.$valeur.') ';
159
				break;
160
				break;
160
				
161
				
161
				case 'evenement':
162
				case 'evenement':
162
					$condition .= 'oo_ce_evenement = '.$this->proteger($valeur);
163
					$condition .= 'oo_ce_evenement = '.$this->proteger($valeur);
163
				break;
164
				break;
164
				
165
				
165
				case 'utilisateur':
166
				case 'utilisateur':
166
					$condition .= 'oo_ce_participant = '.$this->proteger($valeur);	
167
					$condition .= 'oo_ce_participant = '.$this->proteger($valeur);	
167
				break;
168
				break;
168
				
169
				
169
				case 'departement' :
170
				case 'departement' :
170
					$condition .= $this->creerFiltreIdZoneGeo($valeur);
171
					$condition .= $this->creerFiltreIdZoneGeo($valeur);
171
				break;
172
				break;
172
				
173
				
173
				case 'region' :
174
				case 'region' :
174
					$requete_association_region .= 'SELECT * FROM ods_triples WHERE ot_ce_parent = 36 AND ot_valeur = '.$valeur;
175
					$requete_association_region .= 'SELECT * FROM ods_triples WHERE ot_ce_parent = 36 AND ot_valeur = '.$valeur;
175
					$listeAssociationRegions = $this->executerRequete($requete_association_region);
176
					$listeAssociationRegions = $this->executerRequete($requete_association_region);
176
					$departements = array();
177
					$departements = array();
177
					foreach ($listeAssociationRegions as $associationRegion) {
178
					foreach ($listeAssociationRegions as $associationRegion) {
178
						$departements[] = $this->creerFiltreIdZoneGeo($associationRegion['ot_cle']);
179
						$departements[] = $this->creerFiltreIdZoneGeo($associationRegion['ot_cle']);
179
					}
180
					}
180
					$condition .= ' ('. implode(' OR ', $departements).')';
181
					$condition .= ' ('. implode(' OR ', $departements).')';
181
				break;
182
				break;
182
				default:
183
				default:
183
			}
184
			}
184
			
185
			
185
			$condition .= ' AND ';
186
			$condition .= ' AND ';
186
		}
187
		}
187
		
188
		
188
		$condition = rtrim($condition,'AND ');
189
		$condition = rtrim($condition,'AND ');
189
		
190
		
190
		$id_demo = $this->config['appli']['id_participant_demo'];
191
		$id_demo = $this->config['appli']['id_participant_demo'];
191
		$id_admin = $this->config['appli']['id_participant_admin'];
192
		$id_admin = $this->config['appli']['id_participant_admin'];
192
		
193
		
193
		$condition .= ' AND oo_ce_participant != '.$id_demo;
194
		$condition .= ' AND oo_ce_participant != '.$id_demo;
194
		$condition .= ' AND oo_ce_participant != '.$id_admin;
195
		$condition .= ' AND oo_ce_participant != '.$id_admin;
195
		
196
		
196
		return $condition;
197
		return $condition;
197
	}
198
	}
198
	
199
	
199
	private function anneesMultiplesDemandees($annee) {
200
	private function anneesMultiplesDemandees($annee) {
200
		// un ensemble d'identifiants est une suite d'identifiants séparés par des virgules
201
		// un ensemble d'identifiants est une suite d'identifiants séparés par des virgules
201
		// sans virgule terminale
202
		// sans virgule terminale
202
		$reg_exp = "/^(([0-9])+,)*([0-9])+$/";
203
		$reg_exp = "/^(([0-9])+,)*([0-9])+$/";
203
		return preg_match($reg_exp, $annee);
204
		return preg_match($reg_exp, $annee);
204
	}
205
	}
205
	
206
	
206
	public function getExportObservationPlat() {
207
	public function getExportObservationPlat() {
207
				
208
				
208
		$donnees = $this->getExportObservation();
209
		$donnees = $this->getExportObservation();
209
		$donnees_formatees = $this->formaterListeObservationPourExportCSV($donnees);	
210
		$donnees_formatees = $this->formaterListeObservationPourExportCSV($donnees);	
210
			
211
			
211
		return $donnees_formatees ;
212
		return $donnees_formatees ;
212
	}
213
	}
213
	
214
	
214
	public function getExportObservationJson() {
215
	public function getExportObservationJson() {
215
		
216
		
216
		$donnees = $this->getExportObservation();
217
		$donnees = $this->getExportObservation();
217
		$donnees_formatees = $this->formaterListeObservationPourExportJson($donnees);
218
		$donnees_formatees = $this->formaterListeObservationPourExportJson($donnees);
218
 
219
 
219
		$this->type_mime = 'application/json';
220
		$this->type_mime = 'application/json';
220
		
221
		
221
		return $donnees_formatees;
222
		return $donnees_formatees;
222
	}
223
	}
223
	
224
	
224
	public function formaterListeObservationPourExportJson($tableau_observations_infos) {
225
	public function formaterListeObservationPourExportJson($tableau_observations_infos) {
225
		
226
		
226
		$gestionnaire_especes = new OdsEspece($this->config);
227
		$gestionnaire_especes = new OdsEspece($this->config);
227
		$gestionnaire_communes = new OdsCommune($this->config);
228
		$gestionnaire_communes = new OdsCommune($this->config);
228
				
229
				
229
		$especes = $gestionnaire_especes->getToutesEspeces();
230
		$especes = $gestionnaire_especes->getToutesEspeces();
230
		$evenements = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_EVENEMENTS);
231
		$evenements = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_EVENEMENTS);
231
		$milieux = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_MILIEUX);
232
		$milieux = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_MILIEUX);
232
		
233
		
233
		$resultats_formates = array();
234
		$resultats_formates = array();
234
		
235
		
235
		foreach($tableau_observations_infos as $observations_infos) {
236
		foreach($tableau_observations_infos as $observations_infos) {
236
		
237
		
237
			$id_espece = $observations_infos['oi_ce_espece'];
238
			$id_espece = $observations_infos['oi_ce_espece'];
238
			if($id_espece == 0) {
239
			if($id_espece == 0) {
239
				continue;
240
				continue;
240
			}
241
			}
241
			$nom_espece =  $especes[$id_espece]['nom_scientifique'];
242
			$nom_espece =  $especes[$id_espece]['nom_scientifique'];
242
				
243
				
243
			$id_evenement = $observations_infos['oo_ce_evenement'];
244
			$id_evenement = $observations_infos['oo_ce_evenement'];
244
			$chaine_evenement = $evenements[$id_evenement]['ot_cle'];
245
			$chaine_evenement = $evenements[$id_evenement]['ot_cle'];
245
				
246
				
246
			$infos_evenement = $this->renvoyerInformationStadeAPartirChaineTriple($chaine_evenement);
247
			$infos_evenement = $this->renvoyerInformationStadeAPartirChaineTriple($chaine_evenement);
247
				
248
				
248
			$date_observation_formatee = date($this->config['appli']['format_date'], strtotime($observations_infos['oo_date']));
249
			$date_observation_formatee = date($this->config['appli']['format_date'], strtotime($observations_infos['oo_date']));
249
			
250
			
250
			$id_observation = $observations_infos['oo_id_observation'];
251
			$id_observation = $observations_infos['oo_id_observation'];
251
			$infos_formatees = array(
252
			$infos_formatees = array(
252
				'date' => $date_observation_formatee,
253
				'date' => $date_observation_formatee,
253
				'evenenement' => $infos_evenement['nom'],
254
				'evenenement' => $infos_evenement['nom'],
254
				'code_bbch' => $infos_evenement['numero'],
255
				'code_bbch' => $infos_evenement['numero'],
255
				'nom_scientifique' => $nom_espece,
256
				'nom_scientifique' => $nom_espece,
256
				'id_espece' => $id_espece,
257
				'id_espece' => $id_espece,
257
				'participant' => $observations_infos['os_ce_participant']
258
				'participant' => $observations_infos['os_ce_participant']
258
			);
259
			);
259
			
260
			
260
			$id_station = $observations_infos['oi_ce_station'];
261
			$id_station = $observations_infos['oi_ce_station'];
261
			
262
			
262
			if(!isset($resultats_formates[$id_station])) {
263
			if(!isset($resultats_formates[$id_station])) {
263
				$resultats_formates[$id_station]['station'] = $observations_infos['os_nom'];
264
				$resultats_formates[$id_station]['station'] = $observations_infos['os_nom'];
264
				$resultats_formates[$id_station]['code_commune'] = $observations_infos['os_ce_commune'];
265
				$resultats_formates[$id_station]['code_commune'] = $observations_infos['os_ce_commune'];
265
				//$resultats_formates[$id_station]['nom_commune'] = $gestionnaire_communes->obtenirNomCommuneParCodeInsee($observations_infos['os_ce_commune']);
266
				//$resultats_formates[$id_station]['nom_commune'] = $gestionnaire_communes->obtenirNomCommuneParCodeInsee($observations_infos['os_ce_commune']);
266
				$resultats_formates[$id_station]['latitude'] = $observations_infos['os_latitude'];
267
				$resultats_formates[$id_station]['latitude'] = $observations_infos['os_latitude'];
267
				$resultats_formates[$id_station]['longitude'] = $observations_infos['os_longitude'];
268
				$resultats_formates[$id_station]['longitude'] = $observations_infos['os_longitude'];
268
				$resultats_formates[$id_station]['altitude'] = $observations_infos['os_altitude'];
269
				$resultats_formates[$id_station]['altitude'] = $observations_infos['os_altitude'];
269
				if(isset($milieux[$observations_infos['os_ce_environnement']]['ot_valeur'])) {
270
				if(isset($milieux[$observations_infos['os_ce_environnement']]['ot_valeur'])) {
270
					$resultats_formates[$id_station]['milieu'] = $milieux[$observations_infos['os_ce_environnement']]['ot_valeur'];
271
					$resultats_formates[$id_station]['milieu'] = $milieux[$observations_infos['os_ce_environnement']]['ot_valeur'];
271
				} else {
272
				} else {
272
					$resultats_formates[$id_station]['milieu'] = '';
273
					$resultats_formates[$id_station]['milieu'] = '';
273
				}
274
				}
274
				$resultats_formates[$id_station]['participant'] = $observations_infos['os_ce_participant'];
275
				$resultats_formates[$id_station]['participant'] = $observations_infos['os_ce_participant'];
275
			}
276
			}
276
			
277
			
277
			$resultats_formates[$id_station]['obs'][$id_observation] = $infos_formatees;
278
			$resultats_formates[$id_station]['obs'][$id_observation] = $infos_formatees;
278
		}
279
		}
279
		
280
		
280
		return $resultats_formates;
281
		return $resultats_formates;
281
	}
282
	}
282
	
283
	
283
	public function getFichierExportObservationCsv() {
284
	public function getFichierExportObservationCsv() {
284
				
285
				
285
		$donnees = $this->getExportObservation();
286
		$donnees = $this->getExportObservation();
286
		$donnees_formatees = $this->formaterListeObservationPourExportCSV($donnees);		
287
		$donnees_formatees = $this->formaterListeObservationPourExportCSV($donnees);		
287
		$chaine_csv = $this->convertirTableauAssocVersCSV($donnees_formatees);
288
		$chaine_csv = $this->convertirTableauAssocVersCSV($donnees_formatees);
288
			
289
			
289
		$this->envoyerFichier($chaine_csv);
290
		$this->envoyerFichier($chaine_csv);
290
	}
291
	}
291
	
292
	
292
	public function formaterListeObservationPourExportCSV($tableau_observations_infos) {
293
	public function formaterListeObservationPourExportCSV($tableau_observations_infos) {
293
		
294
		
294
		$gestionnaire_especes = new OdsEspece($this->config);
295
		$gestionnaire_especes = new OdsEspece($this->config);
295
		$gestionnaire_communes = new OdsCommune($this->config);
296
		$gestionnaire_communes = new OdsCommune($this->config);
296
		$gestionnaire_utilisateurs = new OdsUtilisateur($this->config);
297
		$gestionnaire_utilisateurs = new OdsUtilisateur($this->config);
297
				
298
				
298
		$especes = $gestionnaire_especes->getToutesEspeces();
299
		$especes = $gestionnaire_especes->getToutesEspeces();
299
		$evenements = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_EVENEMENTS);
300
		$evenements = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_EVENEMENTS);
300
		$milieux = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_MILIEUX);
301
		$milieux = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_MILIEUX);
301
		
302
		
302
		$codes_insee_communes = array();
303
		$codes_insee_communes = array();
303
		
304
		
304
		$resultats_formates = array();
305
		$resultats_formates = array();
305
		
306
		
306
		foreach($tableau_observations_infos as $observations_infos) {
307
		foreach($tableau_observations_infos as $observations_infos) {
307
		
308
		
308
			$id_espece = $observations_infos['oi_ce_espece'];
309
			$id_espece = $observations_infos['oi_ce_espece'];
309
			$nom_espece =  $especes[$id_espece]['nom_scientifique'];
310
			$nom_espece =  $especes[$id_espece]['nom_scientifique'];
310
				
311
				
311
			$id_evenement = $observations_infos['oo_ce_evenement'];
312
			$id_evenement = $observations_infos['oo_ce_evenement'];
312
			$chaine_evenement = $evenements[$id_evenement]['ot_cle'];
313
			$chaine_evenement = $evenements[$id_evenement]['ot_cle'];
313
				
314
				
314
			$infos_evenement = $this->renvoyerInformationStadeAPartirChaineTriple($chaine_evenement);
315
			$infos_evenement = $this->renvoyerInformationStadeAPartirChaineTriple($chaine_evenement);
315
				
316
				
316
			$date_observation_formatee = date($this->config['appli']['format_date'], strtotime($observations_infos['oo_date']));
317
			$date_observation_formatee = date($this->config['appli']['format_date'], strtotime($observations_infos['oo_date']));
317
			
318
			
318
			$id_observation = $observations_infos['oo_id_observation'];
319
			$id_observation = $observations_infos['oo_id_observation'];
319
			
320
			
320
			$infos_formatees = array(
321
			$infos_formatees = array(
321
				'id_observation' => $id_observation,
322
				'id_observation' => $id_observation,
322
				'date' => $date_observation_formatee,
323
				'date' => $date_observation_formatee,
323
				'evenenement' => $infos_evenement['nom'],
324
				'evenenement' => $infos_evenement['nom'],
324
				'code_bbch' => $infos_evenement['numero'],
325
				'code_bbch' => $infos_evenement['numero'],
325
				'nom_scientifique' => $nom_espece,
326
				'nom_scientifique' => $nom_espece,
326
				'station' => $observations_infos['os_nom'],
327
				'station' => $observations_infos['os_nom'],
327
				'code_commune' => $observations_infos['os_ce_commune'],
328
				'code_commune' => $observations_infos['os_ce_commune'],
328
				'nom_commune' => '',
329
				'nom_commune' => '',
329
				'latitude' => $observations_infos['os_latitude'],
330
				'latitude' => $observations_infos['os_latitude'],
330
				'longitude' => $observations_infos['os_longitude'],
331
				'longitude' => $observations_infos['os_longitude'],
331
				'altitude' => $observations_infos['os_altitude'],
332
				'altitude' => $observations_infos['os_altitude'],
332
				'milieu' => $milieux[$observations_infos['os_ce_environnement']]['ot_valeur'],
333
				'milieu' => $milieux[$observations_infos['os_ce_environnement']]['ot_valeur'],
333
				'id_participant' => $observations_infos['os_ce_participant'],
334
				'id_participant' => $observations_infos['os_ce_participant'],
334
				'pseudo_participant' => '',
335
				'pseudo_participant' => '',
335
				'mail_participant' => ''
336
				'mail_participant' => ''
336
			);
337
			);
337
			
338
			
338
			$resultats_formates[] = $infos_formatees;
339
			$resultats_formates[] = $infos_formatees;
339
			
340
			
340
			if(is_numeric($observations_infos['os_ce_commune'])) {
341
			if(is_numeric($observations_infos['os_ce_commune'])) {
341
				$codes_insee_communes[] = $observations_infos['os_ce_commune'];
342
				$codes_insee_communes[] = $observations_infos['os_ce_commune'];
342
			}
343
			}
343
		}
344
		}
344
		
345
		
345
		$correspondance_codes_insee_noms = $gestionnaire_communes->obtenirTableauNomsCommunesParTableauCodesInsee($codes_insee_communes);
346
		$correspondance_codes_insee_noms = $gestionnaire_communes->obtenirTableauNomsCommunesParTableauCodesInsee($codes_insee_communes);
346
		$correspondance_id_utilisateur = $gestionnaire_utilisateurs->getListeUtilisateurFormateeId();
347
		$correspondance_id_utilisateur = $gestionnaire_utilisateurs->getListeUtilisateurFormateeId();
347
		
348
		
348
		foreach($resultats_formates as &$resultat) {
349
		foreach($resultats_formates as &$resultat) {
349
			
350
			
350
			if(isset($correspondance_codes_insee_noms[$resultat['code_commune']]) && trim($correspondance_codes_insee_noms[$resultat['code_commune']]) != '') {								
351
			if(isset($correspondance_codes_insee_noms[$resultat['code_commune']]) && trim($correspondance_codes_insee_noms[$resultat['code_commune']]) != '') {								
351
				$resultat['nom_commune'] = $correspondance_codes_insee_noms[$resultat['code_commune']];
352
				$resultat['nom_commune'] = $correspondance_codes_insee_noms[$resultat['code_commune']];
352
			}
353
			}
353
			
354
			
354
			$resultat['pseudo_participant'] = $correspondance_id_utilisateur[$resultat['id_participant']]['name'];
355
			$resultat['pseudo_participant'] = $correspondance_id_utilisateur[$resultat['id_participant']]['name'];
355
			$resultat['mail_participant'] = $correspondance_id_utilisateur[$resultat['id_participant']]['mail'];
356
			$resultat['mail_participant'] = $correspondance_id_utilisateur[$resultat['id_participant']]['mail'];
356
		}
357
		}
357
		
358
		
358
		return $resultats_formates;
359
		return $resultats_formates;
359
	}
360
	}
360
	
361
	
361
	public function convertirTableauAssocVersCSV($tableau) {
362
	public function convertirTableauAssocVersCSV($tableau) {
362
		
363
		
363
		$csv = '';
364
		$csv = '';
364
		$colonnes = array_keys($tableau[0]);
365
		$colonnes = array_keys($tableau[0]);
365
		$csv .= implode(';',$colonnes).";\n";
366
		$csv .= implode(';',$colonnes).";\n";
366
		
367
		
367
		foreach($tableau as $elements) {
368
		foreach($tableau as $elements) {
368
			$csv .= implode(';',$elements).";\n";
369
			$csv .= implode(';',$elements).";\n";
369
		}
370
		}
370
		
371
		
371
		return $csv;
372
		return $csv;
372
	}
373
	}
373
	
374
	
374
	public function envoyerFichier($contenu) {
375
	public function envoyerFichier($contenu) {
375
		
376
		
376
		$nom_fichier = "observations_export.csv";
377
		$nom_fichier = "observations_export.csv";
377
		$chemin_fichier = $this->config['appli']['chemin_fichiers_temp'].'/'.$nom_fichier;
378
		$chemin_fichier = $this->config['appli']['chemin_fichiers_temp'].'/'.$nom_fichier;
378
		
379
		
379
		file_put_contents($chemin_fichier, $contenu);	
380
		file_put_contents($chemin_fichier, $contenu);	
380
			
381
			
381
		$contenu = file_get_contents($chemin_fichier);
382
		$contenu = file_get_contents($chemin_fichier);
382
		$taille_fichier = filesize($chemin_fichier);
383
		$taille_fichier = filesize($chemin_fichier);
383
		
384
		
384
		unlink($chemin_fichier);
385
		unlink($chemin_fichier);
385
		
386
		
386
		ini_set('zlib.output_compression','Off');
387
		ini_set('zlib.output_compression','Off');
387
		
388
		
388
		header('Pragma: public');
389
		header('Pragma: public');
389
		header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
390
		header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
390
		header('Cache-Control: must-revalidate, pre-check=0, post-check=0, max-age=0');
391
		header('Cache-Control: must-revalidate, pre-check=0, post-check=0, max-age=0');
391
		
392
		
392
		header('Content-Tranfer-Encoding: none');
393
		header('Content-Tranfer-Encoding: none');
393
		
394
		
394
		header('Content-Type: application/octetstream; name="'.$nom_fichier.'"');
395
		header('Content-Type: application/octetstream; name="'.$nom_fichier.'"');
395
		header('Content-Disposition: attachement; filename="'.$nom_fichier.'"');
396
		header('Content-Disposition: attachement; filename="'.$nom_fichier.'"');
396
		
397
		
397
		header('Content-Length: '.$taille_fichier);
398
		header('Content-Length: '.$taille_fichier);
398
		
399
		
399
		echo $contenu;
400
		echo $contenu;
400
		exit();
401
		exit();
401
	}
402
	}
402
	
403
	
403
	private function formaterInformationPourEnvoi($tableauinfos) {
404
	private function formaterInformationPourEnvoi($tableauinfos) {
404
 
405
 
405
	}
406
	}
406
}
407
}
407
?>
408
?>