Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 996 | 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;
13
	private $i18n =  array();
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-------------------------------------+
74 delphine 104
	public function executerRechercheSimple() {
85 delphine 105
		$donnees['type_nom'] = $this->type_nom;
106
		$donnees['nom'] = $this->nom;
74 delphine 107
		if (strlen($donnees['nom']) < 3) {
123 delphine 108
			$donnees['message']['attention'] = 'info_nb_lettres';
74 delphine 109
		} else {
88 delphine 110
			$presence = $this->rechercherNom();
123 delphine 111
			if ($presence == '') { // s'il n'y a pas de nom
112
				$donnees['message']['attention'] = 'info_sp_abs';
720 delphine 113
			} elseif ($presence == 'sans_correspondance') {
114
				$res = Registre::get('resultats');
115
				$id = array_keys($res['resultat']);
116
				$donnees['message']['nom_ss_corresp']['id'] = $id[0];
117
				$nom = array_shift($res['resultat']);
118
				$donnees['message']['nom_ss_corresp']['nom'] = $nom['nom_sci'];
123 delphine 119
			} elseif ($presence != 'ok') { // s'il y a des noms approchés
120
				if (!Registre::get('resultats')) { // s'il n'y a aucun nom exact
121
					$donnees['message']['attention'] = 'info_sp_abs';
122
				}
123
				$donnees['message']['nom_approche'] = $presence;
74 delphine 124
			}
125
		}
179 delphine 126
		$this->executerAccueil($donnees);
88 delphine 127
		if (Registre::get('resultats')) {
185 delphine 128
			$_GET['resultat'] = $this->type_resultat;
76 delphine 129
			$this->executerAction('Resultat', 'executerResultat');
130
		}
74 delphine 131
	}
132
 
729 mathilde 133
 
74 delphine 134
	// regarde si il y a des résultats correspondant au nom recherché sinon recherche un nom approché
88 delphine 135
	// $noms classe métier nom ou nom
136
	private function rechercherNom() {
137
		$noms = ($this->type_nom == 'nom_vernaculaire')
138
				? new NomsVernaculaires(Config::get(Registre::get('parametres.referentiel').'.referentielVerna'))
139
				: new Noms(Registre::get('parametres.referentiel'));
74 delphine 140
		$approche = '';
165 delphine 141
		$res = $noms->getRechercheEtendue($this->nom, $this->type_resultat);
74 delphine 142
		$form = I18n::get('Recherche-form-nom');
143
		if ($res == false || $res['entete']['total'] == 0) { // recherche nom approché
88 delphine 144
			$approche = $this->rechercherNomApproche($noms);
885 aurelien 145
		} elseif ($res['entete']['total'] == 1 || $this->acces_fiche) { // renvoie à la fiche
720 delphine 146
			$approche = $this->traiterAccesFiche($res);
74 delphine 147
		} else { // affiche les résultats
85 delphine 148
			$res['type'] = $this->type_nom;
76 delphine 149
			Registre::set('resultats', $res);
74 delphine 150
			$approche = 'ok';
123 delphine 151
			if ($res['entete']['total'] < 3) { // si moins de 16 noms affiche en plus un nom approché
88 delphine 152
				$approche = $this->rechercherNomApproche($noms);
153
			}
74 delphine 154
		}
155
		return $approche;
156
	}
157
 
720 delphine 158
	private function traiterAccesFiche($res) {
159
		$ids = array_keys($res['resultat']);
160
		if ($this->type_nom == 'nom_vernaculaire') {
161
			$id = explode(':',$res['resultat'][$ids[0]]['nom_retenu.code']);
162
			$id = $id[1];
163
		} else {
164
			if ($res['resultat'][$ids[0]]['retenu'] == 'absent') { // dans le cas d'un nom sans correspondance
165
				$res['type'] = $this->type_nom;
166
				Registre::set('resultats', $res);
167
				$approche = 'sans_correspondance';
168
				return $approche;
169
			} else {
170
				$id = $ids[0];
171
			}
172
		}
173
		$url = $this->urls->obtenirUrlFiche($id, $this->type_nom, $this->nom);
174
		$this->redirigerVers($url);
175
	}
176
 
88 delphine 177
	private function rechercherNomApproche($noms) {
178
		$approche = '';
179
		$res = $noms->getRechercheFloue($this->nom);
180
		if (!($res == false || $res['entete']['total'] == 0)) {
123 delphine 181
			for ($i = 0; $i < 3; $i++) {
182
				$nom_proche = array_shift($res['resultat']);
183
				$approche[$i]['nom'] = ($this->type_nom == 'nom_vernaculaire') ? $nom_proche['nom'] : $nom_proche['nom_sci'];
156 delphine 184
				$approche[$i]['url_nom_approche'] = $this->urls->obtenirUrlRechercheSimple($approche[$i]['nom'], $this->type_nom);
123 delphine 185
			}
88 delphine 186
		}
187
		return $approche;
188
	}
189
 
729 mathilde 190
	//+-----------------------------------------------méthodes utiles---------------------------------+
179 delphine 191
 
