Subversion Repositories eFlore/Applications.cel

Rev

Rev 2498 | Rev 2577 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
// declare(encoding='UTF-8');
/**
 * Widget fournissant des interfaces de saisies simplifiée pour différent projets.
 * Encodage en entrée : utf8
 * Encodage en sortie : utf8
 *
 * Cas d'utilisation et documentation :
 * @link http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=AideCELWidgetSaisie
 *
 * Paramètres :
 * ===> projet = chaine  [par défaut : defaut] : indique le widgetde saisie à charger.
 * ===> mission = chaine  [par défaut : vide] : permet de charger un "sous-widget" vis à vis du projet.
 * Indique quel projet nous voulons charger
 *
 * @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>
 */
class Saisie extends WidgetCommun {

        const DS = DIRECTORY_SEPARATOR;
        const PROJET_DEFAUT = 'defaut';
        const WS_SAISIE = 'CelWidgetSaisie';
        const WS_UPLOAD = 'CelWidgetUploadImageTemp';
        const WS_OBS = 'CelObs';
        const WS_NOM = 'noms';
        const EFLORE_API_VERSION = '0.1';

        private $ns_referentiel = 'bdtfx';
        private $projet = null;
        private $configProjet = null;
        private $configMission = null;

        /**
         * Méthode appelée par défaut pour charger ce widget.
         */
        public function executer() {
                $retour = null;
                extract($this->parametres);

                $this->projet = self::PROJET_DEFAUT;
                if (isset($projet) && trim($projet) != "") {
                        $projets = explode(',', $projet);
                        $this->projet = $projets[0];
                }
                $this->chargerConfigProjet();

                $service = isset($service) ? $service : 'widget';
                $methode = $this->traiterNomMethodeExecuter($service);
                if (method_exists($this, $methode)) {
                        $retour = $this->$methode();
                } else {
                        $this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
                }

                $contenu = null;
                $mime = null;
                if (is_array($retour) && array_key_exists('squelette', $retour)) {
                        $ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html';
                        if ($this->projetASquelette()) {
                                $squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet.self::DS.$retour['squelette'].$ext;
                        } else {
                                $squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'defaut'.self::DS.'defaut'.$ext;
                        }
                        $contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
                        $mime = isset($retour['mime']) ? $retour['mime'] : null;
                } else {
                        if (count($this->messages) == 0) {
                                $this->messages[] = "La méthode du sous-service ne renvoie pas les données dans le bon format.";
                        }
                        $contenu = 'Un problème est survenu : '.print_r($this->messages, true);
                }

                $this->envoyer($contenu, $mime);
        }

        private function chargerConfigProjet() {
                $fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
                if (file_exists($fichier_config)) {
                        if ($this->configProjet = parse_ini_file($fichier_config, true)) {
                                if (isset($_GET['mission'])) {
                                        $mission = strtolower(trim($_GET['mission']));
                                        if (isset($this->configProjet[$mission])) {
                                                $this->configMission = $this->configProjet[$mission];
                                        }
                        }
                        } else {
                                $this->messages[] = "Le fichier ini '$fichier_config' du projet n'a pu être chargé.";
                        }
                } else {
                        $this->debug[] = "Le fichier ini '$fichier_config' du projet n'existe pas.";
                }
        }

