Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
712 jpm 1
<?php
2
/**
2688 mathias 3
 * Widget fournissant des interfaces de saisie simplifiée pour différent projets
712 jpm 4
 *
5
 * Cas d'utilisation et documentation :
2688 mathias 6
 * @link http://www.tela-botanica.org/wikini/AideCarnetEnLigne/wakka.php?wiki=AideCELWidgetSaisie
712 jpm 7
 *
8
 * Paramètres :
2688 mathias 9
 * - projet [par défaut : defaut] : indique le mot-clé à associer aux obs saisies; si un widget de saisie personnalisé
10
 * 		portant ce nom existe, il sera chargé
11
 * - mission [par défaut : vide] : permet de charger un "sous-widget", dans le cas où un widget personnalisé
12
 * 		est associé au projet, et ce widget accepte des sous-widgets (ex: "missions-flore")
712 jpm 13
 *
2408 jpm 14
 * @author Mathias CHOUET <mathias@tela-botanica.org>
15
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
16
 * @author Aurelien PERONNET <aurelien@tela-botanica.org>
17
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
18
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
2688 mathias 19
 * @copyright 1999-2015 Tela Botanica <accueil@tela-botanica.org>
712 jpm 20
 */
21
class Saisie extends WidgetCommun {
1050 jpm 22
 
712 jpm 23
	const DS = DIRECTORY_SEPARATOR;
1345 aurelien 24
	const PROJET_DEFAUT = 'defaut';
1580 jpm 25
	const WS_SAISIE = 'CelWidgetSaisie';
1888 mathias 26
	const WS_UPLOAD = 'CelWidgetUploadImageTemp';
1580 jpm 27
	const WS_OBS = 'CelObs';
28
	const WS_NOM = 'noms';
29
	const EFLORE_API_VERSION = '0.1';
2929 delphine 30
	const REFERENTIEL_DEFAUT = 'bdtfxr';
1050 jpm 31
 
2906 aurel 32
	/** référentiel utilisé pour la complétion des noms scientifiques */
2688 mathias 33
	protected $ns_referentiel;
34
	/** mot-clé associé aux saisies, et template personnalisé si appliquable */
35
	protected $projet = null;
36
	protected $configProjet = null;
37
	protected $configMission = null;
2805 mathias 38
	protected $mission = null;
2786 mathias 39
	/** langue (traduction), charge un template de la forme "defaut_en.tpl.html" */
40
	protected $langue = null;
1050 jpm 41
 
712 jpm 42
	/**
2688 mathias 43
	 * Amorçage du widget
712 jpm 44
	 */
45
	public function executer() {
2688 mathias 46
		// paramètres par défaut
47
		$this->ns_referentiel = self::REFERENTIEL_DEFAUT;
48
		$this->projet = self::PROJET_DEFAUT;
712 jpm 49
 
2688 mathias 50
		// définition du projet / mission
51
		if (isset($this->parametres['projet']) && trim($this->parametres['projet']) != "") {
52
			$projets = explode(',', $this->parametres['projet']);
2577 aurelien 53
			$this->projet = strtolower($projets[0]);
2500 mathias 54
		}
719 jpm 55
		$this->chargerConfigProjet();
1050 jpm 56
 
3042 mathias 57
		// définition de la langue, en mode souple
58
		if (isset($this->parametres['lang'])) {
59
			$this->langue = $this->parametres['lang'];
60
		}
61
 
2688 mathias 62
		// exécution du service (le widget entier ou une sous-partie, par ex "Taxons")
63
		$retour = null;
64
		$service = isset($this->parametres['service']) ? $this->parametres['service'] : 'widget';
719 jpm 65
		$methode = $this->traiterNomMethodeExecuter($service);
712 jpm 66
		if (method_exists($this, $methode)) {
67
			$retour = $this->$methode();
68
		} else {
2688 mathias 69
			$this->messages[] = "Le service '$methode' n'est pas disponible.";
712 jpm 70
		}
71
 
2688 mathias 72
		// injection des données dans le squelette
1050 jpm 73
		$contenu = null;
74
		$mime = null;
75
		if (is_array($retour) && array_key_exists('squelette', $retour)) {
719 jpm 76
			$ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html';
2786 mathias 77
			// Suffixe de template pour la langue - fr par défaut @TODO configurer ça un jour
78
			$suffixeLangue = "";
3042 mathias 79
			// Si "nolang" n'est pas vide, on ne cherchera pas de squelette spécifique à la langue en cours
80
			if ($this->langue != null && $this->langue != "fr" && empty($retour['nolang'])) {
2786 mathias 81
				$suffixeLangue = "_" . $this->langue;
82
			}
83
			// Template par défaut ou spécifique
2406 jpm 84
			if ($this->projetASquelette()) {
2786 mathias 85
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet.self::DS.$retour['squelette'].$suffixeLangue.$ext;
1475 aurelien 86
			} else {
2786 mathias 87
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'defaut'.self::DS.'defaut'.$suffixeLangue.$ext;
1475 aurelien 88
			}
712 jpm 89
			$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
1050 jpm 90
			$mime = isset($retour['mime']) ? $retour['mime'] : null;
91
		} else {
92
			if (count($this->messages) == 0) {
2688 mathias 93
				$this->messages[] = "La méthode du sous-service ne renvoie pas les données dans le bon format";
1050 jpm 94
			}
2688 mathias 95
			$contenu = 'Un problème est survenu : ' . print_r($this->messages, true);
712 jpm 96
		}
1050 jpm 97
 
2688 mathias 98
		// envoi de la page
1050 jpm 99
		$this->envoyer($contenu, $mime);
712 jpm 100
	}
1050 jpm 101
 
2688 mathias 102
	/**
103
	 * Charge le fichier de configuration associé au projet : configurations/nomduprojet.ini
104
	 * Si une mission est définie, charge séparément la section de la configuration concernant
105
	 * cette mission : [nommission]
106
	 */
107
	protected function chargerConfigProjet() {
719 jpm 108
		$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini';
109
		if (file_exists($fichier_config)) {
2408 jpm 110
			if ($this->configProjet = parse_ini_file($fichier_config, true)) {
111
				if (isset($_GET['mission'])) {
2805 mathias 112
					$this->mission = strtolower(trim($_GET['mission']));
113
					if (isset($this->configProjet[$this->mission])) {
114
						$this->configMission = $this->configProjet[$this->mission];
2408 jpm 115
					}
2688 mathias 116
				}
2408 jpm 117
			} else {
2688 mathias 118
				$this->messages[] = "Le fichier de configuration '$fichier_config' n'a pu être chargé.";
1050 jpm 119
			}
719 jpm 120
		} else {
2688 mathias 121
			$this->debug[] = "Le fichier de configuration '$fichier_config' n'existe pas.";
719 jpm 122
		}
123
	}
1526 jpm 124
 
2688 mathias 125
	/**
126
	 * Retourne true si le dossier du projet courant existe, false sinon
127
	 * @return boolean
128
	 */
129
	protected function projetASquelette() {
2408 jpm 130
		return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet);
1476 aurelien 131
	}
