Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php

/** FONCTIONS D'ASSEMBLAGE DE REQUETE 
* 
* @package eflore-projets
* @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
* @author Jean-Pascal MILCENT <jpm@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-2011 Tela Botanica (accueil@tela-botanica.org)
 */

class RequetesAssemblage {
        
        private $table;
        private $total_resultat;
        private $Bdd;
        private $condition;
        
        public function __construct(BDD $bdd) {
                $this->Bdd = $bdd;
        }

        public function getTotal_resultat() {
                return $this->total_resultat;
        }
        
        public  function retournerRequeteJointure($requete_jointure) {
                $jointure = '';
                if ($requete_jointure !== "") {
                        foreach ($requete_jointure as $req_j) {
                                $jointure .= ' '.$req_j['type'].' '.$req_j['table'].' ON '
                                .implode(' AND', $req_j['on']);
                        }
                }
                return $jointure;
        }

        public function assemblerLaRequete(Requete $requete_param) {
                $this->table = $requete_param->table;
                $requete_jointure = $this->retournerRequeteJointure($requete_param->requete_jointure);
                $this->condition = $this->retournerRequeteCondition($requete_param->requete_condition);
                $limites = $this->delimiterResultatsRequete($requete_param->limite_besoin, $requete_param->limite_requete);
                $requete =      ' SELECT '.$requete_param->champs_recherches.' FROM '.$requete_param->table.' '
                .$requete_jointure .' '.$this->condition.' '.$limites;
                return $requete;
        }

        public function retournerRequeteCondition($requete_condition) {
                $condition = '';
                if ($requete_condition !== "") {
                        $condition = ' WHERE '.implode(' AND ', $requete_condition);
                }
                return $condition;
        }

        public function calculerTotalResultat() {
                $requete = 'SELECT count(*) as nombre FROM '.$this->table.' '.$this->condition;
                $res = $this->Bdd->recuperer($requete);
                if ($res) {
                        $this->total_resultat = $res['nombre'];
                } else {
                        $this->total_resultat = 0;
                        $e = 'Données introuvables dans la base';
                        throw new Exception($e, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
                }
        }

        public function delimiterResultatsRequete($limite_besoin, $limite_requete) {
                $this->calculerTotalResultat();
                $requete_limite = '';
                if ($limite_besoin)  {
                        if (($limite_requete['depart'] <=  $this->total_resultat) ){
                                if (($limite_requete['limite'] + $limite_requete['depart'] )
                                < $this->total_resultat  ){
                                $requete_limite = 'LIMIT '.$limite_requete['depart'].', '
                                .$limite_requete['limite'];
                                }
                        } else {
                                $e = "Erreur : la valeur pour le paramètre navigation.départ est supérieure".
                                " au nombre total de résultats.";
                                throw new Exception($e, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
                        }
                }
                return $requete_limite;
        }
}
?>