Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php
class GenerateurNomSciHtml {
        protected $bdd = null;
        protected $table = null;

        protected $num = null;
        protected $compo_nom = array();

        protected $abbr = array (
                        'infra-gen.'    =>      'Infra-Genre',
                        'sect.'                 => 'Section',
                        'subsect.'              => 'Sous-Section',
                        'ser.'                  => 'Série',
                        'subser.'               => 'Sous-Série',
                        'gr.'                   => 'Groupe',
                        'agg.'                  => 'Agrégat',
                        'sp.'                   => 'Espèce',
                        'subsp.'                => 'Sous-Espèce',
                        'infra-sp.'             => 'Infra-Espèce',
                        'var.'                  => 'Variété',
                        'subvar.'               => 'Sous-Variété',
                        'fa'                    => 'Forme',
                        'subf.'                 => 'Sous-Forme',
                        'f. sp.'                => 'Forma species',
                        'proles'                => 'Race prole'
        );

        private $nomSciTpl = '<span class="sci">%s</span>';

        private $nomSupraGenTpl = '<span class="supra_gen">%s</span>';
        private $genTpl = '<span class="gen">%s</span>';
        private $infraGenTpl = '<span class="infra-gen">%s</span>';
        private $spFHTpl = '<span class="gen">%s</span> <span class="sp">%s</span>';
        private $typeEpitheteTpl = '<abbr class="type_epithete" title="%s">%s</abbr>';
        private $infraSpFHTpl = '<span class="gen">%s</span> <span class="sp">%s</span> <abbr class="type-epithete" title="%s">%s</abbr> <span class="infra-sp">%s</span>';

        private $hybrideTpl = '<span class="hyb">× <span class="%s">%s</span></span>';
        private $chimereTpl = '<span class="chimere">+ <span class="%s">%s</span></span>';
        private $formuleHybTpl = '<span class="formule-hyb">%s</span>';
        private $hybriditeTpl = '<span class="%s">%s</span>';

        private $gpGxAvecCvarTpl = '<span class="gp">%s <abbr title="grex">gx</abbr>(%s <abbr title="groupe">Gp</abbr>)</span>';
        private $gpGxSansCvarTpl = '<span class="gp">%s <abbr title="grex">gx</abbr>%s <abbr title="groupe">Gp</abbr></span>';
        private $gxTpl = '<span class="gp">%s <abbr title="grex">gx</abbr></span>';
        private $gpAvecCvarTpl = '<span class="gp">(%s <abbr title="groupe">Gp</abbr>)</span>';
        private $gpSansCvarTpl = '<span class="gp">%s <abbr title="groupe">Gp</abbr></span>';
        private $commTpl = '<span class="commercial">%s</span>';
        private $cvarTpl = '<span class="cultivar">\'%s\'</span>';

        public function generer(Array $nomsDecomposes) {
                $nomsSciHtml = array();
                foreach ($nomsDecomposes as $infos) {
                        $nom = $this->genererNomSciHtml($infos);
                        if ($nom != '') {
                                $nomsSciHtml[$this->num] = $nom;
                        }
                }
                return $nomsSciHtml;
        }

        public function genererNomSciHtml(Array $nomDecomposes) {
                $this->initialiserVariables($nomDecomposes);

                $nomSciHtml = '';
                $nomSciHtml .= $this->ajouterBaliseNomSupraGen();
                $nomSciHtml .= $this->verifierHybridite($this->compo_nom['genre'], 'gen');
                $nomSciHtml .= $this->ajouterBaliseInfraGen();
                $nomSciHtml .= $this->verifierHybridite($this->compo_nom['epithete_sp'], 'sp');
                $nomSciHtml .= $this->ajouterBaliseTypeInfraSp();
                $nomSciHtml .= $this->verifierHybridite($this->compo_nom['epithete_infra_sp'], 'infra-sp');
                $nomSciHtml .= $this->ajouterCultivarGpComm();

                if ($nomSciHtml != '') {
                        $nomSciHtml = sprintf($this->nomSciTpl, trim($nomSciHtml));
                } else {
                        $nomSciHtml = $this->verifierHybridite($this->compo_nom['nom_sci'], 'infra-sp');
                }
                return $nomSciHtml;
        }

        private function initialiserVariables($infos) {
                $this->num = $infos['num_nom'];
                $this->compo_nom = $infos;
        }

