Subversion Repositories Sites.obs-saisons.fr

Rev

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

Rev 207 Rev 210
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
	
8
	
9
	/**
9
	/**
10
 	* Méthodes d'extractions d'informations
10
 	* Méthodes d'extractions d'informations
11
 	*/
11
 	*/
12
	
12
	
13
	/**
13
	/**
14
     * Méthode appelée avec une requête de type GET.
14
     * Méthode appelée avec une requête de type GET.
15
     *
15
     *
16
     */
16
     */
17
    function getElement($param = array()) {
17
    function getElement($param = array()) {
18
    	
18
    	
19
    	$type = $param[0];
19
    	$type = $param[0];
20
    	    	
20
    	    	
21
        if ($type == '*' || is_numeric($type)) {
21
        if ($type == '*' || is_numeric($type)) {
22
            $info = $this->getElementParDefaut($param);
22
            $info = $this->getElementParDefaut($param);
23
        } else {
23
        } else {
24
            $methode = self::PREFIXE.$type;
24
            $methode = self::PREFIXE.$type;
25
            if (method_exists($this, $methode)) {
25
            if (method_exists($this, $methode)) {
26
                array_shift($param);
26
                array_shift($param);
27
                $info = $this->$methode($param);
27
                $info = $this->$methode($param);
28
            } else {
28
            } else {
29
                $this->messages[] = "Le type d'information demandé '$type' n'est pas disponible.";
29
                $this->messages[] = "Le type d'information demandé '$type' n'est pas disponible.";
30
            }
30
            }
31
        }
31
        }
32
       
32
       
33
        // Envoi sur la sortie standard
33
        // Envoi sur la sortie standard
34
        $this->envoyer($info);
34
        $this->envoyer($info);
35
    }
35
    }
36
	
36
	
37
	public function getExportObservation($start = null,$limit = null, $order_by = 'oo_date') {
37
	public function getExportObservation($start = null,$limit = null, $order_by = 'oo_date') {
38
				
38
				
39
		$requete_selection_observations = 'SELECT * FROM ods_observations '.
39
		$requete_selection_observations = 'SELECT * FROM ods_observations '.
40
									' LEFT JOIN ods_individus '. 
40
									' LEFT JOIN ods_individus '. 
41
									' ON oi_id_individu = oo_ce_individu'.
41
									' ON oi_id_individu = oo_ce_individu'.
42
									' LEFT JOIN ods_stations '.
42
									' LEFT JOIN ods_stations '.
43
									' ON oi_ce_station = os_id_station ';
43
									' ON oi_ce_station = os_id_station ';
44
		
44
		
45
		$requete_selection_observations .= $this->construireConditionRequete();
45
		$requete_selection_observations .= $this->construireConditionRequete();
46
		$requete_selection_observations .= ' ORDER BY oo_date DESC';
46
		$requete_selection_observations .= ' ORDER BY oo_date DESC';
47
		
47
		
48
		$res_selection_observations = $this->executerRequete($requete_selection_observations);
48
		$res_selection_observations = $this->executerRequete($requete_selection_observations);
49
				
49
				
50
		return $res_selection_observations;
50
		return $res_selection_observations;
51
	}
51
	}
52
	
52
	
