Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev Author Line No. Line
6 jpm 1
<?php
2
/**
1105 mathias 3
* Retourne la liste des noms répertoriés par le projet chorodep
6 jpm 4
*
1103 mathias 5
* @package chorodep
1111 mathias 6
* @author Mathias Chouet <mathias@tela-botanica.org>
6 jpm 7
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
8
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
9
* @version 1.0
1103 mathias 10
* @copyright 1999-2014 Tela Botanica (accueil@tela-botanica.org)
6 jpm 11
*/
12
 
1103 mathias 13
class Noms extends Commun {
6 jpm 14
 
1103 mathias 15
	protected $serviceNom = 'noms';
16
	protected $table;
17
	protected $masque;
18
	protected $depart;
19
	protected $limite;
20
	protected $tri;
21
	protected $tri_dir;
22
 
1111 mathias 23
	protected function init() {
1103 mathias 24
		$this->masque = array();
25
		$this->depart = 0;
26
		$this->limite = 20;
27
		$this->tri = 'nom_sci';
28
		$this->tri_dir = 'ASC';
29
		$this->traiterVersionProjet();
30
		$this->table = $this->table_version[0];
6 jpm 31
	}
1103 mathias 32
 
33
	/**
34
	 * Récupère les paramètres de navigation
35
	 * @param type $parametres
36
	 */
37
	protected function setDepartLimite($parametres) {
38
		if(isset($parametres['navigation.depart']) && $parametres['navigation.depart'] != '') {
39
			$this->depart = max(0, intval($parametres['navigation.depart']));
6 jpm 40
		}
1103 mathias 41
		if(isset($parametres['navigation.limite']) && $parametres['navigation.limite'] != '') {
42
			$this->limite = max(0, intval($parametres['navigation.limite']));
6 jpm 43
		}
44
	}
1103 mathias 45
 
46
	/**
47
	 * Récupère les paramètres de filtrage
48
	 * @param type $parametres
6 jpm 49
	 */
1103 mathias 50
	protected function setMasque($parametres) {
51
		if(isset($parametres['masque.nom']) && $parametres['masque.nom'] != '') {
52
			$this->masque['nom'] = $parametres['masque.nom'];
6 jpm 53
		}
1103 mathias 54
		if(isset($parametres['masque.zone-geo']) && $parametres['masque.zone-geo'] != '') {
1105 mathias 55
			$zg = $parametres['masque.zone-geo'];
56
			// Tango Corse
57
			if ($zg == '2A' || $zg == '2B') {
58
				$zg = '20';
59
			}
60
			$this->masque['zone-geo'] = $zg;
6 jpm 61
		}
62
	}
1100 mathias 63
 
64
	/**
1103 mathias 65
	 * Récupère les paramètres de tri
66
	 * @param type $parametres
1100 mathias 67
	 */
1103 mathias 68
	protected function setTri($parametres) {
69
		if(isset($parametres['retour.tri']) && $parametres['retour.tri'] != '') {
70
			$this->tri = $parametres['retour.tri'];
6 jpm 71
		}
1103 mathias 72
		if(isset($parametres['retour.ordre']) && in_array($parametres['retour.ordre'], array('ASC', 'DESC'))) {
73
			$this->tri_dir = $parametres['retour.ordre'];
6 jpm 74
		}
75
	}
1103 mathias 76
 
77
	public function consulter($ressources, $parametres) {
78
		$donnees = array();
79
 
80
		$this->setDepartLimite($parametres);
81
		$this->setMasque($parametres);
82
		$this->setTri($parametres);
83
		$noms = $this->listeNoms();
84
		$total = $this->compterNoms();
85
 
86
		$url_base = Config::get('url_service');
87
 
88
		$masqueEnParams = array();
89
		foreach ($this->masque as $k => $v) {
90
			$masqueEnParams[] = 'masque.' . $k . '=' . $v;
6 jpm 91
		}
1103 mathias 92
		$masqueEnParams = implode('&', $masqueEnParams);
93
 
94
		$donnees['entete'] = array(
95
			'masque' => $masqueEnParams,
96
			'total' => $total,
97
			'depart' => $this->depart,
98
			'limite' => $this->limite
99
		);
100
		if ($this->depart > 0) {
101
			$donnees['entete']['href.precedent'] = $url_base . '/' . $this->serviceNom . '?'
102
					. 'navigation.depart=' . max(0, ($this->depart - $this->limite)) . '&navigation.limite=' . $this->limite
103
					. '&retour.tri=' . $this->tri . '&retour.ordre=' . $this->tri_dir
1124 mathias 104
					. ($masqueEnParams ? '&' . $masqueEnParams : '');
1100 mathias 105
		}
1103 mathias 106
		if (($this->depart + $this->limite) < $total ) {
107
			$donnees['entete']['href.suivant'] = $url_base . '/' . $this->serviceNom . '?'
108
					. 'navigation.depart=' . ($this->depart + $this->limite) . '&navigation.limite=' . $this->limite
109
					. '&retour.tri=' . $this->tri . '&retour.ordre=' . $this->tri_dir
1124 mathias 110
					. ($masqueEnParams ? '&' . $masqueEnParams : '');
251 delphine 111
		}
1103 mathias 112
		$donnees['resultat'] = $noms;
113
 
114
		return $donnees;
251 delphine 115
	}
1103 mathias 116
 
1105 mathias 117
	/**
118
	 * Renvoie la liste des noms répertoriés par chorodep; si un masque a été
119
	 * défini sur une zone géographique, retourne aussi la présence sur cette zone
120
	 */
1103 mathias 121
	protected function listeNoms() {
1105 mathias 122
		$req = "SELECT DISTINCT num_nom, nom_sci";
123
		if (isset($this->masque['zone-geo']) && $this->masque['zone-geo'] != null) {
124
			$req .= ", `" . $this->masque['zone-geo'] . "` as presence";
125
		}
126
		$req .= " FROM " . $this->table;
1103 mathias 127
		$req .= $this->construireWhere();
128
		$req .= " ORDER BY ".$this->tri." ".$this->tri_dir." ";
129
		$req .= " LIMIT " . $this->depart . ", " . $this->limite;
130
 
131
		$resultat = $this->getBdd()->recupererTous($req);
132
 
133
		return $resultat;
6 jpm 134
	}
1103 mathias 135
 
136
	protected function compterNoms() {
137
		$req = "SELECT count(DISTINCT num_nom, nom_sci) AS compte FROM " . $this->table;
138
		$req .= $this->construireWhere();
139
		$resultat = $this->getBdd()->recuperer($req);
140
 
141
		return $resultat['compte'];
6 jpm 142
	}
1103 mathias 143
 
144
	protected function construireWhere() {
145
		$where = "";
146
		$conditions = array();
1106 mathias 147
		// élimination des entrées sans nn valide
148
		$conditions[] = "CAST(num_nom AS decimal) != 0";
149
		// masque
1103 mathias 150
		if(!empty($this->masque)) {
151
			if(isset($this->masque['nom'])) {
152
				$masqueNom = $this->getBdd()->proteger($this->masque['nom']);
153
				$conditions[] = "nom_sci LIKE $masqueNom";
6 jpm 154
			}
1103 mathias 155
			if(isset($this->masque['zone-geo'])) {
1105 mathias 156
				$masqueZg = $this->masque['zone-geo'];
157
				$conditions[] = "`$masqueZg` in ('1', '1?')";
6 jpm 158
			}
159
		}
1106 mathias 160
		$where = " WHERE " . implode(' AND ', $conditions);
1103 mathias 161
		return $where;
6 jpm 162
	}
163
}
164
?>