        private function ajouterBaliseNomSupraGen() {
                $html = '';
                if ($this->compo_nom['nom_supra_generique'] != '') {
                        $html = sprintf($this->nomSupraGenTpl, $this->compo_nom['nom_supra_generique']);
                }
                return $html;
        }

        private function ajouterTypeEpithete($type) {
                if (!array_key_exists($type, $this->abbr)) {
                        $this->abbr[$type] = $type;
                }
        }

        private function ajouterBaliseInfraGen() {
                $html = '';
                if ($this->verifierTypeInfraGen()) {
                        $html = $this->ajouterBaliseTypeInfraGen();
                } else {
                        if ($this->avoirInfo('epithete_infra_generique')) {
                                $html = sprintf($this->infraGenTpl, $this->compo_nom['epithete_infra_generique']);
                        }
                }
                return $html;
        }

        private function verifierTypeInfraGen() {
                $ok = false;
                if ($this->compo_nom['type_epithete'] != '' && $this->compo_nom['epithete_infra_generique'] != '') {
                        $this->ajouterTypeEpithete($this->compo_nom['type_epithete']);
                        $ok = true;
                }
                return $ok;
        }

        private function ajouterBaliseTypeInfraGen() {
                $html = '';
                $type = $this->compo_nom['type_epithete'];

                if ($type == 'agg.') {
                        // Ajout de l'infra gen avant le type s'il est égal à agg.
                        $html = ' '.$this->ajouterBaliseInfraGen().
                                        ' '.sprintf($this->typeEpitheteTpl, $this->abbr[$type], $type);
                } else {
                        $html = ' '.sprintf($this->typeEpitheteTpl, $this->abbr[$type], $type).
                                        ' '.$this->ajouterBaliseInfraGen();
                }
                return $html;
        }

        private function ajouterBaliseTypeInfraSp() {
                $html = '';
                $type = $this->compo_nom['type_epithete'];
                $infraSp = $this->compo_nom['epithete_infra_sp'];

                if ($infraSp != '') {
                        if ($type != '') {
                                $this->ajouterTypeEpithete($type);
                                $html = ' '.sprintf($this->typeEpitheteTpl, $this->abbr[$type], $type);
                        } else {
                                $message = "Nom #{$this->num} contient un épithète infra-spécifique mais son type n'est pas renseigné.";
                                throw new Exception($message);
                        }
                }
                return $html;
        }

        private function ajouterCultivarGpComm() {
                $html = '';
                if ($this->avoirInfo('cultivar_groupe')) {
                        $html .= ' '.$this->ajouterCultivarGroupe();
                }
                if ($this->avoirInfo('nom_commercial')) {
                        $html .= ' '.sprintf($this->commTpl, $this->compo_nom['nom_commercial']);
                }
                if ($this->avoirInfo('cultivar')) {
                        $html .= ' '.sprintf($this->cvarTpl, $this->compo_nom['cultivar']);
                }
                return $html;
        }

        private function avoirInfo($valeur) {
                return (isset($this->compo_nom[$valeur]) && $this->compo_nom[$valeur] != '') ? true : false;
        }

