| 1497 | jpm | 1 | <?php
 | 
        
           |  |  | 2 | /**
 | 
        
           |  |  | 3 |  * Service fournissant des informations sur les collections et les structures répondant aux critères de recherche
 | 
        
           | 1753 | mathias | 4 |  * fournis en paramètre.
 | 
        
           | 1698 | raphael | 5 |  *
 | 
        
           | 1753 | mathias | 6 |  * @author Mathias Chouet
 | 
        
           | 1698 | raphael | 7 |  * @author Raphaël Droz <raphael@tela-botanica.org>
 | 
        
           | 1497 | jpm | 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>
 | 
        
           | 1698 | raphael | 11 |  * @copyright 2009, 2013 Tela-Botanica
 | 
        
           | 1497 | jpm | 12 |  */
 | 
        
           |  |  | 13 | class CoelRecherche extends Coel {
 | 
        
           |  |  | 14 |   | 
        
           |  |  | 15 | 	/**
 | 
        
           |  |  | 16 | 	 * Méthode principale appelée avec une requête de type GET.
 | 
        
           |  |  | 17 | 	 * Elle sert d'aiguilleur pour appeller la méthode correspondant au type de recherche passé en paramêtre.
 | 
        
           |  |  | 18 | 	 */
 | 
        
           |  |  | 19 | 	public function getElement($param = array()) {
 | 
        
           |  |  | 20 | 		// Initialisation des variables
 | 
        
           |  |  | 21 | 		$info = array();
 | 
        
           | 1753 | mathias | 22 | 		// Nous recherchons le type de requête demandé
 | 
        
           | 1497 | jpm | 23 | 		$type = $param[0];
 | 
        
           |  |  | 24 | 		$methode = 'getElement'.$type;
 | 
        
           | 1753 | mathias | 25 |   | 
        
           | 1497 | jpm | 26 | 		if (method_exists($this, $methode)) {
 | 
        
           |  |  | 27 | 			array_shift($param);
 | 
        
           |  |  | 28 | 			$info = $this->$methode($param);
 | 
        
           |  |  | 29 | 		} else {
 | 
        
           |  |  | 30 | 			$this->messages[] = "Le type de recherche demandé '$type' n'est pas disponible.";
 | 
        
           |  |  | 31 | 		}
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 | 		// Envoie sur la sortie standard
 | 
        
           | 1700 | raphael | 34 | 		if($this->formatRetour == 'text/plain') {
 | 
        
           |  |  | 35 | 			$this->envoyer($info, 'text/plain', 'utf-8', false);
 | 
        
           |  |  | 36 | 			exit;
 | 
        
           |  |  | 37 | 		}
 | 
        
           | 1497 | jpm | 38 | 		$this->envoyer($info);
 | 
        
           |  |  | 39 | 	}
 | 
        
           | 1753 | mathias | 40 |   | 
        
           |  |  | 41 | 	/** @deprecated retro-compatibilité */
 | 
        
           |  |  | 42 | 	public function getElementParDefaut($param) {
 | 
        
           |  |  | 43 | 		return $this->getElementCollections($param);
 | 
        
           |  |  | 44 | 	}
 | 
        
           |  |  | 45 | 	/** @deprecated retro-compatibilité */
 | 
        
           |  |  | 46 | 	public function getElementNombre($param) {
 | 
        
           |  |  | 47 | 		return $this->getElementNombreCollections($param);
 | 
        
           |  |  | 48 | 	}
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 | 	/**
 | 
        
           |  |  | 51 | 	 * Retourne les collections correspondant aux critères de recherche
 | 
        
           | 1497 | jpm | 52 | 	 */
 | 
        
           | 1753 | mathias | 53 | 	public function getElementCollections($param) {
 | 
        
           | 1497 | jpm | 54 | 		// Initialisation des variables
 | 
        
           |  |  | 55 | 		$info = array();
 | 
        
           | 1753 | mathias | 56 | 		$fromClause = $whereClause = $joinClause = array();
 | 
        
           |  |  | 57 |   | 
        
           | 1497 | jpm | 58 | 		// Pré traitement des paramêtres
 | 
        
           | 1753 | mathias | 59 | 		$p = $this->pretraiterParametresUrlCollections($param, $_GET);
 | 
        
           | 1698 | raphael | 60 |   | 
        
           | 1753 | mathias | 61 | 		// Construction des clauses
 | 
        
           |  |  | 62 | 		self::construireFromEtWhereCollections($p, $fromClause, $joinClause, $whereClause);
 | 
        
           | 1699 | raphael | 63 |   | 
        
           | 1497 | jpm | 64 | 		// Construction de la requête
 | 
        
           | 1698 | raphael | 65 | 		$requete = sprintf(
 | 
        
           | 1700 | raphael | 66 | 			'SELECT %s cs_id_structure, cs_ville, cs_nom, cs_code_postal, cs_latitude, cs_longitude, cc_id_collection, cc_nom'
 | 
        
           |  |  | 67 | 			. ' FROM %s %s'
 | 
        
           |  |  | 68 | 			. ' WHERE %s ORDER BY %s %s -- %s:%d',
 | 
        
           | 1698 | raphael | 69 |   | 
        
           | 1753 | mathias | 70 | 			$this->distinct ? 'DISTINCT' : '',
 | 
        
           | 1700 | raphael | 71 | 			implode(',', $fromClause),
 | 
        
           |  |  | 72 | 			implode(' ', $joinClause),
 | 
        
           |  |  | 73 | 			$whereClause ? implode(" AND ", $whereClause) : TRUE,
 | 
        
           |  |  | 74 | 			is_null($this->orderby) ? 'cs_ville ASC, cs_nom ASC, cc_nom ASC' : $this->orderby,
 | 
        
           |  |  | 75 | 			$this->limit != -1 ? "LIMIT {$this->start}, {$this->limit}" : "",
 | 
        
           | 1753 | mathias | 76 | 			__FILE__,
 | 
        
           |  |  | 77 | 			__LINE__
 | 
        
           |  |  | 78 | 		);
 | 
        
           | 1545 | jpm | 79 |   | 
        
           | 1753 | mathias | 80 | 		//echo "REQUETE: " . $requete;
 | 
        
           |  |  | 81 | 		//exit;
 | 
        
           | 1497 | jpm | 82 | 		// Récupération des résultats
 | 
        
           |  |  | 83 | 		try {
 | 
        
           |  |  | 84 | 			$donnees = $this->bdd->query($requete)->fetchAll(PDO::FETCH_ASSOC);
 | 
        
           |  |  | 85 | 			if ($donnees === false) {
 | 
        
           | 1753 | mathias | 86 | 				$this->messages[] = "La requête n'a retourné aucun résultat.";
 | 
        
           | 1497 | jpm | 87 | 			} else {
 | 
        
           |  |  | 88 | 				$info = $donnees;
 | 
        
           |  |  | 89 | 			}
 | 
        
           |  |  | 90 | 		} catch (PDOException $e) {
 | 
        
           |  |  | 91 | 			$this->messages[] = sprintf($this->getTxt('sql_erreur'), $e->getFile(), $e->getLine(), $e->getMessage());
 | 
        
           |  |  | 92 | 		}
 | 
        
           |  |  | 93 | 		return $info;
 | 
        
           |  |  | 94 | 	}
 | 
        
           | 1753 | mathias | 95 |   | 
        
           |  |  | 96 | 	/**
 | 
        
           |  |  | 97 | 	 * Retourne le nombre de collections correspondant aux critères de recherche
 | 
        
           | 1497 | jpm | 98 | 	 */
 | 
        
           | 1753 | mathias | 99 | 	public function getElementNombreCollections($param) {
 | 
        
           | 1497 | jpm | 100 | 		// Initialisation des variables
 | 
        
           |  |  | 101 | 		$info = array();
 | 
        
           | 1753 | mathias | 102 | 		$fromClause = $whereClause = $joinClause = array();
 | 
        
           |  |  | 103 |   | 
        
           | 1497 | jpm | 104 | 		// Pré traitement des paramêtres
 | 
        
           | 1753 | mathias | 105 | 		$p = $this->pretraiterParametresUrlCollections($param, $_GET);
 | 
        
           | 1497 | jpm | 106 |   | 
        
           | 1753 | mathias | 107 | 		// Construction des clauses
 | 
        
           |  |  | 108 | 		self::construireFromEtWhereCollections($p, $fromClause, $joinClause, $whereClause);
 | 
        
           |  |  | 109 |   | 
        
           | 1497 | jpm | 110 | 		// Construction de la requête
 | 
        
           | 1545 | jpm | 111 | 		// Il est important de compter le nombre d'association structure-collection différentes pour obtenir le bon nombre
 | 
        
           | 1753 | mathias | 112 | 		// @WTF si on compte(*) on a le nombre d'associations structure-collection
 | 
        
           |  |  | 113 | 		// si on compte(cs_id_structure) on a le nombre d'associations structure-collection dont la structure n'est pas null
 | 
        
           |  |  | 114 | 		// en aucun cas on n'a le nombre d'association structure-collection DIFFERENTES, à moins de faire un distinct, ce qui n'est pas le cas ici
 | 
        
           |  |  | 115 | 		// @AMHA on devrait compter(*) car la recherche de collections renvoie même celles qui ont une structure null
 | 
        
           | 1698 | raphael | 116 | 		$requete = sprintf(
 | 
        
           | 1753 | mathias | 117 | 				'SELECT COUNT(%s *) AS nbre, cc_id_collection '
 | 
        
           | 1700 | raphael | 118 | 			. ' FROM %s %s'
 | 
        
           | 1753 | mathias | 119 | 			. ' WHERE %s -- %s:%d',
 | 
        
           |  |  | 120 |   | 
        
           | 1700 | raphael | 121 | 			$this->distinct ? 'DISTINCT' : '',
 | 
        
           |  |  | 122 | 			implode(',', $fromClause),
 | 
        
           |  |  | 123 | 			implode(' ', $joinClause),
 | 
        
           |  |  | 124 | 			$whereClause ? implode(" AND ", $whereClause) : TRUE,
 | 
        
           | 1753 | mathias | 125 | 			__FILE__,
 | 
        
           |  |  | 126 | 			__LINE__
 | 
        
           |  |  | 127 | 		);
 | 
        
           |  |  | 128 | 		// echo "REQUETE: " . $requete;
 | 
        
           |  |  | 129 | 		// exit;
 | 
        
           |  |  | 130 | 		// Récupération des résultats
 | 
        
           |  |  | 131 | 		try {
 | 
        
           |  |  | 132 | 			$donnees = $this->bdd->query($requete)->fetch(PDO::FETCH_ASSOC);
 | 
        
           |  |  | 133 | 			if ($donnees === false) {
 | 
        
           |  |  | 134 | 				$this->messages[] = "La requête n'a retourné aucun résultat.";
 | 
        
           |  |  | 135 | 			} else {
 | 
        
           |  |  | 136 | 				$info = $donnees['nbre'];
 | 
        
           |  |  | 137 | 			}
 | 
        
           |  |  | 138 | 		} catch (PDOException $e) {
 | 
        
           |  |  | 139 | 			$this->messages[] = sprintf($this->getTxt('sql_erreur'), $e->getFile(), $e->getLine(), $e->getMessage());
 | 
        
           |  |  | 140 | 		}
 | 
        
           | 1698 | raphael | 141 |   | 
        
           | 1753 | mathias | 142 | 		return $info;
 | 
        
           |  |  | 143 | 	}
 | 
        
           | 1698 | raphael | 144 |   | 
        
           | 1753 | mathias | 145 | 	/**
 | 
        
           |  |  | 146 | 	 * Retourne les personnes correspondant aux critères de recherche
 | 
        
           |  |  | 147 | 	 */
 | 
        
           |  |  | 148 | 	public function getElementPersonnes($param) {
 | 
        
           |  |  | 149 | 		// Initialisation des variables
 | 
        
           |  |  | 150 | 		$info = array();
 | 
        
           |  |  | 151 | 		$fromClause = $whereClause = $joinClause = array();
 | 
        
           |  |  | 152 |   | 
        
           |  |  | 153 | 		// Pré traitement des paramêtres
 | 
        
           |  |  | 154 | 		$p = $this->pretraiterParametresUrlPersonnes($param, $_GET);
 | 
        
           |  |  | 155 |   | 
        
           |  |  | 156 | 		// Construction des clauses
 | 
        
           |  |  | 157 | 		self::construireFromEtWherePersonnes($p, $fromClause, $joinClause, $whereClause);
 | 
        
           |  |  | 158 |   | 
        
           |  |  | 159 | 		// Construction de la requête
 | 
        
           |  |  | 160 | 		$requete = sprintf(
 | 
        
           |  |  | 161 | 				'SELECT %s cp_id_personne, cp_fmt_nom_complet, cp_prenom, cp_nom, cp_truk_nom_autre, cp_adresse_01, cp_ville, cp_naissance_date, cp_deces_date'
 | 
        
           |  |  | 162 | 				. ' FROM %s %s'
 | 
        
           |  |  | 163 | 				. ' WHERE %s ORDER BY %s %s -- %s:%d',
 | 
        
           |  |  | 164 |   | 
        
           |  |  | 165 | 				$this->distinct ? 'DISTINCT' : '',
 | 
        
           |  |  | 166 | 				implode(',', $fromClause),
 | 
        
           |  |  | 167 | 				implode(' ', $joinClause),
 | 
        
           |  |  | 168 | 				$whereClause ? implode(" AND ", $whereClause) : TRUE,
 | 
        
           |  |  | 169 | 				is_null($this->orderby) ? 'cp_nom ASC, cp_prenom ASC' : $this->orderby,
 | 
        
           |  |  | 170 | 				$this->limit != -1 ? "LIMIT {$this->start}, {$this->limit}" : "",
 | 
        
           |  |  | 171 | 				__FILE__,
 | 
        
           |  |  | 172 | 				__LINE__
 | 
        
           |  |  | 173 | 		);
 | 
        
           |  |  | 174 | 		//echo "REQUETE: " . $requete;
 | 
        
           |  |  | 175 | 		//exit;
 | 
        
           | 1497 | jpm | 176 | 		// Récupération des résultats
 | 
        
           |  |  | 177 | 		try {
 | 
        
           | 1753 | mathias | 178 | 			$donnees = $this->bdd->query($requete)->fetchAll(PDO::FETCH_ASSOC);
 | 
        
           |  |  | 179 | 			if ($donnees === false) {
 | 
        
           |  |  | 180 | 				$this->messages[] = "La requête n'a retourné aucun résultat.";
 | 
        
           |  |  | 181 | 			} else {
 | 
        
           |  |  | 182 | 				$info = $donnees;
 | 
        
           |  |  | 183 | 			}
 | 
        
           |  |  | 184 | 		} catch (PDOException $e) {
 | 
        
           |  |  | 185 | 			$this->messages[] = sprintf($this->getTxt('sql_erreur'), $e->getFile(), $e->getLine(), $e->getMessage());
 | 
        
           |  |  | 186 | 		}
 | 
        
           |  |  | 187 | 		return $info;
 | 
        
           |  |  | 188 | 	}
 | 
        
           |  |  | 189 |   | 
        
           |  |  | 190 | 	/**
 | 
        
           |  |  | 191 | 	 * Retourne le nombre de personnes correspondant aux critères de recherche
 | 
        
           |  |  | 192 | 	 */
 | 
        
           |  |  | 193 | 	public function getElementNombrePersonnes($param) {
 | 
        
           |  |  | 194 | 		// Initialisation des variables
 | 
        
           |  |  | 195 | 		$info = array();
 | 
        
           |  |  | 196 | 		$fromClause = $whereClause = $joinClause = array();
 | 
        
           |  |  | 197 |   | 
        
           |  |  | 198 | 		// Pré traitement des paramêtres
 | 
        
           |  |  | 199 | 		$p = $this->pretraiterParametresUrlPersonnes($param, $_GET);
 | 
        
           |  |  | 200 |   | 
        
           |  |  | 201 | 		// Construction des clauses
 | 
        
           |  |  | 202 | 		self::construireFromEtWherePersonnes($p, $fromClause, $joinClause, $whereClause);
 | 
        
           |  |  | 203 |   | 
        
           |  |  | 204 | 		// Construction de la requête
 | 
        
           |  |  | 205 | 		$requete = sprintf(
 | 
        
           |  |  | 206 | 				'SELECT count(%s *) as nbre'
 | 
        
           |  |  | 207 | 				. ' FROM %s %s'
 | 
        
           |  |  | 208 | 				. ' WHERE %s -- %s:%d',
 | 
        
           |  |  | 209 |   | 
        
           |  |  | 210 | 				$this->distinct ? 'DISTINCT' : '',
 | 
        
           |  |  | 211 | 				implode(',', $fromClause),
 | 
        
           |  |  | 212 | 				implode(' ', $joinClause),
 | 
        
           |  |  | 213 | 				$whereClause ? implode(" AND ", $whereClause) : TRUE,
 | 
        
           |  |  | 214 | 				__FILE__,
 | 
        
           |  |  | 215 | 				__LINE__
 | 
        
           |  |  | 216 | 		);
 | 
        
           |  |  | 217 | 		//echo "REQUETE: " . $requete;
 | 
        
           |  |  | 218 | 		//exit;
 | 
        
           |  |  | 219 | 		// Récupération des résultats
 | 
        
           |  |  | 220 | 		try {
 | 
        
           | 1497 | jpm | 221 | 			$donnees = $this->bdd->query($requete)->fetch(PDO::FETCH_ASSOC);
 | 
        
           |  |  | 222 | 			if ($donnees === false) {
 | 
        
           | 1753 | mathias | 223 | 				$this->messages[] = "La requête n'a retourné aucun résultat.";
 | 
        
           | 1497 | jpm | 224 | 			} else {
 | 
        
           |  |  | 225 | 				$info = $donnees['nbre'];
 | 
        
           |  |  | 226 | 			}
 | 
        
           |  |  | 227 | 		} catch (PDOException $e) {
 | 
        
           |  |  | 228 | 			$this->messages[] = sprintf($this->getTxt('sql_erreur'), $e->getFile(), $e->getLine(), $e->getMessage());
 | 
        
           |  |  | 229 | 		}
 | 
        
           |  |  | 230 | 		return $info;
 | 
        
           |  |  | 231 | 	}
 | 
        
           | 1753 | mathias | 232 |   | 
        
           |  |  | 233 | 	// Voici les paramètres qu'il faut passer au service Collections, tous et dans l'ordre (soit une valeur soit '*')
 | 
        
           |  |  | 234 | 	private function pretraiterParametresUrlCollections($param, $qs) {
 | 
        
           | 1700 | raphael | 235 | 		$params_passes = array(
 | 
        
           |  |  | 236 | 			'mots' => 'str',
 | 
        
           | 1497 | jpm | 237 | 			'sci' => 'bool',
 | 
        
           |  |  | 238 | 			'bot' => 'int',
 | 
        
           | 1753 | mathias | 239 | 			'lieu-stockage' => 'str',
 | 
        
           | 1497 | jpm | 240 | 			'zg' => 'str',
 | 
        
           |  |  | 241 | 			'p' => 'str',
 | 
        
           |  |  | 242 | 			'pr' => 'int',
 | 
        
           | 1700 | raphael | 243 | 			'str-d' => 'frdepliste',
 | 
        
           | 1581 | jpm | 244 | 			'veg' => 'int',
 | 
        
           | 1753 | mathias | 245 | 			'projets' => 'int'
 | 
        
           |  |  | 246 | 		);
 | 
        
           |  |  | 247 | 		return $this->pretraiterParametresUrl($param, $qs, $params_passes);
 | 
        
           |  |  | 248 | 	}
 | 
        
           |  |  | 249 |   | 
        
           |  |  | 250 | 	// Voici les paramètres qu'il faut passer au service Personnes, tous et dans l'ordre (soit une valeur soit '*')
 | 
        
           |  |  | 251 | 	private function pretraiterParametresUrlPersonnes($param, $qs) {
 | 
        
           |  |  | 252 | 		$params_passes = array(
 | 
        
           |  |  | 253 | 			'nom-famille' => 'str',
 | 
        
           |  |  | 254 | 			'adresse' => 'str',
 | 
        
           |  |  | 255 | 			'date-vivant' => 'int'
 | 
        
           |  |  | 256 | 		);
 | 
        
           |  |  | 257 | 		return $this->pretraiterParametresUrl($param, $qs, $params_passes);
 | 
        
           |  |  | 258 | 	}
 | 
        
           |  |  | 259 |   | 
        
           |  |  | 260 | 	private function pretraiterParametresUrl($param, $qs, $params_passes) {
 | 
        
           | 1497 | jpm | 261 | 		$p = $this->traiterParametresUrl(array_keys($params_passes), $param, false);
 | 
        
           | 1506 | jpm | 262 | 		$this->debug[] = $param;
 | 
        
           | 1497 | jpm | 263 | 		foreach ($params_passes as $param_passe => $type) {
 | 
        
           |  |  | 264 | 			if (isset($p[$param_passe])) {
 | 
        
           |  |  | 265 | 				// Suppression des éventuels espaces en début et fin de chaine
 | 
        
           |  |  | 266 | 				$valeur = trim($p[$param_passe]);
 | 
        
           |  |  | 267 |   | 
        
           |  |  | 268 | 				// Type de paramêtre chaine
 | 
        
           |  |  | 269 | 				if ($type == 'str') {
 | 
        
           |  |  | 270 | 					// Suppression des slash
 | 
        
           |  |  | 271 | 					$valeur = stripslashes($valeur);
 | 
        
           |  |  | 272 |   | 
        
           |  |  | 273 | 					// Utilisation d'une recherche de chaîne exacte
 | 
        
           |  |  | 274 | 					if (preg_match('/^"(.*)"$/', $valeur, $match)) {
 | 
        
           |  |  | 275 | 						$valeur = '%'.$match[1].'%';
 | 
        
           |  |  | 276 | 					} else {
 | 
        
           |  |  | 277 | 						// Recherche de mots non liés
 | 
        
           |  |  | 278 | 						$mots = explode(' ', $valeur);
 | 
        
           |  |  | 279 | 						$valeur = '%'.implode ('%', $mots).'%';
 | 
        
           |  |  | 280 | 					}
 | 
        
           |  |  | 281 | 					// Mise en place des quotes pour l'intérogation dans la bdd
 | 
        
           |  |  | 282 | 					$valeur = $this->bdd->quote($valeur);
 | 
        
           |  |  | 283 | 				}
 | 
        
           |  |  | 284 | 				// Type de paramêtre booléen
 | 
        
           |  |  | 285 | 				if ($type == 'bool') {
 | 
        
           |  |  | 286 | 					if (preg_match('/^[0]$/', $valeur)) {
 | 
        
           |  |  | 287 | 						$valeur = false;
 | 
        
           |  |  | 288 | 					} else if (preg_match('/^[1]$/', $valeur)) {
 | 
        
           |  |  | 289 | 						$valeur = true;
 | 
        
           |  |  | 290 | 					} else {
 | 
        
           | 1506 | jpm | 291 | 						$this->messages[] = "Le paramêtre '$param_passe' attend une valeur de type 0 ou 1 et non '$valeur'.";
 | 
        
           | 1497 | jpm | 292 | 						$valeur = null;
 | 
        
           |  |  | 293 | 					}
 | 
        
           |  |  | 294 |   | 
        
           |  |  | 295 | 				}
 | 
        
           |  |  | 296 | 				// Type de paramêtre entier
 | 
        
           |  |  | 297 | 				if ($type == 'int') {
 | 
        
           |  |  | 298 | 					if (!preg_match('/^(?:[0-9]+,\s*)*[0-9]+$/', $valeur)) {
 | 
        
           | 1506 | jpm | 299 | 						$this->messages[] = "Le paramêtre '$param_passe' attend une ou plusieurs valeurs de type entiers ".
 | 
        
           | 1497 | jpm | 300 | 							"séparés par des virgules et non '$valeur'.";
 | 
        
           |  |  | 301 | 						$valeur = null;
 | 
        
           |  |  | 302 | 					}
 | 
        
           |  |  | 303 | 				}
 | 
        
           | 1700 | raphael | 304 |   | 
        
           |  |  | 305 | 				if ($type == 'frdepliste') {
 | 
        
           |  |  | 306 | 					$valeur = array_filter(explode(',', $valeur), create_function('$val', 'return preg_match("/^(\d+|2A|2B)$/i", $val);'));
 | 
        
           |  |  | 307 | 				}
 | 
        
           |  |  | 308 |   | 
        
           | 1497 | jpm | 309 | 				$p[$param_passe] = $valeur;
 | 
        
           |  |  | 310 | 			}
 | 
        
           |  |  | 311 | 		}
 | 
        
           | 1702 | raphael | 312 |   | 
        
           |  |  | 313 |         if(isset($qs['pays'])) {
 | 
        
           |  |  | 314 |             $p['pays'] = array_filter(explode(',', $qs['pays']), create_function('$val', 'return preg_match("/^[A-Z][A-Z]$/", $val);'));
 | 
        
           |  |  | 315 |             if(!$p['pays']) unset($p['pays']);
 | 
        
           |  |  | 316 |         }
 | 
        
           |  |  | 317 |   | 
        
           | 1704 | raphael | 318 |         if(isset($qs['regions'])) {
 | 
        
           |  |  | 319 |             $p['regions'] = array_filter(explode(',', $qs['regions']), create_function('$val', 'return preg_match("/^[A-Z][A-Z]\.\w\w$/", $val);'));
 | 
        
           |  |  | 320 |             if(!$p['regions']) unset($p['regions']);
 | 
        
           |  |  | 321 |         }
 | 
        
           |  |  | 322 |   | 
        
           | 1497 | jpm | 323 | 		return $p;
 | 
        
           |  |  | 324 | 	}
 | 
        
           | 1753 | mathias | 325 |   | 
        
           |  |  | 326 | 	// construit les clauses FROM et WHERE pour la recherche de collections (#CaptainObvious)
 | 
        
           |  |  | 327 | 	static function construireFromEtWhereCollections($p, &$from, &$join, &$where) {
 | 
        
           | 1700 | raphael | 328 | 		$from = array('coel_collection');
 | 
        
           |  |  | 329 | 		$join = array('LEFT JOIN coel_structure ON (cc_ce_structure = cs_id_structure)');
 | 
        
           | 1698 | raphael | 330 | 		$where = array();
 | 
        
           |  |  | 331 |   | 
        
           |  |  | 332 | 		// Gestion du from en fonction des paramêtres
 | 
        
           |  |  | 333 | 		if (isset($p['str-d'])) {// ATTENTION : Remplace $from, doit être situé en première position!
 | 
        
           |  |  | 334 | 			$from = array('coel_structure');
 | 
        
           | 1700 | raphael | 335 | 			$join = array('LEFT JOIN coel_collection ON (cs_id_structure = cc_ce_structure)');
 | 
        
           | 1698 | raphael | 336 | 		}
 | 
        
           | 1497 | jpm | 337 |   | 
        
           |  |  | 338 | 		// Construire from et where en fonction des paramêtres
 | 
        
           |  |  | 339 | 		if (isset($p['mots'])) {
 | 
        
           | 1698 | raphael | 340 | 			$where[] = '(' . implode(' OR ', array(
 | 
        
           |  |  | 341 | 				"cc_nom LIKE {$p['mots']}",
 | 
        
           |  |  | 342 | 				"cc_truk_nom_alternatif LIKE {$p['mots']}",
 | 
        
           |  |  | 343 | 				"cc_truk_code LIKE {$p['mots']}",
 | 
        
           |  |  | 344 | 				"cc_description LIKE {$p['mots']}",
 | 
        
           |  |  | 345 | 				"cc_description_specialiste LIKE {$p['mots']}",
 | 
        
           |  |  | 346 | 				"cc_historique LIKE {$p['mots']}",
 | 
        
           |  |  | 347 | 				"cs_nom LIKE {$p['mots']}",
 | 
        
           |  |  | 348 | 				"cs_truk_nom_alternatif LIKE {$p['mots']}",
 | 
        
           |  |  | 349 | 				"cs_description LIKE {$p['mots']}",
 | 
        
           |  |  | 350 | 				"cs_adresse_01 LIKE {$p['mots']}",
 | 
        
           |  |  | 351 | 				"cs_adresse_02 LIKE {$p['mots']}",
 | 
        
           |  |  | 352 | 				"cs_ville LIKE {$p['mots']}",
 | 
        
           |  |  | 353 | 				"cs_truk_identifiant_alternatif LIKE {$p['mots']}",
 | 
        
           |  |  | 354 | 				"cs_condition_acces LIKE {$p['mots']}",
 | 
        
           |  |  | 355 | 				"cs_condition_usage LIKE {$p['mots']}",
 | 
        
           |  |  | 356 | 				"cs_truk_telephone LIKE {$p['mots']}",
 | 
        
           |  |  | 357 | 				"cs_courriel LIKE {$p['mots']}",
 | 
        
           | 1730 | raphael | 358 | 				"cs_truk_url LIKE {$p['mots']}")) . ')';
 | 
        
           | 1497 | jpm | 359 | 		}
 | 
        
           | 1698 | raphael | 360 |   | 
        
           | 1497 | jpm | 361 | 		if (isset($p['sci'])) {
 | 
        
           |  |  | 362 | 			if ($p['sci'] === true) {
 | 
        
           | 1698 | raphael | 363 | 				$where[] = 'csv_mark_visite_avec_motif = 1';
 | 
        
           | 1502 | jpm | 364 | 			} else if ($p['sci'] === false) {
 | 
        
           | 1698 | raphael | 365 | 				$where[] = 'csv_mark_acces_ss_motif = 1';
 | 
        
           | 1497 | jpm | 366 | 			}
 | 
        
           |  |  | 367 | 		}
 | 
        
           |  |  | 368 | 		if (isset($p['bot'])) {
 | 
        
           | 1698 | raphael | 369 | 			$where[] = "ccb_ce_truk_type IN ({$p['bot']})";
 | 
        
           | 1497 | jpm | 370 | 		}
 | 
        
           | 1753 | mathias | 371 | 		if (isset($p['lieu-stockage'])) {
 | 
        
           |  |  | 372 | 			$where[] = '(' . implode(' OR ', array(
 | 
        
           |  |  | 373 | 				"cs_adresse_01 LIKE {$p['lieu-stockage']}",
 | 
        
           |  |  | 374 | 				"cs_adresse_02 LIKE {$p['lieu-stockage']}",
 | 
        
           |  |  | 375 | 				"cs_code_postal LIKE {$p['lieu-stockage']}",
 | 
        
           |  |  | 376 | 				"cs_ville LIKE {$p['lieu-stockage']}",
 | 
        
           |  |  | 377 | 				"cs_ce_truk_region LIKE {$p['lieu-stockage']}", // @TODO joindre la table meta
 | 
        
           |  |  | 378 | 				"cs_ce_truk_pays LIKE {$p['lieu-stockage']}"
 | 
        
           |  |  | 379 | 			)) . ')';
 | 
        
           |  |  | 380 | 		}
 | 
        
           | 1497 | jpm | 381 | 		if (isset($p['zg'])) {
 | 
        
           | 1698 | raphael | 382 | 			$where[] = "cc_truk_couverture_lieu LIKE {$p['zg']}";
 | 
        
           | 1497 | jpm | 383 | 		}
 | 
        
           |  |  | 384 | 		if (isset($p['p'])) {
 | 
        
           | 1698 | raphael | 385 | 			$where[] = "cp_fmt_nom_complet LIKE {$p['p']}";
 | 
        
           | 1497 | jpm | 386 | 		}
 | 
        
           |  |  | 387 | 		if (isset($p['pr'])) {
 | 
        
           | 1698 | raphael | 388 | 			$where[] = "ccap_id_role IN ({$p['pr']})";
 | 
        
           | 1497 | jpm | 389 | 		}
 | 
        
           | 1702 | raphael | 390 |   | 
        
           |  |  | 391 |         // par défaut, spécifier un département restreint à la France
 | 
        
           | 1704 | raphael | 392 |         // TODO: INNER JOIN
 | 
        
           | 1497 | jpm | 393 | 		if (isset($p['str-d'])) {
 | 
        
           | 1702 | raphael | 394 |             $join[] = 'LEFT JOIN coel_meta_liste_valeur cv ON cv.cmlv_id_valeur = cs_ce_truk_pays';
 | 
        
           | 1721 | raphael | 395 | 			$where[] = "cv.cmlv_abreviation IN ('FR', 'RE', 'YT', 'GP', 'MQ', 'GF', 'NC')";
 | 
        
           | 1700 | raphael | 396 | 			$where[] = sprintf("cs_code_postal REGEXP '^(%s).*'", implode('|', $p['str-d']));
 | 
        
           | 1497 | jpm | 397 | 		}
 | 
        
           | 1698 | raphael | 398 |   | 
        
           | 1704 | raphael | 399 |         // http://download.geonames.org/export/dump/admin1CodesASCII.txt
 | 
        
           |  |  | 400 | 		if (isset($p['regions'])) {
 | 
        
           |  |  | 401 |             $join[] = 'LEFT JOIN coel_meta_liste_valeur cv ON cv.cmlv_id_valeur = cs_ce_truk_region';
 | 
        
           |  |  | 402 | 			$where[] = sprintf('cv.cmlv_abreviation IN ("%s")', implode('","', $p['regions']));
 | 
        
           |  |  | 403 | 		}
 | 
        
           |  |  | 404 |   | 
        
           | 1702 | raphael | 405 | 		if (isset($p['pays'])) {
 | 
        
           | 1721 | raphael | 406 |             if(array_search('FR', $p['pays']) !== FALSE) $p['pays'] = array_merge($p['pays'], array('RE','YT','GP','MQ','GF','NC'));
 | 
        
           | 1702 | raphael | 407 |             $join[] = 'LEFT JOIN coel_meta_liste_valeur cv ON cv.cmlv_id_valeur = cs_ce_truk_pays';
 | 
        
           |  |  | 408 | 			$where[] = sprintf('cv.cmlv_abreviation IN ("%s")', implode('","', $p['pays']));
 | 
        
           |  |  | 409 | 		}
 | 
        
           |  |  | 410 |   | 
        
           | 1506 | jpm | 411 | 		if (isset($p['veg'])) {
 | 
        
           |  |  | 412 | 			$veg = explode(',', $p['veg']);
 | 
        
           |  |  | 413 | 			$veg_nbre = count($veg);
 | 
        
           |  |  | 414 |   | 
        
           |  |  | 415 | 			if ($veg_nbre == 1) {
 | 
        
           | 1698 | raphael | 416 | 				$where[] = "ccb_truk_nature LIKE '%{$p['veg']}%'";
 | 
        
           | 1506 | jpm | 417 | 			} else {
 | 
        
           |  |  | 418 | 				$recherche = array();
 | 
        
           |  |  | 419 | 				foreach ($veg as $id) {
 | 
        
           |  |  | 420 | 					$recherche[] = "ccb_truk_nature LIKE '%$id%'";
 | 
        
           |  |  | 421 | 				}
 | 
        
           | 1698 | raphael | 422 | 				$where[] = '('.implode(' OR ', $recherche).') ';
 | 
        
           | 1506 | jpm | 423 | 			}
 | 
        
           | 1581 | jpm | 424 | 		}
 | 
        
           | 1506 | jpm | 425 |   | 
        
           | 1581 | jpm | 426 | 		if (isset($p['projets'])) {
 | 
        
           | 1698 | raphael | 427 | 			$where[] = "cc_ce_projet IN ({$p['projets']})";
 | 
        
           | 1700 | raphael | 428 | 			$where[] = "cs_ce_projet IN ({$p['projets']})";
 | 
        
           | 1581 | jpm | 429 | 		}
 | 
        
           |  |  | 430 |   | 
        
           | 1698 | raphael | 431 |   | 
        
           | 1497 | jpm | 432 | 		if (isset($p['sci'])) {
 | 
        
           | 1698 | raphael | 433 | 			$join[] = 'LEFT JOIN coel_structure_valorisation ON (cs_id_structure = csv_id_structure)';
 | 
        
           | 1497 | jpm | 434 | 		}
 | 
        
           | 1506 | jpm | 435 | 		if (isset($p['bot']) || isset($p['veg'])) {
 | 
        
           | 1698 | raphael | 436 | 			$join[] = 'LEFT JOIN coel_collection_botanique ON (cc_id_collection = ccb_id_collection)';
 | 
        
           | 1497 | jpm | 437 | 		}
 | 
        
           |  |  | 438 | 		if (isset($p['p']) || isset($p['pr'])) {
 | 
        
           | 1698 | raphael | 439 | 			$join[] = 'LEFT JOIN coel_collection_a_personne ON (cc_id_collection = ccap_id_collection)';
 | 
        
           | 1497 | jpm | 440 | 		}
 | 
        
           |  |  | 441 | 		if (isset($p['p'])) {
 | 
        
           | 1698 | raphael | 442 | 			$join[] = 'LEFT JOIN coel_personne ON (ccap_id_personne = cp_id_personne)';
 | 
        
           | 1497 | jpm | 443 | 		}
 | 
        
           | 1753 | mathias | 444 | 		/*if (isset($p['lieu-stockage'])) { // @TODO on verra plus tard, galère avec la table meta
 | 
        
           |  |  | 445 | 			$join[] = 'LEFT JOIN coel_ ON (cs_ce_truk_region = )';
 | 
        
           |  |  | 446 | 		}*/
 | 
        
           | 1702 | raphael | 447 |   | 
        
           | 1753 | mathias | 448 |         $join = array_unique($join);
 | 
        
           |  |  | 449 | 	}
 | 
        
           | 1702 | raphael | 450 |   | 
        
           | 1753 | mathias | 451 | 	// construit les clauses FROM et WHERE pour la recherche de personnes (#CaptainObvious)
 | 
        
           |  |  | 452 | 	static function construireFromEtWherePersonnes($p, &$from, &$join, &$where) {
 | 
        
           |  |  | 453 | 		$from = array('coel_personne');
 | 
        
           |  |  | 454 | 		$join = array();
 | 
        
           |  |  | 455 | 		$where = array();
 | 
        
           |  |  | 456 |   | 
        
           | 1754 | mathias | 457 | 		//"cp_truk_recolte LIKE {$p['adresse']}", // Inclure le lieu de récolte ?
 | 
        
           |  |  | 458 |   | 
        
           | 1753 | mathias | 459 | 		if (isset($p['nom-famille'])) {
 | 
        
           | 1754 | mathias | 460 | 			$where[] = "cp_nom LIKE {$p['nom-famille']}";
 | 
        
           | 1753 | mathias | 461 | 		}
 | 
        
           |  |  | 462 | 		if (isset($p['adresse'])) {
 | 
        
           |  |  | 463 | 			$where[] = '(' . implode(' OR ', array(
 | 
        
           |  |  | 464 | 				"cp_adresse_01 LIKE {$p['adresse']}",
 | 
        
           |  |  | 465 | 				"cp_adresse_02 LIKE {$p['adresse']}",
 | 
        
           |  |  | 466 | 				"cp_code_postal LIKE {$p['adresse']}",
 | 
        
           |  |  | 467 | 				"cp_ville LIKE {$p['adresse']}",
 | 
        
           | 1754 | mathias | 468 | 				"cp_ce_truk_pays LIKE {$p['adresse']}",
 | 
        
           |  |  | 469 | 				"cp_naissance_lieu LIKE {$p['adresse']}",
 | 
        
           |  |  | 470 | 				"cp_deces_lieu LIKE {$p['adresse']}"
 | 
        
           | 1753 | mathias | 471 | 			)) . ')';
 | 
        
           |  |  | 472 | 		}
 | 
        
           |  |  | 473 | 		if (isset($p['date-vivant'])) {
 | 
        
           |  |  | 474 | 			$where[] = "cp_naissance_date <= {$p['date-vivant']}";
 | 
        
           | 1758 | mathias | 475 | 			$where[] = "cp_deces_date >= {$p['date-vivant']}";
 | 
        
           |  |  | 476 | 			$where[] = "cp_naissance_date IS NOT NULL";
 | 
        
           |  |  | 477 | 			$where[] = "cp_deces_date IS NOT NULL";
 | 
        
           |  |  | 478 | 			//$where[] = "(cp_deces_date IS NULL OR cp_deces_date >= {$p['date-vivant']})";
 | 
        
           | 1753 | mathias | 479 | 		}
 | 
        
           |  |  | 480 |   | 
        
           |  |  | 481 | 		// pour inclure éventuellement les adresses de la structure
 | 
        
           |  |  | 482 | 		/*if (isset($p['adresse'])) {
 | 
        
           |  |  | 483 | 			$join[] = 'JOIN coel_structure_a_personne ON (cp_id_personne = csap_id_personne)';
 | 
        
           |  |  | 484 | 			$join[] = 'JOIN coel_structure ON (cs_id_structure = csap_id_structure)';
 | 
        
           |  |  | 485 | 		}*/
 | 
        
           | 1702 | raphael | 486 |         $join = array_unique($join);
 | 
        
           | 1700 | raphael | 487 | 	}
 | 
        
           | 1497 | jpm | 488 | }
 |