Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php
/**
* Retourne la liste des noms répertoriés par le projet chorodep
* 
* @package chorodep
* @author Mathias Chouet <mathias@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>
* @version 1.0
* @copyright 1999-2014 Tela Botanica (accueil@tela-botanica.org)
*/

class Noms extends Commun {

        protected $serviceNom = 'noms';
        protected $table;
        protected $masque;
        protected $depart;
        protected $limite;
        protected $tri;
        protected $tri_dir;

        protected function init() {
                $this->masque = array();
                $this->depart = 0;
                $this->limite = 20;
                $this->tri = 'nom_sci';
                $this->tri_dir = 'ASC';
                $this->traiterVersionProjet();
                $this->table = $this->table_version[0];
        }

        /**
         * Récupère les paramètres de navigation
         * @param type $parametres
         */
        protected function setDepartLimite($parametres) {
                if(isset($parametres['navigation.depart']) && $parametres['navigation.depart'] != '') {
                        $this->depart = max(0, intval($parametres['navigation.depart']));
                }
                if(isset($parametres['navigation.limite']) && $parametres['navigation.limite'] != '') {
                        $this->limite = max(0, intval($parametres['navigation.limite']));
                }
        }

        /**
         * Récupère les paramètres de filtrage
         * @param type $parametres
         */
        protected function setMasque($parametres) {
                if(isset($parametres['masque.nom']) && $parametres['masque.nom'] != '') {
                        $this->masque['nom'] = $parametres['masque.nom'];
                }
                if(isset($parametres['masque.zone-geo']) && $parametres['masque.zone-geo'] != '') {
                        $zg = $parametres['masque.zone-geo'];
                        // Tango Corse
                        if ($zg == '2A' || $zg == '2B') {
                                $zg = '20';
                        }
                        $this->masque['zone-geo'] = $zg;
                }
        }

        /**
         * Récupère les paramètres de tri
         * @param type $parametres
         */
        protected function setTri($parametres) {
                if(isset($parametres['retour.tri']) && $parametres['retour.tri'] != '') {
                        $this->tri = $parametres['retour.tri'];
                }
                if(isset($parametres['retour.ordre']) && in_array($parametres['retour.ordre'], array('ASC', 'DESC'))) {
                        $this->tri_dir = $parametres['retour.ordre'];
                }
        }

        public function consulter($ressources, $parametres) {
                $donnees = array();

                $this->setDepartLimite($parametres);
                $this->setMasque($parametres);
                $this->setTri($parametres);
                $noms = $this->listeNoms();
                $total = $this->compterNoms();

                $url_base = Config::get('url_service');

                $masqueEnParams = array();
                foreach ($this->masque as $k => $v) {
                        $masqueEnParams[] = 'masque.' . $k . '=' . $v;
                }
                $masqueEnParams = implode('&', $masqueEnParams);

                $donnees['entete'] = array(
                        'masque' => $masqueEnParams,
                        'total' => $total,
                        'depart' => $this->depart,
                        'limite' => $this->limite
                );
                if ($this->depart > 0) {
                        $donnees['entete']['href.precedent'] = $url_base . '/' . $this->serviceNom . '?'
                                        . 'navigation.depart=' . max(0, ($this->depart - $this->limite)) . '&navigation.limite=' . $this->limite
                                        . '&retour.tri=' . $this->tri . '&retour.ordre=' . $this->tri_dir
                                        . '&' . $masqueEnParams;
                }
                if (($this->depart + $this->limite) < $total ) {
                        $donnees['entete']['href.suivant'] = $url_base . '/' . $this->serviceNom . '?'
                                        . 'navigation.depart=' . ($this->depart + $this->limite) . '&navigation.limite=' . $this->limite
                                        . '&retour.tri=' . $this->tri . '&retour.ordre=' . $this->tri_dir
                                        . '&' . $masqueEnParams;
                }
                $donnees['resultat'] = $noms;

                return $donnees;
        }

        /**
         * Renvoie la liste des noms répertoriés par chorodep; si un masque a été
         * défini sur une zone géographique, retourne aussi la présence sur cette zone
         */
        protected function listeNoms() {
                $req = "SELECT DISTINCT num_nom, nom_sci";
                if (isset($this->masque['zone-geo']) && $this->masque['zone-geo'] != null) {
                        $req .= ", `" . $this->masque['zone-geo'] . "` as presence";
                }
                $req .= " FROM " . $this->table;
                $req .= $this->construireWhere();
                $req .= " ORDER BY ".$this->tri." ".$this->tri_dir." ";
                $req .= " LIMIT " . $this->depart . ", " . $this->limite;

                $resultat = $this->getBdd()->recupererTous($req);

                return $resultat;
        }

        protected function compterNoms() {
                $req = "SELECT count(DISTINCT num_nom, nom_sci) AS compte FROM " . $this->table;
                $req .= $this->construireWhere();
                $resultat = $this->getBdd()->recuperer($req);

                return $resultat['compte'];
        }

        protected function construireWhere() {
                $where = "";
                $conditions = array();
                // élimination des entrées sans nn valide
                $conditions[] = "CAST(num_nom AS decimal) != 0";
                // masque
                if(!empty($this->masque)) {
                        if(isset($this->masque['nom'])) {
                                $masqueNom = $this->getBdd()->proteger($this->masque['nom']);
                                $conditions[] = "nom_sci LIKE $masqueNom";
                        }
                        if(isset($this->masque['zone-geo'])) {
                                $masqueZg = $this->masque['zone-geo'];
                                $conditions[] = "`$masqueZg` in ('1', '1?')";
                        }
                }
                $where = " WHERE " . implode(' AND ', $conditions);
                return $where;
        }
}
?>