Subversion Repositories eFlore/Applications.coel-consultation

Rev

Rev 43 | Rev 84 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 43 Rev 54
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
3
/**
4
 * classe Controleur du module Recherche.
4
 * classe Controleur du module Recherche.
5
 *
5
 *
6
 * @package		Collection
6
 * @package		Collection
7
 * @category	Php5
7
 * @category	Php5
8
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
8
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
9
 * @copyright	2010 Tela-Botanica
9
 * @copyright	2010 Tela-Botanica
10
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
10
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
11
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
11
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
12
 * @version		SVN: $Id: Recherche.php 43 2010-04-26 14:46:24Z jpm $
12
 * @version		SVN: $Id: Recherche.php 54 2010-04-29 16:25:44Z jpm $
13
 */
13
 */
14
class Recherche extends ColControleur {
14
class Recherche extends ColControleur {
-
 
15
	
-
 
16
	private $chaine_recherche = null;
-
 
17
	private static $url_exemple = null;
15
	
18
	
16
	//+----------------------------------------------------------------------------------------------------------------+
19
	//+----------------------------------------------------------------------------------------------------------------+
17
	// Méthodes
20
	// Méthodes
18
	/**
21
	/**
19
	 * Fonction d'affichage par défaut, elle appelle la liste des administrateurs
22
	 * Fonction d'affichage par défaut, elle appelle la liste des administrateurs
20
	 */
23
	 */
21
	public function executerActionParDefaut() {
24
	public function executerActionParDefaut() {
22
		return $this->rechercher();
25
		return $this->rechercher();
23
	}
26
	}
24
	
27
	
25
	/**
28
	/**
26
	 * Charge le moteur de recherche et l'envoie à la vue.
29
	 * Charge le moteur de recherche et l'envoie à la vue.
27
	 */
30
	 */
28
	public function chargerMoteurRecherche() {
31
	public function chargerMoteurRecherche() {
29
		$donnees = array();
32
		$donnees = array();
30
		
33
		
31
		// Gestion des données de la requête
34
		// Gestion des données de la requête
32
		$chaine = $this->obtenirChaineRecherche();
-
 
33
		$this->memoriserChaineRecherche($chaine);
-
 
34
		$donnees['recherche'] = htmlspecialchars(stripslashes($chaine));
35
		$donnees['recherche'] = htmlspecialchars($this->obtenirChaineRecherche());
35
		
36
 
36
		// Gestion des urls
37
		// Gestion des urls
37
		$this->url->setVariableRequete('module', 'Recherche');
38
		$this->url->setVariableRequete('module', 'Recherche');
38
		$this->url->setVariableRequete('action', 'rechercher');
39
		$this->url->setVariableRequete('action', 'rechercher');
39
		$donnees['url_form'] = $this->url->getUrl();
40
		$donnees['url_form'] = $this->url->getUrl();
40
		$donnees['url_module'] = 'Recherche';
41
		$donnees['url_module'] = 'Recherche';
41
		$donnees['url_action'] = 'rechercher';
42
		$donnees['url_action'] = 'rechercher';
42
		$this->url->setVariableRequete('recherche', '%s');
43
		$this->url->setVariableRequete('recherche', '%s');
43
		$donnees['url_exemple'] = $this->url->getUrl();
44
		self::$url_exemple = $this->url->getUrl();
44
		$this->url->unsetVariablesRequete(array('module', 'action', 'recherche'));
45
		$this->url->unsetVariablesRequete(array('module', 'action', 'recherche'));
45
		
46
		
46
		// Gestion du squelette et de la sortie
47
		// Gestion du squelette et de la sortie
47
		$this->setSortie(self::RENDU_TETE, $this->getVue('moteur', $donnees));
48
		$this->setSortie(self::RENDU_TETE, $this->getVue('moteur', $donnees));
48
	}
49
	}
49
	
50
	
50
	private function obtenirChaineRecherche() {
51
	private function obtenirChaineRecherche() {
51
		$chaine = '';
52
		$chaine = '';
-
 
53
		if (!is_null($this->chaine_recherche)) {
-
 
54
			$chaine = $this->chaine_recherche;
52
		if (isset($_GET['recherche'])) {
55
		} else if (isset($_GET['recherche'])) {
-
 
56
			// Pré-traitement de la chaine de recherche
53
			$chaine = $_GET['recherche'];
57
			$chaine = $_GET['recherche'];
-
 
58
			// Suppression des slash ajouté automatiquement par PHP devant les guillemets
-
 
59
			$chaine = stripslashes($chaine);
-
 
60
			// Mémorisation de la chaine
-
 
61
			$this->memoriserChaineRecherche($chaine);
-
 
62
			// Stockage dans la classe pour éviter d'effectuer à nouveau le traitement ci-dessus
-
 
63
			$this->chaine_recherche = $chaine;
54
		} else if (isset($_SESSION['col']['recherche'])) {
64
		} else if (isset($_SESSION['col']['recherche'])) {
55
			$chaine = $_SESSION['col']['recherche'];
65
			$chaine = $_SESSION['col']['recherche'];
56
		}
66
		}
57
		return $chaine;
67
		return $chaine;
58
	}
68
	}
59
	
69
	
60
	private function memoriserChaineRecherche($chaine) {
70
	private function memoriserChaineRecherche($chaine) {
61
		$_SESSION['col']['recherche'] = $chaine;
71
		$_SESSION['col']['recherche'] = $chaine;
62
	}
72
	}
-
 
73
 
-
 
74
	public static function getUrlExemple($chaine) {
-
 
75
		$url = '';
-
 
76
		if (!is_null(self::$url_exemple)) {
-
 
77
			// L'utilisation d'urlencode nécessiate de pré-encodé la chaine dans le format de sortie si nécessaire
-
 
78
			if (Config::get('sortie_encodage') != Config::get('appli_encodage')) {
-
 
79
				$chaine = mb_convert_encoding($chaine, Config::get('sortie_encodage'), Config::get('appli_encodage'));
-
 
80
			}
-
 
81
			$chaine = urlencode($chaine);
-
 
82
			$url = sprintf(self::$url_exemple, $chaine);
-
 
83
		}
-
 
84
		return $url;
-
 
85
	}
63
	
86
	
64
	/**
87
	/**
65
	 * Recherche des collections.
88
	 * Recherche des collections.
66
	 * @return string la vue correspondante
89
	 * @return string la vue correspondante
67
	 */
90
	 */
68
	public function rechercher() {
91
	public function rechercher() {
69
		$donnees = array();
92
		$donnees = array();
70
		$rechercheDao = $this->getModele('RechercheDao');
93
		$rechercheDao = $this->getModele('RechercheDao');
71
		$parametres = array('mots' => '*');
94
		$parametres = array('mots' => '*');
72
		
95
		
73
		// Récupération des paramêtres de l'url
96
		// Récupération des paramêtres de l'url
74
		$chaine_de_recherche = ''; 
97
		$chaine_de_recherche = $this->obtenirChaineRecherche(); 
75
		if (isset($_GET['recherche'])) {
98
		if (!empty($chaine_de_recherche)) {
76
			$chaine_de_recherche = $_GET['recherche'];
-
 
77
			$this->url->setVariableRequete('recherche', $chaine_de_recherche);
99
			$this->url->setVariableRequete('recherche', $chaine_de_recherche);
78
		}
100
		}
79
		$parametres = $this->parserChaineDeRecherche($chaine_de_recherche);
101
		$parametres = $this->parserChaineDeRecherche($chaine_de_recherche);
80
		
102
		
81
		// Gestion du nombre de résultats
103
		// Gestion du nombre de résultats
82
		$donnees_total = $rechercheDao->chercherStructureNbre($parametres);
104
		$donnees_total = $rechercheDao->chercherStructureNbre($parametres);
83
		
105
		
84
		// Gestion du fragmenteur
106
		// Gestion du fragmenteur
85
		$options = array(
107
		$options = array(
86
			'url' => $this->url, 
108
			'url' => $this->url, 
87
			'donnees_total' => $donnees_total,
109
			'donnees_total' => $donnees_total,
88
			'donnees_par_page' => Config::get('resultat_par_page_defaut'),
110
			'donnees_par_page' => Config::get('resultat_par_page_defaut'),
89
			'donnees_par_page_choix' => Config::get('resultat_par_page_choix'),
111
			'donnees_par_page_choix' => Config::get('resultat_par_page_choix'),
90
		);
112
		);
91
		$fragmenteur = Composant::fabrique('fragmenteur', $options);
113
		$fragmenteur = Composant::fabrique('fragmenteur', $options);
92
		$donnees['fragmenteur'] = $fragmenteur->executer();
114
		$donnees['fragmenteur'] = $fragmenteur->executer();
93
		list($de, $a) = $fragmenteur->getDeplacementParPageId();
115
		list($de, $a) = $fragmenteur->getDeplacementParPageId();
-
 
116
		$this->url->unsetVariableRequete('recherche');
94
 
117
 
95
		// Gestion de l'accès aux données
118
		// Gestion de l'accès aux données
96
		$rechercheDao->setLimitation(($de - 1), $fragmenteur->getDonneesParPage());
119
		$rechercheDao->setLimitation(($de - 1), $fragmenteur->getDonneesParPage());
97
		$rechercheDao->setDistinction(1);
120
		$rechercheDao->setDistinction(1);
98
		$resultats = $rechercheDao->chercher($parametres);
121
		$resultats = $rechercheDao->chercher($parametres);
99
		
122
		
100
		// Post-traitement des résultats pour l'affichage
123
		// Post-traitement des résultats pour l'affichage
101
		$this->url->setVariableRequete('module', 'Fiche');
124
		$this->url->setVariableRequete('module', 'Fiche');
102
		foreach ($resultats as $resultat) {
125
		foreach ($resultats as $resultat) {
103
			$structure_id = $resultat['cs_id_structure'];
126
			$structure_id = $resultat['cs_id_structure'];
104
			if (!isset($donnees['infos'][$structure_id])) {
127
			if (!isset($donnees['infos'][$structure_id])) {
105
				$this->url->setVariableRequete('action', 'afficherStructure');
128
				$this->url->setVariableRequete('action', 'afficherStructure');
106
				$this->url->setVariableRequete('id', $resultat['cs_id_structure']);
129
				$this->url->setVariableRequete('id', $resultat['cs_id_structure']);
107
				$structure = array(
130
				$structure = array(
108
					'nom' => $resultat['cs_nom'],
131
					'nom' => $resultat['cs_nom'],
109
					'ville' => $resultat['cs_ville'],
132
					'ville' => $resultat['cs_ville'],
110
					'url' => $this->url->getURL());
133
					'url' => $this->url->getURL());
111
				$this->url->unsetVariableRequete('action');
134
				$this->url->unsetVariableRequete('action');
112
				$this->url->unsetVariableRequete('id'); 
135
				$this->url->unsetVariableRequete('id'); 
113
				$donnees['infos'][$structure_id]['structure'] = $structure;
136
				$donnees['infos'][$structure_id]['structure'] = $structure;
114
			}
137
			}
115
			$this->url->setVariableRequete('action', 'afficherCollection');
138
			$this->url->setVariableRequete('action', 'afficherCollection');
116
			$this->url->setVariableRequete('id', $resultat['cc_id_collection']);
139
			$this->url->setVariableRequete('id', $resultat['cc_id_collection']);
117
			$collection = array('nom' => $resultat['cc_nom'],
140
			$collection = array('nom' => $resultat['cc_nom'],
118
				'url' => $this->url->getURL());
141
				'url' => $this->url->getURL());
119
			$this->url->unsetVariableRequete('action');
142
			$this->url->unsetVariableRequete('action');
120
			$this->url->unsetVariableRequete('id');
143
			$this->url->unsetVariableRequete('id');
121
			$donnees['infos'][$structure_id]['collections'][] = $collection;
144
			$donnees['infos'][$structure_id]['collections'][] = $collection;
122
		}
145
		}
123
		$this->url->unsetVariableRequete('module');
146
		$this->url->unsetVariableRequete('module');
124
		
147
		
125
		// Gestion des squelettes
148
		// Gestion des squelettes
126
		$this->chargerMoteurRecherche();
149
		$this->chargerMoteurRecherche();
127
		$resultat = $this->getVue('resultat', $donnees);
150
		$resultat = $this->getVue('resultat', $donnees);
128
		$this->setSortie(self::RENDU_CORPS, $resultat);
151
		$this->setSortie(self::RENDU_CORPS, $resultat);
129
		$this->chargerPiedDePage();
152
		$this->chargerPiedDePage();
130
	}
153
	}
131
	
154
	
132
	private function parserChaineDeRecherche($chaine) {
-
 
133
		// Pré-traitement de la chaine de recherche
-
 
134
		// Suppression des slash ajouté automatiquement par PHP
-
 
135
		$chaine = stripslashes($chaine);
-
 
136
		
155
	private function parserChaineDeRecherche($chaine) {		
137
		$mots = preg_split('/ /i', $chaine, -1, PREG_SPLIT_NO_EMPTY);
156
		$mots = preg_split('/ /i', $chaine, -1, PREG_SPLIT_NO_EMPTY);
138
		$parametres = array('mots' => '');
157
		$parametres = array('mots' => '');
139
		$cle_precedente = null;
158
		$cle_precedente = null;
140
		foreach ($mots as $mot) {
159
		foreach ($mots as $mot) {
141
			if (preg_match('/^(sci|bot|zg|p|pr|str-d):(.*)$/', $mot, $match)) {
160
			if (preg_match('/^(sci|bot|zg|p|pr|str-d):(.*)$/', $mot, $match)) {
142
				$cle = $match[1];
161
				$cle = $match[1];
143
				$cle_precedente = $cle;
162
				$cle_precedente = $cle;
144
				$valeur = $match[2];
163
				$valeur = $match[2];
145
				$parametres[$cle] = $valeur;
164
				$parametres[$cle] = $valeur;
146
			} else if (!is_null($cle_precedente)) {
165
			} else if (!is_null($cle_precedente)) {
147
				$parametres[$cle_precedente] .= ' '.$mot;
166
				$parametres[$cle_precedente] .= ' '.$mot;
148
			} else if (is_null($cle_precedente)) {
167
			} else if (is_null($cle_precedente)) {
149
				if (empty($parametres['mots'])) {
168
				if (empty($parametres['mots'])) {
150
					$parametres['mots'] = $mot;
169
					$parametres['mots'] = $mot;
151
				} else {
170
				} else {
152
					$parametres['mots'] .= ' '.$mot;
171
					$parametres['mots'] .= ' '.$mot;
153
				}
172
				}
154
			}
173
			}
155
		}
174
		}
156
		$this->remplacerAbreviationParId($parametres);
175
		$this->remplacerAbreviationParId($parametres);
157
		
176
		
158
		return $parametres;
177
		return $parametres;
159
	}
178
	}
160
	
179
	
161
	private function remplacerAbreviationParId(&$parametres) {
180
	private function remplacerAbreviationParId(&$parametres) {
162
		// liste 27 : Liste des relations entre une collection et une personne (id:1030)
181
		// liste 27 : Liste des relations entre une collection et une personne (id:1030)
163
		// liste 80 : Liste des types de collection botanique (id:1083)
182
		// liste 80 : Liste des types de collection botanique (id:1083)
164
		$params_a_remplacer = array('bot' => 1083, 'pr' => 1030);
183
		$params_a_remplacer = array('bot' => 1083, 'pr' => 1030);
165
		foreach ($params_a_remplacer as $param => $id_liste) {
184
		foreach ($params_a_remplacer as $param => $id_liste) {
166
			if (isset($parametres[$param])) {
185
			if (isset($parametres[$param])) {
167
				$liste = Ontologie::getListeTrieeParAbreviation($id_liste);
186
				$liste = Ontologie::getListeTrieeParAbreviation($id_liste);
168
				$cle = strtoupper($parametres[$param]);
187
				$cle = strtoupper($parametres[$param]);
169
				if (isset($liste[$cle])) {
188
				if (isset($liste[$cle])) {
170
					$parametres[$param] = $liste[$cle]['id'];
189
					$parametres[$param] = $liste[$cle]['id'];
171
				}
190
				}
172
			}
191
			}
173
		}
192
		}
174
	}
193
	}
175
	
194
	
176
	/**
195
	/**
177
	 * Recherche des collections.
196
	 * Recherche des collections.
178
	 * @return string la vue correspondante
197
	 * @return string la vue correspondante
179
	 */
198
	 */
180
	public function chargerPiedDePage() {
199
	public function chargerPiedDePage() {
181
		$this->setSortie(self::RENDU_PIED, $this->getVue('pied'));
200
		$this->setSortie(self::RENDU_PIED, $this->getVue('pied'));
182
	}
201
	}
183
}
202
}