1050 jpm 132
 
2688 mathias 133
	/**
134
	 * Exécution du widget complet
135
	 * @return Ambigous <string, unknown, multitype:string unknown >
136
	 */
1345 aurelien 137
	public function executerWidget() {
1050 jpm 138
		$widget['squelette'] = $this->projet;
139
		$widget['donnees'] = array();
140
		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
141
		$widget['donnees']['url_ws_saisie'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE);
1580 jpm 142
		$widget['donnees']['url_ws_obs'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_OBS);
1888 mathias 143
		$widget['donnees']['url_ws_upload'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_UPLOAD);
1536 jpm 144
		$widget['donnees']['url_ws_annuaire'] = sprintf($this->config['chemins']['baseURLServicesAnnuaireTpl'], 'utilisateur/identite-par-courriel/');
2082 mathias 145
		$widget['donnees']['url_remarques'] = $this->config['chemins']['widgetRemarquesUrl'];
1526 jpm 146
 
2851 mathias 147
		$widget['donnees']['logo'] = $this->getLogoPage();
2408 jpm 148
		$widget['donnees']['titre'] = $this->getTitrePage();
2805 mathias 149
		$widget['donnees']['nom_mission'] = $this->getNomMissionFlore();
1526 jpm 150
 
2906 aurel 151
		$widget['donnees']['zone_geo'] = $this->getZoneGeo();
2916 mathias 152
		$widget['donnees']['groupe_zones_geo'] = $this->getGroupeZonesGeo();
2851 mathias 153
		$widget['donnees']['referentiel_impose'] = $this->getReferentielImpose();
154
		$widget['donnees']['espece_imposee'] = false;
155
		$widget['donnees']['nn_espece_defaut'] = '';
156
		$widget['donnees']['nom_sci_espece_defaut'] = '';
2498 aurelien 157
		$widget['donnees']['infos_espece'] = '{}';
2848 mathias 158
 
159
		$widget['donnees']['prod'] = ($this->config['parametres']['modeServeur'] == "prod");
3037 mathias 160
 
161
		$widget['donnees']['cleGoogleMaps'] = $this->config['api']['cleGoogleMaps'];
2498 aurelien 162
 
2406 jpm 163
		$projetsAutorises = $this->transformerEnTableau($this->config['projets']['autorises']);
1526 jpm 164
 
2851 mathias 165
		$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
166
		$urlWsNs = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, $this->ns_referentiel, self::WS_NOM);