53
	public function construireConditionRequete() {
53
	public function construireConditionRequete() {
54
		
54
		
55
		$condition = ' WHERE oo_date != "0000-00-00" AND ';
55
		$condition = ' WHERE oo_date != "0000-00-00" AND ';
56
		
56
		
57
		foreach($_GET as $cle => $valeur) {
57
		foreach($_GET as $cle => $valeur) {
58
			
58
			
59
			switch($cle) {
59
			switch($cle) {
60
			
60
			
61
				case 'type_espece':
61
				case 'type_espece':
62
					$condition .= 'oi_ce_espece IN (SELECT oe_id_espece FROM ods_especes WHERE oe_ce_type = '.$this->proteger($valeur).')';
62
					$condition .= 'oi_ce_espece IN (SELECT oe_id_espece FROM ods_especes WHERE oe_ce_type = '.$this->proteger($valeur).')';
63
				break;
63
				break;
64
				
64
				
65
				case 'annee':
65
				case 'annee':
66
					$condition .= 'YEAR(oo_date) = '.$this->proteger($valeur);
66
					$condition .= 'YEAR(oo_date) = '.$this->proteger($valeur);
67
				break;
67
				break;
68
				
68
				
69
				case 'mois':
69
				case 'mois':
70
					$condition .= 'MONTH(oo_date) <= '.$this->proteger($valeur);
70
					$condition .= 'MONTH(oo_date) <= '.$this->proteger($valeur);
71
				break;
71
				break;
72
				
72
				
73
				case 'espece':
73
				case 'espece':
74
					$condition .= 'oi_ce_espece = '.$this->proteger($valeur);
74
					$condition .= 'oi_ce_espece = '.$this->proteger($valeur);
75
				break;
75
				break;
76
				
76
				
77
				case 'evenement':
77
				case 'evenement':
78
					$condition .= 'oo_ce_evenement = '.$this->proteger($valeur);
78
					$condition .= 'oo_ce_evenement = '.$this->proteger($valeur);
79
				break;
79
				break;
80
				
80
				
81
				case 'departement':
81
				case 'departement':
82
					$condition .= 'os_ce_commune LIKE "'.$valeur.'%" ';
82
					$condition .= 'os_ce_commune LIKE "'.$valeur.'%" ';
83
				break;
83
				break;
84
				
84
				
85
				case 'utilisateur':
85
				case 'utilisateur':
86
					$condition .= 'oo_ce_participant = '.$this->proteger($valeur);	
86
					$condition .= 'oo_ce_participant = '.$this->proteger($valeur);	
87
				break;
87
				break;
88
				
88
				
89
				default:
89
				default:
90
				break;
90
				break;
91
			}
91
			}
92
			
92
			
93
			$condition .= ' AND ';
93
			$condition .= ' AND ';
94
		}
94
		}
95
		
95
		
96
		$condition = rtrim($condition,'AND ');
96
		$condition = rtrim($condition,'AND ');
97
		$condition .= ' AND oo_ce_participant != 4';
97
		$condition .= ' AND oo_ce_participant != 4';
98
		
98
		
99
		return $condition;
99
		return $condition;
100
	}
100
	}
-
 
101
	
-
 
102
	public function getExportObservationPlat() {
-
 
103
				
-
 
104
		$donnees = $this->getExportObservation();
-
 
105
		$donnees_formatees = $this->formaterPourExportCSV($donnees);		
-
 
106
			
-
 
107
		return $donnees_formatees ;
-
 
108
	}
101
	
109
	
102
	public function getExportObservationJson() {
110
	public function getExportObservationJson() {
103
		
111
		
104
		$donnees = $this->getExportObservation();
112
		$donnees = $this->getExportObservation();
105
		$donnees_formatees = $this->formaterPourExportJson($donnees);
113
		$donnees_formatees = $this->formaterPourExportJson($donnees);
106
		
114
		
107
		return $donnees_formatees;
115
		return $donnees_formatees;
108
	}
116
	}
109
	
117
	
110
	public function formaterPourExportJson($tableau_observations_infos) {
118
	public function formaterPourExportJson($tableau_observations_infos) {
111
		
119
		
112
		$gestionnaire_especes = new OdsEspece($this->config);
120
		$gestionnaire_especes = new OdsEspece($this->config);
113
		$gestionnaire_communes = new OdsCommune($this->config);
121
		$gestionnaire_communes = new OdsCommune($this->config);
114
				
122
				
115
		$especes = $gestionnaire_especes->getToutesEspeces();
123
		$especes = $gestionnaire_especes->getToutesEspeces();
116
		$evenements = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_EVENEMENTS);
124
		$evenements = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_EVENEMENTS);
117
		
125
		
118
		$resultats_formates = array();
126
		$resultats_formates = array();
119
		
127
		
