Subversion Repositories eFlore/Applications.eflore-consultation

Rev

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();
735 aurelien 51
		$urls = $this->imagesService->getUrlsImagesParIdsNoms($ids);
146 jpm 52
		$this->infosPourTpl['imagesUrls'] = $this->supprimerCodeReftaxDesIds($urls);
53
	}
617 mathilde 54
 
55
	private function obtenirUrlsImagesCoste() {
56
		$this->extraireInfosTaxons();
57
		$this->imagesService->setProjet('coste');
733 aurelien 58
		$tax = implode(',', $this->infosPourTpl['taxons']);
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;
617 mathilde 66
			}
67
		}
68
	}
509 jpm 69
 
617 mathilde 70
	private function extraireInfosTaxons() {
71
		foreach ($this->noms as $id => $nom ) {
72
			if (array_key_exists('num_taxonomique', $nom)) {
73
				$this->infosPourTpl['taxons'][$id] = $nom['num_taxonomique'];
74
			}
75
		}
76
	}
77
 
311 jpm 78
	// TODO : utiliser le conteneur pour charger tous les objets de cette classe
279 gduche 79
	private function chargerRepartition() {
311 jpm 80
		$conteneur = new Conteneur();
81
		$cartesWs = $conteneur->getApiCartes();
82
		$cartesWs->setProjet('chorodep');
83
		$cartesWs->setLargeur('108x101');
84
		$urls = array();
85
		foreach ($this->noms as $id => $nom) {
86
			if (array_key_exists('nom_retenu.id', $nom)) {
87
				$id = $nom['nom_retenu.id'];
88
				$cartesWs->setId("nn:$id");
89
				if (array_key_exists($id, $urls) == false) {
90
					$urls[$id] = $cartesWs->getUrlPng();
91
				}
92
			}
93
		}
94
		$this->infosPourTpl['repartition']['urls'] = $urls;
279 gduche 95
	}
311 jpm 96
 
617 mathilde 97
 
98
 
99
 
146 jpm 100
	private function extraireIdDesNoms() {
101
		$ids = array();
102
		foreach ($this->noms as $id => $nom) {
103
			$idAAjouter = $id;
104
			if (is_numeric($idAAjouter)) {
105
				$ids[] = $idAAjouter;
106
			}
107
			if (array_key_exists('nom_retenu.id', $nom)) {
108
				if (in_array($nom['nom_retenu.id'], $ids) == false) {
109
					$idAAjouter = $nom['nom_retenu.id'];
110
					if (is_numeric($idAAjouter)) {
111
						$ids[] = $idAAjouter;
112
					}
113
				}
114
			}
115
		}
116
		return $ids;
117
	}
118
 
119
	private function supprimerCodeReftaxDesIds($urls) {
120
		$urlsNettoyees = array();
121
		foreach ($urls as $id => $url) {
311 jpm 122
			$id = $this->supprimerCodeReftax($id);
146 jpm 123
			$urlsNettoyees[$id] = $url;
124
		}
125
		return $urlsNettoyees;
126
	}
127
 
311 jpm 128
	private function supprimerCodeReftax($chaine) {
129
		$codeReftax = $this->parametres->reftaxCourant.'.';
130
		$chaine = str_replace($codeReftax, '', $chaine);
131
		return $chaine;
132
	}
133
 
146 jpm 134
	private function extraireInfosNomsPourTplDetermination() {
135
		$nomsSansCorrespondance = array();
136
		$nomsAvecCorrespondance = array();
252 delphine 137
		$synonymesAvecCorrespondance = array();
146 jpm 138
		foreach ($this->noms as $idNomCourant => $nom) {
139
			if ($nom['retenu'] == 'true') {
140
				$nomRetenu = array();
141
				$nomRetenu['nomSciRetenu'] = $nom['nom_sci'];
369 delphine 142
				$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche);
146 jpm 143
				$nomsAvecCorrespondance[$idNomCourant] = $nomRetenu;
144
			} else {
593 aurelien 145
				if ($nom['nom_retenu.libelle'] == null) {
146 jpm 146
					$nomsSansCorrespondance[$idNomCourant] = $nom['nom_sci'];
147
				} else {
148
					$idNomRetenu = $nom['nom_retenu.id'];
149
					if (array_key_exists($nom['nom_retenu.id'], $nomsAvecCorrespondance) == false) {
150
						$nomRetenu = array();
593 aurelien 151
						$nomRetenu['nomSciRetenu'] = $nom['nom_retenu.libelle'];
152
						$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomRetenu, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu.libelle']);
146 jpm 153
						$nomsAvecCorrespondance[$idNomRetenu] = $nomRetenu;
154
					}
155
					$synonyme = array();
156
					$synonyme['nn'] = $idNomCourant;
157
					$synonyme['nomSci'] = $nom['nom_sci'];
593 aurelien 158
					$synonyme['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu.libelle']);
252 delphine 159
					$synonymesAvecCorrespondance[$idNomRetenu][] = $synonyme;
146 jpm 160
				}
161
			}
162
		}
163
 
164
		$this->infosPourTpl['nomsSansCorrespondance'] = (count($nomsSansCorrespondance) > 0) ? $nomsSansCorrespondance : false;
165
		$this->infosPourTpl['noms'] = (count($nomsAvecCorrespondance) > 0) ? $nomsAvecCorrespondance : false;
252 delphine 166
		$this->infosPourTpl['synonymes'] = (count($synonymesAvecCorrespondance) > 0) ? $synonymesAvecCorrespondance : false;
146 jpm 167
	}
168
 
169
	public function trier() {
170
		$this->ajouterAuxNomsScoreSimilariteAvec($this->parametres->masqueRecherche);
171
		$this->trieur->setTableau($this->infosPourTpl['noms']);
172
		$this->trieur->setChampsEtOrdres(array('score' => SORT_DESC));
173
		$this->infosPourTpl['noms'] = $this->trieur->trier();
174
	}
175
 
176
	private function ajouterAuxNomsScoreSimilariteAvec($masque) {
177
		$nom_demande_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($masque));
178
		foreach ($this->infosPourTpl['noms'] as $id => $nom) {
179
			$nom_flou_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($nom['nomSciRetenu']));
180
			$stat = array();
181
			// Prime pour la ressemblance globale :
182
			$score = 500 - levenshtein($nom_flou_ss, $nom_demande_ss);
183
			// On affine
184
			$score = $score + (similar_text($nom_demande_ss, $nom_flou_ss) * 3);
185
			$nom['score'] = $score;
186
			$this->infosPourTpl['noms'][$id] = $nom;
187
		}
188
	}
189
 
190
	public function surligner() {
191
		$this->definirMotsASurligner();
192
		foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
252 delphine 193
			$this->infosPourTpl['noms'][$idNom]['nomSciRetenu'] = $this->surlignerMotsMasqueRecherche($nom['nomSciRetenu']);
194
			if (isset($this->infosPourTpl['synonymes'][$idNom])) {
195
				foreach ($this->infosPourTpl['synonymes'][$idNom] as $idSyn => $synonyme) {
196
					$this->infosPourTpl['synonymes'][$idNom][$idSyn]['nomSci'] = $this->surlignerMotsMasqueRecherche($synonyme['nomSci']);
146 jpm 197
				}
198
			}
199
		}
200
	}
201
 
202
	private function definirMotsASurligner() {
203
		$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
204
	}
205
 
206
	private function surlignerMotsMasqueRecherche($nom) {
207
		$this->surligneur->setTexte($nom);
208
		$nom = $this->surligneur->surlignerMots($this->motsASurligner);
209
		return $nom;
210
	}
211
}
212
?>