Rev 1408 | Rev 1481 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php/*** Service fournissant des informations concernant le CEL au format RSS1, RSS2 ou ATOM.* Encodage en entrée : utf8* Encodage en sortie : utf8* Format du service :* /CelWidgetExport/format* /CelWidgetExport/csv** Les paramêtres :* - "start" indique le numéro du premier item à afficher* - "limit" nombre d'items à afficher** @author Aurélien Peronnet <aurelien@tela-botanica.org>* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>* @version $Id$* @copyright 2012*/class CelWidgetExport extends Cel {private $nom_fichier_export = 'cel_export';private $champs_a_exclure = array('ce_utilisateur' => 'ce_utilisateur','courriel_utilisateur' => 'courriel_utilisateur','transmission' => 'transmission');private $correspondance_champs = array('id_observation' => 'Identifiant Observation','ordre' => 'Ordre Observation','prenom_utilisateur' => 'Prénom','nom_utilisateur' => 'Nom','nom_sel' => 'Nom Sélectionné','nom_sel_nn' => 'Numéro Nomenclatural Nom Selectionné','nom_ret' => 'Nom Retenu','nom_ret_nn' => 'Numéro Nomenclatural Nom Retenu','nt' => 'Numéro Taxonomique','famille' => 'Famille','nom_referentiel' => 'Référentiel Taxonomique','ce_zone_geo' => 'Code Insee','zone_geo' => 'Commune','lieudit' => 'Lieu-Dit','station' => 'Station','milieu' => 'Milieu','latitude' => 'Latitude','longitude' => 'Longitude','geodatum' => 'Référentiel Géographique','date_observation' => 'Date Observation','mots_cles_texte' => 'Mots Clés','commentaire' => 'Commentaires','date_creation' => 'Date Création','date_modification' => 'Date Modification','date_transmission' => 'Date Transmission');private $parametres_autorises = array('utilisateur' => 'courriel_utilisateur','commune' => 'zone_geo','dept' => 'departement','projet' => 'mots_cles','num_taxon' => 'nt','date_debut' => 'date_debut','date_fin' => 'date_fin','taxon' => 'taxon');private $format = 'csv';/*** Méthode appelée avec une requête de type GET.*/public function getElement($params = array()) {$criteres = $this->traiterParametres($_GET);// Seulement les observation publiques$criteres['transmission'] = 1;$chercheur_observations = new RechercheObservation($this->config);$numero_page = isset($criteres['debut']) ? $criteres['debut'] : 0;$limite = isset($criteres['limite']) ? $criteres['limite'] : 0;unset($criteres['limite']);unset($criteres['debut']);$observations = $chercheur_observations->rechercherObservations(null, $criteres, $numero_page, $limite);$observations = array_slice($observations, 0, 10000);//echo count($observations);exit;switch($this->format) {case 'csv':$csv = $this->convertirEnCsv($observations);$this->envoyerCsv($csv);break;case 'xls':$xls = $this->convertirEnXls($observations);$this->envoyerXls($xls);break;default:}}protected function traiterParametres(Array $parametres) {$parametres_traites = array();$this->format = (isset($parametres['format']) && $parametres['format'] != '') ? $parametres['format'] : $this->format;foreach($parametres as $cle => $valeur) {if(trim($valeur) != '' && isset($this->parametres_autorises[$cle])) {$parametres_traites[$this->parametres_autorises[$cle]] = $valeur;}}return $parametres_traites;}private function envoyerCsv($csv) {header('Content-Type: text/csv; charset=UTF-8');header('Content-Disposition: attachment;filename='.$this->nom_fichier_export.'.csv');echo $csv;exit;}private function envoyerXls($workbook) {$workbook->close();exit;}private function convertirEnCsv($data){$chemin_temp = "php://temp";$outstream = fopen($chemin_temp, 'r+');$intitule_champs = array();foreach($data as $ligne) {$ligne = $this->filtrerDonneesSensibles($ligne);$ligne = array_diff_key($ligne, $this->champs_a_exclure);if(empty($intitule_champs)) {$intitule_champs = $this->creerEntetesChamps($ligne);fputcsv($outstream, $intitule_champs, ',', '"');}fputcsv($outstream, $ligne, ',', '"');}rewind($outstream);$csv = stream_get_contents($outstream);fclose($outstream);return $csv;}private function convertirEnXls($data) {$this->extendSpreadsheetProductor = new SpreadsheetProductor();$this->extendSpreadsheetProductor->initSpreadsheet();$workbook = new Spreadsheet_Excel_Writer();$worksheet = $workbook->addWorksheet('Liste');$workbook->send($this->nom_fichier_export.'.xls');$nb_lignes = 1;foreach($data as $ligne) {$ligne = $this->filtrerDonneesSensibles($ligne);$ligne = array_diff_key($ligne, $this->champs_a_exclure);if(empty($intitule_champs)) {$intitule_champs = $this->creerEntetesChamps($ligne);$indice = 0;foreach ($intitule_champs as $intitule) {$worksheet->write(0,$indice,$intitule_champs[$indice]);$indice++;}}$indice = 0;foreach($ligne as $champ) {$worksheet->write($nb_lignes,$indice,$champ);$indice++;}$nb_lignes++;}return $workbook;}private function creerEntetesChamps($noms_colonnes) {$champs_presents = array_intersect_key($this->correspondance_champs, $noms_colonnes);return array_values($champs_presents);}private function filtrerDonneesSensibles($ligne) {if(stripos($ligne['mots_cles_texte'], 'sensible') !== false) {$ligne['latitude'] = '';$ligne['longitude'] = '';}return $ligne;}private function nettoyerChaine($chaine) {$chaine = str_replace("\n",' ',$chaine);$chaine = str_replace("\t",'',$chaine);return $chaine;}}?>