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