Subversion Repositories eFlore/Applications.eflore-consultation

Rev

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