New file |
0,0 → 1,105 |
<?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.csv'; |
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' |
); |
|
/** |
* Méthode appelée avec une requête de type GET. |
*/ |
public function getElement($params = array()) { |
|
$criteres = $_GET; |
// Seulement les observation publiques |
$criteres['transmission'] = 1; |
$chercheur_observations = new RechercheObservation($this->config); |
|
$numero_page = isset($criteres['debut']) ? $criteres['debut'] : 1; |
$limite = isset($criteres['limite']) ? $criteres['limite'] : 50; |
|
unset($criteres['limite']); |
unset($criteres['debut']); |
|
$observations = $chercheur_observations->rechercherObservations(null, $criteres, $numero_page, $limite); |
$csv = $this->convertirEnCsv($observations); |
$this->envoyerCsv($csv); |
} |
|
private function envoyerCsv($csv) { |
header('Content-Type: text/csv; charset=UTF-8'); |
header('Content-Disposition: attachment;filename='.$this->nom_fichier_export); |
echo $csv; |
} |
|
private function convertirEnCsv($data) |
{ |
$chemin_temp = "php://temp"; |
$outstream = fopen($chemin_temp, 'r+'); |
$intitule_champs = array(); |
foreach($data as $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 creerEntetesChamps($noms_colonnes) { |
$champs_presents = array_intersect_key($this->correspondance_champs, $noms_colonnes); |
return array_values($champs_presents); |
} |
} |
?> |