120
		foreach($tableau_observations_infos as $observations_infos) {
128
		foreach($tableau_observations_infos as $observations_infos) {
121
		
129
		
122
			$id_espece = $observations_infos['oi_ce_espece'];
130
			$id_espece = $observations_infos['oi_ce_espece'];
123
			$nom_espece =  $especes[$id_espece]['nom_scientifique'];
131
			$nom_espece =  $especes[$id_espece]['nom_scientifique'];
124
				
132
				
125
			$id_evenement = $observations_infos['oo_ce_evenement'];
133
			$id_evenement = $observations_infos['oo_ce_evenement'];
126
			$chaine_evenement = $evenements[$id_evenement]['ot_cle'];
134
			$chaine_evenement = $evenements[$id_evenement]['ot_cle'];
127
				
135
				
128
			$infos_evenement = $this->renvoyerInformationStadeAPartirChaineTriple($chaine_evenement);
136
			$infos_evenement = $this->renvoyerInformationStadeAPartirChaineTriple($chaine_evenement);
129
				
137
				
130
			$date_observation_formatee = date($this->config['appli']['format_date'], strtotime($observations_infos['oo_date']));
138
			$date_observation_formatee = date($this->config['appli']['format_date'], strtotime($observations_infos['oo_date']));
131
			
139
			
132
			$id_observation = $observations_infos['oo_id_observation'];
140
			$id_observation = $observations_infos['oo_id_observation'];
133
			$infos_formatees = array(
141
			$infos_formatees = array(
134
				'date' => $date_observation_formatee,
142
				'date' => $date_observation_formatee,
135
				'evenenement' => $infos_evenement['nom'],
143
				'evenenement' => $infos_evenement['nom'],
136
				'code_bbch' => $infos_evenement['numero'],
144
				'code_bbch' => $infos_evenement['numero'],
137
				'nom_scientifique' => $nom_espece,
145
				'nom_scientifique' => $nom_espece,
138
			);
146
			);
139
			
147
			
140
			$id_station = $observations_infos['oi_ce_station'];
148
			$id_station = $observations_infos['oi_ce_station'];
141
			
149
			
142
			if(!isset($resultats_formates[$id_station])) {
150
			if(!isset($resultats_formates[$id_station])) {
143
				$resultats_formates[$id_station]['station'] = $observations_infos['os_nom'];
151
				$resultats_formates[$id_station]['station'] = $observations_infos['os_nom'];
144
				$resultats_formates[$id_station]['code_commune'] = $observations_infos['os_ce_commune'];
152
				$resultats_formates[$id_station]['code_commune'] = $observations_infos['os_ce_commune'];
145
				//$resultats_formates[$id_station]['nom_commune'] = $gestionnaire_communes->obtenirNomCommuneParCodeInsee($observations_infos['os_ce_commune']);
153
				//$resultats_formates[$id_station]['nom_commune'] = $gestionnaire_communes->obtenirNomCommuneParCodeInsee($observations_infos['os_ce_commune']);
146
				$resultats_formates[$id_station]['latitude'] = $observations_infos['os_latitude'];
154
				$resultats_formates[$id_station]['latitude'] = $observations_infos['os_latitude'];
147
				$resultats_formates[$id_station]['longitude'] = $observations_infos['os_longitude'];
155
				$resultats_formates[$id_station]['longitude'] = $observations_infos['os_longitude'];
148
				$resultats_formates[$id_station]['altitude'] = $observations_infos['os_altitude'];
156
				$resultats_formates[$id_station]['altitude'] = $observations_infos['os_altitude'];
149
				$resultats_formates[$id_station]['milieu'] = $observations_infos['os_milieu'];
157
				$resultats_formates[$id_station]['milieu'] = $observations_infos['os_milieu'];
150
				$resultats_formates[$id_station]['participant'] = $observations_infos['os_ce_participant'];
158
				$resultats_formates[$id_station]['participant'] = $observations_infos['os_ce_participant'];
151
			}
159
			}
152
			
160
			
153
			$resultats_formates[$id_station]['obs'][$id_observation] = $infos_formatees;
161
			$resultats_formates[$id_station]['obs'][$id_observation] = $infos_formatees;
154
		}
162
		}
155
		
163
		
156
		return $resultats_formates;
164
		return $resultats_formates;
157
	}
165
	}
158
	
166
	
159
	public function getExportObservationCsv() {
-
 
160
		
-
 
161
		$utilisateur = new Utilisateur();
-
 
162
		$est_admin = $utilisateur->estAdmin();
167
	public function getFichierExportObservationCsv() {
163
		
-
 
164
		if($est_admin) {
168
				
165
			$donnees = $this->getExportObservation();
169
		$donnees = $this->getExportObservation();
166
			$donnees_formatees = $this->formaterPourExportCSV($donnees);		
170
		$donnees_formatees = $this->formaterPourExportCSV($donnees);		
167
			$chaine_csv = $this->convertirTableauAssocVersCSV($donnees_formatees);
171
		$chaine_csv = $this->convertirTableauAssocVersCSV($donnees_formatees);
168
			
172
			
169
			$this->envoyerFichier($chaine_csv);
-
 
170
		} else {
-
 
171
			echo "echo non non non non non, vous êtes pas admin !!!!";
-
 
172
		}
173
		$this->envoyerFichier($chaine_csv);
173
	}
174
	}
