Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 1143 | 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()) {
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');
161
		if ($res == false || $res['entete']['total'] == 0) { // recherche nom approché
88 delphine 162
			$approche = $this->rechercherNomApproche($noms);
885 aurelien 163
		} elseif ($res['entete']['total'] == 1 || $this->acces_fiche) { // renvoie à la fiche
720 delphine 164
			$approche = $this->traiterAccesFiche($res);
74 delphine 165
		} else { // affiche les résultats
85 delphine 166
			$res['type'] = $this->type_nom;
76 delphine 167
			Registre::set('resultats', $res);
74 delphine 168
			$approche = 'ok';
123 delphine 169
			if ($res['entete']['total'] < 3) { // si moins de 16 noms affiche en plus un nom approché
88 delphine 170
				$approche = $this->rechercherNomApproche($noms);
171
			}
74 delphine 172
		}
173
		return $approche;
174
	}
175
 
720 delphine 176
	private function traiterAccesFiche($res) {
177
		$ids = array_keys($res['resultat']);
178
		if ($this->type_nom == 'nom_vernaculaire') {
179
			$id = explode(':',$res['resultat'][$ids[0]]['nom_retenu.code']);
180
			$id = $id[1];
181
		} else {
182
			if ($res['resultat'][$ids[0]]['retenu'] == 'absent') { // dans le cas d'un nom sans correspondance
183
				$res['type'] = $this->type_nom;
184
				Registre::set('resultats', $res);
185
				$approche = 'sans_correspondance';
186
				return $approche;
187
			} else {
188
				$id = $ids[0];
189
			}
190
		}
191
		$url = $this->urls->obtenirUrlFiche($id, $this->type_nom, $this->nom);
192
		$this->redirigerVers($url);
193
	}
194
 
88 delphine 195
	private function rechercherNomApproche($noms) {
196
		$approche = '';
197
		$res = $noms->getRechercheFloue($this->nom);
198
		if (!($res == false || $res['entete']['total'] == 0)) {
123 delphine 199
			for ($i = 0; $i < 3; $i++) {
200
				$nom_proche = array_shift($res['resultat']);
201
				$approche[$i]['nom'] = ($this->type_nom == 'nom_vernaculaire') ? $nom_proche['nom'] : $nom_proche['nom_sci'];
156 delphine 202
				$approche[$i]['url_nom_approche'] = $this->urls->obtenirUrlRechercheSimple($approche[$i]['nom'], $this->type_nom);
123 delphine 203
			}
88 delphine 204
		}
205
		return $approche;
206
	}
207
 
729 mathilde 208
	//+-----------------------------------------------méthodes utiles---------------------------------+
179 delphine 209
 
85 delphine 210
	private function capturerParametres() {
720 delphine 211
		if (isset($_REQUEST['nom'])) {
901 aurelien 212
			$this->nom = $this->convertirEncodageEntree(urldecode($_REQUEST['nom']));
85 delphine 213
		}
214
		if (isset($_GET['type_nom'])) {
901 aurelien 215
			$this->type_nom = $this->convertirEncodageEntree(urldecode($_GET['type_nom']));
85 delphine 216
		}
217
		if (isset($_GET['submit'])) {
901 aurelien 218
			$this->submit = $this->convertirEncodageEntree(urldecode($_GET['submit']));
85 delphine 219
		}
885 aurelien 220
		if(isset($_GET['acces_fiche'])) {
221
			$this->acces_fiche = true;
222
		}
174 delphine 223
		if (isset($_GET['niveau'])) {
901 aurelien 224
			Registre::set('parametres.niveau', $this->convertirEncodageEntree($_GET['niveau']));
174 delphine 225
		}
165 delphine 226
		if (isset($_GET['resultat'])) {
901 aurelien 227
			$this->type_resultat = $this->convertirEncodageEntree(urldecode($_GET['resultat']));
165 delphine 228
		} else {
229
			$onglet_resultat = $this->recupererTableauConfig('affich_resultats');
230
			$this->type_resultat = $onglet_resultat[Registre::get('parametres.niveau').'_'.$this->type_nom];
231
		}
85 delphine 232
	}
233
 
729 mathilde 234
	private function capturerParametresAvances() {
996 aurelien 235
		$this->capturerParametresAvancesGeneriques();
236
		$this->capturerParametresAvancesDependantsLangage();
237
		$this->capturerParametresAvancesPresenceSpecifiques();
238
	}
239
 
