Subversion Repositories eFlore/Applications.eflore-consultation

Rev

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