Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 251 | 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();
252 delphine 84
		$synonymesAvecCorrespondance = array();
146 jpm 85
		foreach ($this->noms as $idNomCourant => $nom) {
86
			if ($nom['retenu'] == 'true') {
87
				$nomRetenu = array();
88
				$nomRetenu['nomSciRetenu'] = $nom['nom_sci'];
245 delphine 89
				$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
146 jpm 90
				$nomsAvecCorrespondance[$idNomCourant] = $nomRetenu;
91
			} else {
92
				if ($nom['nom_retenu'] == null) {
93
					$nomsSansCorrespondance[$idNomCourant] = $nom['nom_sci'];
94
				} else {
95
					$idNomRetenu = $nom['nom_retenu.id'];
96
					if (array_key_exists($nom['nom_retenu.id'], $nomsAvecCorrespondance) == false) {
97
						$nomRetenu = array();
98
						$nomRetenu['nomSciRetenu'] = $nom['nom_retenu'];
245 delphine 99
						$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomRetenu, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
146 jpm 100
						$nomsAvecCorrespondance[$idNomRetenu] = $nomRetenu;
101
					}
102
					$synonyme = array();
103
					$synonyme['nn'] = $idNomCourant;
104
					$synonyme['nomSci'] = $nom['nom_sci'];
245 delphine 105
					$synonyme['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
252 delphine 106
					$synonymesAvecCorrespondance[$idNomRetenu][] = $synonyme;
146 jpm 107
				}
108
			}
109
		}
110
 
111
		$this->infosPourTpl['nomsSansCorrespondance'] = (count($nomsSansCorrespondance) > 0) ? $nomsSansCorrespondance : false;
112
		$this->infosPourTpl['noms'] = (count($nomsAvecCorrespondance) > 0) ? $nomsAvecCorrespondance : false;
252 delphine 113
		$this->infosPourTpl['synonymes'] = (count($synonymesAvecCorrespondance) > 0) ? $synonymesAvecCorrespondance : false;
146 jpm 114
	}
115
 
116
	public function trier() {
117
		$this->ajouterAuxNomsScoreSimilariteAvec($this->parametres->masqueRecherche);
118
		$this->trieur->setTableau($this->infosPourTpl['noms']);
119
		$this->trieur->setChampsEtOrdres(array('score' => SORT_DESC));
120
		$this->infosPourTpl['noms'] = $this->trieur->trier();
121
	}
122
 
123
	private function ajouterAuxNomsScoreSimilariteAvec($masque) {
124
		$nom_demande_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($masque));
125
		foreach ($this->infosPourTpl['noms'] as $id => $nom) {
126
			$nom_flou_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($nom['nomSciRetenu']));
127
			$stat = array();
128
			// Prime pour la ressemblance globale :
129
			$score = 500 - levenshtein($nom_flou_ss, $nom_demande_ss);
130
			// On affine
131
			$score = $score + (similar_text($nom_demande_ss, $nom_flou_ss) * 3);
132
			$nom['score'] = $score;
133
			$this->infosPourTpl['noms'][$id] = $nom;
134
		}
135
	}
136
 
137
	public function surligner() {
138
		$this->definirMotsASurligner();
139
		foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
252 delphine 140
			$this->infosPourTpl['noms'][$idNom]['nomSciRetenu'] = $this->surlignerMotsMasqueRecherche($nom['nomSciRetenu']);
141
			if (isset($this->infosPourTpl['synonymes'][$idNom])) {
142
				foreach ($this->infosPourTpl['synonymes'][$idNom] as $idSyn => $synonyme) {
143
					$this->infosPourTpl['synonymes'][$idNom][$idSyn]['nomSci'] = $this->surlignerMotsMasqueRecherche($synonyme['nomSci']);
146 jpm 144
				}
145
			}
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
?>