Subversion Repositories eFlore/Applications.eflore-consultation

Rev

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

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