167
		$urlWsNsSansRef = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM);
168
		$widget['donnees']['url_ws_autocompletion_ns'] = $urlWsNs;
169
		$widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef;
2498 aurelien 170
		$widget['donnees']['ns_referentiel'] = $this->ns_referentiel;
2744 aurelien 171
 
2916 mathias 172
		$widget['donnees']['url_ws_coord_search_tpl'] = $this->config['chemins']['serviceCoordSearchUrl'];
2744 aurelien 173
		$widget['donnees']['url_ws_trace_rue_tpl'] = $this->config['chemins']['serviceTraceRueUrl'];
2851 mathias 174
 
175
		if ($this->especeEstImposee()) {
176
			$nnEspeceImposee = $this->getNnEspeceImposee();
177
			$nom = $this->chargerInfosTaxon($nnEspeceImposee);
178
			$widget['donnees']['espece_imposee'] = true;
179
			$widget['donnees']['nn_espece_defaut'] = $nnEspeceImposee;
180
			$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_complet'];
181
			$widget['donnees']['infos_espece'] = $this->array2js($nom, true);
1536 jpm 182
		}
2406 jpm 183
 
184
		$projetsAListeDeNoms = $this->transformerEnTableau($this->config['projets']['liste_noms']);
3017 delphine 185
		if (in_array($this->projet, $projetsAListeDeNoms) && !$this->especeEstImposee()) {
2406 jpm 186
			$projetsAListeDeNomsSciEtVerna = $this->transformerEnTableau($this->config['projets']['liste_noms_sci_et_verna']);
187
			if (in_array($this->projet, $projetsAListeDeNomsSciEtVerna)) {
1613 jpm 188
				$widget['donnees']['taxons'] = $this->recupererListeNoms();
189
			} else {
190
				$widget['donnees']['taxons'] = $this->recupererListeNomsSci();
2328 jpm 191
			}
2406 jpm 192
		}
193
 
194
		// Chargement de la liste des milieux issues du fichier .ini du projet
195
		$projetsAListeDeMilieux = $this->transformerEnTableau($this->config['projets']['liste_milieux']);
196
		if (in_array($this->projet, $projetsAListeDeMilieux)) {
1345 aurelien 197
			$widget['donnees']['milieux'] = $this->parserMilieux();
719 jpm 198
		}
2406 jpm 199
 
200
		return $widget;
719 jpm 201
	}
1526 jpm 202
 
2688 mathias 203
	protected function getTitrePage() {
2408 jpm 204
		$titre = 'defaut';
205
		if (isset($this->configProjet['titre_page'])) {
206
			$titre = $this->configProjet['titre_page'];
207
		}
208
		if (isset($this->configMission['titre_page'])) {
209
			$titre = $this->configMission['titre_page'];
210
		}
211
		if (isset($_GET['titre'])) {
212
			$titre = $_GET['titre'];
213
		}
2851 mathias 214
		if ($titre === 0) { // wtf ?
2408 jpm 215
			$titre = '';
216
		}
217
		return $titre;
1475 aurelien 218
	}
1050 jpm 219
 