        private function projetASquelette() {
                // fonction très simple qui ne teste que si le dossier du projet courant
                // existe, mais elle suffit pour le moment.
                return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet);
        }

        public function executerWidget() {
                $referentiel_impose = false;
                if (isset($_GET['referentiel']) && $_GET['referentiel'] != '' && $_GET['referentiel'] != "autre") {
                        $this->ns_referentiel = isset($_GET['referentiel']) && $_GET['referentiel'] != '' ? $_GET['referentiel'] : $this->ns_referentiel;
                        $referentiel_impose = true;
                }

                $widget['squelette'] = $this->projet;
                $widget['donnees'] = array();
                $widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
                $widget['donnees']['url_ws_saisie'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE);
                $widget['donnees']['url_ws_obs'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_OBS);
                $widget['donnees']['url_ws_upload'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_UPLOAD);
                $widget['donnees']['url_ws_annuaire'] = sprintf($this->config['chemins']['baseURLServicesAnnuaireTpl'], 'utilisateur/identite-par-courriel/');
                $widget['donnees']['url_remarques'] = $this->config['chemins']['widgetRemarquesUrl'];

                $widget['donnees']['logo'] = isset($_GET['logo']) ? $_GET['logo'] : 'defaut';
                $widget['donnees']['titre'] = $this->getTitrePage();

                $widget['donnees']['referentiel_impose'] = $referentiel_impose;
                $widget['donnees']['espece_imposee'] = false;
                $widget['donnees']['nn_espece_defaut'] = '';
                $widget['donnees']['nom_sci_espece_defaut'] = '';
                $widget['donnees']['infos_espece'] = '{}';
                
                $projetsAutorises = $this->transformerEnTableau($this->config['projets']['autorises']);

                $urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
                $urlWsNs = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, $this->ns_referentiel, self::WS_NOM);
                $urlWsNsSansRef = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM);
                $widget['donnees']['url_ws_autocompletion_ns'] = $urlWsNs;
                $widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
                $widget['donnees']['ns_referentiel'] = $this->ns_referentiel;
                
                if ($this->especeEstImposee()) {
                        $nnEspeceImposee = $this->getNnEspeceImposee();
                        $nom = $this->executerChargementInfosTaxon($nnEspeceImposee);
                        $widget['donnees']['espece_imposee'] = true;
                        $widget['donnees']['nn_espece_defaut'] = $nnEspeceImposee;
                        $widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_sci'];
                        $widget['donnees']['infos_espece'] = $this->array2js($nom, true);
                }

                $projetsAListeDeNoms = $this->transformerEnTableau($this->config['projets']['liste_noms']);
                if (in_array($this->projet, $projetsAListeDeNoms)) {
                        $projetsAListeDeNomsSciEtVerna = $this->transformerEnTableau($this->config['projets']['liste_noms_sci_et_verna']);
                        if (in_array($this->projet, $projetsAListeDeNomsSciEtVerna)) {
                                $widget['donnees']['taxons'] = $this->recupererListeNoms();
                        } else {
                                $widget['donnees']['taxons'] = $this->recupererListeNomsSci();
                        }
                }

                // Chargement de la liste des milieux issues du fichier .ini du projet
                $projetsAListeDeMilieux = $this->transformerEnTableau($this->config['projets']['liste_milieux']);
                if (in_array($this->projet, $projetsAListeDeMilieux)) {
                        $widget['donnees']['milieux'] = $this->parserMilieux();
                }

                return $widget;
        }

        private function getTitrePage() {
                $titre = 'defaut';
                if (isset($this->configProjet['titre_page'])) {
                        $titre = $this->configProjet['titre_page'];
                }
                if (isset($this->configMission['titre_page'])) {
                        $titre = $this->configMission['titre_page'];
                }
                if (isset($_GET['titre'])) {
                        $titre = $_GET['titre'];
                }
                if ($titre === 0) {
                        $titre = '';
                }
                return $titre;
        }

        public function executerTaxons() {
                $widget['squelette'] = $this->projet.'_taxons';
                $widget['squelette_ext'] = '.tpl.js';
                $widget['donnees'] = array();
                $nomsAAfficher = $this->recupererListeNomsSci();
                $taxons_tries = array();
                foreach ($nomsAAfficher as $taxon) {
                        $taxons_tries[$taxon['num_nom_sel']] = $taxon;
                }
                $widget['donnees']['taxons'] = json_encode($taxons_tries);
                return $widget;
        }

        private function recupererListeNomsSci() {
                $taxons = $this->recupererListeTaxon();
                if (is_array($taxons)) {
                        $taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
                }
                return $taxons;
        }

        private function recupererListeNoms() {
                $taxons = $this->recupererListeTaxon();
                $nomsAAfficher = array();
                $nomsSpeciaux = array();
                if (is_array($taxons)) {
                        foreach ($taxons as $taxon) {
                                $nomSciTitle = $taxon['nom_ret'].
                                        ($taxon['nom_fr'] != '' ? ' - '.$taxon['nom_fr'] : '' ).
                                        ($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
                                $nomFrTitle = $taxon['nom_sel'].
                                        ($taxon['nom_ret'] != $taxon['nom_sel']? ' - '.$taxon['nom_ret'] : '' ).
                                        ($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );

                                if ($taxon['groupe'] == 'special') {
                                        $nomsSpeciaux[] = array(
                                                'num_nom' => $taxon['num_nom_sel'],
                                                'nom_a_afficher' => $taxon['nom_fr'],
                                                'nom_a_sauver' => $taxon['nom_sel'],
                                                'nom_title' => $nomSciTitle,
                                                'nom_type' => 'nom-special');
                                } else {
                                        $nomsAAfficher[] = array(
                                                'num_nom' => $taxon['num_nom_sel'],
                                                'nom_a_afficher' => $taxon['nom_sel'],
                                                'nom_a_sauver' => $taxon['nom_sel'],
                                                'nom_title' => $nomSciTitle,
                                                'nom_type' => 'nom-sci');
                                        $nomsAAfficher[] = array(
                                                'num_nom' => $taxon['num_nom_sel'],
                                                'nom_a_afficher' => $taxon['nom_fr'],
                                                'nom_a_sauver' => $taxon['nom_fr'],
                                                'nom_title' => $nomFrTitle,
                                                'nom_type' => 'nom-fr');
                                }
                        }
                        $nomsAAfficher = self::trierTableauMd($nomsAAfficher, array('nom_a_afficher' => SORT_ASC));
                        $nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
                }
                return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
        }

        private function recupererListeTaxon() {
                $taxons = null;
                $fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
                if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
                        $taxons = $this->decomposerFichierTsv($fichier_tsv);
                } else {
                        $this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
                }
                return $taxons;
        }

        private function decomposerFichierTsv($fichier, $delimiter = "\t"){
                $header = null;
                $data = array();
                if (($handle = fopen($fichier, 'r')) !== FALSE) {
                        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
                                if (!$header) {
                                        $header = $row;
                                } else {
                                        $data[] = array_combine($header, $row);
                                }
                        }
                        fclose($handle);
                }
                return $data;
        }

        private function parserMilieux() {
                $infosMilieux = array();
                if (isset($this->configProjet['milieux'])) {
                        $milieux = explode('|', $this->configProjet['milieux']);
                        foreach ($milieux as $milieu) {
                                $milieu = trim($milieu);
                                $details = explode(';', $milieu);
                                if (isset($details[1])) {
                                        $infosMilieux[$details[0]] = $details[1];
                                } else {
                                        $infosMilieux[$details[0]] = '';
                                }
                        }
                        ksort($infosMilieux);
                }
                return $infosMilieux;
        }

        private function especeEstImposee() {
                return (isset($_GET['num_nom']) && $_GET['num_nom'] != ''
                        || isset($this->configProjet['sp_imposee']) || isset($this->configMission['sp_imposee']));
        }

        private function getNnEspeceImposee() {
                $nn = null;
                if (isset($_GET['num_nom']) && is_numeric($_GET['num_nom'])) {
                        $nn = $_GET['num_nom'];
                } else if (isset($this->configProjet['sp_imposee'])) {
                        $nn = $this->configProjet['sp_imposee'];
                } else if (isset($this->configMission['sp_imposee'])) {
                        $nn = $this->configMission['sp_imposee'];
                }
                return $nn;
        }

        private function executerChargementInfosTaxon($num_nom) {
                $url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->ns_referentiel, $num_nom);
                $infos = json_decode(file_get_contents($url_service_infos));
                // trop de champs injectés dans les infos espèces peut
                // faire planter javascript
                $champs_a_garder = array('id', 'nom_sci','nom_sci_complet',
                        'famille','nom_retenu.id', 'nom_retenu.libelle', 'num_taxonomique');
                $resultat = array();
                if (isset($infos) && !empty($infos)) {
                        $infos = (array)$infos;
                        if (isset($infos['nom_sci']) && $infos['nom_sci'] != '') {
                                $resultat = array_intersect_key($infos, array_flip($champs_a_garder));
                                $resultat['retenu'] = ($infos['id'] == $infos['nom_retenu.id']) ? "true" : "false";
                        }
                }
                return $resultat;
        }

        private function array2js($array,$show_keys) {
                $tableauJs = '{}';
                if (!empty($array)) {
                        $total = count($array) - 1;
                        $i = 0;
                        $dimensions = array();
                        foreach ($array as $key => $value) {
                                if (is_array($value)) {
                                        $dimensions[$i] = array2js($value,$show_keys);
                                        if ($show_keys) {
                                                $dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
                                        }
                                } else {
                                        $dimensions[$i] = '"'.addslashes($value).'"';
                                        if ($show_keys) {
                                                $dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
                                        }
                                }
                                if ($i == 0) {
                                        $dimensions[$i] = '{'.$dimensions[$i];
                                }
                                if ($i == $total) {
                                        $dimensions[$i].= '}';
                                }
                                $i++;
                        }
                        $tableauJs = implode(',', $dimensions);
                }
                return $tableauJs;
        }
}