Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 252 | 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();
279 gduche 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
 
48
		$urls = $this->imagesService->getUrlsImagesParIdsNoms($ids);
49
 
50
		$this->infosPourTpl['imagesUrls'] = $this->supprimerCodeReftaxDesIds($urls);
51
	}
52
 
279 gduche 53
	private function chargerRepartition() {
54
		$premierNom = current($this->noms);
55
		$nomRetenuId = $premierNom['nom_retenu.id'];
56
		$serviceRepartition = new Cartes('chorodep', $nomRetenuId, 'nn', "108x101");
57
		$urlPng = $serviceRepartition->getUrlPng();
58
		$this->infosPourTpl['repartition_vignette'] = $urlPng;
59
	}
60
 
146 jpm 61
	private function extraireIdDesNoms() {
62
		$ids = array();
63
		foreach ($this->noms as $id => $nom) {
64
			$idAAjouter = $id;
65
			if (is_numeric($idAAjouter)) {
66
				$ids[] = $idAAjouter;
67
			}
68
			if (array_key_exists('nom_retenu.id', $nom)) {
69
				if (in_array($nom['nom_retenu.id'], $ids) == false) {
70
					$idAAjouter = $nom['nom_retenu.id'];
71
					if (is_numeric($idAAjouter)) {
72
						$ids[] = $idAAjouter;
73
					}
74
				}
75
			}
76
		}
77
		return $ids;
78
	}
79
 
80
	private function supprimerCodeReftaxDesIds($urls) {
81
		$codeReftax = $this->parametres->reftaxCourant.'.';
82
		$urlsNettoyees = array();
83
		foreach ($urls as $id => $url) {
84
			$id = str_replace($codeReftax, '', $id);
85
			$urlsNettoyees[$id] = $url;
86
		}
87
		return $urlsNettoyees;
88
	}
89
 
90
	private function extraireInfosNomsPourTplDetermination() {
91
		$nomsSansCorrespondance = array();
92
		$nomsAvecCorrespondance = array();
252 delphine 93
		$synonymesAvecCorrespondance = array();
146 jpm 94
		foreach ($this->noms as $idNomCourant => $nom) {
95
			if ($nom['retenu'] == 'true') {
96
				$nomRetenu = array();
97
				$nomRetenu['nomSciRetenu'] = $nom['nom_sci'];
245 delphine 98
				$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
146 jpm 99
				$nomsAvecCorrespondance[$idNomCourant] = $nomRetenu;
100
			} else {
101
				if ($nom['nom_retenu'] == null) {
102
					$nomsSansCorrespondance[$idNomCourant] = $nom['nom_sci'];
103
				} else {
104
					$idNomRetenu = $nom['nom_retenu.id'];
105
					if (array_key_exists($nom['nom_retenu.id'], $nomsAvecCorrespondance) == false) {
106
						$nomRetenu = array();
107
						$nomRetenu['nomSciRetenu'] = $nom['nom_retenu'];
245 delphine 108
						$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomRetenu, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
146 jpm 109
						$nomsAvecCorrespondance[$idNomRetenu] = $nomRetenu;
110
					}
111
					$synonyme = array();
112
					$synonyme['nn'] = $idNomCourant;
113
					$synonyme['nomSci'] = $nom['nom_sci'];
245 delphine 114
					$synonyme['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
252 delphine 115
					$synonymesAvecCorrespondance[$idNomRetenu][] = $synonyme;
146 jpm 116
				}
117
			}
118
		}
119
 
120
		$this->infosPourTpl['nomsSansCorrespondance'] = (count($nomsSansCorrespondance) > 0) ? $nomsSansCorrespondance : false;
121
		$this->infosPourTpl['noms'] = (count($nomsAvecCorrespondance) > 0) ? $nomsAvecCorrespondance : false;
252 delphine 122
		$this->infosPourTpl['synonymes'] = (count($synonymesAvecCorrespondance) > 0) ? $synonymesAvecCorrespondance : false;
146 jpm 123
	}
124
 
125
	public function trier() {
126
		$this->ajouterAuxNomsScoreSimilariteAvec($this->parametres->masqueRecherche);
127
		$this->trieur->setTableau($this->infosPourTpl['noms']);
128
		$this->trieur->setChampsEtOrdres(array('score' => SORT_DESC));
129
		$this->infosPourTpl['noms'] = $this->trieur->trier();
130
	}
131
 
132
	private function ajouterAuxNomsScoreSimilariteAvec($masque) {
133
		$nom_demande_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($masque));
134
		foreach ($this->infosPourTpl['noms'] as $id => $nom) {
135
			$nom_flou_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($nom['nomSciRetenu']));
136
			$stat = array();
137
			// Prime pour la ressemblance globale :
138
			$score = 500 - levenshtein($nom_flou_ss, $nom_demande_ss);
139
			// On affine
140
			$score = $score + (similar_text($nom_demande_ss, $nom_flou_ss) * 3);
141
			$nom['score'] = $score;
142
			$this->infosPourTpl['noms'][$id] = $nom;
143
		}
144
	}
145
 
146
	public function surligner() {
147
		$this->definirMotsASurligner();
148
		foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
252 delphine 149
			$this->infosPourTpl['noms'][$idNom]['nomSciRetenu'] = $this->surlignerMotsMasqueRecherche($nom['nomSciRetenu']);
150
			if (isset($this->infosPourTpl['synonymes'][$idNom])) {
151
				foreach ($this->infosPourTpl['synonymes'][$idNom] as $idSyn => $synonyme) {
152
					$this->infosPourTpl['synonymes'][$idNom][$idSyn]['nomSci'] = $this->surlignerMotsMasqueRecherche($synonyme['nomSci']);
146 jpm 153
				}
154
			}
155
		}
156
	}
157
 
158
	private function definirMotsASurligner() {
159
		$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
160
	}
161
 
162
	private function surlignerMotsMasqueRecherche($nom) {
163
		$this->surligneur->setTexte($nom);
164
		$nom = $this->surligneur->surlignerMots($this->motsASurligner);
165
		return $nom;
166
	}
167
}
168
?>