2851 mathias 220
	protected function getLogoPage() {
221
		$logo = 'defaut';
222
		if (isset($this->configProjet['logo_page'])) {
223
			$logo = $this->configProjet['logo_page'];
224
		}
225
		if (isset($this->configMission['logo_page'])) {
226
			$logo = $this->configMission['logo_page'];
227
		}
228
		if (isset($_GET['logo'])) {
229
			$logo = $_GET['logo'];
230
		}
231
		return $logo;
232
	}
2916 mathias 233
 
234
	protected function getGroupeZonesGeo() {
235
		$groupe = null;
236
		if (isset($_GET['groupe_zones_geo'])) {
237
			$groupe = $_GET['groupe_zones_geo'];
238
		}
239
		return $groupe;
240
	}
241
 
2906 aurel 242
	protected function getZoneGeo() {
243
		$zone_geo = null;
244
		if (isset($_GET['zone_geo'])) {
245
			$zone_geo = $_GET['zone_geo'];
246
		}
247
		return $zone_geo;
248
	}
2851 mathias 249
 
250
	protected function getReferentielImpose() {
2869 mathias 251
		$referentiel_impose = true;
2851 mathias 252
		if (!empty($_GET['referentiel']) && $_GET['referentiel'] != "autre") {
253
			$this->ns_referentiel = $_GET['referentiel'];
2869 mathias 254
		} else if (isset($this->configProjet['referentiel'])) {
255
			$this->ns_referentiel = $this->configProjet['referentiel'];
256
		} else if (isset($this->configMission['referentiel'])) {
257
			$this->ns_referentiel = $this->configMission['referentiel'];
258
		} else {
259
			$referentiel_impose = false;
2851 mathias 260
		}
261
		return $referentiel_impose;
262
	}
263
 
2688 mathias 264
	/**
2805 mathias 265
	 * Un nom un peu plus sympatoche à afficher que juste le mot-clef associé; s'il
266
	 * n'est pas défini dans la config, on prend le mot-clef tout de même
267
	 */
268
	protected function getNomMissionFlore() {
269
		$nom = $this->mission;
270
		if (isset($this->configMission['nom_mission'])) {
271
			$nom = $this->configMission['nom_mission'];
272
		}
273
		return $nom;
274
	}
275
 
276
	/**
2688 mathias 277
	 * Remplit un fichier JS avec une variable contenant une liste restreinte de taxons,
278
	 * pour certains projets
279
	 * @return string
280
	 */
2328 jpm 281
	public function executerTaxons() {
282
		$widget['squelette'] = $this->projet.'_taxons';
283
		$widget['squelette_ext'] = '.tpl.js';
1613 jpm 284
		$widget['donnees'] = array();
1629 jpm 285
		$nomsAAfficher = $this->recupererListeNomsSci();
286
		$taxons_tries = array();
1691 raphael 287
		foreach ($nomsAAfficher as $taxon) {
1629 jpm 288
			$taxons_tries[$taxon['num_nom_sel']] = $taxon;
2328 jpm 289
		}
290
		$widget['donnees']['taxons'] = json_encode($taxons_tries);
3042 mathias 291
		//echo "<pre>"; var_dump($widget); echo "</pre>";
292
		// Le squelette n'est pas traduit, seules les données de taxons le sont
293
		$widget['nolang'] = true;
294
 
2328 jpm 295
		return $widget;
296
	}
1613 jpm 297
 
2688 mathias 298
	/**
299
	 * Trie par nom français les taxons lus dans le fichier tsv
300
	 */
301
	protected function recupererListeNomsSci() {
1613 jpm 302
		$taxons = $this->recupererListeTaxon();
303
		if (is_array($taxons)) {
304
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
305
		}
1629 jpm 306
		return $taxons;
1613 jpm 307
	}
308
 
2688 mathias 309
	/**
310
	 * @TODO documenter
311
	 * @return array
312
	 */
