Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php
/** Exemple lancement:
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php sauvages -a chargerTous
*/
class Sauvages extends EfloreScript {
        private $contenu_fichier = array();
        protected $parametres_autorises = array(
                        '-f' => array(true, true, 'Nom du fichier ou du dossier à traiter'));

        public function executer() {
                // Lancement de l'action demandée
                try {
                        $this->initialiserProjet('sauvages');

                        $cmd = $this->getParametre('a');
                        switch ($cmd) {
                                case 'chargerTous' :
                                        $this->creerXmlTaxons();
                                        $this->creerXmlCriteres();
                                        break;
                                case 'creerXmlCriteres' :
                                        $this->creerXmlCriteres();
                                        break;
                                case 'creerXmlTaxons' :
                                        $this->creerXmlTaxons();
                                        break;
                                default :
                                        throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
                        }
                } catch (Exception $e) {
                        $this->traiterErreur($e->getMessage());
                }
        }

        public function creerXmlCriteres() {
                $this->recupererContenuFichier();
                $criteres_label = $this->contenu_fichier[0];
                unset($this->contenu_fichier[0]);
                $description_label = $this->contenu_fichier[1];
                unset($this->contenu_fichier[1]);
                $criteres_valeurs = $this->extraireValeurs();
                $xml = $this->$formerXmlCriteres($criteres_label, $description_label, $criteres_valeurs);//print_r($xml);
                file_put_contents("./xml_criteres.xml", $xml);
        }
        
        
        public function formerXmlCriteres($label, $description, $valeurs) {
                $xml='<keys>
                                <groupe id="1">
                                        <nom>Arbres</nom>
                                        <media>images/pictos/icone_arbre.png</media>
                                        <criteres>';
                foreach ($label as $id=>$criteres) {
                        if ($id > 2) {
                                $xml .= '<critere id="1.'.$id.'">
                                                        <label>'.$criteres.'</label>
                                                        <description>'.$description[$id].'</description>
                                                        <valeurs>';
                                foreach ($valeurs[$id] as $id_val=>$valeur) {
                                        if ($valeur != "0") {
                                                $crit = explode(" ", $criteres);
                                                $val = explode(" ", $valeur);
                                                $image = $crit[0]."_".$val[0];
                                                $xml .= '<valeur code="1.'.$id.'.'.$id_val.'" media="images/pictos/'.$image.'.png">'.$valeur.'</valeur>';
                                        }
                                }
                                $xml .= '</valeurs></critere>';
                        }
                }
                $xml .= "</criteres>
                        </groupe>
                        </keys>";
                return $xml;
        }
        
        private function extraireValeurs() {
                $valeurs = array();
                foreach ($this->contenu_fichier as $ligne) {
                        $i = 0;
                        foreach ($ligne as $critere) {
                                if (!isset($valeurs[$i]) || !in_array(trim($critere), $valeurs[$i])) {
                                        $valeurs[$i][] = trim($critere);
                                }
                                $i++;
                        }
                }
                return $valeurs;
        }
        
        
        public function creerXmlTaxons() {
                $this->recupererContenuFichier();
                $criteres_label = array_flip($this->contenu_fichier[0]);
                unset($this->contenu_fichier[0]);
                unset($this->contenu_fichier[1]);
                $criteres_valeurs = $this->extraireValeurs();
                $xml = $this->formerXmlTaxons($criteres_label, $criteres_valeurs); //print_r($xml);
                file_put_contents("./xml_taxons.xml", $xml);
        }
        
        public function formerXmlTaxons($criteres_label, $criteres_valeurs) {
                $xml='<?xml version="1.0" ?><TAXONS SUBJECT="XML">';
                $rest = new RestClient();
                foreach ($this->contenu_fichier as $id=>$taxon) {
                        $infos_taxon = $this->rechercherInfosTaxon($rest, $taxon[$criteres_label["nom scientifique"]]);
                        $xml .= '<TAXON id="'.$id.'" value="'.
                                ucfirst($taxon[$criteres_label["nom vernaculaire"]]).'" sciName="'.$taxon[$criteres_label["nom scientifique"]].'" groupe="1">
                        <DESCRIPTION>'.$infos_taxon["description"].'</DESCRIPTION>
                        <PICTURES>';
                        if ($infos_taxon["images"] != array()) {
                                foreach ($infos_taxon["images"] as $image) {
                                        $xml .= '<PICTURE media="'.$image["binaire.href"].'"><author>'.$image["auteur.libelle"].'</author></PICTURE>';
                                }
                        }
                        $xml .= '</PICTURES><CRITERIAS>';
                        for ($i=3; $i < count($taxon); $i++) {
                                if (trim($taxon[$i]) !== "0") {
                                        $xml .= '<VALUE code="1.'.$i.'.'.array_search(trim($taxon[$i]), $criteres_valeurs[$i]).'"/>';
                                }
                        }
                        $xml .= '</CRITERIAS></TAXON>';
                }
                $xml .= "</TAXONS>";
                return $xml;
        }
        
