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