Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 252 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 252 Rev 279
1
<?php
1
<?php
2
class DeterminationFormateur implements Formateur {
2
class DeterminationFormateur implements Formateur {
3
 
3
 
4
	const TPL_VUE = 'determination';
4
	const TPL_VUE = 'determination';
5
 
5
 
6
	private $parametres = null;
6
	private $parametres = null;
7
	private $surligneur = null;
7
	private $surligneur = null;
8
	private $trieur = null;
8
	private $trieur = null;
9
	private $urls = null;
9
	private $urls = null;
10
	private $fusioneur = null;
10
	private $fusioneur = null;
11
	private $manipulateurDeChaine = null;
11
	private $manipulateurDeChaine = null;
12
	private $imagesService = null;
12
	private $imagesService = null;
13
 
13
 
14
	private $motsASurligner = array();
14
	private $motsASurligner = array();
15
	private $noms = array();
15
	private $noms = array();
16
	private $infosPourTpl = array();
16
	private $infosPourTpl = array();
17
 
17
	
18
	public function __construct(ParametresResultats $parametres, Array $resultats,
18
	public function __construct(ParametresResultats $parametres, Array $resultats,
19
		Surligneur $surligneur = null, Trieur $trieur = null, AppUrls $urls = null,
19
		Surligneur $surligneur = null, Trieur $trieur = null, AppUrls $urls = null,
20
		ChaineManipulateur $manipulateurDeChaine = null, Images $imagesService = null) {
20
		ChaineManipulateur $manipulateurDeChaine = null, Images $imagesService = null) {
21
 
21
 
22
		$this->parametres = $parametres;
22
		$this->parametres = $parametres;
23
		$this->noms = $resultats['resultat'];
23
		$this->noms = $resultats['resultat'];
24
		$this->surligneur = (is_null($surligneur)) ? new Surligneur() : $surligneur;
24
		$this->surligneur = (is_null($surligneur)) ? new Surligneur() : $surligneur;
25
		$this->trieur = (is_null($trieur)) ? new Trieur() : $trieur;
25
		$this->trieur = (is_null($trieur)) ? new Trieur() : $trieur;
26
		$this->urls = (is_null($urls)) ? new AppUrls() : $urls;
26
		$this->urls = (is_null($urls)) ? new AppUrls() : $urls;
27
		$this->manipulateurDeChaine = is_null($manipulateurDeChaine) ? new ChaineManipulateur() : $manipulateurDeChaine;
27
		$this->manipulateurDeChaine = is_null($manipulateurDeChaine) ? new ChaineManipulateur() : $manipulateurDeChaine;
28
		$this->imagesService = is_null($imagesService) ? new Images($this->parametres->projetImg) : $imagesService;
28
		$this->imagesService = is_null($imagesService) ? new Images($this->parametres->projetImg) : $imagesService;
29
	}
29
	}
30
 
30
 
31
	public function getTplInfos() {
31
	public function getTplInfos() {
32
		return $this->infosPourTpl;
32
		return $this->infosPourTpl;
33
	}
33
	}
34
 
34
 
35
	public function getTplNom() {
35
	public function getTplNom() {
36
		return self::TPL_VUE;
36
		return self::TPL_VUE;
37
	}
37
	}
38
 
38
 
39
	public function formater() {
39
	public function formater() {
40
		$this->obtenirUrlsPhotos();
40
		$this->obtenirUrlsPhotos();
-
 
41
		$this->chargerRepartition();
41
		$this->extraireInfosNomsPourTplDetermination();
42
		$this->extraireInfosNomsPourTplDetermination();
42
	}
43
	}
43
 
44
 
44
	private function obtenirUrlsPhotos() {
45
	private function obtenirUrlsPhotos() {
45
		$ids = $this->extraireIdDesNoms();
46
		$ids = $this->extraireIdDesNoms();
46
 
47
 
47
		$urls = $this->imagesService->getUrlsImagesParIdsNoms($ids);
48
		$urls = $this->imagesService->getUrlsImagesParIdsNoms($ids);
48
 
49
 
49
		$this->infosPourTpl['imagesUrls'] = $this->supprimerCodeReftaxDesIds($urls);
50
		$this->infosPourTpl['imagesUrls'] = $this->supprimerCodeReftaxDesIds($urls);
50
	}
51
	}
-
 
52
 
-
 
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
	}
51
 
60
	
52
	private function extraireIdDesNoms() {
61
	private function extraireIdDesNoms() {
53
		$ids = array();
62
		$ids = array();
54
		foreach ($this->noms as $id => $nom) {
63
		foreach ($this->noms as $id => $nom) {
55
			$idAAjouter = $id;
64
			$idAAjouter = $id;
56
			if (is_numeric($idAAjouter)) {
65
			if (is_numeric($idAAjouter)) {
57
				$ids[] = $idAAjouter;
66
				$ids[] = $idAAjouter;
58
			}
67
			}
59
			if (array_key_exists('nom_retenu.id', $nom)) {
68
			if (array_key_exists('nom_retenu.id', $nom)) {
60
				if (in_array($nom['nom_retenu.id'], $ids) == false) {
69
				if (in_array($nom['nom_retenu.id'], $ids) == false) {
61
					$idAAjouter = $nom['nom_retenu.id'];
70
					$idAAjouter = $nom['nom_retenu.id'];
62
					if (is_numeric($idAAjouter)) {
71
					if (is_numeric($idAAjouter)) {
63
						$ids[] = $idAAjouter;
72
						$ids[] = $idAAjouter;
64
					}
73
					}
65
				}
74
				}
66
			}
75
			}
67
		}
76
		}
68
		return $ids;
77
		return $ids;
69
	}
78
	}
70
 
79
 
71
	private function supprimerCodeReftaxDesIds($urls) {
80
	private function supprimerCodeReftaxDesIds($urls) {
72
		$codeReftax = $this->parametres->reftaxCourant.'.';
81
		$codeReftax = $this->parametres->reftaxCourant.'.';
73
		$urlsNettoyees = array();
82
		$urlsNettoyees = array();
74
		foreach ($urls as $id => $url) {
83
		foreach ($urls as $id => $url) {
75
			$id = str_replace($codeReftax, '', $id);
84
			$id = str_replace($codeReftax, '', $id);
76
			$urlsNettoyees[$id] = $url;
85
			$urlsNettoyees[$id] = $url;
77
		}
86
		}
78
		return $urlsNettoyees;
87
		return $urlsNettoyees;
79
	}
88
	}
80
 
89
 
81
	private function extraireInfosNomsPourTplDetermination() {
90
	private function extraireInfosNomsPourTplDetermination() {
82
		$nomsSansCorrespondance = array();
91
		$nomsSansCorrespondance = array();
83
		$nomsAvecCorrespondance = array();
92
		$nomsAvecCorrespondance = array();
84
		$synonymesAvecCorrespondance = array();
93
		$synonymesAvecCorrespondance = array();
85
		foreach ($this->noms as $idNomCourant => $nom) {
94
		foreach ($this->noms as $idNomCourant => $nom) {
86
			if ($nom['retenu'] == 'true') {
95
			if ($nom['retenu'] == 'true') {
87
				$nomRetenu = array();
96
				$nomRetenu = array();
88
				$nomRetenu['nomSciRetenu'] = $nom['nom_sci'];
97
				$nomRetenu['nomSciRetenu'] = $nom['nom_sci'];
89
				$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
98
				$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
90
				$nomsAvecCorrespondance[$idNomCourant] = $nomRetenu;
99
				$nomsAvecCorrespondance[$idNomCourant] = $nomRetenu;
91
			} else {
100
			} else {
92
				if ($nom['nom_retenu'] == null) {
101
				if ($nom['nom_retenu'] == null) {
93
					$nomsSansCorrespondance[$idNomCourant] = $nom['nom_sci'];
102
					$nomsSansCorrespondance[$idNomCourant] = $nom['nom_sci'];
94
				} else {
103
				} else {
95
					$idNomRetenu = $nom['nom_retenu.id'];
104
					$idNomRetenu = $nom['nom_retenu.id'];
96
					if (array_key_exists($nom['nom_retenu.id'], $nomsAvecCorrespondance) == false) {
105
					if (array_key_exists($nom['nom_retenu.id'], $nomsAvecCorrespondance) == false) {
97
						$nomRetenu = array();
106
						$nomRetenu = array();
98
						$nomRetenu['nomSciRetenu'] = $nom['nom_retenu'];
107
						$nomRetenu['nomSciRetenu'] = $nom['nom_retenu'];
99
						$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomRetenu, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
108
						$nomRetenu['urlFiche'] = $this->urls->obtenirUrlFiche($idNomRetenu, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
100
						$nomsAvecCorrespondance[$idNomRetenu] = $nomRetenu;
109
						$nomsAvecCorrespondance[$idNomRetenu] = $nomRetenu;
101
					}
110
					}
102
					$synonyme = array();
111
					$synonyme = array();
103
					$synonyme['nn'] = $idNomCourant;
112
					$synonyme['nn'] = $idNomCourant;
104
					$synonyme['nomSci'] = $nom['nom_sci'];
113
					$synonyme['nomSci'] = $nom['nom_sci'];
105
					$synonyme['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
114
					$synonyme['urlFiche'] = $this->urls->obtenirUrlFiche($idNomCourant, $this->parametres->typeNom, $this->parametres->masqueRecherche, $nom['nom_retenu']);
106
					$synonymesAvecCorrespondance[$idNomRetenu][] = $synonyme;
115
					$synonymesAvecCorrespondance[$idNomRetenu][] = $synonyme;
107
				}
116
				}
108
			}
117
			}
109
		}
118
		}
110
 
119
 
111
		$this->infosPourTpl['nomsSansCorrespondance'] = (count($nomsSansCorrespondance) > 0) ? $nomsSansCorrespondance : false;
120
		$this->infosPourTpl['nomsSansCorrespondance'] = (count($nomsSansCorrespondance) > 0) ? $nomsSansCorrespondance : false;
112
		$this->infosPourTpl['noms'] = (count($nomsAvecCorrespondance) > 0) ? $nomsAvecCorrespondance : false;
121
		$this->infosPourTpl['noms'] = (count($nomsAvecCorrespondance) > 0) ? $nomsAvecCorrespondance : false;
113
		$this->infosPourTpl['synonymes'] = (count($synonymesAvecCorrespondance) > 0) ? $synonymesAvecCorrespondance : false;
122
		$this->infosPourTpl['synonymes'] = (count($synonymesAvecCorrespondance) > 0) ? $synonymesAvecCorrespondance : false;
114
	}
123
	}
115
 
124
 
116
	public function trier() {
125
	public function trier() {
117
		$this->ajouterAuxNomsScoreSimilariteAvec($this->parametres->masqueRecherche);
126
		$this->ajouterAuxNomsScoreSimilariteAvec($this->parametres->masqueRecherche);
118
		$this->trieur->setTableau($this->infosPourTpl['noms']);
127
		$this->trieur->setTableau($this->infosPourTpl['noms']);
119
		$this->trieur->setChampsEtOrdres(array('score' => SORT_DESC));
128
		$this->trieur->setChampsEtOrdres(array('score' => SORT_DESC));
120
		$this->infosPourTpl['noms'] = $this->trieur->trier();
129
		$this->infosPourTpl['noms'] = $this->trieur->trier();
121
	}
130
	}
122
 
131
 
123
	private function ajouterAuxNomsScoreSimilariteAvec($masque) {
132
	private function ajouterAuxNomsScoreSimilariteAvec($masque) {
124
		$nom_demande_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($masque));
133
		$nom_demande_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($masque));
125
		foreach ($this->infosPourTpl['noms'] as $id => $nom) {
134
		foreach ($this->infosPourTpl['noms'] as $id => $nom) {
126
			$nom_flou_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($nom['nomSciRetenu']));
135
			$nom_flou_ss = strtolower($this->manipulateurDeChaine->supprimerAccents($nom['nomSciRetenu']));
127
			$stat = array();
136
			$stat = array();
128
			// Prime pour la ressemblance globale :
137
			// Prime pour la ressemblance globale :
129
			$score = 500 - levenshtein($nom_flou_ss, $nom_demande_ss);
138
			$score = 500 - levenshtein($nom_flou_ss, $nom_demande_ss);
130
			// On affine
139
			// On affine
131
			$score = $score + (similar_text($nom_demande_ss, $nom_flou_ss) * 3);
140
			$score = $score + (similar_text($nom_demande_ss, $nom_flou_ss) * 3);
132
			$nom['score'] = $score;
141
			$nom['score'] = $score;
133
			$this->infosPourTpl['noms'][$id] = $nom;
142
			$this->infosPourTpl['noms'][$id] = $nom;
134
		}
143
		}
135
	}
144
	}
