Subversion Repositories eFlore/Projets.eflore-projets

Rev

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