Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php
class NomDAO {
        private $bdd = null;
        private $versions = null;
        private $requeteNbreNomsTotal = null;

        public function __construct(Ressources $ressources, Parametres $parametres, Bdd $bdd, Versions $versions) {
                $this->ressources = $ressources;
                $this->parametres = $parametres;
                $this->bdd = $bdd;
                $this->versions = $versions;
        }

        public function rechercherInfosNom() {
                $table = $this->getTable();
                $detailsId = $this->ressources->getDetailsId();
                $detailsId = $this->bdd->proteger($detailsId);
                $requete =
                                'SELECT ns.*,  '.
                                '       nr.nom_sci AS nr_nom_sci, nb.nom_sci AS nb_nom_sci '.
                                "FROM $table AS ns ".
                                "       LEFT JOIN $table AS nr ON (ns.num_nom_retenu = nr.num_nom) ".
                                "       LEFT JOIN $table AS nb ON (ns.basionyme = nb.num_nom) ".
                                "WHERE ns.num_nom = $detailsId ";
                $resultats = $this->bdd->recuperer($requete);
                $nom = new NomDO($resultats);
                return $nom;
        }

        public function rechercher() {
                $clause = $this->getClauseSelectSpeciale();
                $table = $this->getTable();
                $conditions = $this->getConditions();
                $where = $this->getWhere($conditions);
                $navigation = $this->getNavigation();

                $requete = "SELECT $clause ns.*, ".
                        '       nr.nom_sci AS nr_nom_sci, nb.nom_sci AS nb_nom_sci '.
                        "FROM $table AS ns ".
                        "       LEFT JOIN $table AS nr ON (ns.num_nom_retenu = nr.num_nom) ".
                        "       LEFT JOIN $table AS nb ON (ns.basionyme = nb.num_nom) ".
                        $where.' '.$conditions.' '.
                        'ORDER BY ns.nom_sci ASC '.
                        "LIMIT $navigation ";
                $this->requeteNbreNomsTotal = $this->transformerRequetePourNbreNomsTotal($requete);
                $resultats = $this->bdd->recupererTous($requete);

                return $resultats;
        }

        public function rechercherFloue() {
                $clause = $this->getClauseSelectSpeciale();
                $table = $this->getTable();
                $masque = $this->parametres->getMasquePourBdd();
                $where = $this->getWhere();
                $navigation = $this->getNavigation();

                $requete = "SELECT $clause ns.*, ".
                        '       nr.nom_sci AS nr_nom_sci, nb.nom_sci AS nb_nom_sci '.
                        "FROM $table AS ns ".
                        "       LEFT JOIN $table AS nr ON (ns.num_nom_retenu = nr.num_nom) ".
                        "       LEFT JOIN $table AS nb ON (ns.basionyme = nb.num_nom) ".
                        $where .
                        ($masque ? ($where ? ' AND ' : ' WHERE ').
                                "       (SOUNDEX(ns.nom_sci) = SOUNDEX($masque)) ".
                                "       OR (SOUNDEX(REVERSE(ns.nom_sci)) = SOUNDEX(REVERSE($masque))) " : '').
                        'ORDER BY ns.nom_sci ASC '.
                        "LIMIT $navigation ";
                $this->requeteNbreNomsTotal = $this->transformerRequetePourNbreNomsTotal($requete);
                $resultats = $this->bdd->recupererTous($requete);
                return $resultats;
        }

        private function getClauseSelectSpeciale() {
                $clause = (Config::get('bdd_protocole') == 'mysql') ? 'SQL_CALC_FOUND_ROWS' : '';
                return $clause;
        }

        private function getTable() {
                $versions = $this->versions->getVersions();
                $derniereVersion = end($versions);
                $projetNom = strtolower($this->ressources->getProjetNom());
                return $projetNom.'_v'.$derniereVersion;
        }

        private function getConditions() {
                $masquesStrictes = array('nn', 'rg');
                $paramsMasque = array(
                        '' => 'nom_sci',
                        'nn' => 'num_nom',
                        'rg' => 'rang',
                        'sg' => 'nom_supra_generique',
                        'gen' => 'genre',
                        'sp' => 'epithete_sp',
                        'ssp' => 'epithete_infra_sp',
                        'au' => 'auteur',
                        'an' => 'annee');

                $operateurParDefaut = $this->getOperateurCondition();
                $conditionsSql = array();
                foreach ($paramsMasque as $typeMasque => $champ) {
                        $operateur = in_array($typeMasque, $masquesStrictes) ? '=' : $operateurParDefaut;
                        if ($valeurMasque = $this->parametres->getMasquePourBdd($typeMasque)) {
                                $conditionsSql[] = "ns.$champ $operateur $valeurMasque";
                        }
                }
                return implode(' AND ', $conditionsSql);
        }

        private function getOperateurCondition() {
                $operateur = '';
                $recherche = $this->parametres->get('recherche');
                if ($recherche == 'stricte') {
                        $operateur = '=';
                } else if ($recherche == 'etendue') {
                        $operateur = 'LIKE';
                }
                return $operateur;
        }

        private function getWhere($conditions = '') {
                $where = '';
                if ($this->ressources->getServiceNom() == 'taxons') {
                        $where = 'WHERE ns.num_nom = ns.num_nom_retenu ';
                } else if ($conditions != '') {
                        $where = 'WHERE ';
                }
                return $where;
        }

        private function getNavigation() {
                $debut = (int) $this->parametres->get('navigation.depart');
                $nbre = $this->parametres->get('navigation.limite');
                $navigation = "$debut,$nbre";
                return $navigation;
        }

        private function transformerRequetePourNbreNomsTotal($requete) {
                $requete = preg_replace('/SELECT .* FROM/', 'SELECT COUNT(*) AS nbre FROM', $requete);
                $requete = preg_replace('/LIMIT [0-9]+,[0-9]+/', '', $requete);
                return $requete;
        }

        public function recupererNombreNomsTotal() {
                if (Config::get('bdd_protocole') == 'mysql') {
                        $requete = 'SELECT FOUND_ROWS() AS nbre';
                } else {
                        $requete = $this->requeteNbreNomsTotal;
                }

                $nombre = $this->bdd->recuperer($requete);
                return (int) $nombre['nbre'];
        }
}
?>