Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 132 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
76 delphine 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Classe mère du module Liste.
5
 *
6
 * @category	PHP 5.2
7
 * @package		eflore-consultation
8
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
9
 * @author		Delphine CAUQUIL <delphine@tela-botanica.org>
10
 * @copyright	2011 Tela-Botanica
11
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
12
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
13
 * @version		$Id$
14
 */
15
class Resultat extends aControleur {
106 jpm 16
 
95 delphine 17
	private $resultatType = 'determination';
135 jpm 18
	private $masqueRecherche = '';
132 jpm 19
	private $projetImg = 'cel';
20
	private $reftaxCourant = 'bdtfx';
106 jpm 21
 
84 jpm 22
	public function initialiser() {
23
		$this->capturerParametres();
132 jpm 24
		$this->reftaxCourant = Registre::get('parametres.referentiel');
25
		$this->projetImg = Config::get($this->reftaxCourant.'.referentielImages');
84 jpm 26
	}
106 jpm 27
 
84 jpm 28
	private function capturerParametres() {
29
		if (isset($_GET['resultat'])) {
30
			$this->resultatType = $_GET['resultat'];
31
		}
135 jpm 32
		if (isset($_GET['nom'])) {
33
			$this->masqueRecherche = $_GET['nom'];
34
		}
84 jpm 35
	}
106 jpm 36
 
76 delphine 37
	public function executerActionParDefaut() {
38
		$this->executerResultat();
39
	}
106 jpm 40
 
76 delphine 41
	public function executerResultat() {
42
		$resultats = Registre::get('resultats');
106 jpm 43
		$donnees = array();
84 jpm 44
		$donnees['typeResultat'] = $this->resultatType;
106 jpm 45
		$donnees['urlResAlphab'] = $this->obtenirUrlResultatAlphab();
84 jpm 46
		$donnees['urlResRetenu'] = $this->obtenirUrlResultatRetenu();
95 delphine 47
		$donnees['urlResDetermination'] = $this->obtenirUrlResultatDetermination();
106 jpm 48
		$donnees['urlResDecompo'] = $this->obtenirUrlResultatDecompo();
76 delphine 49
		$donnees['nbreTaxons'] = $resultats['entete']['total'];
84 jpm 50
		$donnees['nomsHtml'] = $this->getNoms($resultats['resultat']);
106 jpm 51
 
76 delphine 52
		$this->setSortie(self::RENDU_CORPS, $this->getVue('resultat', $donnees));
53
	}
106 jpm 54
 
84 jpm 55
	private function getNoms($resultats) {
56
		$noms = null;
57
		$methode = 'genererListe'.ucwords($this->resultatType);
58
		$noms = $this->$methode($resultats);
59
		return $noms;
60
	}
106 jpm 61
 
62
	private function genererListeAlphab($resultats) {
135 jpm 63
		$noms = $this->trierParAlphabNaturel($resultats);
64
		$noms = $this->surlignerNoms($noms);
65
 
84 jpm 66
		$donnees = array();
135 jpm 67
		$donnees['noms'] = $noms;
84 jpm 68
		return $this->getVue('resultat_liste_noms', $donnees);
69
	}
106 jpm 70
 
135 jpm 71
	private function surlignerNoms($noms) {
72
		foreach ($noms as $id => $nom) {
73
			$noms[$id]['nom_sci'] = $this->surlignerMotsMasqueRecherche($nom['nom_sci']);
74
		}
84 jpm 75
		return $noms;
76
	}
106 jpm 77
 
135 jpm 78
	private function surlignerMotsMasqueRecherche($nom) {
79
		$mots = explode(' ', $this->masqueRecherche);
80
		foreach ($mots as $mot) {
81
			$nom = $this->surlignerMotDansTxt($mot, $nom);
82
		}
83
		return $nom;
84
	}
85
 
86
	private function surlignerMotDansTxt($mot, $txt){
87
		$ind = stripos($txt, $mot);
88
		$len = strlen($mot);
89
		$surlignage = $txt;
90
		if ($ind !== false){
91
			$debut = substr($txt, 0, $ind);
92
			$milieu = substr($txt, $ind, $len);
93
			$fin = $this->surlignerMotDansTxt($mot, substr($txt, $ind + $len));
94
			$surlignage = $debut.'<span class="surlignage">'.$milieu.'</span>'.$fin;
95
		}
96
		return $surlignage;
97
	}
98
 
99
	private function trierParAlphabNaturel($noms) {
100
		$noms = Tableau::trierMDType($noms, array('nom_sci' => SORT_ASC), Tableau::TRI_NATUREL);
101
		return $noms;
102
	}
103
 
84 jpm 104
	private function genererListeRetenu($resultats) {
135 jpm 105
		$noms = $this->trierParNomsRetenus($resultats);
106
		$noms = $this->surlignerNoms($noms);
84 jpm 107
		$donnees = array();
135 jpm 108
		$donnees['noms'] = $noms;
84 jpm 109
		return $this->getVue('resultat_liste_noms', $donnees);
110
	}
106 jpm 111
 
84 jpm 112
	private function trierParNomsRetenus($noms) {
113
		$nomsRetenus = array();
114
		$nomsSynonymes = array();
115
		foreach ($noms as $id => $nom) {
116
			if ($nom['retenu'] == 'true') {
117
				$nomsRetenus[$id] = $nom;
118
			} else {
119
				$nomsSynonymes[$id] = $nom;
120
			}
121
		}
135 jpm 122
		$nomsRetenus = Tableau::trierMDType($nomsRetenus, array('nom_sci' => SORT_ASC), Tableau::TRI_NATUREL);
123
		$nomsSynonymes = Tableau::trierMDType($nomsSynonymes, array('nom_sci' => SORT_ASC), Tableau::TRI_NATUREL);
84 jpm 124
		$noms = array_merge($nomsRetenus, $nomsSynonymes);
125
		return $noms;
126
	}
106 jpm 127
 
95 delphine 128
	private function genererListeDetermination($resultats) {
129
		$donnees = array();
132 jpm 130
		$donnees['imagesUrls'] = $this->obtenirUrlsPhotos($resultats);
135 jpm 131
		$noms = $this->extraireNomsPourDetermination($resultats);
132
		$donnees['nomsSansCorrespondance'] = isset($noms['nsc']) ? $noms['nsc'] : false;
133
		$noms = isset($noms['nac']) ? $noms['nac'] : false;
134
 
135
		if ($noms) {
136
			$noms = $this->tierNomsPourDetermination($noms);
137
			$noms = $this->surlignerNomsPourDetermination($noms);
138
		}
139
		$donnees['noms'] = $noms;
95 delphine 140
		return $this->getVue('resultat_determination_noms', $donnees);
141
	}
84 jpm 142
 
132 jpm 143
	private function obtenirUrlsPhotos($noms) {
144
		$ids = $this->extraireIdDesNoms($noms);
145
		$Images = new Images($this->projetImg);
146
		$urls = $Images->getUrlsImagesParIdsNoms($ids);
147
		$urlsReftaxCourant = $this->supprimerCodeReftaxDesIds($urls);
148
		return $urlsReftaxCourant;
149
	}
150
 
151
	private function extraireIdDesNoms($noms) {
130 jpm 152
		$ids = array();
153
		foreach ($noms as $id => $nom) {
154
			$ids[] = $id;
155
		}
132 jpm 156
		return $ids;
157
	}
158
 
159
	private function supprimerCodeReftaxDesIds($urls) {
160
		$codeReftax = $this->reftaxCourant.'.';
130 jpm 161
		foreach ($urls as $id => $url) {
132 jpm 162
			$id = str_replace($codeReftax, '', $id);
163
			$urls[$id] = $url;
130 jpm 164
		}
132 jpm 165
		return $urls;
130 jpm 166
	}
167
 
135 jpm 168
	private function surlignerNomsPourDetermination($noms) {
169
		foreach ($noms as $idNom => $nom) {
170
			$nom['nom_sci_retenu'] = $this->surlignerMotsMasqueRecherche($nom['nom_sci_retenu']);
171
			if (isset($nom['synonymes'])) {
172
				foreach ($nom['synonymes'] as $idSyn => $synonyme) {
173
					$nom['synonymes'][$idSyn]['nom_sci'] = $this->surlignerMotsMasqueRecherche($synonyme['nom_sci']);
174
				}
175
			}
176
			$noms[$idNom] = $nom;
177
		}
178
		return $noms;
179
	}
180
 
181
	private function extraireNomsPourDetermination($noms) {
130 jpm 182
		$nomsSansCorrespondance = array();
135 jpm 183
		$nomsAvecCorrespondance = array();
106 jpm 184
		foreach ($noms as $idNomCourant => $nom) {
95 delphine 185
			if ($nom['retenu'] == 'true') {
106 jpm 186
				$nomRetenu = array();
187
				$nomRetenu['nom_sci_retenu'] = $nom['nom_sci'];
188
				$nomRetenu['url'] = $this->obtenirUrlFiche($idNomCourant);
135 jpm 189
				$nomsAvecCorrespondance[$idNomCourant] = $nomRetenu;
106 jpm 190
			} else {
130 jpm 191
				if ($nom['nom_retenu'] == null) {
135 jpm 192
					$nomsSansCorrespondance[$idNomCourant] = $nom['nom_sci'];
130 jpm 193
				} else {
194
					$idNomRetenu = $nom['nom_retenu.id'];
135 jpm 195
					if (array_key_exists($nom['nom_retenu.id'], $nomsAvecCorrespondance) == false) {
130 jpm 196
						$nomRetenu = array();
197
						$nomRetenu['nom_sci_retenu'] = $nom['nom_retenu'];
198
						$nomRetenu['url'] = $this->obtenirUrlFiche($idNomRetenu);
135 jpm 199
						$nomsAvecCorrespondance[$idNomRetenu] = $nomRetenu;
130 jpm 200
					}
135 jpm 201
					$nomsAvecCorrespondance[$idNomRetenu]['synonymes'][] = array('nn' => $idNomCourant, 'nom_sci' => $nom['nom_sci']);
106 jpm 202
				}
130 jpm 203
 
95 delphine 204
			}
205
		}
135 jpm 206
		$nomsPourDetermination = array();
132 jpm 207
		if (count($nomsSansCorrespondance) > 0) {
135 jpm 208
			$nomsPourDetermination['nsc'] = $nomsSansCorrespondance;
132 jpm 209
		}
135 jpm 210
		if (count($nomsAvecCorrespondance) > 0) {
211
			$nomsPourDetermination['nac'] = $nomsAvecCorrespondance;
212
		}
213
		return $nomsPourDetermination;
95 delphine 214
	}
106 jpm 215
 
135 jpm 216
	public function tierNomsPourDetermination($noms) {
217
		$nom_demande_ss = strtolower(Chaine::supprimerAccents($this->masqueRecherche));
218
		foreach ($noms as $id => $nom) {
219
			$nom_flou_ss = strtolower(Chaine::supprimerAccents($nom['nom_sci_retenu']));
220
			$stat = array();
221
			// Prime pour la ressemblance globale :
222
			$score = 500 - levenshtein($nom_flou_ss, $nom_demande_ss);
223
			// On affine
224
			$score = $score + (similar_text($nom_demande_ss, $nom_flou_ss) * 3);
225
			$nom['score'] = $score;
226
			$noms[$id] = $nom;
227
		}
228
		$noms = Tableau::trierMD($noms, array('score' => SORT_DESC));
229
		return $noms;
230
	}
231
 
106 jpm 232
	private function genererListeDecompo($noms) {
233
		return '<p>À réaliser</p>';
234
	}
76 delphine 235
}
236
?>