Subversion Repositories eFlore/Projets.eflore-projets

Rev

Go to most recent revision | Details | 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;
6
	private $bdd = null;
7
	private $projet = null;
8
	private $versions = null;
9
	private $listeUrl = null;
10
	private $detailsHrefTpl = null;
11
	private $ontologieHrefTpl = null;
12
	private $champsProjet = array();
13
 
14
	private $nbreTotalNoms = 0;
15
	private $noms = array();
16
 
17
	public function __construct(Ressources $ressources, Parametres $parametres, Bdd $bdd) {
18
		$this->ressources = $ressources;
19
		$this->parametres = $parametres;
20
		$this->bdd = $bdd;
21
	}
22
 
23
	public function setProjet($projet) {
24
		$this->projet = $projet;
25
	}
26
 
27
	public function setVersions($versions) {
28
		$this->versions = $versions;
29
	}
30
 
31
public function setChampsProjet($champsProjet) {
32
		$this->champsProjet = $champsProjet;
33
	}
34
 
35
	public function setDetailsHrefTpl($tpl) {
36
		$this->detailsHrefTpl = $tpl;
37
	}
38
 
39
	public function setOntologieHrefTpl($tpl) {
40
		$this->ontologieHrefTpl = $tpl;
41
	}
42
 
43
	public function setListeUrl($url) {
44
		$this->listeUrl = $url;
45
	}
46
 
47
	public function consulter() {
48
		$this->noms = $this->rechercher();
49
		$this->nbreNomsTotal = $this->recupererNombreNomsTotal();
50
		$this->trierNoms();
51
		$retour = $this->construireTableauRetour();
52
		return $retour;
53
	}
54
 
55
	private function getTable() {
56
		$versions = $this->versions->getVersions();
57
		return $this->projet.'_v'.end($versions);
58
	}
59
 
60
	private function rechercher() {
61
		$resultats = array();
62
		$recherche = $this->parametres->get('recherche');
63
 
64
		if ($recherche == 'stricte') {
65
			$resultats = $this->rechercherStricte();
66
		} else if ($recherche == 'etendue') {
67
			$resultats = $this->rechercherEtendue();
68
		} else if ($recherche == 'floue') {
69
			$resultats = $this->rechercherFloue();
70
		}
71
 
72
		return $resultats;
73
	}
74
 
75
	private function rechercherStricte() {
76
		$table = $this->getTable();
77
		$conditions = array();
78
		if ($masque = $this->parametres->getMasquePourBdd()) {
79
			$conditions[] = "ns.nom_sci = $masque";
80
		}
81
		if ($masqueSg = $this->parametres->getMasquePourBdd('sg')) {
82
			$conditions[] = "ns.nom_supra_generique = $masqueSg";
83
		}
84
		if ($masqueGen = $this->parametres->getMasquePourBdd('gen')) {
85
			$conditions[] = "ns.genre = $masqueGen";
86
		}
87
		if ($masqueSp = $this->parametres->getMasquePourBdd('sp')) {
88
			$conditions[] = "ns.epithete_sp = $masqueSp";
89
		}
90
 
91
		$requete = 'SELECT SQL_CALC_FOUND_ROWS ns.*,  '.
92
					'	nr.nom_sci AS nr_nom_sci, nb.nom_sci AS nb_nom_sci '.
93
					"FROM $table AS ns ".
94
					"	LEFT JOIN $table AS nr ON (ns.num_nom_retenu = nr.num_nom) ".
95
					"	LEFT JOIN $table AS nb ON (ns.basionyme = nb.num_nom) ".
96
					(count($conditions) > 0 ? 'WHERE ' : '').
97
					implode(' AND ', $conditions).
98
					'ORDER BY ns.nom_sci ASC '.
99
					'LIMIT 0,100';
100
		$resultats = $this->bdd->recupererTous($requete);
101
 
102
		return $resultats;
103
	}
104
 