        private function rechercherInfosTaxon($rest, $ns) {
                $info = array("description" => "", "images" => array());
                $reponse = $this->consulterWebService($rest, "bdtfx", "taxons", "?masque=".urlencode(rtrim($ns)));
                if (is_array($reponse["resultat"])) {
                        $id = key($reponse["resultat"]);
                } else {
                        $reponse = $this->consulterWebService($rest, "bdtfx", "noms", "??retour.champs=nom_retenu.id&masque=".urlencode(rtrim($ns)));
                        if (is_array($reponse["resultat"])) {
                                $id = $reponse["resultat"][key($reponse["resultat"])]["nom_retenu.id"];
                        }
                }
                $reponse = $this->consulterWebService($rest, "coste", "textes", "/bdtfx.nn:".$id);
                if ($reponse['texte'] != ""){
                        $info["description"] = str_replace("**", "", $reponse['texte']);
                        $info["description"] = str_replace("//", "", $info["description"]);
                } else {
                        $reponse = $this->consulterWebService($rest, "baseflor", "informations", "/bdtfx.nn:".$id);
                        $info["description"] = $this->formaterBaseflor($reponse);
                }
                $reponse = $this->consulterWebService($rest, "cel", "images", "?navigation.limite=5&retour.format=CRS&masque.nn=".$id);
                $info["images"] = $reponse["resultats"];
                return $info;
        }
        
        private function formaterBaseflor($reponse) {
                $description = "En cours de rédaction.";
                if ($reponse["idiotaxon"] != "") {
                        $description = $reponse["idiotaxon"]." – ".
                                                        "Formation végétale : ".$reponse["form_vegetale"]."; ".
                                                        "Inflorescence : ".$reponse["inflorescence"]."; ".
                                                        "Couleur de la fleur : ".$reponse["couleur_fleur"]."; ".
                                                        "Sexualité : ".$reponse["sexualite"]."; ".
                                                        "Fruit : ".$reponse["fruit"]."; ".
                                                        "Pollinisation : ".$reponse["pollinisation"]."; ".
                                                        "Dissémination : ".$reponse["dissemination"]."; ".
                                                        "Ecologie : ".$reponse["carac_ecolo"]." – ".
                                                        "Chorologie : ".$reponse["chorologie"]."; ".
                                                        "Floraison :  ".$reponse["floraison"];
                }
                return $description;
        }
        
        private function consulterWebService($rest, $projet,$resssource,$parametres) {
                $url_id = "http://www.tela-botanica.org/service:eflore:0.1/".$projet."/".$resssource.$parametres;
                $reponse = $rest->consulter($url_id);
                return json_decode($reponse, true);
        }
        
        
        private function recupererContenuFichier() {
                $nomFichier = Config::get('dossierCsv').Config::get('projet');
                if ($nomFichier && file_exists($nomFichier) ){
                        $extensionFichier = strtolower(strrchr($nomFichier, '.'));
                        if ($extensionFichier === ".csv"){
                                $file = new SplFileObject($nomFichier);
                                $file->setFlags(SplFileObject::SKIP_EMPTY);
                                $i = 0;
                                echo "Traitement du fichier : ";
                                while (!$file->eof()){
                                        $ligne_csv = $file->fgetcsv($delimiter = ';');
                                                $this->contenu_fichier[$i] = $ligne_csv;
                                        echo str_repeat(chr(8), ( strlen( $i ) + 1 ))."\t".$i++;
                                }
                                echo "\n";
                        } else {
                                $this->traiterErreur("Le fichier : $nomFichier n'est pas au format csv.");
                        }
                } else {
                        $this->traiterErreur("Le fichier : $nomFichier n'existe pas.");
                }
        }
}
?>