136
 
145
 
137
	public function surligner() {
146
	public function surligner() {
138
		$this->definirMotsASurligner();
147
		$this->definirMotsASurligner();
139
		foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
148
		foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
140
			$this->infosPourTpl['noms'][$idNom]['nomSciRetenu'] = $this->surlignerMotsMasqueRecherche($nom['nomSciRetenu']);
149
			$this->infosPourTpl['noms'][$idNom]['nomSciRetenu'] = $this->surlignerMotsMasqueRecherche($nom['nomSciRetenu']);
141
			if (isset($this->infosPourTpl['synonymes'][$idNom])) {
150
			if (isset($this->infosPourTpl['synonymes'][$idNom])) {
142
				foreach ($this->infosPourTpl['synonymes'][$idNom] as $idSyn => $synonyme) {
151
				foreach ($this->infosPourTpl['synonymes'][$idNom] as $idSyn => $synonyme) {
143
					$this->infosPourTpl['synonymes'][$idNom][$idSyn]['nomSci'] = $this->surlignerMotsMasqueRecherche($synonyme['nomSci']);
152
					$this->infosPourTpl['synonymes'][$idNom][$idSyn]['nomSci'] = $this->surlignerMotsMasqueRecherche($synonyme['nomSci']);
144
				}
153
				}
145
			}
154
			}
146
		}
155
		}
147
	}
156
	}
148
 
157
 
149
	private function definirMotsASurligner() {
158
	private function definirMotsASurligner() {
150
		$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
159
		$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
151
	}
160
	}
152
 
161
 
153
	private function surlignerMotsMasqueRecherche($nom) {
162
	private function surlignerMotsMasqueRecherche($nom) {
154
		$this->surligneur->setTexte($nom);
163
		$this->surligneur->setTexte($nom);
155
		$nom = $this->surligneur->surlignerMots($this->motsASurligner);
164
		$nom = $this->surligneur->surlignerMots($this->motsASurligner);
156
		return $nom;
165
		return $nom;
157
	}
166
	}
158
}
167
}
159
?>
168
?>