| 519 | 
           mathilde | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
              | 
        
        
            | 
            | 
           3 | 
           /** FONCTIONS D'ASSEMBLAGE DE REQUETE
  | 
        
        
            | 
            | 
           4 | 
           *
  | 
        
        
            | 
            | 
           5 | 
           * @package eflore-projets
  | 
        
        
            | 
            | 
           6 | 
           * @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
  | 
        
        
            | 
            | 
           7 | 
           * @author Delphine CAUQUIL <delphine@tela-botanica.org>
  | 
        
        
            | 
            | 
           8 | 
           * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
  | 
        
        
            | 
            | 
           9 | 
           * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
  | 
        
        
            | 
            | 
           10 | 
           * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
  | 
        
        
            | 
            | 
           11 | 
           * @version 1.0
  | 
        
        
            | 
            | 
           12 | 
           * @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
  | 
        
        
            | 
            | 
           13 | 
            */
  | 
        
        
            | 
            | 
           14 | 
              | 
        
        
            | 
            | 
           15 | 
           class RequetesAssemblage {
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           	private $table;
  | 
        
        
            | 
            | 
           18 | 
           	private $total_resultat;
  | 
        
        
            | 
            | 
           19 | 
           	private $Bdd;
  | 
        
        
            | 
            | 
           20 | 
           	private $condition;
  | 
        
        
            | 
            | 
           21 | 
              | 
        
        
            | 
            | 
           22 | 
           	public function __construct(BDD $bdd) {
  | 
        
        
            | 
            | 
           23 | 
           		$this->Bdd = $bdd;
  | 
        
        
            | 
            | 
           24 | 
           	}
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           	public function getTotal_resultat() {
  | 
        
        
            | 
            | 
           27 | 
           		return $this->total_resultat;
  | 
        
        
            | 
            | 
           28 | 
           	}
  | 
        
        
            | 
            | 
           29 | 
              | 
        
        
            | 
            | 
           30 | 
           	public  function retournerRequeteJointure($requete_jointure) {
  | 
        
        
            | 
            | 
           31 | 
           		$jointure = '';
  | 
        
        
            | 
            | 
           32 | 
           		if ($requete_jointure !== "") {
  | 
        
        
            | 
            | 
           33 | 
           			foreach ($requete_jointure as $req_j) {
  | 
        
        
            | 
            | 
           34 | 
           				$jointure .= ' '.$req_j['type'].' '.$req_j['table'].' ON '
  | 
        
        
            | 
            | 
           35 | 
           				.implode(' AND', $req_j['on']);
  | 
        
        
            | 
            | 
           36 | 
           			}
  | 
        
        
            | 
            | 
           37 | 
           		}
  | 
        
        
            | 
            | 
           38 | 
           		return $jointure;
  | 
        
        
            | 
            | 
           39 | 
           	}
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
           	public function assemblerLaRequete(Requete $requete_param) {
  | 
        
        
            | 
            | 
           42 | 
           		$this->table = $requete_param->table;
  | 
        
        
            | 
            | 
           43 | 
           		$requete_jointure = $this->retournerRequeteJointure($requete_param->requete_jointure);
  | 
        
        
            | 
            | 
           44 | 
           		$this->condition = $this->retournerRequeteCondition($requete_param->requete_condition);
  | 
        
        
            | 
            | 
           45 | 
           		$limites = $this->delimiterResultatsRequete($requete_param->limite_besoin, $requete_param->limite_requete);
  | 
        
        
            | 
            | 
           46 | 
           		$requete = 	' SELECT '.$requete_param->champs_recherches.' FROM '.$requete_param->table.' '
  | 
        
        
            | 
            | 
           47 | 
           		.$requete_jointure .' '.$this->condition.' '.$limites;
  | 
        
        
            | 
            | 
           48 | 
           		return $requete;
  | 
        
        
            | 
            | 
           49 | 
           	}
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
           	public function retournerRequeteCondition($requete_condition) {
  | 
        
        
            | 
            | 
           52 | 
           		$condition = '';
  | 
        
        
            | 
            | 
           53 | 
           		if ($requete_condition !== "") {
  | 
        
        
            | 
            | 
           54 | 
           			$condition = ' WHERE '.implode(' AND ', $requete_condition);
  | 
        
        
            | 
            | 
           55 | 
           		}
  | 
        
        
            | 
            | 
           56 | 
           		return $condition;
  | 
        
        
            | 
            | 
           57 | 
           	}
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
           	public function calculerTotalResultat() {
  | 
        
        
            | 
            | 
           60 | 
           		$requete = 'SELECT count(*) as nombre FROM '.$this->table.' '.$this->condition;
  | 
        
        
            | 
            | 
           61 | 
           		$res = $this->Bdd->recuperer($requete);
  | 
        
        
            | 
            | 
           62 | 
           		if ($res) {
  | 
        
        
            | 
            | 
           63 | 
           			$this->total_resultat = $res['nombre'];
  | 
        
        
            | 
            | 
           64 | 
           		} else {
  | 
        
        
            | 
            | 
           65 | 
           			$this->total_resultat = 0;
  | 
        
        
            | 
            | 
           66 | 
           			$e = 'Données introuvables dans la base';
  | 
        
        
            | 
            | 
           67 | 
           			throw new Exception($e, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
  | 
        
        
            | 
            | 
           68 | 
           		}
  | 
        
        
            | 
            | 
           69 | 
           	}
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
           	public function delimiterResultatsRequete($limite_besoin, $limite_requete) {
  | 
        
        
            | 
            | 
           72 | 
           		$this->calculerTotalResultat();
  | 
        
        
            | 
            | 
           73 | 
           		$requete_limite = '';
  | 
        
        
            | 
            | 
           74 | 
           		if ($limite_besoin)  {
  | 
        
        
            | 
            | 
           75 | 
           			if (($limite_requete['depart'] <  $this->total_resultat) &&
  | 
        
        
            | 
            | 
           76 | 
           			(($limite_requete['limite'] + $limite_requete['depart'] )
  | 
        
        
            | 
            | 
           77 | 
           			< $this->total_resultat  )) {
  | 
        
        
            | 
            | 
           78 | 
           				$requete_limite = 'LIMIT '.$limite_requete['depart'].', '
  | 
        
        
            | 
            | 
           79 | 
           				.$limite_requete['limite'];
  | 
        
        
            | 
            | 
           80 | 
           			} else {
  | 
        
        
            | 
            | 
           81 | 
           				$e = "Erreur : la valeur pour le paramètre navigation.départ est supérieure".
  | 
        
        
            | 
            | 
           82 | 
           				" au nombre total de résultats.";
  | 
        
        
            | 
            | 
           83 | 
           				throw new Exception($e, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
  | 
        
        
            | 
            | 
           84 | 
           			}
  | 
        
        
            | 
            | 
           85 | 
           		}
  | 
        
        
            | 
            | 
           86 | 
           		return $requete_limite;
  | 
        
        
            | 
            | 
           87 | 
           	}
  | 
        
        
            | 
            | 
           88 | 
           }
  | 
        
        
            | 
            | 
           89 | 
           ?>
  |