Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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