Subversion Repositories eFlore/Applications.eflore-consultation

Rev

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