174
	
175
	
175
	public function formaterPourExportCSV($tableau_observations_infos) {
176
	public function formaterPourExportCSV($tableau_observations_infos) {
176
		
177
		
177
		$gestionnaire_especes = new OdsEspece($this->config);
178
		$gestionnaire_especes = new OdsEspece($this->config);
178
		$gestionnaire_communes = new OdsCommune($this->config);
179
		$gestionnaire_communes = new OdsCommune($this->config);
179
				
180
				
180
		$especes = $gestionnaire_especes->getToutesEspeces();
181
		$especes = $gestionnaire_especes->getToutesEspeces();
181
		$evenements = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_EVENEMENTS);
182
		$evenements = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_EVENEMENTS);
-
 
183
		
-
 
184
		$codes_insee_communes = array();
182
		
185
		
183
		$resultats_formates = array();
186
		$resultats_formates = array();
184
		
187
		
185
		foreach($tableau_observations_infos as $observations_infos) {
188
		foreach($tableau_observations_infos as $observations_infos) {
186
		
189
		
187
			$id_espece = $observations_infos['oi_ce_espece'];
190
			$id_espece = $observations_infos['oi_ce_espece'];
188
			$nom_espece =  $especes[$id_espece]['nom_scientifique'];
191
			$nom_espece =  $especes[$id_espece]['nom_scientifique'];
189
				
192
				
190
			$id_evenement = $observations_infos['oo_ce_evenement'];
193
			$id_evenement = $observations_infos['oo_ce_evenement'];
191
			$chaine_evenement = $evenements[$id_evenement]['ot_cle'];
194
			$chaine_evenement = $evenements[$id_evenement]['ot_cle'];
192
				
195
				
193
			$infos_evenement = $this->renvoyerInformationStadeAPartirChaineTriple($chaine_evenement);
196
			$infos_evenement = $this->renvoyerInformationStadeAPartirChaineTriple($chaine_evenement);
194
				
197
				
195
			$date_observation_formatee = date($this->config['appli']['format_date'], strtotime($observations_infos['oo_date']));
198
			$date_observation_formatee = date($this->config['appli']['format_date'], strtotime($observations_infos['oo_date']));
196
			
199
			
197
			$id_observation = $observations_infos['oo_id_observation'];
200
			$id_observation = $observations_infos['oo_id_observation'];
198
			
201
			
199
			$infos_formatees = array(
202
			$infos_formatees = array(
200
				'id_observation' => $id_observation,
203
				'id_observation' => $id_observation,
201
				'date' => $date_observation_formatee,
204
				'date' => $date_observation_formatee,
202
				'evenenement' => $infos_evenement['nom'],
205
				'evenenement' => $infos_evenement['nom'],
203
				'code_bbch' => $infos_evenement['numero'],
206
				'code_bbch' => $infos_evenement['numero'],
204
				'nom_scientifique' => $nom_espece,
207
				'nom_scientifique' => $nom_espece,
205
				'station' => $observations_infos['os_nom'],
208
				'station' => $observations_infos['os_nom'],
206
				'code_commune' => $observations_infos['os_ce_commune'],
209
				'code_commune' => $observations_infos['os_ce_commune'],
207
				'nom_commune' => $gestionnaire_communes->obtenirNomCommuneParCodeInsee($observations_infos['os_ce_commune']),
210
				'nom_commune' => '',
208
				'latitude' => $observations_infos['os_latitude'],
211
				'latitude' => $observations_infos['os_latitude'],
209
				'longitude' => $observations_infos['os_longitude'],
212
				'longitude' => $observations_infos['os_longitude'],
210
				'altitude' => $observations_infos['os_altitude'],
213
				'altitude' => $observations_infos['os_altitude'],
211
				'milieu' => $observations_infos['os_milieu'],
214
				'milieu' => $observations_infos['os_milieu'],
212
				'participant' => $observations_infos['os_ce_participant']
215
				'participant' => $observations_infos['os_ce_participant']
213
			);
216
			);
214
			
217
			
215
			$resultats_formates[] = $infos_formatees;
218
			$resultats_formates[] = $infos_formatees;
-
 
219
			
-
 
220
			if(is_numeric($observations_infos['os_ce_commune'])) {
-
 
221
				$codes_insee_communes[] = $observations_infos['os_ce_commune'];
-
 
222
			}
-
 
223
		}
-
 
224
		
-
 
225
		$correspondance_codes_insee_noms = $gestionnaire_communes->obtenirTableauNomsCommunesParTableauCodesInsee($codes_insee_communes);
-
 
226
		
-
 
227
		foreach($resultats_formates as &$resultat) {
-
 
228
			
-
 
229
			if(isset($correspondance_codes_insee_noms[$resultat['code_commune']]) && trim($correspondance_codes_insee_noms[$resultat['code_commune']]) != '') {				
-
 
230
				
-
 
231
				$resultat['nom_commune'] = $correspondance_codes_insee_noms[$resultat['code_commune']];
-
 
232
			}
216
		}
233
		}
