Subversion Repositories Sites.obs-saisons.fr

Rev

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

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