313
	protected function recupererListeNoms() {
1613 jpm 314
		$taxons = $this->recupererListeTaxon();
315
		$nomsAAfficher = array();
316
		$nomsSpeciaux = array();
317
		if (is_array($taxons)) {
318
			foreach ($taxons as $taxon) {
319
				$nomSciTitle = $taxon['nom_ret'].
320
					($taxon['nom_fr'] != '' ? ' - '.$taxon['nom_fr'] : '' ).
321
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
322
				$nomFrTitle = $taxon['nom_sel'].
323
					($taxon['nom_ret'] != $taxon['nom_sel']? ' - '.$taxon['nom_ret'] : '' ).
324
					($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
325
 
326
				if ($taxon['groupe'] == 'special') {
327
					$nomsSpeciaux[] = array(
328
						'num_nom' => $taxon['num_nom_sel'],
329
						'nom_a_afficher' => $taxon['nom_fr'],
330
						'nom_a_sauver' => $taxon['nom_sel'],
331
						'nom_title' => $nomSciTitle,
332
						'nom_type' => 'nom-special');
333
				} else {
334
					$nomsAAfficher[] = array(
335
						'num_nom' => $taxon['num_nom_sel'],
336
						'nom_a_afficher' => $taxon['nom_sel'],
337
						'nom_a_sauver' => $taxon['nom_sel'],
338
						'nom_title' => $nomSciTitle,
339
						'nom_type' => 'nom-sci');
340
					$nomsAAfficher[] = array(
341
						'num_nom' => $taxon['num_nom_sel'],
342
						'nom_a_afficher' => $taxon['nom_fr'],
343
						'nom_a_sauver' => $taxon['nom_fr'],
344
						'nom_title' => $nomFrTitle,
345
						'nom_type' => 'nom-fr');
346
				}
347
			}
348
			$nomsAAfficher = self::trierTableauMd($nomsAAfficher, array('nom_a_afficher' => SORT_ASC));
349
			$nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
350
		}
351
		return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
352
	}
2328 jpm 353
 
2688 mathias 354
	/**
355
	 * Lit une liste de taxons depuis un fichier tsv fourni
356
	 */
357
	protected function recupererListeTaxon() {
358
		$taxons = array();
3042 mathias 359
		$fichier_tsv = null;
3017 delphine 360
		if ($this->projet == 'missions-flore') {
361
			$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_'.$this->mission.'_taxons.tsv';
362
		} else {
3042 mathias 363
			// recherche d'un fichier traduit (pour les noms vernaculaires)
364
			$suffixeLangue = "";
365
			if ($this->langue != null && $this->langue != "fr") {
366
				$suffixeLangue = "_" . $this->langue;
367
				$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons'.$suffixeLangue.'.tsv';
368
			}
369
			// si le fichier de taxons traduit n'est pas disponible ou qu'on n'a
370
			// pas demandé de langue particulière, on se rabat sur celui par défaut
371
			if (! file_exists($fichier_tsv)) {
372
				$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv';
373
			}
3017 delphine 374
		}
2328 jpm 375
		if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) {
712 jpm 376
			$taxons = $this->decomposerFichierTsv($fichier_tsv);
2328 jpm 377
		} else {
378
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
379
		}
380
		return $taxons;
712 jpm 381
	}
1050 jpm 382
 
2688 mathias 383
	/**
384
	 * Découpe un fihcier tsv
385
	 */
386
	protected function decomposerFichierTsv($fichier, $delimiter = "\t"){
2328 jpm 387
		$header = null;
388
		$data = array();
389
		if (($handle = fopen($fichier, 'r')) !== FALSE) {
390
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
391
				if (!$header) {
392
					$header = $row;
393
				} else {
394
					$data[] = array_combine($header, $row);
395
				}
396
			}
397
			fclose($handle);
398
		}
399
		return $data;
400
	}
401
 
2688 mathias 402
	/**
403
	 * Récupère la liste des milieux depuis la section [milieux] de la configuration
404
	 * du projet, si elle existe
405
	 * @return array
406
	 */
407
	protected function parserMilieux() {
1050 jpm 408
		$infosMilieux = array();
1536 jpm 409
		if (isset($this->configProjet['milieux'])) {
410
			$milieux = explode('|', $this->configProjet['milieux']);
411
			foreach ($milieux as $milieu) {
2328 jpm 412
				$milieu = trim($milieu);
1536 jpm 413
				$details = explode(';', $milieu);
414
				if (isset($details[1])) {
415
					$infosMilieux[$details[0]] = $details[1];
416
				} else {
417
					$infosMilieux[$details[0]] = '';
418
				}
1050 jpm 419
			}
1536 jpm 420
			ksort($infosMilieux);
1050 jpm 421
		}
422
		return $infosMilieux;
423
	}
1526 jpm 424
 
