Subversion Repositories eFlore/Applications.cel

Rev

Rev 2458 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
// declare(encoding='UTF-8');
/**
 * Service d'export vers feuille de calcul d'une sélection de relevés.
 *
 * @internal   Mininum PHP version : 5.2
 * @category   CEL
 * @package    Services
 * @subpackage Observations
 * @version    0.1
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
 * @author     Aurelien 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>
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
 */

set_include_path(get_include_path() . PATH_SEPARATOR . dirname(dirname(realpath(__FILE__))) . '/lib');
// la sortie est binaire (xls), mais OLE n'est pas compatible E_ALL en PHP-5.4
error_reporting(error_reporting() ^ E_STRICT);
require_once 'lib/OLE.php';
require_once 'lib/Spreadsheet/Excel/Writer.php';

class InventoryExport extends Cel  {

        private $extendSpreadsheetProductor;

        public function __construct($config) {
                parent::__construct($config);

                $this->extendSpreadsheetProductor = new SpreadsheetProductor();
                $this->extendSpreadsheetProductor->initSpreadsheet();
        }

        public function getElement($uid){
                // Controle detournement utilisateur
                $this->controleUtilisateur($uid[0]);

                $criteres = $_GET;
                $chercheur_observations = new RechercheObservation($this->config);

                // Creating a workbook
                $workbook = new Spreadsheet_Excel_Writer();
                $workbook->setVersion(8);
                $workbook->send('liste.xls');

                // Creating a worksheet
                $worksheet = $workbook->addWorksheet('Liste');
                $worksheet->setInputEncoding('utf-8');
                $worksheet->write(0,0,'Espece');
                $worksheet->write(0,1,'Numero nomenclatural');
                $worksheet->write(0,2,'Nom retenu');
                $worksheet->write(0,3,'Numero nomenclatural nom retenu');
                $worksheet->write(0,4,'Numero taxonomique');
                $worksheet->write(0,5,'Famille');
                $worksheet->write(0,6,'Referentiel taxonomique');
                $worksheet->write(0,7,'Commune');
                $worksheet->write(0,8,'Identifiant Commune');
                $worksheet->write(0,9,'Date');
                $worksheet->write(0,10,'Lieu-dit');
                $worksheet->write(0,11,'Station');
                $worksheet->write(0,12,'Milieu');
                $worksheet->write(0,13,'Notes');
                $worksheet->write(0,14,'Latitude');
                $worksheet->write(0,15,'Longitude');
                $worksheet->write(0,16,'Referentiel Geographique');
                $worksheet->write(0,17,'Ordre');
                $worksheet->write(0,18,'Identifiant');

                $numero_page = isset($criteres['numero_page']) ? $criteres['numero_page'] : 0;
                $limite = isset($criteres['limite']) ? $criteres['limite'] : 0;

                $observations = $chercheur_observations->rechercherObservations($uid[0], $criteres, $numero_page, $limite)->get();
                if (!$observations) {
                        header('HTTP/1.0 204 No Content');
                        exit;
                }

                $ids_obs = array();
                $indices_lignes_obs = array();

                $i = 1;
                foreach ($observations as &$obs) {
                        $obs = $this->denullifierTableauValeurCel($obs);
                        $ids_obs[] = $obs['id_observation'];
                        $indices_lignes_obs[$obs['id_observation']] = $i;

                        if ($obs['date_observation'] != '0000-00-00 00:00:00') {
                                $obs['date_observation'] = $this->formaterDate($obs['date_observation']);
                        } else {
                                $obs['date_observation'] = '00/00/0000';
                        }

                        $worksheet->write($i,0,$obs['nom_sel']);
                        $worksheet->write($i,1,$obs['nom_sel_nn']);
                        $worksheet->write($i,2,$obs['nom_ret']);
                        $worksheet->write($i,3,$obs['nom_ret_nn']);
                        $worksheet->write($i,4,$obs['nt']);
                        $worksheet->write($i,5,$obs['famille']);
                        $worksheet->write($i,6,$obs['nom_referentiel']);
                        $worksheet->write($i,7,$obs['zone_geo']);
                        $worksheet->write($i,8,$this->convertirCodeZoneGeoVersDepartement($obs['ce_zone_geo']));
                        $worksheet->write($i,9,$obs['date_observation']);
                        $worksheet->write($i,10,$obs['lieudit']);
                        $worksheet->write($i,11,$obs['station']);
                        $worksheet->write($i,12,$obs['milieu']);
                        $worksheet->write($i,13,$obs['commentaire']);
                        $worksheet->write($i,14,$obs['latitude']);
                        $worksheet->write($i,15,$obs['longitude']);
                        $worksheet->write($i,16,$obs['geodatum']);
                        $worksheet->write($i,17,$obs['ordre']);
                        $worksheet->write($i,18,$obs['id_observation']);
                        $i++;
                }

                $indice_dernier_champ_supp = 19;
                $indices_champs_supp = array();
                $gestion_champs_etendus = new GestionChampsEtendus($this->config, 'obs');
                $champs_supp_par_obs = $gestion_champs_etendus->consulterParLots($ids_obs);

                foreach ($champs_supp_par_obs as $id_obs => &$champs_supp) {
                        foreach ($champs_supp as  &$champ_etendu) {
                                $cle = $champ_etendu->cle;
                                $label = $champ_etendu->label;
                                $valeur = $champ_etendu->valeur;
                                if(!isset($indices_champs_supp[$cle])) {
                                        $indices_champs_supp[$cle] = $indice_dernier_champ_supp;
                                        $worksheet->write(0, $indice_dernier_champ_supp, utf8_decode($label));
                                        $indice_dernier_champ_supp++;
                                }
                                $num_ligne_obs = $indices_lignes_obs[$id_obs];
                                $worksheet->write($num_ligne_obs,$indices_champs_supp[$cle],utf8_decode($valeur));
                        }
                }

                $workbook->close();
                exit();
        }
}