Subversion Repositories eFlore/Applications.eflore-consultation

Rev

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