85 delphine 192
	private function capturerParametres() {
720 delphine 193
		if (isset($_REQUEST['nom'])) {
901 aurelien 194
			$this->nom = $this->convertirEncodageEntree(urldecode($_REQUEST['nom']));
85 delphine 195
		}
196
		if (isset($_GET['type_nom'])) {
901 aurelien 197
			$this->type_nom = $this->convertirEncodageEntree(urldecode($_GET['type_nom']));
85 delphine 198
		}
199
		if (isset($_GET['submit'])) {
901 aurelien 200
			$this->submit = $this->convertirEncodageEntree(urldecode($_GET['submit']));
85 delphine 201
		}
885 aurelien 202
		if(isset($_GET['acces_fiche'])) {
203
			$this->acces_fiche = true;
204
		}
174 delphine 205
		if (isset($_GET['niveau'])) {
901 aurelien 206
			Registre::set('parametres.niveau', $this->convertirEncodageEntree($_GET['niveau']));
174 delphine 207
		}
165 delphine 208
		if (isset($_GET['resultat'])) {
901 aurelien 209
			$this->type_resultat = $this->convertirEncodageEntree(urldecode($_GET['resultat']));
165 delphine 210
		} else {
211
			$onglet_resultat = $this->recupererTableauConfig('affich_resultats');
212
			$this->type_resultat = $onglet_resultat[Registre::get('parametres.niveau').'_'.$this->type_nom];
213
		}
85 delphine 214
	}
215
 
729 mathilde 216
	private function capturerParametresAvances() {
996 aurelien 217
		$this->capturerParametresAvancesGeneriques();
218
		$this->capturerParametresAvancesDependantsLangage();
219
		$this->capturerParametresAvancesPresenceSpecifiques();
220
	}
221
 
222
	private function capturerParametresAvancesGeneriques() {
223
		foreach($this->parametresAvancesGeneriques as $param) {
224
			if (isset($_GET[$param]) && $_GET[$param] != '') {
225
				$this->param[$param] = $this->convertirEncodageEntree(urldecode($_GET[$param]));
226
			}
729 mathilde 227
		}
996 aurelien 228
	}
229
 
230
	private function capturerParametresAvancesDependantsLangage() {
231
		if (isset($_GET['au']) && $_GET['au'] != ''
232
		&& $_GET['au'] != $this->convertirEncodageEntree(urlencode($this->i18n['valeur-form-auteur']))) {
901 aurelien 233
			$this->param['au'] = $this->convertirEncodageEntree(urldecode($_GET['au']));
729 mathilde 234
		}
235
		if (isset($_GET['bib']) && $_GET['bib'] != ''
996 aurelien 236
		&& $_GET['bib'] != $this->convertirEncodageEntree(urlencode($this->i18n['valeur-form-bib']))) {
901 aurelien 237
			$this->param['bib'] = $this->convertirEncodageEntree(urldecode($_GET['bib']));
729 mathilde 238
		}
239
		if (isset($_GET['and']) && $_GET['and'] != ''
996 aurelien 240
		&& $_GET['and'] != $this->convertirEncodageEntree(urlencode($this->i18n['valeur-form-date']))) {
901 aurelien 241
			$this->param['and'] = $this->convertirEncodageEntree(urldecode($_GET['and']));
729 mathilde 242
		}
243
		if (isset($_GET['anf']) && $_GET['anf'] != ''
996 aurelien 244
		&& $_GET['anf'] != urlencode($this->i18n['valeur-form-date'])) {
901 aurelien 245
			$this->param['anf'] = $this->convertirEncodageEntree(urldecode($_GET['anf']));
729 mathilde 246
		}
996 aurelien 247
	}
248
 
249
	private function capturerParametresAvancesPresenceSpecifiques()  {
250
		$champs_presence = $this->obtenirChampsPresence();
251
		foreach($champs_presence as $champ) {
252
			$param = $champ['param'];
253
			if (isset($_GET[$param]) && $_GET[$param] != '') {
254
				$this->param[$param] = $this->convertirEncodageEntree(urldecode($_GET[$param]));
255
			}
729 mathilde 256
		}
996 aurelien 257
	}
258
 
259
	private function obtenirChampsPresence() {
260
		$tableau_champs_presence = array();
261
		$referentiel = Registre::get('parametres.referentiel');
262
		$champs_presence = Config::get($referentiel.'.champsPresence');
263
		$champs_presence_spl = explode('|', $champs_presence);
264
		foreach($champs_presence_spl as $champ) {
265
			$label_param_champ = explode(':', $champ);
266
			$tableau_champs_presence[] = array('param' => $label_param_champ[1],
267
														'label' => $label_param_champ[0]);
729 mathilde 268
		}
996 aurelien 269
		return $tableau_champs_presence;
729 mathilde 270
	}
271
 
165 delphine 272
	protected function recupererTableauConfig($param) {
273
		$tableau = array();
274
		$tableauPartiel = explode(',', Config::get($param));
275
		$tableauPartiel = array_map('trim', $tableauPartiel);
276
		foreach ($tableauPartiel as $champ) {
277
			if (strpos($champ, '=') === false) {
278
				$tableau[] = $champ;
279
			} else {
280
				list($cle, $val) = explode('=', $champ);
281
				$tableau[$cle] = $val;
282
			}
283
		}
284
		return $tableau;
285
	}
901 aurelien 286
 
287
 
288
	 /**
289
	 * Convertion des valeurs de requête dans l'encodage de l'application (voir fichier config.ini : appli_encodage),
290
	 * A cause d'un bug en cours d'investigation, celle ci utilise des paramètres différents de la fonction de conversion
291
	 * D'encodage de sortie
292
	 * Cette convertion a lieu seulement si les formats sont différents.
293
	 */
294
	private function convertirEncodageEntree($contenu) {
295
		if (Config::get('sortie_encodage') != Config::get('appli_encodage')) {
296
			$contenu = mb_convert_encoding($contenu, Config::get('appli_encodage'), Config::get('sortie_encodage'));
297
		}
298
		return $contenu;
299
	}
165 delphine 300
 
74 delphine 301
}
302
?>