105
	private function rechercherEtendue() {
106
		$table = $this->getTable();
107
		$conditions = array();
108
		if ($masque = $this->parametres->getMasquePourBdd()) {
109
			$conditions[] = "ns.nom_sci LIKE $masque";
110
		}
111
		if ($masqueSg = $this->parametres->getMasquePourBdd('sg')) {
112
			$conditions[] = "ns.nom_supra_generique LIKE $masqueSg";
113
		}
114
		if ($masqueGen = $this->parametres->getMasquePourBdd('gen')) {
115
			$conditions[] = "ns.genre LIKE $masqueGen";
116
		}
117
		if ($masqueSp = $this->parametres->getMasquePourBdd('sp')) {
118
			$conditions[] = "ns.epithete_sp LIKE $masqueSp";
119
		}
120
		$requete = 'SELECT SQL_CALC_FOUND_ROWS ns.*,  '.
121
			'	nr.nom_sci AS nr_nom_sci, nb.nom_sci AS nb_nom_sci '.
122
			"FROM $table AS ns ".
123
			"	LEFT JOIN $table AS nr ON (ns.num_nom_retenu = nr.num_nom) ".
124
			"	LEFT JOIN $table AS nb ON (ns.basionyme = nb.num_nom) ".
125
			(count($conditions) > 0 ? 'WHERE ' : '').
126
			implode(' AND ', $conditions).
127
			'ORDER BY ns.nom_sci ASC '.
128
			'LIMIT 0,100';
129
 
130
		$resultats = $this->bdd->recupererTous($requete);
131
		return $resultats;
132
	}
133
 
134
	private function rechercherFloue() {
135
		$table = $this->getTable();
136
		$masque = $this->parametres->getMasquePourBdd();
137
		$requete = 'SELECT SQL_CALC_FOUND_ROWS ns.*,  '.
138
			'	nr.nom_sci AS nr_nom_sci, nb.nom_sci AS nb_nom_sci '.
139
			"FROM $table AS ns ".
140
			"	LEFT JOIN $table AS nr ON (ns.num_nom_retenu = nr.num_nom) ".
141
			"	LEFT JOIN $table AS nb ON (ns.basionyme = nb.num_nom) ".
142
			($masque ? 'WHERE '.
143
				"	(SOUNDEX(ns.nom_sci) = SOUNDEX($masque)) ".
144
				"	OR (SOUNDEX(REVERSE(ns.nom_sci)) = SOUNDEX(REVERSE($masque))) " : '').
145
			'ORDER BY ns.nom_sci ASC '.
146
			'LIMIT 0,100';
147
		$resultats = $this->bdd->recupererTous($requete);
148
		return $resultats;
149
	}
150
 
151
	private function recupererNombreNomsTotal() {
152
		$requete = 'SELECT FOUND_ROWS() AS nbre';
153
		$nombre = $this->bdd->recuperer($requete);
154
		return (int) $nombre['nbre'];
155
	}
156
 
157
	private function trierNoms() {
158
		$recherche = $this->parametres->get('recherche');
159
		if ($recherche == 'floue') {
160
			$this->trierRechercheFloue();
161
		}
162
	}
163
 
164
	public function trierRechercheFloue() {
165
		$nomDemande = $this->parametres->get('masque');
166
		$nomDemandeSimple = strtolower(Chaine::supprimerAccents($nomDemande));
167
 
168
		foreach ($this->noms as $id => $nom) {
169
			$nomFlouSimple = strtolower(Chaine::supprimerAccents($nom['nom_sci']));
170
			// Prime pour la ressemblance globale :
171
			$score = 500 - levenshtein($nomFlouSimple, $nomDemandeSimple);
172
			// On affine
173
			$score += similar_text($nomDemandeSimple, $nomFlouSimple) * 3;
174
			$this->noms[$id]['score'] = $score;
175
		}
176
		$noms = $this->noms;
177
		$this->noms = Tableau::trierMD($noms, array('score' => false));
178
		//print_r($this->noms);
179
	}
180
 
181
	private function construireTableauRetour() {
182
		$retour = array('entete' => array(), 'resultats' => array());
183
		$retour['resultats'] = $this->construireResultats();
184
		$retour['entete'] = $this->construireEntete();
185
		return $retour;
186
	}
187
 
188
	private function construireResultats() {
189
		$nomsFormates = array();
190
		foreach ($this->noms as $nom) {
191
			$id = $nom['num_nom'];
192
			$nomsFormates[$id] = $this->formaterNom($nom);
193
		}
194
		return $nomsFormates;
195
	}
196
 
