Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 255 | Rev 268 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 255 Rev 260
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
3
/**
4
* Gère le sous-service Taxons de Cartes.
4
* Gère le sous-service Taxons de Cartes.
5
*
5
*
6
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=EfloreApi01Cartes
6
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=EfloreApi01Cartes
7
*
7
*
8
* @package eFlore/services
8
* @package eFlore/services
9
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
9
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
10
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
10
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
11
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
11
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
12
* @version 1.0
12
* @version 1.0
13
* @copyright 1999-2012 Tela Botanica (accueil@tela-botanica.org)
13
* @copyright 1999-2012 Tela Botanica (accueil@tela-botanica.org)
14
*/
14
*/
15
// TODO : Config et Outils sont des classes statiques qui doivent poser des pb pour les tests...
15
// TODO : Config et Outils sont des classes statiques qui doivent poser des pb pour les tests...
16
class TaxonsCartes {
16
class TaxonsCartes {
17
 
17
 
18
	private $parametres = array();
18
	private $parametres = array();
19
	private $ressources = array();
19
	private $ressources = array();
20
 
20
 
21
		const CODE_REFTAX_DEFAUT = 'bdtfx';
21
	const CODE_REFTAX_DEFAUT = 'bdtfx';
22
	const TYPE_ID_DEFAUT = 'nn';
22
	const TYPE_ID_DEFAUT = 'nn';
23
	const CARTE_DEFAUT = 'france_02';
23
	const CARTE_DEFAUT = 'france_02';
24
	const FORMAT_DEFAUT = '550';
24
	const FORMAT_DEFAUT = '550';
-
 
25
	const VERSION_DEFAUT = '+';
25
	const MIME_SVG = 'image/svg+xml';
26
	const MIME_SVG = 'image/svg+xml';
26
	const MIME_PNG = 'image/png';
27
	const MIME_PNG = 'image/png';
27
	const PRESENCE_CHOROLOGIE = '1';
28
	const PRESENCE_CHOROLOGIE = '1';
28
 
29
 
29
	private $config = array();
30
	private $config = array();
30
	private $cheminCartesBase = '';
31
	private $cheminCartesBase = '';
31
	private $formats_supportes = array(self::MIME_SVG, self::MIME_PNG);
32
	private $formatsSupportes = array(self::MIME_SVG, self::MIME_PNG);
-
 
33
	private $reftaxSupportes = array(self::CODE_REFTAX_DEFAUT);
32
	private $UrlNavigation = null;
34
	private $UrlNavigation = null;
33
	private $taxonsDemandes = array();
35
	private $taxonsDemandes = array();
34
	private $imgLargeur = 0;
36
	private $imgLargeur = 0;
35
	private $imgHauteur = 0;
37
	private $imgHauteur = 0;
36
	private $tableMeta = '';
38
	private $tableMeta = '';
37
	private $tableOntologie = '';
39
	private $tableOntologie = '';
38
	private $tableChorodep = '';
40
	private $tableChorodep = '';
39
	private $metadonnees = '';
41
	private $metadonnees = '';
40
	private $ontologies = '';
42
	private $ontologies = '';
41
	private $priorites = '';
43
	private $priorites = '';
42
	private $version = '';
44
	private $version = '';
43
	private $legende = array();
45
	private $legende = array();
44
	private $donnees = array();
46
	private $donnees = array();
45
 
47
 
46
 
48
 
47
	public function __construct(Conteneur $conteneur) {
49
	public function __construct(Conteneur $conteneur) {
48
		$this->Bdd = $conteneur->getBdd();
50
		$this->Bdd = $conteneur->getBdd();
49
		$this->tableOntologie = $conteneur->getParametre('bdd_table_ontologies');
51
		$this->tableOntologie = $conteneur->getParametre('bdd_table_ontologies');
50
		$this->config = $conteneur->getParametre('Cartes');
52
		$this->config = $conteneur->getParametre('Cartes');
51
		$this->tableMeta = $conteneur->getParametre('bdd_table_meta');
53
		$this->tableMeta = $conteneur->getParametre('bdd_table_meta');
52
		$this->tableOntologie = $conteneur->getParametre('bdd_table_ontologies');
54
		$this->tableOntologie = $conteneur->getParametre('bdd_table_ontologies');
53
		$this->chargerMetadonneesDerniereVersion();
-
 
54
		$this->version = $this->metadonnees['version'];
-
 
55
		$this->tableChorodep = 'chorodep_v'.str_replace('.', '_', $this->version);
-
 
56
		$this->chargerOntologies();
-
 
57
		$this->chargerLegende();
-
 
58
		$this->chargerPrioritesLegende();
-
 
59
		$this->cheminCartesBase = $this->config['chemin'];
55
		$this->cheminCartesBase = $this->config['chemin'];
60
		$cacheOptions = array('mise_en_cache' => $this->config['cache']['miseEnCache'],
56
		$cacheOptions = array('mise_en_cache' => $this->config['cache']['miseEnCache'],
61
					'stockage_chemin' => $this->config['cache']['stockageChemin'],
57
					'stockage_chemin' => $this->config['cache']['stockageChemin'],
62
					'duree_de_vie' => $this->config['cache']['dureeDeVie']);
58
					'duree_de_vie' => $this->config['cache']['dureeDeVie']);
63
		$this->cache = $conteneur->getCacheSimple($cacheOptions);
59
		$this->cache = $conteneur->getCacheSimple($cacheOptions);
64
	}
60
	}
65
 
61
 
66
	public function consulter($ressources, $parametres) {
62
	public function consulter($ressources, $parametres) {
67
		$this->parametres = $parametres;
63
		$this->parametres = $parametres;
68
		$this->ressources = $ressources;
64
		$this->ressources = $ressources;
69
 
65
 
70
		$this->definirValeurParDefautDesParametres();
66
		$this->definirValeurParDefautDesParametres();
71
		$this->verifierParametres();
67
		$this->verifierParametres();
72
 
68
 
73
		$resultat = $this->obtenirResultat();
69
		$resultat = $this->obtenirResultat();
74
 
70
 
75
		return $resultat;
71
		return $resultat;
76
	}
72
	}
77
 
73
 
78
	private function definirValeurParDefautDesParametres() {
74
	private function definirValeurParDefautDesParametres() {
79
		if (isset($this->parametres['retour']) == false) {
75
		if (isset($this->parametres['retour']) == false) {
80
			$this->parametres['retour'] = self::MIME_SVG;
76
			$this->parametres['retour'] = self::MIME_SVG;
81
		}
77
		}
82
		if (isset($this->parametres['retour.format']) == false) {
78
		if (isset($this->parametres['retour.format']) == false) {
83
			$this->parametres['retour.format'] = self::FORMAT_DEFAUT;
79
			$this->parametres['retour.format'] = self::FORMAT_DEFAUT;
84
		}
80
		}
-
 
81
		if (isset($this->parametres['version.projet']) == false) {
-
 
82
			$this->parametres['version.projet'] = self::VERSION_DEFAUT;
-
 
83
		}
85
	}
84
	}
86
 
85
 
87
	private function verifierParametres() {
86
	private function verifierParametres() {
88
		$erreurs = array();
87
		$erreurs = array();
89
 
88
 
90
		if (isset($this->parametres['retour']) == false) {
89
		if (isset($this->parametres['retour']) == false) {
91
			$erreurs[] = "Le paramètre type de retour 'retour' est obligatoire.";
90
			$erreurs[] = "Le paramètre type de retour 'retour' est obligatoire.";
92
		}
91
		}
93
		if ($this->verifierValeurParametreRetour() == false) {
92
		if ($this->verifierValeurParametreRetour() == false) {
94
			$erreurs[] = "Le type de retour '{$this->parametres['retour']}' n'est pas supporté.";
93
			$erreurs[] = "Le type de retour '{$this->parametres['retour']}' n'est pas supporté.";
95
		}
94
		}
96
		if (isset($this->parametres['retour.format']) == false) {
95
		if (isset($this->parametres['retour.format']) == false) {
97
			$erreurs[] = "Le paramètre de format de retour 'retour.format' est obligatoire.";
96
			$erreurs[] = "Le paramètre de format de retour 'retour.format' est obligatoire.";
98
		}
97
		}
99
		if ($this->verifierValeurParametreFormat() == false) {
98
		if ($this->verifierValeurParametreFormat() == false) {
100
			$erreurs[] = "Le type de format '{$this->parametres['retour.format']}' n'est pas supporté.".
99
			$erreurs[] = "Le type de format '{$this->parametres['retour.format']}' n'est pas supporté.".
101
					"Veuillez indiquer un nombre entier correspondant à la largeur désirée pour la carte.";
100
				"Veuillez indiquer un nombre entier correspondant à la largeur désirée pour la carte.";
-
 
101
		}
-
 
102
		if ($this->verifierValeurParametreVersionProjet() == false) {
-
 
103
			$erreurs[] = "Le format de version.projet '{$this->parametres['version.projet']}' n'est pas supporté.".
-
 
104
				"Veuillez indiquer le code '+' (=dernière version) ou une année sur 4 chiffres séparé d'un mois ".
-
 
105
				"sur deux chiffres par un point. Ex. : 2012.01";
102
		}
106
		}
103
 
107
 
104
		if (count($erreurs) > 0) {
108
		if (count($erreurs) > 0) {
105
			$message = implode('<br />', $erreurs);
109
			$message = implode('<br />', $erreurs);
106
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
110
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
107
			throw new Exception($message, $code);
111
			throw new Exception($message, $code);
108
		}
112
		}
109
	}
113
	}
110
 
114
 
111
	private function verifierValeurParametreRetour() {
115
	private function verifierValeurParametreRetour() {
112
		return in_array($this->parametres['retour'], $this->formats_supportes);
116
		return in_array($this->parametres['retour'], $this->formatsSupportes);
113
	}
117
	}
114
 
118
 
115
	private function verifierValeurParametreFormat() {
119
	private function verifierValeurParametreFormat() {
116
		if ($ok = preg_match('/^([0-9]+)$/', $this->parametres['retour.format'], $match)) {
120
		if ($ok = preg_match('/^([0-9]+)$/', $this->parametres['retour.format'], $match)) {
117
			$this->imgLargeur = $match[1];
121
			$this->imgLargeur = $match[1];
118
		} else if ($ok = preg_match('/^([0-9]+)x([0-9]+)$/', $this->parametres['retour.format'], $match)) {
122
		} else if ($ok = preg_match('/^([0-9]+)x([0-9]+)$/', $this->parametres['retour.format'], $match)) {
119
			$this->imgLargeur = $match[1];
123
			$this->imgLargeur = $match[1];
120
			$this->imgHauteur = $match[2];
124
			$this->imgHauteur = $match[2];
121
		}
125
		}
122
		return $ok;
126
		return $ok;
123
	}
127
	}
-
 
128
	private function verifierValeurParametreVersionProjet() {
-
 
129
		$ok = (preg_match('/^(?:[+]|[0-9]{4}\.[0-9]{2})$/', $this->parametres['version.projet']) == 0) ? false : true;
-
 
130
		return $ok;
-
 
131
	}
124
 
132
 
125
	private function obtenirResultat() {
133
	private function obtenirResultat() {
126
		$this->analyserIdentifiants();
134
		$this->analyserIdentifiants();
-
 
135
		$this->verifierIdentifiants();
-
 
136
 
-
 
137
		$this->chargerMetadonnees();
-
 
138
		$this->definirVersion();
-
 
139
		$this->tableChorodep = 'chorodep_v'.str_replace('.', '_', $this->version);
-
 
140
		$this->chargerOntologies();
-
 
141
		$this->chargerLegende();
127
 
142
		$this->chargerPrioritesLegende();
128
		$this->chargerDonnees();
143
		$this->chargerDonnees();
129
		$svg = $this->genererSVG();
144
		$svg = $this->genererSVG();
130
		$img = ($this->parametres['retour'] == self::MIME_PNG) ? $this->convertirEnPNG($svg) : $svg;
145
		$img = ($this->parametres['retour'] == self::MIME_PNG) ? $this->convertirEnPNG($svg) : $svg;
131
 
146
 
132
		$resultat = new ResultatService();
147
		$resultat = new ResultatService();
133
		$resultat->corps = $img;
148
		$resultat->corps = $img;
134
		$resultat->mime = $this->parametres['retour'];
149
		$resultat->mime = $this->parametres['retour'];
135
 
150
 
136
		return $resultat;
151
		return $resultat;
137
	}
152
	}
138
 
153
 
139
	private function analyserIdentifiants() {
154
	private function analyserIdentifiants() {
140
		$ids = $this->ressources[0];
155
		$ids = $this->ressources[0];
141
		if (preg_match('/^[0-9]+$/', $ids)) {
156
		if (preg_match('/^[0-9]+$/', $ids)) {
142
			$this->taxonsDemandes[self::CODE_REFTAX_DEFAUT]['nn'][] = $ids;
157
			$this->taxonsDemandes[self::CODE_REFTAX_DEFAUT]['nn'][] = $ids;
143
		} else {
158
		} else {
144
			// ceci contient potentiellement des formes ref_tax1.nn:1,2;ref_tax2.nt:3,4
159
			// ceci contient potentiellement des formes ref_tax1.nn:1,2;ref_tax2.nt:3,4
145
			$projetsListeEtNumNoms = explode(';', $ids);
160
			$projetsListeEtNumNoms = explode(';', $ids);
146
			if (count($projetsListeEtNumNoms) > 0) {
161
			if (count($projetsListeEtNumNoms) > 0) {
147
				foreach ($projetsListeEtNumNoms as $projetEtNumNoms) {
162
				foreach ($projetsListeEtNumNoms as $projetEtNumNoms) {
148
					$projetEtNumNoms = (strpos($projetEtNumNoms, ':')) ? $projetEtNumNoms : self::CODE_REFTAX_DEFAUT.'.nn:'.$projetEtNumNoms;
163
					$projetEtNumNoms = (strpos($projetEtNumNoms, ':')) ? $projetEtNumNoms : self::CODE_REFTAX_DEFAUT.'.nn:'.$projetEtNumNoms;
149
					list($projetEtType, $numNoms) = explode(':', $projetEtNumNoms);
164
					list($projetEtType, $numNoms) = explode(':', $projetEtNumNoms);
-
 
165
 
-
 
166
					$projetEtType = (strpos($projetEtType, '.')) ? $projetEtType : self::CODE_REFTAX_DEFAUT.'.'.$projetEtType;
150
					list($projet, $type) = explode('.', $projetEtType);
167
					list($projet, $type) = explode('.', $projetEtType);
-
 
168
 
151
					$this->taxonsDemandes[$projet][$type] = explode(',', $numNoms);
169
					$this->taxonsDemandes[$projet][$type] = explode(',', $numNoms);
152
				}
170
				}
153
			}
171
			}
154
		}
172
		}
155
	}
173
	}
-
 
174
 
-
 
175
	private function verifierIdentifiants() {
-
 
176
		foreach (array_keys($this->taxonsDemandes) as $reftax) {
-
 
177
			$ok = in_array($reftax, $this->reftaxSupportes);
-
 
178
			if ($ok == false) {
-
 
179
				$message = "Le reférentiel taxonomique '$reftax' n'est pas pris en compte par ce projet.";
-
 
180
				$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
-
 
181
				throw new Exception($message, $code);
-
 
182
			}
-
 
183
		}
-
 
184
	}
156
 
185
 
157
	private function chargerMetadonneesDerniereVersion() {
186
	private function chargerMetadonnees() {
158
		$requete = 'SELECT * '.
187
		$requete = 'SELECT * '.
159
				"FROM {$this->tableMeta} ".
188
			"FROM {$this->tableMeta} ".
160
				"ORDER BY date_creation DESC ".
-
 
161
				"LIMIT 0,1 ";
189
			"ORDER BY date_creation DESC ";
162
		$resultats = $this->Bdd->recuperer($requete);
190
		$resultats = $this->Bdd->recupererTous($requete);
163
 
191
 
164
		if (!is_array($resultats) || count($resultats) <= 0) {
192
		if (!is_array($resultats) || count($resultats) <= 0) {
165
			$message = "Les données de version n'a pu être chargée pour la ressource demandée";
193
			$message = "Les méta-données n'ont pu être chargée pour la ressource demandée";
166
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
194
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
167
			throw new Exception($message, $code);
195
			throw new Exception($message, $code);
168
		}
196
		}
169
 
197
 
170
		$this->metadonnees = $resultats;
198
		$this->metadonnees = $resultats;
-
 
199
	}
-
 
200
 
-
 
201
	private function definirVersion() {
-
 
202
		if ($this->parametres['version.projet'] == '+') {
-
 
203
			$this->version = $this->metadonnees[0]['version'];
-
 
204
		} else {
-
 
205
			$this->version = $this->parametres['version.projet'];
171
 
206
		}
172
	}
207
	}
173
 
208
 
174
	private function chargerOntologies() {
209
	private function chargerOntologies() {
175
		$requete = 'SELECT * '.
210
		$requete = 'SELECT * '.
176
						"FROM {$this->tableOntologie} ";
211
			"FROM {$this->tableOntologie} ";
177
		$resultats = $this->Bdd->recupererTous($requete);
212
		$resultats = $this->Bdd->recupererTous($requete);
178
 
213
 
179
		if (!is_array($resultats) || count($resultats) <= 0) {
214
		if (!is_array($resultats) || count($resultats) <= 0) {
180
			$message = "Les données de légende n'ont pu être chargées pour la ressource demandée";
215
			$message = "Les données de légende n'ont pu être chargées pour la ressource demandée";
181
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
216
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
182
			throw new Exception($message, $code);
217
			throw new Exception($message, $code);
183
		}
218
		}
184
 
219
 
185
		foreach ($resultats as $ontologie) {
220
		foreach ($resultats as $ontologie) {
186
			$this->ontologies[$ontologie['id']] = $this->extraireComplementsOntologies($ontologie);
221
			$this->ontologies[$ontologie['id']] = $this->extraireComplementsOntologies($ontologie);
187
		}
222
		}
188
	}
223
	}
189
 
224
 
190
 
225
 
191
	private function extraireComplementsOntologies($ontologie) {
226
	private function extraireComplementsOntologies($ontologie) {
192
		if ($ontologie['complements'] != '') {
227
		if ($ontologie['complements'] != '') {
193
			$complements = explode(',', trim($ontologie['complements']));
228
			$complements = explode(',', trim($ontologie['complements']));
194
			foreach ($complements as $complement) {
229
			foreach ($complements as $complement) {
195
				list($cle, $val) = explode('=', trim($complement));
230
				list($cle, $val) = explode('=', trim($complement));
196
				$ontologie[trim($cle)] = trim($val);
231
				$ontologie[trim($cle)] = trim($val);
197
			}
232
			}
198
		}
233
		}
199
		return $ontologie;
234
		return $ontologie;
200
	}
235
	}
201
 
236
 
202
	private function chargerLegende() {
237
	private function chargerLegende() {
203
		foreach ($this->ontologies as $ontologie) {
238
		foreach ($this->ontologies as $ontologie) {
204
			if ($ontologie['classe_id'] == self::PRESENCE_CHOROLOGIE) {
239
			if ($ontologie['classe_id'] == self::PRESENCE_CHOROLOGIE) {
205
				$this->legende[$ontologie['code']] = $ontologie['legende'];
240
				$this->legende[$ontologie['code']] = $ontologie['legende'];
206
			}
241
			}
207
		}
242
		}
208
	}
243
	}
209
 
244
 
210
	private function chargerPrioritesLegende() {
245
	private function chargerPrioritesLegende() {
211
		foreach ($this->ontologies as $ontologie) {
246
		foreach ($this->ontologies as $ontologie) {
212
			if ($ontologie['classe_id'] == self::PRESENCE_CHOROLOGIE && isset($ontologie['priorite'])) {
247
			if ($ontologie['classe_id'] == self::PRESENCE_CHOROLOGIE && isset($ontologie['priorite'])) {
213
				$this->priorites[$ontologie['code']] = $ontologie['priorite'];
248
				$this->priorites[$ontologie['code']] = $ontologie['priorite'];
214
			}
249
			}
215
		}
250
		}
216
	}
251
	}
217
 
252
 
218
	private function chargerDonnees() {
253
	private function chargerDonnees() {
219
		$conditions = $this->getConditions();
254
		$conditions = $this->getConditions();
220
		$requete = 'SELECT * '.
255
		$requete = 'SELECT * '.
221
					"FROM {$this->tableChorodep} ".
256
					"FROM {$this->tableChorodep} ".
222
		(isset($conditions) ? 'WHERE '.implode(' AND ', $conditions) : '');
257
		(isset($conditions) ? 'WHERE '.implode(' AND ', $conditions) : '');
223
		$resultat = $this->Bdd->recupererTous($requete);
258
		$resultat = $this->Bdd->recupererTous($requete);
224
 
259
 
225
		if (!is_array($resultat) || count($resultat) <= 0) {
260
		if (!is_array($resultat) || count($resultat) <= 0) {
226
			$message = "Aucune donnée ne correspond à la ressource demandée";
261
			$message = "Aucune donnée ne correspond à la ressource demandée";
227
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
262
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
228
			throw new Exception($message, $code);
263
			throw new Exception($message, $code);
229
		}
264
		}
230
		$this->donnees = $resultat;
265
		$this->donnees = $resultat;
231
	}
266
	}
232
 
267
 
233
	private function getConditions() {
268
	private function getConditions() {
234
		$conditions = null;
269
		$conditions = null;
235
		if ($nnListe = $this->getListeNumNom()) {
270
		if ($nnListe = $this->getListeNumNom()) {
236
			$conditions[] = "num_nom IN ($nnListe) ";
271
			$conditions[] = "num_nom IN ($nnListe) ";
237
		}
272
		}
238
		if ($ntListe = $this->getListeNumTax()) {
273
		if ($ntListe = $this->getListeNumTax()) {
239
			$conditions[] = "num_tax IN ($ntListe) ";
274
			$conditions[] = "num_tax IN ($ntListe) ";
240
		}
275
		}
241
		return $conditions;
276
		return $conditions;
242
	}
277
	}
243
 
278
 
244
	private function getListeNumNom() {
279
	private function getListeNumNom() {
245
		$nnListe = null;
280
		$nnListe = null;
246
		$refTax = self::CODE_REFTAX_DEFAUT;
281
		$refTax = self::CODE_REFTAX_DEFAUT;
247
		if (isset($this->taxonsDemandes[$refTax])) {
282
		if (isset($this->taxonsDemandes[$refTax])) {
248
			$nnProteges = array();
283
			$nnProteges = array();
249
			if (isset($this->taxonsDemandes[$refTax]['nn'])) {
284
			if (isset($this->taxonsDemandes[$refTax]['nn'])) {
250
				foreach ($this->taxonsDemandes[$refTax]['nn'] as $nn) {
285
				foreach ($this->taxonsDemandes[$refTax]['nn'] as $nn) {
251
					$nnProteges[] = $this->Bdd->proteger($nn);
286
					$nnProteges[] = $this->Bdd->proteger($nn);
252
				}
287
				}
253
				$nnListe = implode(',', $nnProteges);
288
				$nnListe = implode(',', $nnProteges);
254
			}
289
			}
255
		}
290
		}
256
		return $nnListe;
291
		return $nnListe;
257
	}
292
	}
258
 
293
 
259
	private function getListeNumTax() {
294
	private function getListeNumTax() {
260
		$ntListe = null;
295
		$ntListe = null;
261
		$refTax = self::CODE_REFTAX_DEFAUT;
296
		$refTax = self::CODE_REFTAX_DEFAUT;
262
		if (isset($this->taxonsDemandes[$refTax])) {
297
		if (isset($this->taxonsDemandes[$refTax])) {
263
			$ntProteges = array();
298
			$ntProteges = array();
264
			if (isset($this->taxonsDemandes[$refTax]['nt'])) {
299
			if (isset($this->taxonsDemandes[$refTax]['nt'])) {
265
				foreach ($this->taxonsDemandes[$refTax]['nt'] as $nt) {
300
				foreach ($this->taxonsDemandes[$refTax]['nt'] as $nt) {
266
					$ntProteges[] = $this->Bdd->proteger($nt);
301
					$ntProteges[] = $this->Bdd->proteger($nt);
267
				}
302
				}
268
				$ntListe = implode(',', $ntProteges);
303
				$ntListe = implode(',', $ntProteges);
269
			}
304
			}
270
		}
305
		}
271
		return $ntListe;
306
		return $ntListe;
272
	}
307
	}
273
 
308
 
274
	private function genererSVG() {
309
	private function genererSVG() {
275
		$dom = new DOMDocument('1.0', 'UTF-8');
310
		$dom = new DOMDocument('1.0', 'UTF-8');
276
		$dom->validateOnParse = true;
311
		$dom->validateOnParse = true;
277
 
312
 
278
		$fichierCarteSvg = $this->cheminCartesBase.self::CARTE_DEFAUT.'.svg';
313
		$fichierCarteSvg = $this->cheminCartesBase.self::CARTE_DEFAUT.'.svg';
279
		$dom->load($fichierCarteSvg);
314
		$dom->load($fichierCarteSvg);
280
 
315
 
281
		$racineElement = $dom->documentElement;
316
		$racineElement = $dom->documentElement;
282
		$racineElement->setAttribute('width', $this->imgLargeur);
317
		$racineElement->setAttribute('width', $this->imgLargeur);
283
 
318
 
284
		$css = $this->creerCssCarte();
319
		$css = $this->creerCssCarte();
285
		$styleElement = $dom->getElementsByTagName('style')->item(0);
320
		$styleElement = $dom->getElementsByTagName('style')->item(0);
286
		$css = $styleElement->nodeValue.$css;
321
		$css = $styleElement->nodeValue.$css;
287
		$txtCss = $dom->createCDATASection($css);
322
		$txtCss = $dom->createCDATASection($css);
288
		$styleElement->nodeValue = '';
323
		$styleElement->nodeValue = '';
289
		$styleElement->appendChild($txtCss);
324
		$styleElement->appendChild($txtCss);
290
 
325
 
291
		$titre = $this->creerTitre();
326
		$titre = $this->creerTitre();
292
		$titreCdata = $dom->createCDATASection($titre);
327
		$titreCdata = $dom->createCDATASection($titre);
293
		$titreElement = $dom->getElementsByTagName('title')->item(0);
328
		$titreElement = $dom->getElementsByTagName('title')->item(0);
294
		$titreElement->nodeValue = '';
329
		$titreElement->nodeValue = '';
295
		$titreElement->appendChild($titreCdata);
330
		$titreElement->appendChild($titreCdata);
296
 
331
 
297
		$taxonTitre = $this->creerTitreTaxon();
332
		$taxonTitre = $this->creerTitreTaxon();
298
		$taxonCdata = $dom->createCDATASection($taxonTitre);
333
		$taxonCdata = $dom->createCDATASection($taxonTitre);
299
		$xpath = new DOMXPath($dom);
334
		$xpath = new DOMXPath($dom);
300
		$taxonTitreEl = $xpath->query("//*[@id='titre-taxon']")->item(0);
335
		$taxonTitreEl = $xpath->query("//*[@id='titre-taxon']")->item(0);
301
		$taxonTitreEl->nodeValue = '';
336
		$taxonTitreEl->nodeValue = '';
302
		$taxonTitreEl->setAttribute('title', $taxonTitre);
337
		$taxonTitreEl->setAttribute('title', $taxonTitre);
303
		$taxonTitreEl->appendChild($taxonCdata);
338
		$taxonTitreEl->appendChild($taxonCdata);
304
 
339
 
305
		$svg = $dom->saveXML();
340
		$svg = $dom->saveXML();
306
		return $svg;
341
		return $svg;
307
	}
342
	}
308
 
343
 
309
	private function creerCssCarte() {
344
	private function creerCssCarte() {
310
		$css = '';
345
		$css = '';
311
		$this->getZonesPriorites();
346
		$this->getZonesPriorites();
312
		$zonesCouleurs = $this->getZonesCouleurs();
347
		$zonesCouleurs = $this->getZonesCouleurs();
313
		foreach ($zonesCouleurs as $couleur => $zonesClasses) {
348
		foreach ($zonesCouleurs as $couleur => $zonesClasses) {
314
			$classes = implode(', ', $zonesClasses);
349
			$classes = implode(', ', $zonesClasses);
315
			$css .= "$classes{\nfill:$couleur;\n}\n";
350
			$css .= "$classes{\nfill:$couleur;\n}\n";
316
		}
351
		}
317
		return $css;
352
		return $css;
318
	}
353
	}
319
 
354
 
320
	private function getZonesPriorites() {
355
	private function getZonesPriorites() {
321
		$this->zones = array();
356
		$this->zones = array();
322
		$zonesPrioritaires = array();
357
		$zonesPrioritaires = array();
323
		foreach ($this->donnees as $donnee) {
358
		foreach ($this->donnees as $donnee) {
324
			foreach ($donnee as $zoneId => $codeLegende) {
359
			foreach ($donnee as $zoneId => $codeLegende) {
325
				if (preg_match('/^[0-9][0-9ab]$/i', $zoneId)) {
360
				if (preg_match('/^[0-9][0-9ab]$/i', $zoneId)) {
326
					if (array_key_exists($codeLegende, $this->priorites)) {
361
					if (array_key_exists($codeLegende, $this->priorites)) {
327
						$priorite = $this->priorites[$codeLegende];
362
						$priorite = $this->priorites[$codeLegende];
328
						if (array_key_exists($zoneId, $zonesPrioritaires) == false) {
363
						if (array_key_exists($zoneId, $zonesPrioritaires) == false) {
329
							$zonesPrioritaires[$zoneId] = 0;
364
							$zonesPrioritaires[$zoneId] = 0;
330
						}
365
						}
331
						if ($priorite > $zonesPrioritaires[$zoneId]) {
366
						if ($priorite > $zonesPrioritaires[$zoneId]) {
332
							$zonesPrioritaires[$zoneId] = $priorite;
367
							$zonesPrioritaires[$zoneId] = $priorite;
333
							$this->zones[$zoneId] = $codeLegende;
368
							$this->zones[$zoneId] = $codeLegende;
334
						}
369
						}
335
					}
370
					}
336
				}
371
				}
337
			}
372
			}
338
		}
373
		}
339
	}
374
	}
340
 
375
 
341
	private function getZonesCouleurs() {
376
	private function getZonesCouleurs() {
342
		$zones = array();
377
		$zones = array();
-
 
378
		ksort($this->zones);
343
		foreach ($this->zones as $zoneId => $codeLegende) {
379
		foreach ($this->zones as $zoneId => $codeLegende) {
344
			if (array_key_exists($codeLegende, $this->legende)) {
380
			if (array_key_exists($codeLegende, $this->legende)) {
345
				$couleur = $this->legende[$codeLegende];
381
				$couleur = $this->legende[$codeLegende];
346
				$zones[$couleur][] = strtolower(".departement$zoneId");
382
				$zones[$couleur][] = strtolower('.departement'.sprintf('%02s', $zoneId));
347
			}
383
			}
348
		}
384
		}
349
		return $zones;
385
		return $zones;
350
	}
386
	}
351
 
387
 
352
	private function creerTitre() {
388
	private function creerTitre() {
353
		$titre = "Carte en cours d'élaboration pour ".$this->creerTitreTaxon();
389
		$titre = "Carte en cours d'élaboration pour ".$this->creerTitreTaxon();
354
		return $titre;
390
		return $titre;
355
	}
391
	}
356
 
392
 
357
	private function creerTitreTaxon() {
393
	private function creerTitreTaxon() {
358
		$noms = array();
394
		$noms = array();
359
		foreach ($this->donnees as $donnee) {
395
		foreach ($this->donnees as $donnee) {
360
			$noms[] = $donnee['nom_sci'];
396
			$noms[] = $donnee['nom_sci'];
361
		}
397
		}
362
		$titre = implode(', ', $noms);
398
		$titre = implode(', ', $noms);
363
		return $titre;
399
		return $titre;
364
	}
400
	}
365
 
401
 
366
	private function convertirEnPNG($svg) {
402
	private function convertirEnPNG($svg) {
367
		$png = null;
403
		$png = null;
368
		if (extension_loaded('imagick')) {
404
		if (extension_loaded('imagick')) {
369
			$png = $this->convertirEnPNGAvecImageMagick($svg);
405
			$png = $this->convertirEnPNGAvecImageMagick($svg);
370
		} else {
406
		} else {
371
			$message = "Impossible de générer l'image sur le serveur. Extenssion ImageMagick abscente.";
407
			$message = "Impossible de générer l'image sur le serveur. Extenssion ImageMagick abscente.";
372
			$code = RestServeur::HTTP_CODE_ERREUR;
408
			$code = RestServeur::HTTP_CODE_ERREUR;
373
			throw new Exception($message, $code);
409
			throw new Exception($message, $code);
374
		}
410
		}
375
		return $svg;
411
		return $svg;
376
	}
412
	}
377
 
413
 
378
	private function convertirEnPNGAvecImageMagick($svg) {
414
	private function convertirEnPNGAvecImageMagick($svg) {
379
		$convertisseur = new Imagick();
415
		$convertisseur = new Imagick();
380
		$convertisseur->readImageBlob($svg);
416
		$convertisseur->readImageBlob($svg);
381
		$convertisseur->setImageFormat("png24");
417
		$convertisseur->setImageFormat("png24");
382
		$convertisseur->resizeImage($this->imgLargeur, $this->imgHauteur, imagick::FILTER_LANCZOS, 0);
418
		$convertisseur->resizeImage($this->imgLargeur, $this->imgHauteur, imagick::FILTER_LANCZOS, 0);
383
		$png = $convertisseur->getImageBlob();
419
		$png = $convertisseur->getImageBlob();
384
		$convertisseur->clear();
420
		$convertisseur->clear();
385
		$convertisseur->destroy();
421
		$convertisseur->destroy();
386
		return $png;
422
		return $png;
387
	}
423
	}
388
 
424
 
389
	public function getParametreTableau($cle) {
425
	public function getParametreTableau($cle) {
390
		$tableau = array();
426
		$tableau = array();
391
		$parametre = $this->config[$cle];
427
		$parametre = $this->config[$cle];
392
		if (empty($parametre) === false) {
428
		if (empty($parametre) === false) {
393
			$tableauPartiel = explode(',', $parametre);
429
			$tableauPartiel = explode(',', $parametre);
394
			$tableauPartiel = array_map('trim', $tableauPartiel);
430
			$tableauPartiel = array_map('trim', $tableauPartiel);
395
			foreach ($tableauPartiel as $champ) {
431
			foreach ($tableauPartiel as $champ) {
396
				if (strpos($champ, '=') === false) {
432
				if (strpos($champ, '=') === false) {
397
					$tableau[] = trim($champ);
433
					$tableau[] = trim($champ);
398
				} else {
434
				} else {
399
					list($cle, $val) = explode('=', $champ);
435
					list($cle, $val) = explode('=', $champ);
400
					$tableau[trim($cle)] = trim($val);
436
					$tableau[trim($cle)] = trim($val);
401
				}
437
				}
402
			}
438
			}
403
		}
439
		}
404
		return $tableau;
440
		return $tableau;
405
	}
441
	}
406
}
442
}
407
?>
443
?>