Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 135 | 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);
137 jpm 80
		$surligneur = new Surligneur();
81
		$surligneur->setTexte($nom);
82
		$nom = $surligneur->surlignerMots($mots);
135 jpm 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 extraireNomsPourDetermination($noms) {
130 jpm 169
		$nomsSansCorrespondance = array();
135 jpm 170
		$nomsAvecCorrespondance = array();
106 jpm 171
		foreach ($noms as $idNomCourant => $nom) {
95 delphine 172
			if ($nom['retenu'] == 'true') {
106 jpm 173
				$nomRetenu = array();
174
				$nomRetenu['nom_sci_retenu'] = $nom['nom_sci'];
175
				$nomRetenu['url'] = $this->obtenirUrlFiche($idNomCourant);
135 jpm 176
				$nomsAvecCorrespondance[$idNomCourant] = $nomRetenu;
106 jpm 177
			} else {
130 jpm 178
				if ($nom['nom_retenu'] == null) {
135 jpm 179
					$nomsSansCorrespondance[$idNomCourant] = $nom['nom_sci'];
130 jpm 180
				} else {
181
					$idNomRetenu = $nom['nom_retenu.id'];
135 jpm 182
					if (array_key_exists($nom['nom_retenu.id'], $nomsAvecCorrespondance) == false) {
130 jpm 183
						$nomRetenu = array();
184
						$nomRetenu['nom_sci_retenu'] = $nom['nom_retenu'];
185
						$nomRetenu['url'] = $this->obtenirUrlFiche($idNomRetenu);
135 jpm 186
						$nomsAvecCorrespondance[$idNomRetenu] = $nomRetenu;
130 jpm 187
					}
135 jpm 188
					$nomsAvecCorrespondance[$idNomRetenu]['synonymes'][] = array('nn' => $idNomCourant, 'nom_sci' => $nom['nom_sci']);
106 jpm 189
				}
130 jpm 190
 
95 delphine 191
			}
192
		}
135 jpm 193
		$nomsPourDetermination = array();
132 jpm 194
		if (count($nomsSansCorrespondance) > 0) {
135 jpm 195
			$nomsPourDetermination['nsc'] = $nomsSansCorrespondance;
132 jpm 196
		}
135 jpm 197
		if (count($nomsAvecCorrespondance) > 0) {
198
			$nomsPourDetermination['nac'] = $nomsAvecCorrespondance;
199
		}
200
		return $nomsPourDetermination;
95 delphine 201
	}
106 jpm 202
 
135 jpm 203
	public function tierNomsPourDetermination($noms) {
204
		$nom_demande_ss = strtolower(Chaine::supprimerAccents($this->masqueRecherche));
205
		foreach ($noms as $id => $nom) {
206
			$nom_flou_ss = strtolower(Chaine::supprimerAccents($nom['nom_sci_retenu']));
207
			$stat = array();
208
			// Prime pour la ressemblance globale :
209
			$score = 500 - levenshtein($nom_flou_ss, $nom_demande_ss);
210
			// On affine
211
			$score = $score + (similar_text($nom_demande_ss, $nom_flou_ss) * 3);
212
			$nom['score'] = $score;
213
			$noms[$id] = $nom;
214
		}
215
		$noms = Tableau::trierMD($noms, array('score' => SORT_DESC));
216
		return $noms;
217
	}
218
 
137 jpm 219
	private function surlignerNomsPourDetermination($noms) {
220
		foreach ($noms as $idNom => $nom) {
221
			$nom['nom_sci_retenu'] = $this->surlignerMotsMasqueRecherche($nom['nom_sci_retenu']);
222
			if (isset($nom['synonymes'])) {
223
				foreach ($nom['synonymes'] as $idSyn => $synonyme) {
224
					$nom['synonymes'][$idSyn]['nom_sci'] = $this->surlignerMotsMasqueRecherche($synonyme['nom_sci']);
225
				}
226
			}
227
			$noms[$idNom] = $nom;
228
		}
229
		return $noms;
230
	}
231
 
106 jpm 232
	private function genererListeDecompo($noms) {
233
		return '<p>À réaliser</p>';
234
	}
76 delphine 235
}
236
?>