Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 206 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
206 jpm 1
<?php
2
class NomsListeGenerique implements NomsListe {
3
 
4
	private $parametres = null;
5
	private $ressources = null;
208 jpm 6
	private $nomDao = null;
7
	private $nomFormateur = null;
8
 
206 jpm 9
	private $listeUrl = null;
10
	private $nbreTotalNoms = 0;
11
	private $noms = array();
12
 
208 jpm 13
	public function __construct(Ressources $ressources, Parametres $parametres, NomDAO $nomDao, NomFormateur $nomFormateur) {
206 jpm 14
		$this->ressources = $ressources;
15
		$this->parametres = $parametres;
208 jpm 16
		$this->nomDao = $nomDao;
17
		$this->nomFormateur = $nomFormateur;
206 jpm 18
	}
19
 
20
	public function setListeUrl($url) {
21
		$this->listeUrl = $url;
22
	}
23
 
24
	public function consulter() {
25
		$this->noms = $this->rechercher();
208 jpm 26
		$this->nbreNomsTotal = $this->nomDao->recupererNombreNomsTotal();
206 jpm 27
		$this->trierNoms();
28
		$retour = $this->construireTableauRetour();
29
		return $retour;
30
	}
31
 
32
 
33
	private function rechercher() {
34
		$resultats = array();
35
		$recherche = $this->parametres->get('recherche');
36
 
37
		if ($recherche == 'stricte') {
208 jpm 38
			$resultats = $this->nomDao->rechercherStricte();
206 jpm 39
		} else if ($recherche == 'etendue') {
208 jpm 40
			$resultats = $this->nomDao->rechercherEtendue();
206 jpm 41
		} else if ($recherche == 'floue') {
208 jpm 42
			$resultats = $this->nomDao->rechercherFloue();
206 jpm 43
		}
44
 
45
		return $resultats;
46
	}
47
 
48
	private function trierNoms() {
49
		$recherche = $this->parametres->get('recherche');
50
		if ($recherche == 'floue') {
51
			$this->trierRechercheFloue();
52
		}
53
	}
54
 
55
	public function trierRechercheFloue() {
56
		$nomDemande = $this->parametres->get('masque');
57
		$nomDemandeSimple = strtolower(Chaine::supprimerAccents($nomDemande));
58
 
59
		foreach ($this->noms as $id => $nom) {
60
			$nomFlouSimple = strtolower(Chaine::supprimerAccents($nom['nom_sci']));
61
			// Prime pour la ressemblance globale :
62
			$score = 500 - levenshtein($nomFlouSimple, $nomDemandeSimple);
63
			// On affine
64
			$score += similar_text($nomDemandeSimple, $nomFlouSimple) * 3;
65
			$this->noms[$id]['score'] = $score;
66
		}
67
		$noms = $this->noms;
68
		$this->noms = Tableau::trierMD($noms, array('score' => false));
69
		//print_r($this->noms);
70
	}
71
 
72
	private function construireTableauRetour() {
73
		$retour = array('entete' => array(), 'resultats' => array());
74
		$retour['resultats'] = $this->construireResultats();
75
		$retour['entete'] = $this->construireEntete();
76
		return $retour;
77
	}
78
 
79
	private function construireResultats() {
80
		$nomsFormates = array();
81
		foreach ($this->noms as $nom) {
82
			$id = $nom['num_nom'];
83
			$nomsFormates[$id] = $this->formaterNom($nom);
84
		}
85
		return $nomsFormates;
86
	}
87
 
88
	private function formaterNom($infos) {
89
		$nomAFormater = new NomDO($infos);
208 jpm 90
		$this->nomFormateur->setNomAFormater($nomAFormater);
91
		$this->nomFormateur->setChampsRetour($this->parametres->getListe('retour.champs'));
92
		$nom = $this->nomFormateur->formaterListe();
206 jpm 93
		return $nom;
94
	}
95
 
96
	private function formaterHref($nom) {
97
		$href = sprintf($this->detailsHrefTpl, $nom['num_nom']);
98
		return $href;
99
	}
100
 
101
	private function construireEntete() {
102
		$entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
103
		$entete['masque'] = $this->formaterEnteteMasque();
104
		$entete['depart'] = (int) $this->parametres->get('navigation.depart');
105
		$entete['limite'] = (int) $this->parametres->get('navigation.limite');
106
		$entete['total'] = $this->nbreNomsTotal;
107
		if ($hrefPrecedent = $this->formaterEnteteHrefPrecedent()) {
108
			$entete['href.precedent'] = $hrefPrecedent;
109
		}
110
		if ($hrefSuivant = $this->formaterEnteteHrefSuivant()) {
111
			$entete['href.suivant'] = $hrefSuivant;
112
		}
113
		return $entete;
114
	}
115
 
116
	private function formaterEnteteMasque() {
117
		$masqueComplet = array();
118
		if ($this->parametres->exister('masque')) {
119
			$masque = '';
120
			$masque .= 'nom_sci='.$this->parametres->get('masque');
121
			if ($this->parametres->get('recherche') == 'etendue') {
122
				$masque .= '%';
123
			}
124
			$masqueComplet[] = $masque;
125
		}
126
		if ($this->parametres->exister('masque.sg')) {
127
			$masque = '';
128
			$masque .= 'nom_supra_generique='.$this->parametres->get('masque.sg');
129
			if ($this->parametres->get('recherche') == 'etendue') {
130
				$masque .= '%';
131
			}
132
			$masqueComplet[] = $masque;
133
		}
134
		if ($this->parametres->exister('masque.gen')) {
135
			$masque = '';
136
			$masque .= 'genre='.$this->parametres->get('masque.gen');
137
			if ($this->parametres->get('recherche') == 'etendue') {
138
				$masque .= '%';
139
			}
140
			$masqueComplet[] = $masque;
141
		}
142
		if ($this->parametres->exister('masque.sp')) {
143
			$masque = '';
144
			$masque .= 'epithete_sp='.$this->parametres->get('masque.sp');
145
			if ($this->parametres->get('recherche') == 'etendue') {
146
				$masque .= '%';
147
			}
148
			$masqueComplet[] = $masque;
149
		}
150
		return implode('&', $masqueComplet);
151
	}
152
 
153
	private function formaterEnteteHrefPrecedent() {
154
		$limite = $this->parametres->get('navigation.limite');
155
		$departActuel = $this->parametres->get('navigation.depart');
156
		$departPrecedent = $departActuel - $limite;
157
		$href = null;
158
		if ($departPrecedent >= 0) {
159
			$squelette = $this->construireTplHrefNavigation();
160
			$href = sprintf($squelette, $departPrecedent, $limite);
161
		}
162
		return $href;
163
	}
164
 
165
	private function formaterEnteteHrefSuivant() {
166
		$limite = $this->parametres->get('navigation.limite');
167
		$departActuel = $this->parametres->get('navigation.depart');
168
		$departSuivant = $departActuel + $limite;
169
		$href = null;
170
		if ($departSuivant < $this->nbreNomsTotal) {
171
			$squelette = $this->construireTplHrefNavigation();
172
			$href = sprintf($squelette, $departSuivant, $limite);
173
		}
174
		return $href;
175
	}
176
 
177
	private function construireTplHrefNavigation() {
178
		$requetes = array();
179
		$this->parametres->rewind();
180
		while (is_null($parametre = $this->parametres->key()) === false) {
181
			if (strpos($parametre, 'navigation') === false) {
182
				$valeur = $this->parametres->current();
183
				$requetes[] = "$parametre=$valeur";
184
			}
185
			$this->parametres->next();
186
		}
187
		$requetes[] = "navigation.depart=%s";
188
		$requetes[] = "navigation.limite=%s";
189
		$tpl = $this->listeUrl.'?'.implode('&', $requetes);
190
		return $tpl;
191
	}
192
}
193
?>