Subversion Repositories eFlore/Projets.eflore-projets

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
208 jpm 1
<?php
2
class NomDAO {
3
	private $bdd = null;
4
	private $versions = null;
220 jpm 5
	private $requeteNbreNomsTotal = null;
208 jpm 6
 
215 jpm 7
	public function __construct(Ressources $ressources, Parametres $parametres, Bdd $bdd, Versions $versions) {
208 jpm 8
		$this->ressources = $ressources;
9
		$this->parametres = $parametres;
10
		$this->bdd = $bdd;
11
		$this->versions = $versions;
12
	}
13
 
14
	public function rechercherInfosNom() {
15
		$table = $this->getTable();
16
		$detailsId = $this->ressources->getDetailsId();
17
		$detailsId = $this->bdd->proteger($detailsId);
18
		$requete =
19
				'SELECT ns.*,  '.
20
				'	nr.nom_sci AS nr_nom_sci, nb.nom_sci AS nb_nom_sci '.
21
				"FROM $table AS ns ".
22
				"	LEFT JOIN $table AS nr ON (ns.num_nom_retenu = nr.num_nom) ".
23
				"	LEFT JOIN $table AS nb ON (ns.basionyme = nb.num_nom) ".
24
				"WHERE ns.num_nom = $detailsId ";
25
		$resultats = $this->bdd->recuperer($requete);
26
		$nom = new NomDO($resultats);
27
		return $nom;
28
	}
29
 
216 jpm 30
	public function rechercher() {
220 jpm 31
		$clause = $this->getClauseSelectSpeciale();
208 jpm 32
		$table = $this->getTable();
216 jpm 33
		$conditions = $this->getConditions();
34
		$where = $this->getWhere($conditions);
209 jpm 35
		$navigation = $this->getNavigation();
216 jpm 36
 
220 jpm 37
		$requete = "SELECT $clause ns.*, ".
209 jpm 38
			'	nr.nom_sci AS nr_nom_sci, nb.nom_sci AS nb_nom_sci '.
39
			"FROM $table AS ns ".
40
			"	LEFT JOIN $table AS nr ON (ns.num_nom_retenu = nr.num_nom) ".
41
			"	LEFT JOIN $table AS nb ON (ns.basionyme = nb.num_nom) ".
220 jpm 42
			$where.' '.$conditions.' '.
209 jpm 43
			'ORDER BY ns.nom_sci ASC '.
44
			"LIMIT $navigation ";
220 jpm 45
		$this->requeteNbreNomsTotal = $this->transformerRequetePourNbreNomsTotal($requete);
208 jpm 46
		$resultats = $this->bdd->recupererTous($requete);
47
 
48
		return $resultats;
49
	}
50
 
51
	public function rechercherFloue() {
220 jpm 52
		$clause = $this->getClauseSelectSpeciale();
208 jpm 53
		$table = $this->getTable();
54
		$masque = $this->parametres->getMasquePourBdd();
212 jpm 55
		$where = $this->getWhere();
209 jpm 56
		$navigation = $this->getNavigation();
220 jpm 57
 
58
		$requete = "SELECT $clause ns.*, ".
209 jpm 59
			'	nr.nom_sci AS nr_nom_sci, nb.nom_sci AS nb_nom_sci '.
60
			"FROM $table AS ns ".
61
			"	LEFT JOIN $table AS nr ON (ns.num_nom_retenu = nr.num_nom) ".
62
			"	LEFT JOIN $table AS nb ON (ns.basionyme = nb.num_nom) ".
212 jpm 63
			$where .
64
			($masque ? ($where ? ' AND ' : ' WHERE ').
65
				"	(SOUNDEX(ns.nom_sci) = SOUNDEX($masque)) ".
66
				"	OR (SOUNDEX(REVERSE(ns.nom_sci)) = SOUNDEX(REVERSE($masque))) " : '').
209 jpm 67
			'ORDER BY ns.nom_sci ASC '.
68
			"LIMIT $navigation ";
220 jpm 69
		$this->requeteNbreNomsTotal = $this->transformerRequetePourNbreNomsTotal($requete);
208 jpm 70
		$resultats = $this->bdd->recupererTous($requete);
71
		return $resultats;
72
	}
73
 
220 jpm 74
	private function getClauseSelectSpeciale() {
75
		$clause = (Config::get('bdd_protocole') == 'mysql') ? 'SQL_CALC_FOUND_ROWS' : '';
76
		return $clause;
77
	}
78
 
216 jpm 79
	private function getTable() {
80
		$versions = $this->versions->getVersions();
81
		$derniereVersion = end($versions);
82
		$projetNom = strtolower($this->ressources->getProjetNom());
83
		return $projetNom.'_v'.$derniereVersion;
209 jpm 84
	}
85
 
216 jpm 86
	private function getConditions() {
87
		$masquesStrictes = array('nn', 'rg');
88
		$paramsMasque = array(
89
			'' => 'nom_sci',
90
			'nn' => 'num_nom',
91
			'rg' => 'rang',
92
			'sg' => 'nom_supra_generique',
93
			'gen' => 'genre',
94
			'sp' => 'epithete_sp',
95
			'ssp' => 'epithete_infra_sp',
96
			'au' => 'auteur',
97
			'an' => 'annee');
98
 
99
		$operateurParDefaut = $this->getOperateurCondition();
100
		$conditionsSql = array();
101
		foreach ($paramsMasque as $typeMasque => $champ) {
102
			$operateur = in_array($typeMasque, $masquesStrictes) ? '=' : $operateurParDefaut;
103
			if ($valeurMasque = $this->parametres->getMasquePourBdd($typeMasque)) {
104
				$conditionsSql[] = "ns.$champ $operateur $valeurMasque";
105
			}
106
		}
107
		return implode(' AND ', $conditionsSql);
108
	}
109
 
110
	private function getOperateurCondition() {
111
		$operateur = '';
112
		$recherche = $this->parametres->get('recherche');
113
		if ($recherche == 'stricte') {
114
			$operateur = '=';
115
		} else if ($recherche == 'etendue') {
116
			$operateur = 'LIKE';
117
		}
118
		return $operateur;
119
	}
120
 
121
	private function getWhere($conditions = '') {
209 jpm 122
		$where = '';
123
		if ($this->ressources->getServiceNom() == 'taxons') {
124
			$where = 'WHERE ns.num_nom = ns.num_nom_retenu ';
216 jpm 125
		} else if ($conditions != '') {
209 jpm 126
			$where = 'WHERE ';
127
		}
128
		return $where;
129
	}
130
 
216 jpm 131
	private function getNavigation() {
132
		$debut = (int) $this->parametres->get('navigation.depart');
133
		$nbre = $this->parametres->get('navigation.limite');
134
		$navigation = "$debut,$nbre";
135
		return $navigation;
136
	}
137
 
220 jpm 138
	private function transformerRequetePourNbreNomsTotal($requete) {
139
		$requete = preg_replace('/SELECT .* FROM/', 'SELECT COUNT(*) AS nbre FROM', $requete);
140
		$requete = preg_replace('/LIMIT [0-9]+,[0-9]+/', '', $requete);
141
		return $requete;
142
	}
143
 
208 jpm 144
	public function recupererNombreNomsTotal() {
220 jpm 145
		if (Config::get('bdd_protocole') == 'mysql') {
146
			$requete = 'SELECT FOUND_ROWS() AS nbre';
147
		} else {
148
			$requete = $this->requeteNbreNomsTotal;
149
		}
150
 
208 jpm 151
		$nombre = $this->bdd->recuperer($requete);
152
		return (int) $nombre['nbre'];
153
	}
154
}
155
?>