Subversion Repositories eFlore/Applications.eflore-consultation

Rev

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