Subversion Repositories eFlore/Applications.eflore-consultation

Rev

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

Rev Author Line No. Line
146 jpm 1
<?php
2
class DeterminationFormateur implements Formateur {
3
 
159 jpm 4
	const TPL_VUE = 'determination';
146 jpm 5
 
6
	private $parametres = null;
7
	private $surligneur = null;
8
	private $trieur = null;
9
	private $urls = null;
10
	private $fusioneur = null;
11
	private $manipulateurDeChaine = null;
12
	private $imagesService = null;
13
 
14
	private $motsASurligner = array();
15
	private $noms = array();
16
	private $infosPourTpl = array();
617 mathilde 17
 
311 jpm 18
 
146 jpm 19
	public function __construct(ParametresResultats $parametres, Array $resultats,
20
		Surligneur $surligneur = null, Trieur $trieur = null, AppUrls $urls = null,
21
		ChaineManipulateur $manipulateurDeChaine = null, Images $imagesService = null) {
22
 
23
		$this->parametres = $parametres;
24
		$this->noms = $resultats['resultat'];
25
		$this->surligneur = (is_null($surligneur)) ? new Surligneur() : $surligneur;
26
		$this->trieur = (is_null($trieur)) ? new Trieur() : $trieur;
27
		$this->urls = (is_null($urls)) ? new AppUrls() : $urls;
28
		$this->manipulateurDeChaine = is_null($manipulateurDeChaine) ? new ChaineManipulateur() : $manipulateurDeChaine;
29
		$this->imagesService = is_null($imagesService) ? new Images($this->parametres->projetImg) : $imagesService;
30
	}
31
 
32
	public function getTplInfos() {
33
		return $this->infosPourTpl;
34
	}
35
 
36
	public function getTplNom() {
37
		return self::TPL_VUE;
38
	}
39
 
40
	public function formater() {
617 mathilde 41
		$this->obtenirUrlsImagesCoste();
146 jpm 42
		$this->obtenirUrlsPhotos();
279 gduche 43
		$this->chargerRepartition();
146 jpm 44
		$this->extraireInfosNomsPourTplDetermination();
617 mathilde 45
 
146 jpm 46
	}
47
 
48
	private function obtenirUrlsPhotos() {
617 mathilde 49
		$this->imagesService->setProjet('cel');
146 jpm 50
		$ids = $this->extraireIdDesNoms();
51
		$urls = $this->imagesService->getUrlsImagesParIdsNoms($ids);
52
		$this->infosPourTpl['imagesUrls'] = $this->supprimerCodeReftaxDesIds($urls);
53
	}
617 mathilde 54
 
55
	private function obtenirUrlsImagesCoste() {
56
		$this->extraireInfosTaxons();
57
		$this->imagesService->setProjet('coste');
58
		foreach ($this->infosPourTpl['taxons'] as $nn => $tax) {
59
			$this->imagesService->setNnTaxon($tax);
60
			$costeImg = $this->imagesService->getInfosImagesTaxons();
61
			if (!empty($costeImg)) {
62
				foreach ($costeImg as  $infos) {
63
					$num_taxon = $infos['num_taxonomique'];
64
					$images[$num_taxon][] = $infos['binaire.href'];
65
					$this->infosPourTpl['imagesCoste'] = $images;
66
				}
67
			}
68
		}
69
 
70
	}
509 jpm 71
 
617 mathilde 72
	private function extraireInfosTaxons() {
73
		foreach ($this->noms as $id => $nom ) {
74
			if (array_key_exists('num_taxonomique', $nom)) {
75
				$this->infosPourTpl['taxons'][$id] = $nom['num_taxonomique'];
76
			}
77
		}
78
	}
79
 
311 jpm 80
	// TODO : utiliser le conteneur pour charger tous les objets de cette classe
279 gduche 81
	private function chargerRepartition() {
311 jpm 82
		$conteneur = new Conteneur();
83
		$cartesWs = $conteneur->getApiCartes();
84
		$cartesWs->setProjet('chorodep');
85
		$cartesWs->setLargeur('108x101');
86
		$urls = array();
87
		foreach ($this->noms as $id => $nom) {
88
			if (array_key_exists('nom_retenu.id', $nom)) {
89
				$id = $nom['nom_retenu.id'];
90
				$cartesWs->setId("nn:$id");
91
				if (array_key_exists($id, $urls) == false) {
92
					$urls[$id] = $cartesWs->getUrlPng();
93
				}
94
			}
95
		}
96
		$this->infosPourTpl['repartition']['urls'] = $urls;
279 gduche 97
	}
311 jpm 98
 
617 mathilde 99
 
100
 
101
 
146 jpm 102
	private function extraireIdDesNoms() {
103
		$ids = array();
104
		foreach ($this->noms as $id => $nom) {
105
			$idAAjouter = $id;
106
			if (is_numeric($idAAjouter)) {
107
				$ids[] = $idAAjouter;
108
			}
109
			if (array_key_exists('nom_retenu.id', $nom)) {
110
				if (in_array($nom['nom_retenu.id'], $ids) == false) {
111
					$idAAjouter = $nom['nom_retenu.id'];
112
					if (is_numeric($idAAjouter)) {
113
						$ids[] = $idAAjouter;
114
					}
115
				}
116
			}
117
		}
118
		return $ids;
119
	}
120
 
121
	private function supprimerCodeReftaxDesIds($urls) {
122
		$urlsNettoyees = array();
123
		foreach ($urls as $id => $url) {
311 jpm 124
			$id = $this->supprimerCodeReftax($id);
146 jpm 125
			$urlsNettoyees[$id] = $url;
126
		}
127
		return $urlsNettoyees;
128
	}
129
 
311 jpm 130
	private function supprimerCodeReftax($chaine) {
131
		$codeReftax = $this->parametres->reftaxCourant.'.';
132
		$chaine = str_replace($codeReftax, '', $chaine);
133
		return $chaine;
134
	}
135
 
146 jpm 136
	private function extraireInfosNomsPourTplDetermination() {
137
		$nomsSansCorrespondance = array();
138
		$nomsAvecCorrespondance = array();
252 delphine 139
		$synonymesAvecCorrespondance = array();
146 jpm 140
		foreach ($this->noms as $idNomCourant => $nom) {
141
			if ($nom['retenu'] == 'true') {
142
				$nomRetenu = array();
143
				$nomRetenu['nomSciRetenu'] = $nom['nom_sci'];
369 delphine 144
				$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche);
146 jpm 145
				$nomsAvecCorrespondance[$idNomCourant] = $nomRetenu;
146
			} else {
593 aurelien 147
				if ($nom['nom_retenu.libelle'] == null) {
146 jpm 148
					$nomsSansCorrespondance[$idNomCourant] = $nom['nom_sci'];
149
				} else {
150
					$idNomRetenu = $nom['nom_retenu.id'];
151
					if (array_key_exists($nom['nom_retenu.id'], $nomsAvecCorrespondance) == false) {
152
						$nomRetenu = array();
593 aurelien 153
						$nomRetenu['nomSciRetenu'] = $nom['nom_retenu.libelle'];
154
						$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomRetenu, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu.libelle']);
146 jpm 155
						$nomsAvecCorrespondance[$idNomRetenu] = $nomRetenu;
156
					}
157
					$synonyme = array();
158
					$synonyme['nn'] = $idNomCourant;
159
					$synonyme['nomSci'] = $nom['nom_sci'];
593 aurelien 160
					$synonyme['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu.libelle']);
252 delphine 161
					$synonymesAvecCorrespondance[$idNomRetenu][] = $synonyme;
146 jpm 162
				}
163
			}
164
		}
