Subversion Repositories Applications.referentiel

Rev

Rev 285 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 285 Rev 289
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		Referentiel
6
 * @package		Referentiel
7
 * @category	Php5
7
 * @category	Php5
8
 * @author		Delphine CAUQUIL <delphine@tela-botanica.org>
8
 * @author		Delphine CAUQUIL <delphine@tela-botanica.org>
9
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
9
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
10
 * @copyright	2010 Tela-Botanica
10
 * @copyright	2010 Tela-Botanica
11
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
11
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
12
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
12
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
13
 * @version		SVN: $Id: Recherche.php 147 2010-09-06 09:37:22Z jpm $
13
 * @version		SVN: $Id: Recherche.php 147 2010-09-06 09:37:22Z jpm $
14
 */
14
 */
15
class Recherche extends AppliControleur {
15
class Recherche extends AppliControleur {
16
	
16
	
17
	private $chaine_recherche = null;
17
	private $chaine_recherche = null;
18
	private static $url_recherche = null;
18
	private static $url_recherche = null;
19
	private static $url_fiche_taxon = null;
19
	private static $url_fiche_taxon = null;
20
	private $referentiel = null;
20
	private $referentiel = null;
21
	private $donnees_par_page;
21
	private $donnees_par_page;
22
	private $donnees_par_page_choix;
22
	private $donnees_par_page_choix;
23
	
23
	
24
	//+----------------------------------------------------------------------------------------------------------------+
24
	//+----------------------------------------------------------------------------------------------------------------+
25
	// Méthodes
25
	// Méthodes
26
	/**
26
	/**
27
	 * Fonction d'affichage par défaut
27
	 * Fonction d'affichage par défaut
28
	 */
28
	 */
29
	public function executerActionParDefaut() {
29
	public function executerActionParDefaut() {
30
		return $this->rechercher();
30
		return $this->rechercher();
31
	}
31
	}
32
	
32
	
33
	/**
33
	/**
34
	 * Charge le moteur de recherche et l'envoie à la vue.
34
	 * Charge le moteur de recherche et l'envoie à la vue.
35
	 */
35
	 */
36
	public function chargerMoteurRecherche($ref = null, $affiche_rech_avancee = null) {
36
	public function chargerMoteurRecherche($ref = null, $affiche_rech_avancee = null) {
37
		$donnees = array();
37
		$donnees = array();
38
		$this->referentiel = $ref;
38
		$this->referentiel = $ref;
39
		// Gestion des données de la requête
39
		// Gestion des données de la requête
40
		$donnees['recherche'] = htmlspecialchars($this->obtenirChaineRecherche());
40
		$donnees['recherche'] = htmlspecialchars($this->obtenirChaineRecherche());
41
 
41
 
42
		// Gestion des urls
42
		// Gestion des urls
43
		$donnees['url_form'] = $this->obtenirUrlRecherche($this->referentiel);
43
		$donnees['url_form'] = $this->obtenirUrlRecherche($this->referentiel);
44
		$donnees['url_referentiel'] = $this->referentiel;
44
		$donnees['url_referentiel'] = $this->referentiel;
45
		$referentielDao = new ReferentielDao();
45
		$referentielDao = new ReferentielDao();
46
		$donnees['nom_referentiel'] = strtolower($referentielDao->getNom($this->referentiel));
46
		$donnees['nom_referentiel'] = strtolower($referentielDao->getNom($this->referentiel));
47
		$donnees['code_referentiel'] = strtoupper($this->referentiel);
47
		$donnees['code_referentiel'] = strtoupper($this->referentiel);
-
 
48
		$donnees['date_miseajour'] = $referentielDao->getDateMiseAJour($this->referentiel);
48
		$donnees['forum_referentiel'] = Config::get('forum.'.$this->referentiel);
49
		$donnees['forum_referentiel'] = Config::get('forum.'.$this->referentiel);
49
		$donnees['site_referentiel'] = Config::get('site.'.$this->referentiel);
50
		$donnees['site_referentiel'] = Config::get('site.'.$this->referentiel);
50
		$donnees['url_module'] = 'Recherche';
51
		$donnees['url_module'] = 'Recherche';
51
		$donnees['url_action'] = 'rechercher';
52
		$donnees['url_action'] = 'rechercher';
52
		$donnees['affiche_moteur'] = $affiche_rech_avancee;
53
		$donnees['affiche_moteur'] = $affiche_rech_avancee;
53
		self::$url_recherche = $this->obtenirUrlRecherche('%s','%s');
54
		self::$url_recherche = $this->obtenirUrlRecherche('%s','%s');
54
		self::$url_fiche_taxon = $this->obtenirUrlFicheTaxon($this->referentiel, '%s');
55
		self::$url_fiche_taxon = $this->obtenirUrlFicheTaxon($this->referentiel, '%s');
55
		
56
		
56
		// Gestion du squelette et de la sortie
57
		// Gestion du squelette et de la sortie
57
		$this->setSortie(self::RENDU_TETE, $this->getVue('moteur', $donnees));
58
		$this->setSortie(self::RENDU_TETE, $this->getVue('moteur', $donnees));
58
	}
59
	}
59
	
60
	
60
	private function obtenirChaineRecherche() {
61
	private function obtenirChaineRecherche() {
61
		$chaine = '';
62
		$chaine = '';
62
		if (!is_null($this->chaine_recherche)) {
63
		if (!is_null($this->chaine_recherche)) {
63
			$chaine = $this->chaine_recherche;
64
			$chaine = $this->chaine_recherche;
64
		} else if (isset($_GET['recherche'])) {
65
		} else if (isset($_GET['recherche'])) {
65
			// Pré-traitement de la chaine de recherche
66
			// Pré-traitement de la chaine de recherche
66
			$chaine = $_GET['recherche'];
67
			$chaine = $_GET['recherche'];
67
			// Suppression des slash ajouté automatiquement par PHP devant les guillemets
68
			// Suppression des slash ajouté automatiquement par PHP devant les guillemets
68
			$chaine = stripslashes($chaine);
69
			$chaine = stripslashes($chaine);
69
			// Mémorisation de la chaine
70
			// Mémorisation de la chaine
70
			$this->memoriserChaineRecherche($chaine);
71
			$this->memoriserChaineRecherche($chaine);
71
			// Stockage dans la classe pour éviter d'effectuer à nouveau le traitement ci-dessus
72
			// Stockage dans la classe pour éviter d'effectuer à nouveau le traitement ci-dessus
72
			$this->chaine_recherche = $chaine;
73
			$this->chaine_recherche = $chaine;
73
		} else if (isset($_SESSION['ref']['recherche'])) {
74
		} else if (isset($_SESSION['ref']['recherche'])) {
74
			$chaine = $_SESSION['ref']['recherche'];
75
			$chaine = $_SESSION['ref']['recherche'];
75
		}
76
		}
76
		return $chaine;
77
		return $chaine;
77
	}
78
	}
78
	
79
	
79
	private function memoriserChaineRecherche($chaine) {
80
	private function memoriserChaineRecherche($chaine) {
80
		$_SESSION['ref']['recherche'] = $chaine;
81
		$_SESSION['ref']['recherche'] = $chaine;
81
	}
82
	}
82
 
83
 
83
	// url pour les exemples du moteur de recherche
84
	// url pour les exemples du moteur de recherche
84
	public static function getUrlRecherche($chaine, $referentiel = null) {
85
	public static function getUrlRecherche($chaine, $referentiel = null) {
85
		$url = '';
86
		$url = '';
86
		if ($referentiel == null) $referentiel = $this->referentiel;
87
		if ($referentiel == null) $referentiel = $this->referentiel;
87
		if (!is_null(self::$url_recherche)) {
88
		if (!is_null(self::$url_recherche)) {
88
			// L'utilisation d'urlencode nécessiate de pré-encodé la chaine dans le format de sortie si nécessaire
89
			// L'utilisation d'urlencode nécessiate de pré-encodé la chaine dans le format de sortie si nécessaire
89
			if (Config::get('sortie_encodage') != Config::get('appli_encodage')) {
90
			if (Config::get('sortie_encodage') != Config::get('appli_encodage')) {
90
				$chaine = mb_convert_encoding($chaine, Config::get('sortie_encodage'), Config::get('appli_encodage'));
91
				$chaine = mb_convert_encoding($chaine, Config::get('sortie_encodage'), Config::get('appli_encodage'));
91
			}
92
			}
92
			$chaine = urlencode($chaine);
93
			$chaine = urlencode($chaine);
93
			$url = sprintf(self::$url_recherche, $referentiel, $chaine);
94
			$url = sprintf(self::$url_recherche, $referentiel, $chaine);
94
		}
95
		}
95
		return $url;
96
		return $url;
96
	}
97
	}
97
	
98
	
98
	public static function getUrlFicheTaxon($taxon) {
99
	public static function getUrlFicheTaxon($taxon) {
99
		$url = '';
100
		$url = '';
100
		if (!is_null(self::$url_fiche_taxon)) {
101
		if (!is_null(self::$url_fiche_taxon)) {
101
			$url = sprintf(self::$url_fiche_taxon, $taxon['num_nom']);
102
			$url = sprintf(self::$url_fiche_taxon, $taxon['num_nom']);
102
		}
103
		}
103
		return $url;
104
		return $url;
104
	}
105
	}
105
	
106
	
106
	/**
107
	/**
107
	 * Recherche sur référentiel.
108
	 * Recherche sur référentiel.
108
	 * @return string la vue correspondante
109
	 * @return string la vue correspondante
109
	 */
110
	 */
110
	public function rechercher() {
111
	public function rechercher() {
111
		// Paramétres de pagination par défault
112
		// Paramétres de pagination par défault
112
		$this->donnees_par_page = Config::get('resultat_par_page_defaut');
113
		$this->donnees_par_page = Config::get('resultat_par_page_defaut');
113
		$this->donnees_par_page_choix = Config::get('resultat_par_page_choix');
114
		$this->donnees_par_page_choix = Config::get('resultat_par_page_choix');
114
		
115
		
115
		$donnees = array();
116
		$donnees = array();
116
		$rechercheDao =  new RechercheDao();
117
		$rechercheDao =  new RechercheDao();
117
		$parametres = array('mots' => '*');
118
		$parametres = array('mots' => '*');
118
		// code du référentiel courant
119
		// code du référentiel courant
119
		if (isset($_GET['ref'])) { 
120
		if (isset($_GET['ref'])) { 
120
			$this->referentiel = strtolower($_GET['ref']);
121
			$this->referentiel = strtolower($_GET['ref']);
121
		}
122
		}
122
		// Gestion des squelettes
123
		// Gestion des squelettes
123
		$this->definirCommeModulePrincipal(get_class($this));
124
		$this->definirCommeModulePrincipal(get_class($this));
124
		$this->construireMenu($this->referentiel);
125
		$this->construireMenu($this->referentiel);
125
		$this->construireFilAriane($this->referentiel);
126
		$this->construireFilAriane($this->referentiel);
126
		$this->chargerMoteurRecherche($this->referentiel, 10);
127
		$this->chargerMoteurRecherche($this->referentiel, 10);
127
		
128
		
128
		// Récupération des paramètres de l'url
129
		// Récupération des paramètres de l'url
129
		$chaine_de_recherche = $this->obtenirChaineRecherche(); 
130
		$chaine_de_recherche = $this->obtenirChaineRecherche(); 
130
		if (!empty($chaine_de_recherche)) {
131
		if (!empty($chaine_de_recherche)) {
131
			$this->url->setVariablesRequete(array('module' => 'Recherche',
132
			$this->url->setVariablesRequete(array('module' => 'Recherche',
132
				'action' => 'rechercher',
133
				'action' => 'rechercher',
133
				'ref' => $this->referentiel,
134
				'ref' => $this->referentiel,
134
				'recherche' => $chaine_de_recherche));
135
				'recherche' => $chaine_de_recherche));
135
		}
136
		}
136
		$parametres = $this->parserChaineDeRecherche($chaine_de_recherche);
137
		$parametres = $this->parserChaineDeRecherche($chaine_de_recherche);
137
		$parametres['ref'] = $this->referentiel;
138
		$parametres['ref'] = $this->referentiel;
138
		
139
		
139
		// requête spéciale de recherche de tous les synonymes et du nom retenu d'un paramétre
140
		// requête spéciale de recherche de tous les synonymes et du nom retenu d'un paramétre
140
		if (isset($parametres['tax']) && ($parametres['tax'] == 1)) {
141
		if (isset($parametres['tax']) && ($parametres['tax'] == 1)) {
141
			$resultat = $this->chercherSynonymes($rechercheDao, $parametres);
142
			$resultat = $this->chercherSynonymes($rechercheDao, $parametres);
142
		} else if (isset($parametres['classif'])) {
143
		} else if (isset($parametres['classif'])) {
143
			$this->chargerTableauManuel('presence');
144
			$this->chargerTableauManuel('presence');
144
			$donnees['valeur_presence'] = $this->tableau_manuel['presence'];
145
			$donnees['valeur_presence'] = $this->tableau_manuel['presence'];
145
			$donnees['referentiel'] = $this->referentiel;
146
			$donnees['referentiel'] = $this->referentiel;
146
			$donnees['fragmenteur'] = $this->gererFragmenteur($rechercheDao, $parametres, 'NombreClassif');
147
			$donnees['fragmenteur'] = $this->gererFragmenteur($rechercheDao, $parametres, 'NombreClassif');
147
			$donnees['resultats'] = $rechercheDao->chercher('Classification', $parametres);
148
			$donnees['resultats'] = $rechercheDao->chercher('Classification', $parametres);
148
			$resultat = $this->getVue('resultat_recherche', $donnees);
149
			$resultat = $this->getVue('resultat_recherche', $donnees);
149
		} else {
150
		} else {
150
			$this->chargerTableauManuel('presence');
151
			$this->chargerTableauManuel('presence');
151
			$donnees['valeur_presence'] = $this->tableau_manuel['presence'];
152
			$donnees['valeur_presence'] = $this->tableau_manuel['presence'];
152
			$donnees['referentiel'] = $this->referentiel;
153
			$donnees['referentiel'] = $this->referentiel;
153
			$donnees['fragmenteur'] = $this->gererFragmenteur($rechercheDao, $parametres);
154
			$donnees['fragmenteur'] = $this->gererFragmenteur($rechercheDao, $parametres);
154
			$donnees['resultats'] = $rechercheDao->chercher('ParDefaut', $parametres);
155
			$donnees['resultats'] = $rechercheDao->chercher('ParDefaut', $parametres);
155
			$resultat = $this->getVue('resultat_recherche', $donnees);
156
			$resultat = $this->getVue('resultat_recherche', $donnees);
156
		}
157
		}
157
		
158
		
158
		
159
		
159
		$this->setSortie(self::RENDU_CORPS, $resultat);
160
		$this->setSortie(self::RENDU_CORPS, $resultat);
160
	}
161
	}
161
	
162
	
162
	private function chercherSynonymes($rechercheDao, $parametres){
163
	private function chercherSynonymes($rechercheDao, $parametres){
163
		$this->donnees_par_page = 2;
164
		$this->donnees_par_page = 2;
164
		$this->donnees_par_page_choix = '1,2,5,10'; 
165
		$this->donnees_par_page_choix = '1,2,5,10'; 
165
		$donnees['referentiel'] = $this->referentiel;
166
		$donnees['referentiel'] = $this->referentiel;
166
		$donnees['fragmenteur'] = $this->gererFragmenteur($rechercheDao, $parametres, 'NombreTaxon');
167
		$donnees['fragmenteur'] = $this->gererFragmenteur($rechercheDao, $parametres, 'NombreTaxon');
167
		$resultats = $rechercheDao->chercher('ParTaxon', $parametres);
168
		$resultats = $rechercheDao->chercher('ParTaxon', $parametres);
168
		foreach ($resultats as $res) {
169
		foreach ($resultats as $res) {
169
			if ($res['num_nom'] == $res['num_nom_retenu']) {
170
			if ($res['num_nom'] == $res['num_nom_retenu']) {
170
				$donnees['resultats']['nom_retenu'][$res['num_nom_retenu']] = $res;
171
				$donnees['resultats']['nom_retenu'][$res['num_nom_retenu']] = $res;
171
			} else {
172
			} else {
172
				$donnees['resultats'][$res['num_nom_retenu']][$res['num_nom']] = $res;
173
				$donnees['resultats'][$res['num_nom_retenu']][$res['num_nom']] = $res;
173
			}
174
			}
174
		}
175
		}
175
		$resultat = $this->getVue('resultat_recherche_taxon', $donnees);
176
		$resultat = $this->getVue('resultat_recherche_taxon', $donnees);
176
		return $resultat;
177
		return $resultat;
177
	}
178
	}
178
	
179
	
179
	private function gererFragmenteur($rechercheDao, $parametres, $type = 'Nombre') {
180
	private function gererFragmenteur($rechercheDao, $parametres, $type = 'Nombre') {
180
		// Gestion du nombre de résultats
181
		// Gestion du nombre de résultats
181
		$donnees_total = $rechercheDao->chercherStructureNbre($type, $parametres);
182
		$donnees_total = $rechercheDao->chercherStructureNbre($type, $parametres);
182
		
183
		
183
		// Gestion du fragmenteur
184
		// Gestion du fragmenteur
184
		$options = array(
185
		$options = array(
185
			'url' => $this->url, 
186
			'url' => $this->url, 
186
			'donnees_total' => $donnees_total,
187
			'donnees_total' => $donnees_total,
187
			'donnees_par_page' => $this->donnees_par_page,
188
			'donnees_par_page' => $this->donnees_par_page,
188
			'donnees_par_page_choix' => $this->donnees_par_page_choix);
189
			'donnees_par_page_choix' => $this->donnees_par_page_choix);
189
		$fragmenteur = Composant::fabrique('fragmenteur', $options);
190
		$fragmenteur = Composant::fabrique('fragmenteur', $options);
190
		$donnees['fragmenteur'] = $fragmenteur->executer();
191
		$donnees['fragmenteur'] = $fragmenteur->executer();
191
		list($de, $a) = $fragmenteur->getDeplacementParPageId();
192
		list($de, $a) = $fragmenteur->getDeplacementParPageId();
192
		$this->url->unsetVariablesRequete(array('recherche', 'page'));
193
		$this->url->unsetVariablesRequete(array('recherche', 'page'));
193
		
194
		
194
		// Gestion de l'accès aux données
195
		// Gestion de l'accès aux données
195
		$rechercheDao->setLimitation(($de - 1), $fragmenteur->getDonneesParPage());
196
		$rechercheDao->setLimitation(($de - 1), $fragmenteur->getDonneesParPage());
196
		$rechercheDao->setDistinction(1);
197
		$rechercheDao->setDistinction(1);
197
		return $donnees['fragmenteur'];
198
		return $donnees['fragmenteur'];
198
	}
199
	}
199
	
200
	
200
	private function parserChaineDeRecherche($chaine) {		
201
	private function parserChaineDeRecherche($chaine) {		
201
		$mots = preg_split('/ /i', $chaine, -1, PREG_SPLIT_NO_EMPTY);
202
		$mots = preg_split('/ /i', $chaine, -1, PREG_SPLIT_NO_EMPTY);
202
		$parametres = array('mots' => '');
203
		$parametres = array('mots' => '');
203
		$cle_precedente = null;
204
		$cle_precedente = null;
204
		foreach ($mots as $mot) {
205
		foreach ($mots as $mot) {
205
			if (preg_match('/^(sg|gen|sp|ssp|au|an|nn|bib|nr|tax|pre|taxref|classif|rg):(.*)$/', $mot, $match)) {
206
			if (preg_match('/^(sg|gen|sp|ssp|au|an|nn|bib|nr|tax|pre|taxref|classif|rg):(.*)$/', $mot, $match)) {
206
				$cle = $match[1];
207
				$cle = $match[1];
207
				$cle_precedente = $cle;
208
				$cle_precedente = $cle;
208
				$valeur = $match[2];
209
				$valeur = $match[2];
209
				$parametres[$cle] = $valeur;
210
				$parametres[$cle] = $valeur;
210
			} else if (!is_null($cle_precedente)) {
211
			} else if (!is_null($cle_precedente)) {
211
				$parametres[$cle_precedente] .= ' '.$mot;
212
				$parametres[$cle_precedente] .= ' '.$mot;
212
			} else if (is_null($cle_precedente)) {
213
			} else if (is_null($cle_precedente)) {
213
				if (empty($parametres['mots'])) {
214
				if (empty($parametres['mots'])) {
214
					$parametres['mots'] = $mot;
215
					$parametres['mots'] = $mot;
215
				} else {
216
				} else {
216
					$parametres['mots'] .= ' '.$mot;
217
					$parametres['mots'] .= ' '.$mot;
217
				}
218
				}
218
			}
219
			}
219
		}		
220
		}		
220
		return $parametres;
221
		return $parametres;
221
	}
222
	}
222
	
223
	
223
	// charger tableau de $this->manuel
224
	// charger tableau de $this->manuel
224
	private function chargerTableauManuel($titreTableau) {
225
	private function chargerTableauManuel($titreTableau) {
225
		$champs = explode(',', $this->manuel[$titreTableau]);
226
		$champs = explode(',', $this->manuel[$titreTableau]);
226
		foreach ($champs as $champ) {
227
		foreach ($champs as $champ) {
227
			list($champ_nom, $type) = explode('=', trim($champ));
228
			list($champ_nom, $type) = explode('=', trim($champ));
228
			$this->tableau_manuel[$titreTableau][$champ_nom] = $type;
229
			$this->tableau_manuel[$titreTableau][$champ_nom] = $type;
229
		}
230
		}
230
	}
231
	}
231
}
232
}