217
		
234
		
218
		return $resultats_formates;
235
		return $resultats_formates;
219
	}
236
	}
220
	
237
	
221
	public function convertirTableauAssocVersCSV($tableau) {
238
	public function convertirTableauAssocVersCSV($tableau) {
222
		
239
		
223
		$csv = '';
240
		$csv = '';
224
		$colonnes = array_keys($tableau[0]);
241
		$colonnes = array_keys($tableau[0]);
225
		$csv .= implode(';',$colonnes).";\n";
242
		$csv .= implode(';',$colonnes).";\n";
226
		
243
		
227
		foreach($tableau as $elements) {
244
		foreach($tableau as $elements) {
228
			$csv .= implode(';',$elements).";\n";
245
			$csv .= implode(';',$elements).";\n";
229
		}
246
		}
230
		
247
		
231
		return $csv;
248
		return $csv;
232
	}
249
	}
233
	
250
	
234
	public function envoyerFichier($contenu) {
251
	public function envoyerFichier($contenu) {
235
		
252
		
236
		$nom_fichier = "observations_export.csv";
253
		$nom_fichier = "observations_export.csv";
237
		$chemin_fichier = $this->config['appli']['chemin_fichiers_temp'].'/'.$nom_fichier;
254
		$chemin_fichier = $this->config['appli']['chemin_fichiers_temp'].'/'.$nom_fichier;
238
		
255
		
239
		file_put_contents($chemin_fichier, $contenu);	
256
		file_put_contents($chemin_fichier, $contenu);	
240
			
257
			
241
		$contenu = file_get_contents($chemin_fichier);
258
		$contenu = file_get_contents($chemin_fichier);
242
		$taille_fichier = filesize($chemin_fichier);
259
		$taille_fichier = filesize($chemin_fichier);
243
		
260
		
244
		unlink($chemin_fichier);
261
		unlink($chemin_fichier);
245
		
262
		
246
		ini_set('zlib.output_compression','Off');
263
		ini_set('zlib.output_compression','Off');
247
		
264
		
248
		header('Pragma: public');
265
		header('Pragma: public');
249
		header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
266
		header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
250
		header('Cache-Control: must-revalidate, pre-check=0, post-check=0, max-age=0');
267
		header('Cache-Control: must-revalidate, pre-check=0, post-check=0, max-age=0');
251
		
268
		
252
		header('Content-Tranfer-Encoding: none');
269
		header('Content-Tranfer-Encoding: none');
253
		
270
		
254
		header('Content-Type: application/octetstream; name="'.$nom_fichier.'"');
271
		header('Content-Type: application/octetstream; name="'.$nom_fichier.'"');
255
		header('Content-Disposition: attachement; filename="'.$nom_fichier.'"');
272
		header('Content-Disposition: attachement; filename="'.$nom_fichier.'"');
256
		
273
		
257
		header('Content-Length: '.$taille_fichier);
274
		header('Content-Length: '.$taille_fichier);
258
		
275
		
259
		echo $contenu;
276
		echo $contenu;
260
		exit();
277
		exit();
261
	}
278
	}
262
	
279
	
263
	private function formaterInformationPourEnvoi($tableauinfos) {
280
	private function formaterInformationPourEnvoi($tableauinfos) {
264
 
281
 
265
	}
282
	}
266
}
283
}
267
?>
284
?>