2688 mathias 425
	/**
426
	 * Retourne true si le widget est restreint à une espèce, false sinon
427
	 * @return boolean
428
	 */
429
	protected function especeEstImposee() {
430
		return ((isset($_GET['num_nom']) && $_GET['num_nom'] != '')
2408 jpm 431
			|| isset($this->configProjet['sp_imposee']) || isset($this->configMission['sp_imposee']));
1418 aurelien 432
	}
1526 jpm 433
 
2688 mathias 434
	/**
435
	 * Retourne le numéro nomenclatural (nn) de l'espèce imposée si tel est
436
	 * le cas, null sinon
437
	 * @return string
438
	 */
439
	protected function getNnEspeceImposee() {
2328 jpm 440
		$nn = null;
441
		if (isset($_GET['num_nom']) && is_numeric($_GET['num_nom'])) {
442
			$nn = $_GET['num_nom'];
443
		} else if (isset($this->configProjet['sp_imposee'])) {
444
			$nn = $this->configProjet['sp_imposee'];
2408 jpm 445
		} else if (isset($this->configMission['sp_imposee'])) {
446
			$nn = $this->configMission['sp_imposee'];
2328 jpm 447
		}
448
		return $nn;
449
	}
450
 
2688 mathias 451
	/**
452
	 * Consulte un webservice pour obtenir des informations sur le taxon dont le
453
	 * numéro nomenclatural est $num_nom (ce sont donc plutôt des infos sur le nom
454
	 * et non le taxon?)
455
	 * @param string|int $num_nom
456
	 * @return array
457
	 */
458
	protected function chargerInfosTaxon($num_nom) {
2408 jpm 459
		$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->ns_referentiel, $num_nom);
1418 aurelien 460
		$infos = json_decode(file_get_contents($url_service_infos));
2688 mathias 461
		// trop de champs injectés dans les infos espèces peuvent
1909 raphael 462
		// faire planter javascript
2801 mathias 463
		$champs_a_garder = array('id', 'nom_sci','nom_sci_complet', 'nom_complet',
2839 mathias 464
			'famille','nom_retenu.id', 'nom_retenu_complet', 'num_taxonomique');
1419 aurelien 465
		$resultat = array();
1539 jpm 466
		if (isset($infos) && !empty($infos)) {
1419 aurelien 467
			$infos = (array)$infos;
2367 jpm 468
			if (isset($infos['nom_sci']) && $infos['nom_sci'] != '') {
1909 raphael 469
				$resultat = array_intersect_key($infos, array_flip($champs_a_garder));
470
				$resultat['retenu'] = ($infos['id'] == $infos['nom_retenu.id']) ? "true" : "false";
1916 jpm 471
			}
1419 aurelien 472
		}
1418 aurelien 473
		return $resultat;
474
	}
1050 jpm 475
 
2688 mathias 476
	/**
477
	 * Convertit un tableau PHP en Javascript - @WTF pourquoi ne pas faire un json_encode ?
478
	 * @param array $array
479
	 * @param boolean $show_keys
480
	 * @return une portion de JSON représentant le tableau
481
	 */
482
	protected function array2js($array,$show_keys) {
1536 jpm 483
		$tableauJs = '{}';
484
		if (!empty($array)) {
485
			$total = count($array) - 1;
486
			$i = 0;
487
			$dimensions = array();
488
			foreach ($array as $key => $value) {
489
				if (is_array($value)) {
490
					$dimensions[$i] = array2js($value,$show_keys);
491
					if ($show_keys) {
492
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
493
					}
494
				} else {
495
					$dimensions[$i] = '"'.addslashes($value).'"';
496
					if ($show_keys) {
497
						$dimensions[$i] = '"'.$key.'":'.$dimensions[$i];
498
					}
1526 jpm 499
				}
1536 jpm 500
				if ($i == 0) {
501
					$dimensions[$i] = '{'.$dimensions[$i];
1526 jpm 502
				}
1536 jpm 503
				if ($i == $total) {
504
					$dimensions[$i].= '}';
505
				}
506
				$i++;
1526 jpm 507
			}
1536 jpm 508
			$tableauJs = implode(',', $dimensions);
1526 jpm 509
		}
1536 jpm 510
		return $tableauJs;
1526 jpm 511
	}
2906 aurel 512
}