240
	private function capturerParametresAvancesGeneriques() {
241
		foreach($this->parametresAvancesGeneriques as $param) {
242
			if (isset($_GET[$param]) && $_GET[$param] != '') {
243
				$this->param[$param] = $this->convertirEncodageEntree(urldecode($_GET[$param]));
244
			}
729 mathilde 245
		}
996 aurelien 246
	}
247
 
248
	private function capturerParametresAvancesDependantsLangage() {
249
		if (isset($_GET['au']) && $_GET['au'] != ''
250
		&& $_GET['au'] != $this->convertirEncodageEntree(urlencode($this->i18n['valeur-form-auteur']))) {
901 aurelien 251
			$this->param['au'] = $this->convertirEncodageEntree(urldecode($_GET['au']));
729 mathilde 252
		}
253
		if (isset($_GET['bib']) && $_GET['bib'] != ''
996 aurelien 254
		&& $_GET['bib'] != $this->convertirEncodageEntree(urlencode($this->i18n['valeur-form-bib']))) {
901 aurelien 255
			$this->param['bib'] = $this->convertirEncodageEntree(urldecode($_GET['bib']));
729 mathilde 256
		}
257
		if (isset($_GET['and']) && $_GET['and'] != ''
996 aurelien 258
		&& $_GET['and'] != $this->convertirEncodageEntree(urlencode($this->i18n['valeur-form-date']))) {
901 aurelien 259
			$this->param['and'] = $this->convertirEncodageEntree(urldecode($_GET['and']));
729 mathilde 260
		}
261
		if (isset($_GET['anf']) && $_GET['anf'] != ''
996 aurelien 262
		&& $_GET['anf'] != urlencode($this->i18n['valeur-form-date'])) {
901 aurelien 263
			$this->param['anf'] = $this->convertirEncodageEntree(urldecode($_GET['anf']));
729 mathilde 264
		}
996 aurelien 265
	}
266
 
1140 raphael 267
	private function capturerParametresAvancesPresenceSpecifiques()	 {
996 aurelien 268
		$champs_presence = $this->obtenirChampsPresence();
269
		foreach($champs_presence as $champ) {
270
			$param = $champ['param'];
271
			if (isset($_GET[$param]) && $_GET[$param] != '') {
272
				$this->param[$param] = $this->convertirEncodageEntree(urldecode($_GET[$param]));
273
			}
729 mathilde 274
		}
996 aurelien 275
	}
276
 
277
	private function obtenirChampsPresence() {
278
		$tableau_champs_presence = array();
279
		$referentiel = Registre::get('parametres.referentiel');
1140 raphael 280
		$champs_presence_spl = explode('|', Config::get($referentiel.'.champsPresence'));
996 aurelien 281
		foreach($champs_presence_spl as $champ) {
282
			$label_param_champ = explode(':', $champ);
1165 aurelien 283
			if(count($label_param_champ) >= 2) {
284
				$tableau_champs_presence[] = array('param' => $label_param_champ[1],
996 aurelien 285
														'label' => $label_param_champ[0]);
1165 aurelien 286
			}
729 mathilde 287
		}
996 aurelien 288
		return $tableau_champs_presence;
729 mathilde 289
	}
290
 
165 delphine 291
	protected function recupererTableauConfig($param) {
292
		$tableau = array();
293
		$tableauPartiel = explode(',', Config::get($param));
294
		$tableauPartiel = array_map('trim', $tableauPartiel);
295
		foreach ($tableauPartiel as $champ) {
296
			if (strpos($champ, '=') === false) {
297
				$tableau[] = $champ;
298
			} else {
299
				list($cle, $val) = explode('=', $champ);
300
				$tableau[$cle] = $val;
301
			}
302
		}
303
		return $tableau;
304
	}
901 aurelien 305
 
306
 
307
	 /**
308
	 * Convertion des valeurs de requête dans l'encodage de l'application (voir fichier config.ini : appli_encodage),
309
	 * A cause d'un bug en cours d'investigation, celle ci utilise des paramètres différents de la fonction de conversion
310
	 * D'encodage de sortie
311
	 * Cette convertion a lieu seulement si les formats sont différents.
312
	 */
313
	private function convertirEncodageEntree($contenu) {
314
		if (Config::get('sortie_encodage') != Config::get('appli_encodage')) {
315
			$contenu = mb_convert_encoding($contenu, Config::get('appli_encodage'), Config::get('sortie_encodage'));
316
		}
317
		return $contenu;
318
	}
165 delphine 319
 
74 delphine 320
}
321
?>