Rev 210 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php
class OdsExport extends OdsTriple {
const PREFIXE = 'get';
const ABBR_LISTE_EVENEMENTS = 'evenement';
const ABBR_LISTE_ESPECES = 'espece';
/**
* Méthodes d'extractions d'informations
*/
/**
* Méthode appelée avec une requête de type GET.
*
*/
function getElement($param = array()) {
$type = $param[0];
if ($type == '*' || is_numeric($type)) {
$info = $this->getElementParDefaut($param);
} else {
$methode = self::PREFIXE.$type;
if (method_exists($this, $methode)) {
array_shift($param);
$info = $this->$methode($param);
} else {
$this->messages[] = "Le type d'information demandé '$type' n'est pas disponible.";
}
}
// Envoi sur la sortie standard
$this->envoyer($info);
}
public function getExportObservation($start = null,$limit = null, $order_by = 'oo_date') {
$requete_selection_observations = 'SELECT * FROM ods_observations '.
' LEFT JOIN ods_individus '.
' ON oi_id_individu = oo_ce_individu'.
' LEFT JOIN ods_stations '.
' ON oi_ce_station = os_id_station ';
$requete_selection_observations .= $this->construireConditionRequete();
$requete_selection_observations .= ' ORDER BY oo_date DESC';
$res_selection_observations = $this->executerRequete($requete_selection_observations);
return $res_selection_observations;
}
public function construireConditionRequete() {
$condition = ' WHERE oo_date != "0000-00-00" AND ';
foreach($_GET as $cle => $valeur) {
switch($cle) {
case 'type_espece':
$condition .= 'oi_ce_espece IN (SELECT oe_id_espece FROM ods_especes WHERE oe_ce_type = '.$this->proteger($valeur).')';
break;
case 'annee':
$condition .= 'YEAR(oo_date) = '.$this->proteger($valeur);
break;
case 'mois':
$condition .= 'MONTH(oo_date) <= '.$this->proteger($valeur);
break;
case 'espece':
$condition .= 'oi_ce_espece = '.$this->proteger($valeur);
break;
case 'evenement':
$condition .= 'oo_ce_evenement = '.$this->proteger($valeur);
break;
case 'departement':
$condition .= 'os_ce_commune LIKE "'.$valeur.'%" ';
break;
case 'utilisateur':
$condition .= 'oo_ce_participant = '.$this->proteger($valeur);
break;
default:
break;
}
$condition .= ' AND ';
}
$condition = rtrim($condition,'AND ');
$condition .= ' AND oo_ce_participant != 4';
return $condition;
}
public function getExportObservationJson() {
$donnees = $this->getExportObservation();
$donnees_formatees = $this->formaterPourExportJson($donnees);
return $donnees_formatees;
}
public function formaterPourExportJson($tableau_observations_infos) {
$gestionnaire_especes = new OdsEspece($this->config);
$gestionnaire_communes = new OdsCommune($this->config);
$especes = $gestionnaire_especes->getToutesEspeces();
$evenements = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_EVENEMENTS);
$resultats_formates = array();
foreach($tableau_observations_infos as $observations_infos) {
$id_espece = $observations_infos['oi_ce_espece'];
$nom_espece = $especes[$id_espece]['nom_scientifique'];
$id_evenement = $observations_infos['oo_ce_evenement'];
$chaine_evenement = $evenements[$id_evenement]['ot_cle'];
$infos_evenement = $this->renvoyerInformationStadeAPartirChaineTriple($chaine_evenement);
$date_observation_formatee = date($this->config['appli']['format_date'], strtotime($observations_infos['oo_date']));
$id_observation = $observations_infos['oo_id_observation'];
$infos_formatees = array(
'date' => $date_observation_formatee,
'evenenement' => $infos_evenement['nom'],
'code_bbch' => $infos_evenement['numero'],
'nom_scientifique' => $nom_espece,
);
$id_station = $observations_infos['oi_ce_station'];
if(!isset($resultats_formates[$id_station])) {
$resultats_formates[$id_station]['station'] = $observations_infos['os_nom'];
$resultats_formates[$id_station]['code_commune'] = $observations_infos['os_ce_commune'];
//$resultats_formates[$id_station]['nom_commune'] = $gestionnaire_communes->obtenirNomCommuneParCodeInsee($observations_infos['os_ce_commune']);
$resultats_formates[$id_station]['latitude'] = $observations_infos['os_latitude'];
$resultats_formates[$id_station]['longitude'] = $observations_infos['os_longitude'];
$resultats_formates[$id_station]['altitude'] = $observations_infos['os_altitude'];
$resultats_formates[$id_station]['milieu'] = $observations_infos['os_milieu'];
$resultats_formates[$id_station]['participant'] = $observations_infos['os_ce_participant'];
}
$resultats_formates[$id_station]['obs'][$id_observation] = $infos_formatees;
}
return $resultats_formates;
}
public function getExportObservationCsv() {
$utilisateur = new Utilisateur();
$est_admin = $utilisateur->estAdmin();
if($est_admin) {
$donnees = $this->getExportObservation();
$donnees_formatees = $this->formaterPourExportCSV($donnees);
$chaine_csv = $this->convertirTableauAssocVersCSV($donnees_formatees);
$this->envoyerFichier($chaine_csv);
} else {
echo "echo non non non non non, vous êtes pas admin !!!!";
}
}
public function formaterPourExportCSV($tableau_observations_infos) {
$gestionnaire_especes = new OdsEspece($this->config);
$gestionnaire_communes = new OdsCommune($this->config);
$especes = $gestionnaire_especes->getToutesEspeces();
$evenements = $this->obtenirValeursListeParAbreviation(self::ABBR_LISTE_EVENEMENTS);
$resultats_formates = array();
foreach($tableau_observations_infos as $observations_infos) {
$id_espece = $observations_infos['oi_ce_espece'];
$nom_espece = $especes[$id_espece]['nom_scientifique'];
$id_evenement = $observations_infos['oo_ce_evenement'];
$chaine_evenement = $evenements[$id_evenement]['ot_cle'];
$infos_evenement = $this->renvoyerInformationStadeAPartirChaineTriple($chaine_evenement);
$date_observation_formatee = date($this->config['appli']['format_date'], strtotime($observations_infos['oo_date']));
$id_observation = $observations_infos['oo_id_observation'];
$infos_formatees = array(
'id_observation' => $id_observation,
'date' => $date_observation_formatee,
'evenenement' => $infos_evenement['nom'],
'code_bbch' => $infos_evenement['numero'],
'nom_scientifique' => $nom_espece,
'station' => $observations_infos['os_nom'],
'code_commune' => $observations_infos['os_ce_commune'],
'nom_commune' => $gestionnaire_communes->obtenirNomCommuneParCodeInsee($observations_infos['os_ce_commune']),
'latitude' => $observations_infos['os_latitude'],
'longitude' => $observations_infos['os_longitude'],
'altitude' => $observations_infos['os_altitude'],
'milieu' => $observations_infos['os_milieu'],
'participant' => $observations_infos['os_ce_participant']
);
$resultats_formates[] = $infos_formatees;
}
return $resultats_formates;
}
public function convertirTableauAssocVersCSV($tableau) {
$csv = '';
$colonnes = array_keys($tableau[0]);
$csv .= implode(';',$colonnes).";\n";
foreach($tableau as $elements) {
$csv .= implode(';',$elements).";\n";
}
return $csv;
}
public function envoyerFichier($contenu) {
$nom_fichier = "observations_export.csv";
$chemin_fichier = $this->config['appli']['chemin_fichiers_temp'].'/'.$nom_fichier;
file_put_contents($chemin_fichier, $contenu);
$contenu = file_get_contents($chemin_fichier);
$taille_fichier = filesize($chemin_fichier);
unlink($chemin_fichier);
ini_set('zlib.output_compression','Off');
header('Pragma: public');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: must-revalidate, pre-check=0, post-check=0, max-age=0');
header('Content-Tranfer-Encoding: none');
header('Content-Type: application/octetstream; name="'.$nom_fichier.'"');
header('Content-Disposition: attachement; filename="'.$nom_fichier.'"');
header('Content-Length: '.$taille_fichier);
echo $contenu;
exit();
}
private function formaterInformationPourEnvoi($tableauinfos) {
}
}
?>