Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 1120 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
74 delphine 1
<?php
2
class Recherche extends aControleur {
3
 
4
	//+----------------------------------------------------------------------------------------------------------------+
5
	// Méthodes
85 delphine 6
	protected $nom = null;
7
	protected $type_nom = 'nom_scientifique';
165 delphine 8
	protected $type_resultat = '';
85 delphine 9
	protected $submit = '';
885 aurelien 10
	protected $acces_fiche = false;
729 mathilde 11
	private $recherche_avancee;
12
	private $param;
1140 raphael 13
	private $i18n =	 array();
729 mathilde 14
 
996 aurelien 15
	private $parametresAvancesGeneriques = array('gen','fam','nn','nt','sp','ssp','type','sto','sti','stc');
16
 
85 delphine 17
	public function initialiser() {
18
		$this->capturerParametres();
729 mathilde 19
		$this->capturerParametresAvances();
20
		$this->i18n = I18n::get('Recherche-form-avancee');
85 delphine 21
	}
729 mathilde 22
 
74 delphine 23
	/**
24
	 * Fonction d'affichage par défaut
25
	 */
26
	public function executerActionParDefaut() {
179 delphine 27
		$this->executerAccueil();
74 delphine 28
	}
179 delphine 29
 
182 delphine 30
	public function executerAccueil($donneesMoteur = array()) {
179 delphine 31
		$niveau = new Niveau();
32
		$donnees['form_niveau'] = $niveau->afficherNiveau();
33
		$recherchesimple = new RechercheSimple();
182 delphine 34
		$donnees['form_nom'] = $recherchesimple->executerFormulaireNom($donneesMoteur);
729 mathilde 35
		if (Registre::get('parametres.niveau') != 1) {
36
			$recherche_avancee = new RechercheAvancee();
37
			$donnees['form_recherche_av'] = $recherche_avancee->executerFormulaireRechercheAv($donneesMoteur);
38
		}
1120 aurelien 39
		$donnees['description'] = "";
40
 
41
		// le descriptif du référentiel est affiché lorsque seul le moteur est affiché (donc ni résultats, ni fiche)
42
		if(Registre::get('parametres.module') == 'recherche' &&
43
			Registre::get('parametres.action') == 'action-par-defaut' &&
44
			!Registre::get('resultats')) {
45
			$referentiel = Registre::get('parametres.referentiel');
46
			$wiki = new Wikini();
47
			$description = $wiki->getDescriptionReferentielFormate($referentiel);
48
			$donnees['description'] = $description;
49
		}
179 delphine 50
		$this->afficherAccueil($donnees);
51
	}
52
 
53
	private function afficherAccueil($donnees) {
54
		$donnees['i18n'] = I18n::get('Recherche-accueil');
216 delphine 55
		$this->setSortie(self::RENDU_CORPS, $this->getVue('recherche_accueil', $donnees), true);
179 delphine 56
	}
74 delphine 57
 
729 mathilde 58
 
59
	//+---------------------------------------------recherche avancee-------------------------------------+
60
	public function executerRechercheAvancee() {
901 aurelien 61
		$this->param = $this->nettoyerParametresDefautRechercheAvancee($this->param);
729 mathilde 62
		$donnees['param'] = $this->param;
63
		$presence = $this->rechercherCriteresDemandes();
901 aurelien 64
		if (empty($presence) && !empty($this->param)) {
65
			$donnees['message_av']['attention'] = 'info_res_vide';
66
		}
729 mathilde 67
		$this->executerAccueil($donnees);
68
		if (Registre::get('resultats')) {
69
			$_GET['resultat'] = $this->type_resultat;
70
			$this->executerAction('Resultat', 'executerResultat');
71
		}
72
	}
901 aurelien 73
 
74
	private function nettoyerParametresDefautRechercheAvancee($params) {
75
		$params_nettoyes = array();
76
		foreach ($params as $cle => $param) {
77
			if(!preg_match("#^\(.*\)$#", $param)) {
78
				$params_nettoyes[$cle] = $param;
79
			}
80
		}
81
		return $params_nettoyes;
82
	}
729 mathilde 83
 
84
	public function rechercherCriteresDemandes() {
85
		$noms = new Noms(Registre::get('parametres.referentiel'));
86
		$res = $noms->getRechercheAvancee($this->param);
87
		if ($res != false || $res['entete']['total'] != 0) {
88
			if ($res['entete']['total'] == 1 ) {
89
				$ids = array_keys($res['resultat']);
90
				$nom = $res['resultat'][$ids[0]]['nom_sci'];
91
				$url = $this->urls->obtenirUrlFiche($ids[0], 'nom_scientifique', $nom);
92
				$this->redirigerVers($url);
93
			} else {
94
			$res['type'] = $this->type_nom;
95
			Registre::set('resultats', $res);
96
			}
97
		} else {
98
			$res = '';
99
		}
100
		return $res;
101
	}
102
 
103
	//+---------------------------------------------recherche simple-------------------------------------+
1140 raphael 104
	/*
105
	  grep-friendly: ICI->executerRechercheSimple()
106
	  En effet, cette méthode n'est jamais invoquée explicitement.
107
	  Le processus est le suivant:
108
	  * URL = index.php?type_nom=...&referentiel=..&module=recherche&action=rechercheSimple
109
	  * AppControleur::initialiser()
110
	  * `-> AppControleur::$parametres['action'] = $_GET['action']
111
	  * `-> AppControleur::executerModule()
112
	  *	 `-> effecture $module->$action()
113
	  * qui nous appelle ici
114
	 */
74 delphine 115
	public function executerRechercheSimple() {
85 delphine 116
		$donnees['type_nom'] = $this->type_nom;
117
		$donnees['nom'] = $this->nom;
74 delphine 118
		if (strlen($donnees['nom']) < 3) {
123 delphine 119
			$donnees['message']['attention'] = 'info_nb_lettres';
74 delphine 120
		} else {
88 delphine 121
			$presence = $this->rechercherNom();
123 delphine 122
			if ($presence == '') { // s'il n'y a pas de nom
123
				$donnees['message']['attention'] = 'info_sp_abs';
720 delphine 124
			} elseif ($presence == 'sans_correspondance') {
125
				$res = Registre::get('resultats');
126
				$id = array_keys($res['resultat']);
127
				$donnees['message']['nom_ss_corresp']['id'] = $id[0];
128
				$nom = array_shift($res['resultat']);
129
				$donnees['message']['nom_ss_corresp']['nom'] = $nom['nom_sci'];
123 delphine 130
			} elseif ($presence != 'ok') { // s'il y a des noms approchés
131
				if (!Registre::get('resultats')) { // s'il n'y a aucun nom exact
132
					$donnees['message']['attention'] = 'info_sp_abs';
133
				}
134
				$donnees['message']['nom_approche'] = $presence;
74 delphine 135
			}
136
		}
1140 raphael 137
 
179 delphine 138
		$this->executerAccueil($donnees);
88 delphine 139
		if (Registre::get('resultats')) {
185 delphine 140
			$_GET['resultat'] = $this->type_resultat;
76 delphine 141
			$this->executerAction('Resultat', 'executerResultat');
142
		}
74 delphine 143
	}
144
 
729 mathilde 145
 
74 delphine 146
	// regarde si il y a des résultats correspondant au nom recherché sinon recherche un nom approché
88 delphine 147
	// $noms classe métier nom ou nom
148
	private function rechercherNom() {
149
		$noms = ($this->type_nom == 'nom_vernaculaire')
150
				? new NomsVernaculaires(Config::get(Registre::get('parametres.referentiel').'.referentielVerna'))
151
				: new Noms(Registre::get('parametres.referentiel'));
74 delphine 152
		$approche = '';
165 delphine 153
		$res = $noms->getRechercheEtendue($this->nom, $this->type_resultat);
74 delphine 154
		$form = I18n::get('Recherche-form-nom');
155
		if ($res == false || $res['entete']['total'] == 0) { // recherche nom approché
88 delphine 156
			$approche = $this->rechercherNomApproche($noms);
885 aurelien 157
		} elseif ($res['entete']['total'] == 1 || $this->acces_fiche) { // renvoie à la fiche
720 delphine 158
			$approche = $this->traiterAccesFiche($res);
74 delphine 159
		} else { // affiche les résultats
85 delphine 160
			$res['type'] = $this->type_nom;
76 delphine 161
			Registre::set('resultats', $res);
74 delphine 162
			$approche = 'ok';
123 delphine 163
			if ($res['entete']['total'] < 3) { // si moins de 16 noms affiche en plus un nom approché
88 delphine 164
				$approche = $this->rechercherNomApproche($noms);
165
			}
74 delphine 166
		}
167
		return $approche;
168
	}
169
 
720 delphine 170
	private function traiterAccesFiche($res) {
171
		$ids = array_keys($res['resultat']);
172
		if ($this->type_nom == 'nom_vernaculaire') {
173
			$id = explode(':',$res['resultat'][$ids[0]]['nom_retenu.code']);
174
			$id = $id[1];
175
		} else {
176
			if ($res['resultat'][$ids[0]]['retenu'] == 'absent') { // dans le cas d'un nom sans correspondance
177
				$res['type'] = $this->type_nom;
178
				Registre::set('resultats', $res);
179
				$approche = 'sans_correspondance';
180
				return $approche;
181
			} else {
182
				$id = $ids[0];
183
			}
184
		}
185
		$url = $this->urls->obtenirUrlFiche($id, $this->type_nom, $this->nom);
186
		$this->redirigerVers($url);
187
	}
188
 
88 delphine 189
	private function rechercherNomApproche($noms) {
190
		$approche = '';
191
		$res = $noms->getRechercheFloue($this->nom);
192
		if (!($res == false || $res['entete']['total'] == 0)) {
123 delphine 193
			for ($i = 0; $i < 3; $i++) {
194
				$nom_proche = array_shift($res['resultat']);
195
				$approche[$i]['nom'] = ($this->type_nom == 'nom_vernaculaire') ? $nom_proche['nom'] : $nom_proche['nom_sci'];
156 delphine 196
				$approche[$i]['url_nom_approche'] = $this->urls->obtenirUrlRechercheSimple($approche[$i]['nom'], $this->type_nom);
123 delphine 197
			}
88 delphine 198
		}
199
		return $approche;
200
	}
201
 
729 mathilde 202
	//+-----------------------------------------------méthodes utiles---------------------------------+
179 delphine 203
 
85 delphine 204
	private function capturerParametres() {
720 delphine 205
		if (isset($_REQUEST['nom'])) {
901 aurelien 206
			$this->nom = $this->convertirEncodageEntree(urldecode($_REQUEST['nom']));
85 delphine 207
		}
208
		if (isset($_GET['type_nom'])) {
901 aurelien 209
			$this->type_nom = $this->convertirEncodageEntree(urldecode($_GET['type_nom']));
85 delphine 210
		}
211
		if (isset($_GET['submit'])) {
901 aurelien 212
			$this->submit = $this->convertirEncodageEntree(urldecode($_GET['submit']));
85 delphine 213
		}
885 aurelien 214
		if(isset($_GET['acces_fiche'])) {
215
			$this->acces_fiche = true;
216
		}
174 delphine 217
		if (isset($_GET['niveau'])) {
901 aurelien 218
			Registre::set('parametres.niveau', $this->convertirEncodageEntree($_GET['niveau']));
174 delphine 219
		}
165 delphine 220
		if (isset($_GET['resultat'])) {
901 aurelien 221
			$this->type_resultat = $this->convertirEncodageEntree(urldecode($_GET['resultat']));
165 delphine 222
		} else {
223
			$onglet_resultat = $this->recupererTableauConfig('affich_resultats');
224
			$this->type_resultat = $onglet_resultat[Registre::get('parametres.niveau').'_'.$this->type_nom];
225
		}
85 delphine 226
	}
227
 
729 mathilde 228
	private function capturerParametresAvances() {
996 aurelien 229
		$this->capturerParametresAvancesGeneriques();
230
		$this->capturerParametresAvancesDependantsLangage();
231
		$this->capturerParametresAvancesPresenceSpecifiques();
232
	}
233
 
234
	private function capturerParametresAvancesGeneriques() {
235
		foreach($this->parametresAvancesGeneriques as $param) {
236
			if (isset($_GET[$param]) && $_GET[$param] != '') {
237
				$this->param[$param] = $this->convertirEncodageEntree(urldecode($_GET[$param]));
238
			}
729 mathilde 239
		}
996 aurelien 240
	}
241
 
242
	private function capturerParametresAvancesDependantsLangage() {
243
		if (isset($_GET['au']) && $_GET['au'] != ''
244
		&& $_GET['au'] != $this->convertirEncodageEntree(urlencode($this->i18n['valeur-form-auteur']))) {
901 aurelien 245
			$this->param['au'] = $this->convertirEncodageEntree(urldecode($_GET['au']));
729 mathilde 246
		}
247
		if (isset($_GET['bib']) && $_GET['bib'] != ''
996 aurelien 248
		&& $_GET['bib'] != $this->convertirEncodageEntree(urlencode($this->i18n['valeur-form-bib']))) {
901 aurelien 249
			$this->param['bib'] = $this->convertirEncodageEntree(urldecode($_GET['bib']));
729 mathilde 250
		}
251
		if (isset($_GET['and']) && $_GET['and'] != ''
996 aurelien 252
		&& $_GET['and'] != $this->convertirEncodageEntree(urlencode($this->i18n['valeur-form-date']))) {
901 aurelien 253
			$this->param['and'] = $this->convertirEncodageEntree(urldecode($_GET['and']));
729 mathilde 254
		}
255
		if (isset($_GET['anf']) && $_GET['anf'] != ''
996 aurelien 256
		&& $_GET['anf'] != urlencode($this->i18n['valeur-form-date'])) {
901 aurelien 257
			$this->param['anf'] = $this->convertirEncodageEntree(urldecode($_GET['anf']));
729 mathilde 258
		}
996 aurelien 259
	}
260
 
1140 raphael 261
	private function capturerParametresAvancesPresenceSpecifiques()	 {
996 aurelien 262
		$champs_presence = $this->obtenirChampsPresence();
263
		foreach($champs_presence as $champ) {
264
			$param = $champ['param'];
265
			if (isset($_GET[$param]) && $_GET[$param] != '') {
266
				$this->param[$param] = $this->convertirEncodageEntree(urldecode($_GET[$param]));
267
			}
729 mathilde 268
		}
996 aurelien 269
	}
270
 
271
	private function obtenirChampsPresence() {
272
		$tableau_champs_presence = array();
273
		$referentiel = Registre::get('parametres.referentiel');
1140 raphael 274
		$champs_presence_spl = explode('|', Config::get($referentiel.'.champsPresence'));
996 aurelien 275
		foreach($champs_presence_spl as $champ) {
276
			$label_param_champ = explode(':', $champ);
277
			$tableau_champs_presence[] = array('param' => $label_param_champ[1],
278
														'label' => $label_param_champ[0]);
729 mathilde 279
		}
996 aurelien 280
		return $tableau_champs_presence;
729 mathilde 281
	}
282
 
165 delphine 283
	protected function recupererTableauConfig($param) {
284
		$tableau = array();
285
		$tableauPartiel = explode(',', Config::get($param));
286
		$tableauPartiel = array_map('trim', $tableauPartiel);
287
		foreach ($tableauPartiel as $champ) {
288
			if (strpos($champ, '=') === false) {
289
				$tableau[] = $champ;
290
			} else {
291
				list($cle, $val) = explode('=', $champ);
292
				$tableau[$cle] = $val;
293
			}
294
		}
295
		return $tableau;
296
	}
901 aurelien 297
 
298
 
299
	 /**
300
	 * Convertion des valeurs de requête dans l'encodage de l'application (voir fichier config.ini : appli_encodage),
301
	 * A cause d'un bug en cours d'investigation, celle ci utilise des paramètres différents de la fonction de conversion
302
	 * D'encodage de sortie
303
	 * Cette convertion a lieu seulement si les formats sont différents.
304
	 */
305
	private function convertirEncodageEntree($contenu) {
306
		if (Config::get('sortie_encodage') != Config::get('appli_encodage')) {
307
			$contenu = mb_convert_encoding($contenu, Config::get('appli_encodage'), Config::get('sortie_encodage'));
308
		}
309
		return $contenu;
310
	}
165 delphine 311
 
74 delphine 312
}
313
?>