Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 369 | 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();
311 jpm 17
 
146 jpm 18
	public function __construct(ParametresResultats $parametres, Array $resultats,
19
		Surligneur $surligneur = null, Trieur $trieur = null, AppUrls $urls = null,
20
		ChaineManipulateur $manipulateurDeChaine = null, Images $imagesService = null) {
21
 
22
		$this->parametres = $parametres;
23
		$this->noms = $resultats['resultat'];
24
		$this->surligneur = (is_null($surligneur)) ? new Surligneur() : $surligneur;
25
		$this->trieur = (is_null($trieur)) ? new Trieur() : $trieur;
26
		$this->urls = (is_null($urls)) ? new AppUrls() : $urls;
27
		$this->manipulateurDeChaine = is_null($manipulateurDeChaine) ? new ChaineManipulateur() : $manipulateurDeChaine;
28
		$this->imagesService = is_null($imagesService) ? new Images($this->parametres->projetImg) : $imagesService;
29
	}
30
 
31
	public function getTplInfos() {
32
		return $this->infosPourTpl;
33
	}
34
 
35
	public function getTplNom() {
36
		return self::TPL_VUE;
37
	}
38
 
39
	public function formater() {
40
		$this->obtenirUrlsPhotos();
279 gduche 41
		$this->chargerRepartition();
146 jpm 42
		$this->extraireInfosNomsPourTplDetermination();
43
	}
44
 
45
	private function obtenirUrlsPhotos() {
46
		$ids = $this->extraireIdDesNoms();
47
		$urls = $this->imagesService->getUrlsImagesParIdsNoms($ids);
48
		$this->infosPourTpl['imagesUrls'] = $this->supprimerCodeReftaxDesIds($urls);
49
	}
509 jpm 50
 
311 jpm 51
	// TODO : utiliser le conteneur pour charger tous les objets de cette classe
279 gduche 52
	private function chargerRepartition() {
311 jpm 53
		$conteneur = new Conteneur();
54
		$cartesWs = $conteneur->getApiCartes();
55
		$cartesWs->setProjet('chorodep');
56
		$cartesWs->setLargeur('108x101');
57
		$urls = array();
58
		foreach ($this->noms as $id => $nom) {
59
			if (array_key_exists('nom_retenu.id', $nom)) {
60
				$id = $nom['nom_retenu.id'];
61
				$cartesWs->setId("nn:$id");
62
				if (array_key_exists($id, $urls) == false) {
63
					$urls[$id] = $cartesWs->getUrlPng();
64
				}
65
			}
66
		}
67
		$this->infosPourTpl['repartition']['urls'] = $urls;
279 gduche 68
	}
311 jpm 69
 
146 jpm 70
	private function extraireIdDesNoms() {
71
		$ids = array();
72
		foreach ($this->noms as $id => $nom) {
73
			$idAAjouter = $id;
74
			if (is_numeric($idAAjouter)) {
75
				$ids[] = $idAAjouter;
76
			}
77
			if (array_key_exists('nom_retenu.id', $nom)) {
78
				if (in_array($nom['nom_retenu.id'], $ids) == false) {
79
					$idAAjouter = $nom['nom_retenu.id'];
80
					if (is_numeric($idAAjouter)) {
81
						$ids[] = $idAAjouter;
82
					}
83
				}
84
			}
85
		}
86
		return $ids;
87
	}
88
 
89
	private function supprimerCodeReftaxDesIds($urls) {
90
		$urlsNettoyees = array();
91
		foreach ($urls as $id => $url) {
311 jpm 92
			$id = $this->supprimerCodeReftax($id);
146 jpm 93
			$urlsNettoyees[$id] = $url;
94
		}
95
		return $urlsNettoyees;
96
	}
97
 
311 jpm 98
	private function supprimerCodeReftax($chaine) {
99
		$codeReftax = $this->parametres->reftaxCourant.'.';
100
		$chaine = str_replace($codeReftax, '', $chaine);
101
		return $chaine;
102
	}
103
 