165
 
166
		$this->infosPourTpl['nomsSansCorrespondance'] = (count($nomsSansCorrespondance) > 0) ? $nomsSansCorrespondance : false;
167
		$this->infosPourTpl['noms'] = (count($nomsAvecCorrespondance) > 0) ? $nomsAvecCorrespondance : false;
252 delphine 168
		$this->infosPourTpl['synonymes'] = (count($synonymesAvecCorrespondance) > 0) ? $synonymesAvecCorrespondance : false;
146 jpm 169
	}
170
 
171
	public function trier() {
172
		$this->ajouterAuxNomsScoreSimilariteAvec($this->parametres->masqueRecherche);
173
		$this->trieur->setTableau($this->infosPourTpl['noms']);
174
		$this->trieur->setChampsEtOrdres(array('score' => SORT_DESC));
175
		$this->infosPourTpl['noms'] = $this->trieur->trier();
176
	}
177
 
178
	private function ajouterAuxNomsScoreSimilariteAvec($masque) {
179
		$nom_demande_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($masque));
180
		foreach ($this->infosPourTpl['noms'] as $id => $nom) {
181
			$nom_flou_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($nom['nomSciRetenu']));
182
			$stat = array();
183
			// Prime pour la ressemblance globale :
184
			$score = 500 - levenshtein($nom_flou_ss, $nom_demande_ss);
185
			// On affine
186
			$score = $score + (similar_text($nom_demande_ss, $nom_flou_ss) * 3);
187
			$nom['score'] = $score;
188
			$this->infosPourTpl['noms'][$id] = $nom;
189
		}
190
	}
191
 
192
	public function surligner() {
193
		$this->definirMotsASurligner();
194
		foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
252 delphine 195
			$this->infosPourTpl['noms'][$idNom]['nomSciRetenu'] = $this->surlignerMotsMasqueRecherche($nom['nomSciRetenu']);
196
			if (isset($this->infosPourTpl['synonymes'][$idNom])) {
197
				foreach ($this->infosPourTpl['synonymes'][$idNom] as $idSyn => $synonyme) {
198
					$this->infosPourTpl['synonymes'][$idNom][$idSyn]['nomSci'] = $this->surlignerMotsMasqueRecherche($synonyme['nomSci']);
146 jpm 199
				}
200
			}
201
		}
202
	}
203
 
204
	private function definirMotsASurligner() {
205
		$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
206
	}
207
 
208
	private function surlignerMotsMasqueRecherche($nom) {
209
		$this->surligneur->setTexte($nom);
210
		$nom = $this->surligneur->surlignerMots($this->motsASurligner);
211
		return $nom;
212
	}
213
}
214
?>