197
	private function formaterNom($infos) {
198
		$nomAFormater = new NomDO($infos);
199
		$formateur = new NomFormateur($nomAFormater);
200
		$formateur->setBdd($this->bdd);
201
		$formateur->setDetailsHrefTpl($this->detailsHrefTpl);
202
		$formateur->setChampsRetour($this->parametres->getListe('retour.champs'));
203
		$nom = $formateur->formaterListe();
204
		return $nom;
205
	}
206
 
207
	private function formaterHref($nom) {
208
		$href = sprintf($this->detailsHrefTpl, $nom['num_nom']);
209
		return $href;
210
	}
211
 
212
	private function construireEntete() {
213
		$entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
214
		$entete['masque'] = $this->formaterEnteteMasque();
215
		$entete['depart'] = (int) $this->parametres->get('navigation.depart');
216
		$entete['limite'] = (int) $this->parametres->get('navigation.limite');
217
		$entete['total'] = $this->nbreNomsTotal;
218
		if ($hrefPrecedent = $this->formaterEnteteHrefPrecedent()) {
219
			$entete['href.precedent'] = $hrefPrecedent;
220
		}
221
		if ($hrefSuivant = $this->formaterEnteteHrefSuivant()) {
222
			$entete['href.suivant'] = $hrefSuivant;
223
		}
224
		return $entete;
225
	}
226
 
227
	private function formaterEnteteMasque() {
228
		$masqueComplet = array();
229
		if ($this->parametres->exister('masque')) {
230
			$masque = '';
231
			$masque .= 'nom_sci='.$this->parametres->get('masque');
232
			if ($this->parametres->get('recherche') == 'etendue') {
233
				$masque .= '%';
234
			}
235
			$masqueComplet[] = $masque;
236
		}
237
		if ($this->parametres->exister('masque.sg')) {
238
			$masque = '';
239
			$masque .= 'nom_supra_generique='.$this->parametres->get('masque.sg');
240
			if ($this->parametres->get('recherche') == 'etendue') {
241
				$masque .= '%';
242
			}
243
			$masqueComplet[] = $masque;
244
		}
245
		if ($this->parametres->exister('masque.gen')) {
246
			$masque = '';
247
			$masque .= 'genre='.$this->parametres->get('masque.gen');
248
			if ($this->parametres->get('recherche') == 'etendue') {
249
				$masque .= '%';
250
			}
251
			$masqueComplet[] = $masque;
252
		}
253
		if ($this->parametres->exister('masque.sp')) {
254
			$masque = '';
255
			$masque .= 'epithete_sp='.$this->parametres->get('masque.sp');
256
			if ($this->parametres->get('recherche') == 'etendue') {
257
				$masque .= '%';
258
			}
259
			$masqueComplet[] = $masque;
260
		}
261
		return implode('&', $masqueComplet);
262
	}
263
 
264
	private function formaterEnteteHrefPrecedent() {
265
		$limite = $this->parametres->get('navigation.limite');
266
		$departActuel = $this->parametres->get('navigation.depart');
267
		$departPrecedent = $departActuel - $limite;
268
		$href = null;
269
		if ($departPrecedent >= 0) {
270
			$squelette = $this->construireTplHrefNavigation();
271
			$href = sprintf($squelette, $departPrecedent, $limite);
272
		}
273
		return $href;
274
	}
275
 
276
	private function formaterEnteteHrefSuivant() {
277
		$limite = $this->parametres->get('navigation.limite');
278
		$departActuel = $this->parametres->get('navigation.depart');
279
		$departSuivant = $departActuel + $limite;
280
		$href = null;
281
		if ($departSuivant < $this->nbreNomsTotal) {
282
			$squelette = $this->construireTplHrefNavigation();
283
			$href = sprintf($squelette, $departSuivant, $limite);
284
		}
285
		return $href;
286
	}
287
 
288
	private function construireTplHrefNavigation() {
289
		$requetes = array();
290
		$this->parametres->rewind();
291
		while (is_null($parametre = $this->parametres->key()) === false) {
292
			if (strpos($parametre, 'navigation') === false) {
293
				$valeur = $this->parametres->current();
294
				$requetes[] = "$parametre=$valeur";
295
			}
296
			$this->parametres->next();
297
		}
298
		$requetes[] = "navigation.depart=%s";
299
		$requetes[] = "navigation.limite=%s";
300
		$tpl = $this->listeUrl.'?'.implode('&', $requetes);
301
		return $tpl;
302
	}
303
}
304
?>