146 jpm 104
	private function extraireInfosNomsPourTplDetermination() {
105
		$nomsSansCorrespondance = array();
106
		$nomsAvecCorrespondance = array();
252 delphine 107
		$synonymesAvecCorrespondance = array();
146 jpm 108
		foreach ($this->noms as $idNomCourant => $nom) {
109
			if ($nom['retenu'] == 'true') {
110
				$nomRetenu = array();
111
				$nomRetenu['nomSciRetenu'] = $nom['nom_sci'];
369 delphine 112
				$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche);
146 jpm 113
				$nomsAvecCorrespondance[$idNomCourant] = $nomRetenu;
114
			} else {
115
				if ($nom['nom_retenu'] == null) {
116
					$nomsSansCorrespondance[$idNomCourant] = $nom['nom_sci'];
117
				} else {
118
					$idNomRetenu = $nom['nom_retenu.id'];
119
					if (array_key_exists($nom['nom_retenu.id'], $nomsAvecCorrespondance) == false) {
120
						$nomRetenu = array();
121
						$nomRetenu['nomSciRetenu'] = $nom['nom_retenu'];
245 delphine 122
						$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomRetenu, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
146 jpm 123
						$nomsAvecCorrespondance[$idNomRetenu] = $nomRetenu;
124
					}
125
					$synonyme = array();
126
					$synonyme['nn'] = $idNomCourant;
127
					$synonyme['nomSci'] = $nom['nom_sci'];
245 delphine 128
					$synonyme['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
252 delphine 129
					$synonymesAvecCorrespondance[$idNomRetenu][] = $synonyme;
146 jpm 130
				}
131
			}
132
		}
133
 
134
		$this->infosPourTpl['nomsSansCorrespondance'] = (count($nomsSansCorrespondance) > 0) ? $nomsSansCorrespondance : false;
135
		$this->infosPourTpl['noms'] = (count($nomsAvecCorrespondance) > 0) ? $nomsAvecCorrespondance : false;
252 delphine 136
		$this->infosPourTpl['synonymes'] = (count($synonymesAvecCorrespondance) > 0) ? $synonymesAvecCorrespondance : false;
146 jpm 137
	}
138
 
139
	public function trier() {
140
		$this->ajouterAuxNomsScoreSimilariteAvec($this->parametres->masqueRecherche);
141
		$this->trieur->setTableau($this->infosPourTpl['noms']);
142
		$this->trieur->setChampsEtOrdres(array('score' => SORT_DESC));
143
		$this->infosPourTpl['noms'] = $this->trieur->trier();
144
	}
145
 
146
	private function ajouterAuxNomsScoreSimilariteAvec($masque) {
147
		$nom_demande_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($masque));
148
		foreach ($this->infosPourTpl['noms'] as $id => $nom) {
149
			$nom_flou_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($nom['nomSciRetenu']));
150
			$stat = array();
151
			// Prime pour la ressemblance globale :
152
			$score = 500 - levenshtein($nom_flou_ss, $nom_demande_ss);
153
			// On affine
154
			$score = $score + (similar_text($nom_demande_ss, $nom_flou_ss) * 3);
155
			$nom['score'] = $score;
156
			$this->infosPourTpl['noms'][$id] = $nom;
157
		}
158
	}
159
 
160
	public function surligner() {
161
		$this->definirMotsASurligner();
162
		foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
252 delphine 163
			$this->infosPourTpl['noms'][$idNom]['nomSciRetenu'] = $this->surlignerMotsMasqueRecherche($nom['nomSciRetenu']);
164
			if (isset($this->infosPourTpl['synonymes'][$idNom])) {
165
				foreach ($this->infosPourTpl['synonymes'][$idNom] as $idSyn => $synonyme) {
166
					$this->infosPourTpl['synonymes'][$idNom][$idSyn]['nomSci'] = $this->surlignerMotsMasqueRecherche($synonyme['nomSci']);
146 jpm 167
				}
168
			}
169
		}
170
	}
171
 
172
	private function definirMotsASurligner() {
173
		$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
174
	}
175
 
176
	private function surlignerMotsMasqueRecherche($nom) {
177
		$this->surligneur->setTexte($nom);
178
		$nom = $this->surligneur->surlignerMots($this->motsASurligner);
179
		return $nom;
180
	}
181
}
182
?>