Subversion Repositories eFlore/Applications.coel

Rev

Rev 1646 | Rev 1699 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1646 Rev 1698
Line 1... Line 1...
1
<?php
1
<?php
2
/**
2
/**
3
 * Service fournissant des informations sur les collections et les structures répondant aux critères de recherche
3
 * Service fournissant des informations sur les collections et les structures répondant aux critères de recherche
4
 * fournis en paramêtre.
4
 * fournis en paramêtre.
5
 * Encodage en entrée : utf8
-
 
6
 * Encodage en sortie : utf8
-
 
7
 * 
5
 *
-
 
6
 * @author Raphaël Droz <raphael@tela-botanica.org>
8
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
7
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
9
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
8
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
10
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
9
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
11
 * @version $Id$
-
 
12
 * @copyright 2009
10
 * @copyright 2009, 2013 Tela-Botanica
13
 */
11
 */
14
class CoelRecherche extends Coel {
12
class CoelRecherche extends Coel {
Line 15... Line 13...
15
 
13
 
16
	/**
14
	/**
Line 39... Line 37...
39
 	/* Méthode pour récupérer une liste d'information sur les collections et/ou structures en fonction de mots et de
37
 	/* Méthode pour récupérer une liste d'information sur les collections et/ou structures en fonction de mots et de
40
 	 * restrictions.
38
 	 * restrictions.
41
	 * Appelée avec les paramêtres d'url suivant :
39
	 * Appelée avec les paramêtres d'url suivant :
42
	 * /CoelRecherche/ParDefaut/_
40
	 * /CoelRecherche/ParDefaut/_
43
	 * ou les _ représentent dans l'ordre : mots
41
	 * ou les _ représentent dans l'ordre : mots
44
	 * Si un des paramêtres est abscent, il prendre la valeur *
42
	 * Si un des paramêtres est absent, il prendre la valeur *
45
	 */
43
	 */
46
	public function getElementParDefaut($param) {
44
	public function getElementParDefaut($param) {
47
		// Initialisation des variables
45
		// Initialisation des variables
48
		$info = array();
46
		$info = array();
Line 49... Line 47...
49
				
47
				
50
		// Pré traitement des paramêtres
48
		// Pré traitement des paramêtres
-
 
49
		$p = $this->pretraiterParametresUrl($param);
-
 
50
 
-
 
51
        $fromClause = $whereClause = $joinClause = array();
Line 51... Line 52...
51
		$p = $this->pretraiterParametresUrl($param);
52
        self::construireFromEtWhere($p, $fromClause, $joinClause, $whereClause).
-
 
53
		
52
		
54
		// Construction de la requête
-
 
55
		$requete = sprintf(
53
		// Construction de la requête
56
            'SELECT %s cs_id_structure, cs_ville, cs_nom, cs_code_postal, cs_latitude, cs_longitude, cc_id_collection, cc_nom'
-
 
57
            . ' FROM %s %s'
54
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' cs_id_structure, cs_ville, cs_nom, '.
58
            . ' WHERE %s ORDER BY %s LIMIT %d, %d -- %s:%d',
-
 
59
 
-
 
60
            $this->distinct ? 'DISTINCT' : '',
-
 
61
 
-
 
62
            implode(',', $fromClause),
-
 
63
            implode(' ', $joinClause),
55
					'cs_code_postal, cs_latitude, cs_longitude, cc_id_collection, cc_nom '.
64
 
56
					$this->construireFromEtWhere($p).
65
            $whereClause ? implode(" AND ", $whereClause) : TRUE,
-
 
66
            is_null($this->orderby) ? 'cs_ville ASC, cs_nom ASC, cc_nom ASC' : $this->orderby,
-
 
67
            $this->start, $this->limit,
Line 57... Line 68...
57
					'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'cs_ville ASC, cs_nom ASC, cc_nom ASC').' '.
68
            __FILE__, __LINE__);
58
					"LIMIT $this->start, $this->limit ";
69
 
59
		
70
		
60
		// Récupération des résultats
71
		// Récupération des résultats
Line 74... Line 85...
74
	/* Méthode pour récupérer une liste d'information sur les collections et/ou structures en fonction de mots et de
85
	/* Méthode pour récupérer une liste d'information sur les collections et/ou structures en fonction de mots et de
75
 	 * restrictions.
86
 	 * restrictions.
76
	 * Appelée avec les paramêtres d'url suivant :
87
	 * Appelée avec les paramêtres d'url suivant :
77
	 * /CoelRecherche/ParDefaut/_
88
	 * /CoelRecherche/ParDefaut/_
78
	 * ou les _ représentent dans l'ordre : mots
89
	 * ou les _ représentent dans l'ordre : mots
79
	 * Si un des paramêtres est abscent, il prendre la valeur *
90
	 * Si un des paramêtres est absent, il prendre la valeur *
80
	 */
91
	 */
81
	public function getElementNombre($param) {
92
	public function getElementNombre($param) {
82
		// Initialisation des variables
93
		// Initialisation des variables
83
		$info = array();
94
		$info = array();
Line 84... Line 95...
84
				
95
				
85
		// Pré traitement des paramêtres
96
		// Pré traitement des paramêtres
Line -... Line 97...
-
 
97
		$p = $this->pretraiterParametresUrl($param);
-
 
98
 
-
 
99
        $fromClause = $whereClause = $joinClause = array();
86
		$p = $this->pretraiterParametresUrl($param);
100
        self::construireFromEtWhere($p, $fromClause, $joinClause, $whereClause).
87
 
101
 
-
 
102
		// Construction de la requête
88
		// Construction de la requête
103
		// Il est important de compter le nombre d'association structure-collection différentes pour obtenir le bon nombre
-
 
104
		$requete = sprintf(
-
 
105
            'SELECT %s COUNT(cs_id_structure) AS nbre, cc_id_collection '
-
 
106
            . ' FROM %s %s'
89
		// Il est important de compter le nombre d'association structure-collection différentes pour obtenir le bon nombre
107
            . ' WHERE %s ORDER BY %s LIMIT %d, %d -- %s:%d',
-
 
108
 
-
 
109
            $this->distinct ? 'DISTINCT' : '',
-
 
110
 
-
 
111
            implode(',', $fromClause),
-
 
112
            implode(' ', $joinClause),
90
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' COUNT(cs_id_structure) AS nbre, cc_id_collection '.
113
 
-
 
114
            $whereClause ? implode(" AND ", $whereClause) : TRUE,
-
 
115
            is_null($this->orderby) ? 'cs_ville ASC, cs_nom ASC' : $this->orderby,
-
 
116
            $this->start, $this->limit,
Line 91... Line 117...
91
					$this->construireFromEtWhere($p).
117
            __FILE__, __LINE__);
92
					'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'cs_ville ASC, cs_nom ASC').' ';
118
 
93
 
119
 
94
		// Récupération des résultats
120
		// Récupération des résultats
Line 167... Line 193...
167
		}
193
		}
Line 168... Line 194...
168
		
194
		
169
		return $p;
195
		return $p;
Line 170... Line 196...
170
	}
196
	}
171
	
197
	
-
 
198
	static function construireFromEtWhere($p, &$from, &$join, &$where) {
172
	private function construireFromEtWhere($p) {
199
        $from = array('coel_collection');
-
 
200
        $join = array('LEFT JOIN coel_structure ON (cc_ce_structure = cs_id_structure)');
-
 
201
		$where = array();
-
 
202
 
173
		// Initialisation de variables
203
		// Gestion du from en fonction des paramêtres
174
		$from_et_where = '';
204
		if (isset($p['str-d'])) {// ATTENTION : Remplace $from, doit être situé en première position!
175
		$from = 'FROM coel_collection '.
205
			$from = array('coel_structure');
Line 176... Line 206...
176
			'	LEFT JOIN coel_structure ON (cc_ce_structure = cs_id_structure) ';
206
            $join = array('LEFT JOIN coel_collection ON (cs_id_structure = cc_ce_structure)');
177
		$where = 'WHERE ';
207
		}
178
		
208
		
179
		// Construire from et where en fonction des paramêtres 
209
		// Construire from et where en fonction des paramêtres 
180
		if (isset($p['mots'])) {
210
		if (isset($p['mots'])) {
181
			$where .= 'AND ('.	
211
			$where[] = '(' . implode(' OR ', array(
182
				"	cc_nom LIKE {$p['mots']} ".
212
				"cc_nom LIKE {$p['mots']}",
183
				"	OR cc_truk_nom_alternatif LIKE {$p['mots']} ".
213
				"cc_truk_nom_alternatif LIKE {$p['mots']}",
184
				"	OR cc_truk_code LIKE {$p['mots']} ".
214
				"cc_truk_code LIKE {$p['mots']}",
185
				"	OR cc_description LIKE {$p['mots']} ".
-
 
186
				"	OR cc_description_specialiste LIKE {$p['mots']} ".
215
				"cc_description LIKE {$p['mots']}",
187
				"	OR cc_historique LIKE {$p['mots']} ".
216
				"cc_description_specialiste LIKE {$p['mots']}",
188
	
217
				"cc_historique LIKE {$p['mots']}",
189
				"	OR cs_nom LIKE {$p['mots']} ".
218
				"cs_nom LIKE {$p['mots']}",
190
				"	OR cs_truk_nom_alternatif LIKE {$p['mots']} ".
219
				"cs_truk_nom_alternatif LIKE {$p['mots']}",
191
				"	OR cs_description LIKE {$p['mots']} ".
220
				"cs_description LIKE {$p['mots']}",
192
				"	OR cs_adresse_01 LIKE {$p['mots']} ".
221
				"cs_adresse_01 LIKE {$p['mots']}",
193
				"	OR cs_adresse_02 LIKE {$p['mots']} ".
222
				"cs_adresse_02 LIKE {$p['mots']}",
194
				"	OR cs_ville LIKE {$p['mots']} ".
223
				"cs_ville LIKE {$p['mots']}",
195
				"	OR cs_truk_identifiant_alternatif LIKE {$p['mots']} ".
224
				"cs_truk_identifiant_alternatif LIKE {$p['mots']}",
196
				"	OR cs_condition_acces LIKE {$p['mots']} ".
225
				"cs_condition_acces LIKE {$p['mots']}",
197
				"	OR cs_condition_usage LIKE {$p['mots']} ".
226
				"cs_condition_usage LIKE {$p['mots']}",
198
				"	OR cs_truk_telephone LIKE {$p['mots']} ".
-
 
199
				"	OR cs_courriel LIKE {$p['mots']} ".
227
				"cs_truk_telephone LIKE {$p['mots']}",
-
 
228
				"cs_courriel LIKE {$p['mots']}",
200
				"	OR cs_truk_url LIKE {$p['mots']} ".
229
				"cs_truk_url LIKE {$p['mots']}") . ')');
201
				') ';
230
		}
202
		}
231
 
203
		if (isset($p['sci'])) {
232
		if (isset($p['sci'])) {
204
			if ($p['sci'] === true) {
233
			if ($p['sci'] === true) {
205
				$where .= 'AND csv_mark_visite_avec_motif = 1 ';
234
				$where[] = 'csv_mark_visite_avec_motif = 1';
206
			} else if ($p['sci'] === false) {
235
			} else if ($p['sci'] === false) {
-
 
236
				$where[] = 'csv_mark_acces_ss_motif = 1';
207
				$where .= 'AND csv_mark_acces_ss_motif = 1 ';
237
			}
208
			}
238
		}
209
		}
239
 
210
		if (isset($p['bot'])) {
240
		if (isset($p['bot'])) {
211
			$where .= "AND ccb_ce_truk_type IN ({$p['bot']}) ";
241
			$where[] = "ccb_ce_truk_type IN ({$p['bot']})";
212
		}
242
		}
213
		if (isset($p['zg'])) {
243
		if (isset($p['zg'])) {
214
			$where .= "AND cc_truk_couverture_lieu LIKE {$p['zg']} ";
244
			$where[] = "cc_truk_couverture_lieu LIKE {$p['zg']}";
215
		}
245
		}
216
		if (isset($p['p'])) {
246
		if (isset($p['p'])) {
217
			$where .= "AND cp_fmt_nom_complet LIKE {$p['p']} ";
247
			$where[] = "cp_fmt_nom_complet LIKE {$p['p']}";
218
		}
248
		}
219
		if (isset($p['pr'])) {
249
		if (isset($p['pr'])) {
220
			$where .= "AND ccap_id_role IN ({$p['pr']}) ";
250
			$where[] = "ccap_id_role IN ({$p['pr']})";
221
		}
251
		}
222
		if (isset($p['str-d'])) {
252
		if (isset($p['str-d'])) {
223
			$where .= 'AND cs_ce_truk_pays = 2654 '.
253
			$where[] = 'cs_ce_truk_pays = 2654';
224
				"AND cs_code_postal LIKE '{$p['str-d']}%' ";
254
            $where[] = "cs_code_postal LIKE '{$p['str-d']}%'";
225
		}
255
		}
226
		
256
 
Line 227... Line 257...
227
		if (isset($p['veg'])) {
257
		if (isset($p['veg'])) {
228
			$veg = explode(',', $p['veg']);
258
			$veg = explode(',', $p['veg']);
229
			$veg_nbre = count($veg);
259
			$veg_nbre = count($veg);
230
			
260
			
231
			if ($veg_nbre == 1) {
261
			if ($veg_nbre == 1) {
232
				$where .= "AND ccb_truk_nature LIKE '%{$p['veg']}%' ";
262
				$where[] = "ccb_truk_nature LIKE '%{$p['veg']}%'";
233
			} else {
263
			} else {
234
				$recherche = array();
264
				$recherche = array();
235
				foreach ($veg as $id) {
265
				foreach ($veg as $id) {
236
					$recherche[] = "ccb_truk_nature LIKE '%$id%'";
266
					$recherche[] = "ccb_truk_nature LIKE '%$id%'";
Line 237... Line 267...
237
				}
267
				}
238
				$where .= 'AND ('.implode(' OR ', $recherche).') ';
268
				$where[] = '('.implode(' OR ', $recherche).') ';
239
			}
269
			}
240
		}
270
		}
Line 241... Line -...
241
		
-
 
242
		if (isset($p['projets'])) {
271
		
243
			$where .= "AND cc_ce_projet IN ({$p['projets']}) ".
-
 
244
				"AND cs_ce_projet IN ({$p['projets']}) ";
-
 
245
		}
-
 
246
		
-
 
247
		$where = str_replace('WHERE AND', 'WHERE', $where);
-
 
248
		
272
		if (isset($p['projets'])) {
249
		// Gestion du from en fonction des paramêtres
273
			$where[] = "cc_ce_projet IN ({$p['projets']})";
250
		if (isset($p['str-d'])) {// ATTENTION : Remplace $from, doit être situé en première position!
274
            $where[] = "cs_ce_projet IN ({$p['projets']})";
251
			$from = 'FROM coel_structure '.
275
		}
252
					'	LEFT JOIN coel_collection ON (cs_id_structure = cc_ce_structure) ';
276
		
253
		}
277
 
254
		if (isset($p['sci'])) {
278
		if (isset($p['sci'])) {
255
			$from .= '	LEFT JOIN coel_structure_valorisation ON (cs_id_structure = csv_id_structure) ';
279
			$join[] = 'LEFT JOIN coel_structure_valorisation ON (cs_id_structure = csv_id_structure)';
256
		}
280
		}
257
		if (isset($p['bot']) || isset($p['veg'])) {
281
		if (isset($p['bot']) || isset($p['veg'])) {
258
			$from .= '	LEFT JOIN coel_collection_botanique ON (cc_id_collection = ccb_id_collection) ';
282
			$join[] = 'LEFT JOIN coel_collection_botanique ON (cc_id_collection = ccb_id_collection)';
259
		}
283
		}
260
		if (isset($p['p']) || isset($p['pr'])) {
-
 
261
			$from .= '	LEFT JOIN coel_collection_a_personne ON (cc_id_collection = ccap_id_collection) ';
-
 
262
		}
-
 
263
		if (isset($p['p'])) {
-
 
264
			$from .= '	LEFT JOIN coel_personne ON (ccap_id_personne = cp_id_personne) ';
-
 
265
		}
-
 
266
		
284
		if (isset($p['p']) || isset($p['pr'])) {
267
		// Retour du From et Where associé
-
 
268
		if (count($p) == 0) {
-
 
269
			$from_et_where = $from;
285
			$join[] = 'LEFT JOIN coel_collection_a_personne ON (cc_id_collection = ccap_id_collection)';
270
		} else {
-
 
271
			$from_et_where = $from.$where;
286
		}