        /**
         * Permet d'ajouter les groupes de cultivar en fonction de la présence d'un cultivar et de la présence d'un grex
         *
         * L'ensemble des individus obtenu par une fécondation particulière s'appelle un le Grex (que pour les orchidées).
         * Au sein du grex, certains individus peuvent se distinguer par des formes, des coloris ou autres qui font que
         * l'obtenteur du grex va les sélectionner.
         * les noms de groupes de cultivars sont suivis de l'abréviation « Gp » et placés entre parenthèses
         * les noms de grex, s'ils sont utilisés devant une épithète de cultivar, ne se mettent pas entre parenthèses
         *       ex : Cymbidium Alexanderi gx 'Westonbirt' (cultivar_groupe = Alexanderi gx) ;
         * les noms de groupe se mettent entre parenthèses s'ils sont devant une épithète de cultivar
         *       ex : Dracaena fragrans (Deremenis Gp) 'Christianne' (cultivar_groupe = Deremenis)
         * Un grex peut contenir des groupes (rédaction d'un exemple de l'ICNCP)
         *       ex : × Rhyncosophrocattleya Marie Lemon Stick grex Francis Suzuki Group
         *       ou : × Rhyncosophrocattleya Marie Lemon Stick gx Francis Suzuki Gp
         * @param unknown_type $val
         *
         */
        private function ajouterCultivarGroupe() {
                $html = '';
                // si le champ cultivar_groupe n'est pas vide
                if ($this->avoirInfo('cultivar_groupe')) {
                        $groupe = trim($this->compo_nom['cultivar_groupe']);

                        // Sélection des templates
                        $tplGx = $this->gxTpl;
                        $tplGpGx = $this->gpGxSansCvarTpl;
                        $tplGp = $this->gpSansCvarTpl;
                        // s'il y a un cultivar, on ajoute des parenthèses au groupe (mais pas au grex)
                        if ($this->avoirInfo('cultivar')) {
                                $tplGpGx = $this->gpGxAvecCvarTpl;
                                $tplGp = $this->gpAvecCvarTpl;
                        }

                        // Création du HTML du groupe de cultivar
                        if (strrpos($groupe, ' gx ') !== false) {//si le grex est composé de groupe
                                $gpEtGx = explode(' gx ', $groupe);
                                $html = sprintf($tplGpGx, $gpEtGx[0], $gpEtGx[1]);
                        } else if (preg_match('/ gx$/', $groupe)) {//s'il y a un grex et pas de groupe
                                $gx = str_replace(' gx', '', $groupe);
                                $html = sprintf($tplGx, $gx);
                        } else { //s'il n'y a pas de grex mais un groupe
                                $html = sprintf($tplGp, $groupe);
                        }
                }
                return $html;
        }

        /**
         *
         * Permet de repérer s'il s'agit d'un hybride (infra-sp, genre, sp) ou d'une chimère.
         * @param unknown_type $val
         * @param unknown_type $type
         */
        private function verifierHybridite($epithete, $type) {
                $html = '';
                if ($epithete != '') {
                        if (substr($epithete, 0, 2) == 'x ') {
                                $hybride = str_replace('x ', '', $epithete);
                                $html = ' '.sprintf($this->hybrideTpl, $type, $hybride);
                        } elseif (substr($epithete, 0, 2) == '+ ') {
                                $hybride = str_replace('+ ', '', $epithete);
                                $html = ' '.sprintf($this->chimereTpl, $type, $hybride);
                        } else if (substr_count($epithete, ' x ') > 1) {// Cas d'une formule d'hybridité comprenant des parents hybrides
                                $html = ' '.$this->insererBaliseFormuleHyb($epithete);
                        } elseif (substr_count($epithete, ' x ') == 1) {// Cas d'une formule d'hybridité simple
                                $html = ' '.$this->ajouterFomuleHybridite($epithete, $type);
                        } else {// Autre cas...
                                $html = ' '.sprintf($this->hybriditeTpl, $type, $epithete);
                        }
                }
                return $html;
        }

        private function ajouterFomuleHybridite($formule, $type) {
                $tab_x = explode(' x ', $formule);
                $formule_hyb = array();
                switch ($type) {
                        case 'gen'              :
                                foreach ($tab_x as $hyb) {
                                        $formule_hyb[] = sprintf($this->genTpl, $hyb);
                                }
                                break;
                        case 'sp'               :
                                foreach ($tab_x as $hyb) {
                                        if (substr_count($hyb, ' ') >= 1) {
                                                list($gen, $sp) = explode(' ', $hyb);
                                                $formule_hyb[] = sprintf($this->spFHTpl, $gen, $sp);
                                        } else if (preg_match('/^[A-Z]/', $hyb)) {
                                                $gen = $hyb;
                                                $formule_hyb[] = sprintf($this->genTpl, $gen);
                                        }
                                }
                                break;
                        case 'infra-sp' :
                                foreach ($tab_x as $hyb) {
                                        list($gen, $sp, $typeEpithete, $infraSp) = explode (' ', $hyb);
                                        if (isset($infraSp)) {
                                                $formule_hyb[] = sprintf($this->infraSpFHTpl, $gen, $sp, $this->abbr[$typeEpithete], $typeEpithete, $infraSp);
                                        } else {
                                                $formule_hyb[] = sprintf($this->spFHTpl, $gen, $sp);
                                        }
                                }
                                break;
                        default : break;
                }
                return $this->insererBaliseFormuleHyb(implode(' x ', $formule_hyb));
        }

        private function insererBaliseFormuleHyb($formule) {
                return sprintf($this->formuleHybTpl, $formule);
        }
}
?>