projet = chaine [par défaut : Biodiversite34] * Indique quel projet nous voulons charger * * @author Jean-Pascal MILCENT * @license GPL v3 * @license CECILL v2 * @version $Id$ * @copyright Copyright (c) 2010, 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'; private $NS_PROJET_VERSION = '1.01'; const EFLORE_API_VERSION = '0.1'; private $NS_PROJET = 'bdtfx'; private $projetsVersions = array(); private $projet = null; private $configProjet = null; /** * Méthode appelée par défaut pour charger ce widget. */ public function executer() { $retour = null; extract($this->parametres); $this->projet = isset($projet) ? $projet : self::PROJET_DEFAUT; $this->chargerConfigProjet(); $this->chargerProjetsVersion(); $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)) { $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 chargerProjetsVersion() { if (isset($this->configProjet)) { foreach ($this->configProjet as $config => $valeur) { if(strstr($config,'.version')) { $this->projetsVersions[str_replace('.version', '', $config)] = $valeur; } } } } public function executerWidget() { $referentiel_impose = false; if (isset($_GET['referentiel']) && $_GET['referentiel'] != '') { $this->NS_PROJET = isset($_GET['referentiel']) && $_GET['referentiel'] != '' ? $_GET['referentiel'] : $this->NS_PROJET; $this->NS_PROJET_VERSION = $this->projetsVersions[$this->NS_PROJET]; $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'] = isset($_GET['titre']) ? $_GET['titre'] : 'defaut'; $widget['donnees']['titre'] = ($widget['donnees']['titre'] == '0') ? '' : $widget['donnees']['titre']; // cas du projet par défaut ou bien d'un projet n'ayant pas de squelette spécifique if ($this->projet == 'defaut' || $this->projet == 'florileges' || $this->projet == 'sauvages' || !$this->projetASquelette()) { $urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl']; $urlWsNs = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, $this->NS_PROJET, 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_PROJET.':'.$this->NS_PROJET_VERSION; $widget['donnees']['ns_projet'] = $this->NS_PROJET; $widget['donnees']['ns_version'] = $this->NS_PROJET_VERSION; $widget['donnees']['referentiel_impose'] = $referentiel_impose; $widget['donnees']['projets_versions'] = $this->projetsVersions; $widget['donnees']['espece_imposee'] = false; $widget['donnees']['nn_espece_defaut'] = ''; $widget['donnees']['nom_sci_espece_defaut'] = ''; $widget['donnees']['infos_espece'] = '{}'; if ($this->especeEstImposee()) { $nom = $this->executerChargementInfosTaxon($_GET['num_nom']); $widget['donnees']['espece_imposee'] = true; $widget['donnees']['nn_espece_defaut'] = $_GET['num_nom']; $widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_sci']; $widget['donnees']['infos_espece'] = $this->array2js($nom, true); } } if ($this->projet != 'defaut' || $this->projetASquelette()) { if ($this->projet == 'florileges' || $this->projet == 'sauvages') { $widget['donnees']['taxons'] = $this->recupererListeNoms(); } else { $widget['donnees']['taxons'] = $this->recupererListeNomsSci(); } $widget['donnees']['milieux'] = $this->parserMilieux(); } return $widget; } 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 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) { $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'] != ''); } private function executerChargementInfosTaxon($num_nom) { $url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->NS_PROJET, $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; } } ?>