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();
|
1348 |
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() {
|
1348 |
jpm |
40 |
$this->extraireInfosTaxons();
|
|
|
41 |
|
931 |
delphine |
42 |
$this->obtenirUrlsDessins();
|
146 |
jpm |
43 |
$this->obtenirUrlsPhotos();
|
279 |
gduche |
44 |
$this->chargerRepartition();
|
1348 |
jpm |
45 |
|
146 |
jpm |
46 |
$this->extraireInfosNomsPourTplDetermination();
|
1273 |
aurelien |
47 |
$this->infosPourTpl['urlWidget'] = Config::get('base_url_widget_saisie')."?referentiel=".Registre::get('parametres.referentiel');
|
146 |
jpm |
48 |
}
|
1348 |
jpm |
49 |
|
931 |
delphine |
50 |
private function obtenirUrlsDessins() {
|
|
|
51 |
if (Config::get(Registre::get('parametres.referentiel').'.baseDessins') != "") {
|
|
|
52 |
$this->imagesService->setProjet(Config::get(Registre::get('parametres.referentiel').'.baseDessins'));
|
|
|
53 |
$tax = implode(',', $this->infosPourTpl['taxons']);
|
|
|
54 |
$this->imagesService->setNnTaxon($tax);
|
1115 |
mathias |
55 |
$costeImg = $this->imagesService->setApi(Eflore::API_EFLORE)->getInfosImagesTaxons();
|
931 |
delphine |
56 |
if (!empty($costeImg)) {
|
|
|
57 |
foreach ($costeImg as $infos) {
|
|
|
58 |
$num_taxon = $infos['num_taxonomique'];
|
|
|
59 |
$images[$num_taxon][] = $infos['binaire.href'];
|
|
|
60 |
$this->infosPourTpl['dessins'] = $images;
|
|
|
61 |
}
|
617 |
mathilde |
62 |
}
|
|
|
63 |
}
|
|
|
64 |
}
|
1348 |
jpm |
65 |
|
617 |
mathilde |
66 |
private function extraireInfosTaxons() {
|
|
|
67 |
foreach ($this->noms as $id => $nom ) {
|
|
|
68 |
if (array_key_exists('num_taxonomique', $nom)) {
|
|
|
69 |
$this->infosPourTpl['taxons'][$id] = $nom['num_taxonomique'];
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
}
|
1348 |
jpm |
73 |
|
872 |
delphine |
74 |
private function obtenirUrlsPhotos() {
|
1060 |
aurelien |
75 |
if (Config::get(Registre::get('parametres.referentiel').'.baseImages') != "") {
|
|
|
76 |
$this->imagesService->setProjet(Config::get(Registre::get('parametres.referentiel').'.baseImages'));
|
|
|
77 |
$ids = $this->extraireIdDesNoms();
|
1115 |
mathias |
78 |
$urls = $this->imagesService->setApi(Eflore::API_EFLORE)->getUrlsImagesParIdsNoms($ids);
|
1060 |
aurelien |
79 |
$this->infosPourTpl['imagesUrls'] = $this->supprimerCodeReferentielDesIds($urls);
|
|
|
80 |
}
|
279 |
gduche |
81 |
}
|
1348 |
jpm |
82 |
|
146 |
jpm |
83 |
private function extraireIdDesNoms() {
|
|
|
84 |
$ids = array();
|
|
|
85 |
foreach ($this->noms as $id => $nom) {
|
|
|
86 |
$idAAjouter = $id;
|
|
|
87 |
if (is_numeric($idAAjouter)) {
|
|
|
88 |
$ids[] = $idAAjouter;
|
|
|
89 |
}
|
|
|
90 |
if (array_key_exists('nom_retenu.id', $nom)) {
|
|
|
91 |
if (in_array($nom['nom_retenu.id'], $ids) == false) {
|
|
|
92 |
$idAAjouter = $nom['nom_retenu.id'];
|
|
|
93 |
if (is_numeric($idAAjouter)) {
|
|
|
94 |
$ids[] = $idAAjouter;
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
return $ids;
|
|
|
100 |
}
|
1348 |
jpm |
101 |
|
931 |
delphine |
102 |
private function supprimerCodeReferentielDesIds($urls) {
|
146 |
jpm |
103 |
$urlsNettoyees = array();
|
|
|
104 |
foreach ($urls as $id => $url) {
|
931 |
delphine |
105 |
$id = $this->supprimerCodeReferentiel($id);
|
146 |
jpm |
106 |
$urlsNettoyees[$id] = $url;
|
|
|
107 |
}
|
|
|
108 |
return $urlsNettoyees;
|
|
|
109 |
}
|
1348 |
jpm |
110 |
|
931 |
delphine |
111 |
private function supprimerCodeReferentiel($chaine) {
|
997 |
aurelien |
112 |
$codeReferentiel = Registre::get('parametres.referentiel').'.';
|
931 |
delphine |
113 |
$chaine = str_replace($codeReferentiel, '', $chaine);
|
311 |
jpm |
114 |
return $chaine;
|
|
|
115 |
}
|
1348 |
jpm |
116 |
|
872 |
delphine |
117 |
// TODO : utiliser le conteneur pour charger tous les objets de cette classe
|
|
|
118 |
private function chargerRepartition() {
|
1348 |
jpm |
119 |
$referentiel = Registre::get('parametres.referentiel');
|
|
|
120 |
if (Config::get("$referentiel.baseRepartition") != '') {
|
931 |
delphine |
121 |
$conteneur = new Conteneur();
|
|
|
122 |
$cartesWs = $conteneur->getApiCartes();
|
|
|
123 |
$cartesWs->setProjet(Config::get(Registre::get('parametres.referentiel').'.baseRepartition'));
|
|
|
124 |
$cartesWs->setLargeur('108x101');
|
|
|
125 |
$urls = array();
|
|
|
126 |
foreach ($this->noms as $id => $nom) {
|
1348 |
jpm |
127 |
if (array_key_exists('num_taxonomique', $nom)) {
|
|
|
128 |
$nt = $nom['num_taxonomique'];
|
|
|
129 |
$cartesWs->setId("$referentiel.nt:$nt");
|
931 |
delphine |
130 |
if (array_key_exists($id, $urls) == false) {
|
|
|
131 |
$urls[$id] = $cartesWs->getUrlPng();
|
|
|
132 |
}
|
872 |
delphine |
133 |
}
|
|
|
134 |
}
|
931 |
delphine |
135 |
$this->infosPourTpl['repartition']['urls'] = $urls;
|
872 |
delphine |
136 |
}
|
|
|
137 |
}
|
311 |
jpm |
138 |
|
872 |
delphine |
139 |
private function extraireInfosNomsPourTplDetermination() {
|
|
|
140 |
$tri = $this->diviserResultats();
|
|
|
141 |
$this->infosPourTpl['nomsSansCorrespondance'] = isset($tri['sansCorres']) ? $tri['sansCorres'] : false;
|
|
|
142 |
$this->infosPourTpl['noms'] = isset($tri['retenus']) ? $tri['retenus'] : false;
|
|
|
143 |
$this->infosPourTpl['synonymes'] = isset($tri['synonymes'] ) ? $tri['synonymes'] : false;
|
|
|
144 |
}
|
1348 |
jpm |
145 |
|
792 |
mathilde |
146 |
/**
|
1348 |
jpm |
147 |
* division ordonnée par
|
|
|
148 |
* 1 - noms retenus qui commencent par la requete
|
792 |
mathilde |
149 |
* 2 - requete contenue dans un synonyme dont le nom retenu ne contient pas la requete
|
|
|
150 |
* 3 - requete contenue dans un hybride retenu
|
|
|
151 |
* 4 - requete contenue dans un nom retenu mais pas au début
|
|
|
152 |
* 5 - requete contenue dans un nom sans correspondance
|
1348 |
jpm |
153 |
*
|
792 |
mathilde |
154 |
*/
|
|
|
155 |
private function diviserResultats() {
|
|
|
156 |
$tri = array();
|
|
|
157 |
$sansCorres = array();
|
|
|
158 |
$synonymes = array();
|
|
|
159 |
$retenus = array();
|
|
|
160 |
foreach ($this->noms as $cle => $valeurs) {
|
|
|
161 |
if ($valeurs['nom_retenu.libelle'] == null) {//sans correspondances
|
915 |
delphine |
162 |
$sansCorres[$cle] = $valeurs['nom_sci_complet'];
|
792 |
mathilde |
163 |
} elseif ($valeurs['retenu'] == 'true') { // retenus
|
923 |
delphine |
164 |
if (preg_match('/ x |^x /',$valeurs['nom_sci'] ) ) {
|
792 |
mathilde |
165 |
//hybrides
|
880 |
aurelien |
166 |
$retenus[2][$cle] = $this->retournerInfosNomRetenu($cle,$valeurs['nom_sci_complet'], $valeurs['rang.libelle']);
|
923 |
delphine |
167 |
} elseif (strripos($valeurs['nom_sci_complet'], $this->parametres->masqueRecherche) === 0) {//preg_match('/^'.strtolower($this->parametres->masqueRecherche).' |^'.strtolower($this->parametres->masqueRecherche).'$/', strtolower($valeurs['nom_sci']) ) ) {
|
792 |
mathilde |
168 |
//commence par
|
880 |
aurelien |
169 |
$retenus[0][$cle] = $this->retournerInfosNomRetenu($cle,$valeurs['nom_sci_complet'], $valeurs['rang.libelle']);
|
1348 |
jpm |
170 |
} else {
|
792 |
mathilde |
171 |
//contient
|
880 |
aurelien |
172 |
$retenus[3][$cle] = $this->retournerInfosNomRetenu($cle,$valeurs['nom_sci_complet'], $valeurs['rang.libelle']);
|
146 |
jpm |
173 |
}
|
792 |
mathilde |
174 |
} else {//synonymes
|
|
|
175 |
$idNomRetenu = $valeurs['nom_retenu.id'];
|
923 |
delphine |
176 |
if (in_array($valeurs['nom_retenu.id'], array_keys($this->noms)) == false) {
|
|
|
177 |
//synonymes dont le nom retenu n'est pas un résultat de recherche
|
1189 |
mathias |
178 |
$retenus[1][$idNomRetenu] = $this->retournerInfosNomRetenu($idNomRetenu,$valeurs['nom_retenu.libelle'], $valeurs['rang.libelle']);
|
792 |
mathilde |
179 |
$this->infosPourTpl['taxons'][$idNomRetenu] = $valeurs['num_taxonomique']; //num taxon pour images coste
|
|
|
180 |
}
|
|
|
181 |
$synonymes[$idNomRetenu][] = $this->retournerInfosSynonyme($cle, $valeurs);
|
146 |
jpm |
182 |
}
|
|
|
183 |
}
|
792 |
mathilde |
184 |
ksort($retenus);
|
|
|
185 |
$tri['retenus'] = $retenus;
|
|
|
186 |
$tri['synonymes'] = $synonymes;
|
|
|
187 |
$tri['sansCorres'] = $sansCorres;
|
|
|
188 |
return $tri;
|
146 |
jpm |
189 |
}
|
1348 |
jpm |
190 |
|
792 |
mathilde |
191 |
private function retournerInfosNomRetenu($cle, $nom_sci, $rang) {
|
|
|
192 |
$infos = array();
|
|
|
193 |
$infos['nomSciRetenu'] = $nom_sci;
|
|
|
194 |
$infos['urlFiche'] = $this->urls->obtenirUrlFiche($cle, $this->parametres->typeNom, strtolower($this->parametres->masqueRecherche), $nom_sci);
|
|
|
195 |
$infos['rang'] = $rang;
|
|
|
196 |
return $infos;
|
1348 |
jpm |
197 |
}
|
|
|
198 |
|
792 |
mathilde |
199 |
private function retournerInfosSynonyme($cle, $valeurs) {
|
|
|
200 |
$infos = array();
|
|
|
201 |
$infos['nn'] = $cle;
|
902 |
delphine |
202 |
$infos['nomSci'] = $valeurs['nom_sci_complet'];
|
792 |
mathilde |
203 |
$infos['urlFiche'] = $this->urls->obtenirUrlFiche($cle, $this->parametres->typeNom, $this->parametres->masqueRecherche, $valeurs['nom_retenu.libelle']);
|
|
|
204 |
return $infos;
|
|
|
205 |
}
|
1348 |
jpm |
206 |
|
|
|
207 |
public function trier() {
|
1004 |
aurelien |
208 |
$nomsRetenus = array();
|
1348 |
jpm |
209 |
foreach ($this->infosPourTpl['noms'] as $categorie => $valeurs ) { //classement alpha par groupes
|
1004 |
aurelien |
210 |
$nomsRetenus += $this->classerAlphabetiquement('nomSciRetenu', $valeurs);
|
|
|
211 |
}
|
|
|
212 |
$this->infosPourTpl['noms'] = $nomsRetenus;
|
1348 |
jpm |
213 |
$this->infosPourTpl['nomsSansCorrespondance'] =
|
1004 |
aurelien |
214 |
$this->classerAlphabetiquement('nomSciRetenu', $this->infosPourTpl['nomsSansCorrespondance'] );
|
146 |
jpm |
215 |
}
|
1348 |
jpm |
216 |
|
146 |
jpm |
217 |
private function ajouterAuxNomsScoreSimilariteAvec($masque) {
|
|
|
218 |
$nom_demande_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($masque));
|
|
|
219 |
foreach ($this->infosPourTpl['noms'] as $id => $nom) {
|
|
|
220 |
$nom_flou_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($nom['nomSciRetenu']));
|
|
|
221 |
$stat = array();
|
|
|
222 |
// Prime pour la ressemblance globale :
|
|
|
223 |
$score = 500 - levenshtein($nom_flou_ss, $nom_demande_ss);
|
|
|
224 |
// On affine
|
|
|
225 |
$score = $score + (similar_text($nom_demande_ss, $nom_flou_ss) * 3);
|
|
|
226 |
$nom['score'] = $score;
|
|
|
227 |
$this->infosPourTpl['noms'][$id] = $nom;
|
|
|
228 |
}
|
|
|
229 |
}
|
1348 |
jpm |
230 |
|
872 |
delphine |
231 |
private function classerAlphabetiquement($champs, $valeurs) {
|
|
|
232 |
$this->trieur->setTableau($valeurs);
|
931 |
delphine |
233 |
$this->trieur->setChampsEtOrdres(array($champs => 'nat'));
|
872 |
delphine |
234 |
return $this->trieur->trier();
|
|
|
235 |
}
|
146 |
jpm |
236 |
|
|
|
237 |
public function surligner() {
|
|
|
238 |
$this->definirMotsASurligner();
|
|
|
239 |
foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
|
1359 |
mathias |
240 |
$this->infosPourTpl['noms'][$idNom]['nomSciRetenuSurligne'] = $this->surlignerMotsMasqueRecherche($nom['nomSciRetenu']);
|
252 |
delphine |
241 |
if (isset($this->infosPourTpl['synonymes'][$idNom])) {
|
|
|
242 |
foreach ($this->infosPourTpl['synonymes'][$idNom] as $idSyn => $synonyme) {
|
|
|
243 |
$this->infosPourTpl['synonymes'][$idNom][$idSyn]['nomSci'] = $this->surlignerMotsMasqueRecherche($synonyme['nomSci']);
|
146 |
jpm |
244 |
}
|
|
|
245 |
}
|
|
|
246 |
}
|
|
|
247 |
}
|
1348 |
jpm |
248 |
|
146 |
jpm |
249 |
private function definirMotsASurligner() {
|
|
|
250 |
$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
|
|
|
251 |
}
|
|
|
252 |
private function surlignerMotsMasqueRecherche($nom) {
|
|
|
253 |
$this->surligneur->setTexte($nom);
|
|
|
254 |
$nom = $this->surligneur->surlignerMots($this->motsASurligner);
|
|
|
255 |
return $nom;
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
?>
|