Subversion Repositories eFlore/Applications.cel

Rev

Rev 3543 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3228 delphine 1
 <?php
3120 delphine 2
// declare(encoding='UTF-8');
3
/**
4
 * Service affichant les dernières photo publiques du CEL ouvrable sous forme de diaporama.
5
 * Encodage en entrée : utf8
6
 * Encodage en sortie : utf8
7
 *
8
 * Cas d'utilisation et documentation :
9
 * @link http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=AideCELWidgetPhoto
10
 *
11
 * Paramètres :
12
 * ===> extra = booléen (1 ou 0)  [par défaut : 1]
13
 * Affiche / Cache la vignette en taille plus importante au bas du widget.
14
 * ===> vignette = [0-9]+,[0-9]+  [par défaut : 4,3]
15
 * Indique le nombre de vignette par ligne et le nombre de ligne.
16
 *
3228 delphine 17
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
18
 * @license	GPL v3 <http://www.gnu.org/licenses/gpl.txt>
19
 * @license	CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
20
 * @version	$Id$
21
 * @copyright	Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
3120 delphine 22
 */
23
class Saisie2 extends WidgetCommun {
3239 idir 24
 
3228 delphine 25
	const DS = DIRECTORY_SEPARATOR;
26
	const SERVICE_DEFAUT = 'saisie';
3240 idir 27
	const WS_SAISIE = 'CelWidgetSaisie';
28
	const WS_UPLOAD = 'CelWidgetUploadImageTemp';
29
	const WS_OBS = 'CelObs';
3425 idir 30
	const WS_COORD = 'CoordSearch';
3228 delphine 31
	const LANGUE_DEFAUT = 'fr';
32
	const PROJET_DEFAUT = 'base';
33
	const WS_NOM = 'noms';
34
	const EFLORE_API_VERSION = '0.1';
3425 idir 35
	const WIDGETS_SPECIAUX = ['tb_aupresdemonarbre','tb_streets','tb_lichensgo'];
36
	const SQUELETTES_SPECIAUX = ['arbres','plantes','lichens'];
37
	const WS_OBS_LIST = 'InventoryObservationList';
38
	const WS_IMG_LIST = 'celImage';
3228 delphine 39
	private $cel_url_tpl = null;
40
	/** Si spécifié, on ajoute une barre de navigation inter-applications */
41
	private $bar;
42
	//private $parametres_autorises = array('projet', 'type', 'langue', 'order');
43
	private $parametres_autorises = array(
44
			'projet' => 'projet',
45
			'type' => 'type',
46
			'langue' => 'langue',
47
			'order' => 'order'
48
	);
49
	/**
50
	 * Méthode appelée par défaut pour charger ce widget.
51
	 */
52
	public function executer() {
53
		$retour = null;
54
		// Pour la création de l'id du cache nous ne tenons pas compte du paramètre de l'url callback
55
		unset($this->parametres['callback']);
56
		extract($this->parametres);
57
		$this->bar = (isset($bar)) ? $bar : false;
58
		/* Le fichier Framework.php du Framework de Tela Botanica doit être appelé avant tout autre chose dans l'application.
59
		 Sinon, rien ne sera chargé.
60
		 L'emplacement du Framework peut varier en fonction de l'environnement (test, prod...). Afin de faciliter la configuration
61
		 de l'emplacement du Framework, un fichier framework.defaut.php doit être renommé en framework.php et configuré pour chaque installation de
62
		 l'application.
63
		 Chemin du fichier chargeant le framework requis */
64
		$framework = dirname(__FILE__).'/framework.php';
65
		if (!file_exists($framework)) {
66
			$e = "Veuillez paramêtrer l'emplacement et la version du Framework dans le fichier $framework";
67
			trigger_error($e, E_USER_ERROR);
68
		} else {
69
			// Inclusion du Framework
70
			require_once $framework;
71
			// Ajout d'information concernant cette application
72
			Framework::setCheminAppli(__FILE__);// Obligatoire
73
			Framework::setInfoAppli(Config::get('info'));// Optionnel
3239 idir 74
 
3228 delphine 75
		}
76
		$langue = (isset($langue)) ? $langue : self::LANGUE_DEFAUT;
77
		$this->langue = I18n::setLangue($langue);
78
		$this->parametres['langue'] = $langue;
3120 delphine 79
 
3228 delphine 80
		if (!isset($mode)) {
81
			$mode = self::SERVICE_DEFAUT;
82
		}
83
		if (!isset($projet)) {
84
			$this->parametres['projet'] = self::PROJET_DEFAUT;
85
		}
3239 idir 86
 
3228 delphine 87
		$methode = $this->traiterNomMethodeExecuter($mode);
3239 idir 88
		if (method_exists($this, $methode)) {
89
			$retour = $this->$methode();
3228 delphine 90
		} else {
91
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
92
		}
3239 idir 93
 
3257 delphine 94
		$contenu = ''; //print_r($retour);exit;
3228 delphine 95
		if (is_null($retour)) {
96
			$this->messages[] = 'La ressource demandée a retourné une valeur nulle.';
97
		} else {
3239 idir 98
			if (isset($retour['donnees'])) {
3271 idir 99
				// ne pas afficher le projet dans l'url si on est dans saisie de base
3286 idir 100
				$projet_dans_url = ( $this->parametres['projet'] !== 'base') ? '?'. $this->parametres['projet'].'&'.'langue='.$this->parametres['langue'] : '';
3271 idir 101
 
3260 idir 102
				$retour['donnees']['conf_mode'] = $this->config['parametres']['modeServeur'];
3249 idir 103
				$retour['donnees']['prod'] = ($this->config['parametres']['modeServeur'] === 'prod');
3228 delphine 104
				$retour['donnees']['bar'] = $this->bar;
3268 idir 105
				$retour['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], $this->config['manager']['cheminDos']);
3228 delphine 106
				$retour['donnees']['url_ws_annuaire'] = sprintf($this->config['chemins']['baseURLServicesAnnuaireTpl'], 'utilisateur/identite-par-courriel/');
3240 idir 107
				$retour['donnees']['url_ws_saisie'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE);
108
				$retour['donnees']['url_ws_obs'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_OBS);
109
				$retour['donnees']['url_ws_upload'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_UPLOAD);
3286 idir 110
				$retour['donnees']['authTpl'] = $this->config['manager']['authTpl']. $projet_dans_url;
3228 delphine 111
				$retour['donnees']['mode'] = $mode;
3425 idir 112
				$retour['donnees']['langue'] = $langue;
113
				$retour['donnees']['widgets_url'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'],'');
114
				$retour['donnees']['url_ws_obs_list'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_OBS_LIST);
115
				$retour['donnees']['url_ws_cel_imgs'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_IMG_LIST) . '/liste-ids?obsId=';
116
				$retour['donnees']['url_ws_cel_img_url'] = str_replace ( '%s.jpg', '{id}XS', $this->config['chemins']['celImgUrlTpl'] );
117
				$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$retour['squelette'].'.tpl.html';
118
				switch ( $retour['squelette'] ) {
119
					case 'apa':
120
						$retour['donnees']['squelette'] = $this->parametres['projet'];
121
						break;
122
					case 'apaforms':
123
						$retour['donnees']['squelette'] = $this->parametres['squelette'];
124
						break;
125
					default:
126
						$retour['donnees']['squelette'] = $retour['squelette'];
127
						break;
3260 idir 128
				}
3228 delphine 129
				$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
130
			} else {
131
				$this->messages[] = 'Les données à transmettre au squelette sont nulles.';
132
			}
133
		}
134
		$this->envoyer($contenu);
135
	}
3120 delphine 136
 
3239 idir 137
 
3228 delphine 138
	private function executerSaisie() {
139
		$retour = array();
3425 idir 140
		if (in_array($this->parametres['projet'], self::WIDGETS_SPECIAUX) ) {
141
			if (isset($this->parametres['squelette']) && in_array($this->parametres['squelette'], self::SQUELETTES_SPECIAUX) ) {
142
				$retour['squelette'] = 'apaforms';
143
			} else {
144
				$retour['squelette'] = 'apa';
145
			}
146
		} else {
147
			$retour['squelette'] = 'saisie';
148
			$retour['donnees']['general']     = I18n::get('General');
149
			$retour['donnees']['aide']        = I18n::get('Aide');
150
			$retour['donnees']['observateur'] = I18n::get('Observateur');
151
			$retour['donnees']['observation'] = I18n::get('Observation');
152
			$retour['donnees']['image']       = I18n::get('Image');
153
	 		$retour['donnees']['chpsupp']     = I18n::get('Chpsupp');
154
	 		$retour['donnees']['resume']      = I18n::get('Resume');
155
		}
3228 delphine 156
		$retour['donnees']['widget'] = $this->rechercherProjet();
157
		return $retour;
158
	}
3239 idir 159
 
3228 delphine 160
	/* Recherche si le projet existe sinon va chercher les infos de base */
161
	private function rechercherProjet() {
162
		// projet avec un squelette défini (et non juste un mot-clé d'observation)
3239 idir 163
		$estProjetDefini = true;
164
		$tab = array();
3228 delphine 165
		$url = $this->config['manager']['celUrlTpl'].'?projet='.$this->parametres['projet'].'&langue='.$this->parametres['langue'];
3239 idir 166
		$json = $this->getDao()->consulter($url);
3425 idir 167
		if (!in_array($this->parametres['projet'], self::WIDGETS_SPECIAUX)){
168
			if ( $json !== "[]") {
169
				$tab = $this->rechercherChampsSupp();
170
			} else {
171
				$url = $this->config['manager']['celUrlTpl'].'?projet=base&langue='.$this->parametres['langue'];
172
				$json = $this->getDao()->consulter($url);
173
				$estProjetDefini = false;
174
			}
3228 delphine 175
		}
3239 idir 176
		$tableau = json_decode($json, true);
3228 delphine 177
		$tableau = $this->traiterParametres($estProjetDefini, $tableau[0]);
3425 idir 178
		if (
179
			isset($this->parametres['squelette']) &&
180
			($this->parametres['squelette'] === 'plantes' || $this->parametres['squelette'] === 'lichens')
181
		) {
182
			$tableau['type_especes'] = 'liste';
183
			if ( $this->parametres['squelette'] === 'lichens' ) {
184
				$tableau['referentiel'] = 'taxref';
185
			}
186
		}
3228 delphine 187
		$tableau['especes'] = $this->rechercherInfosEspeces($tableau);
3377 idir 188
		if (isset($tableau['type_especes']) && 'fixe' === $tableau['type_especes']) {
189
			// si on trouve ":" dans referentiel, referentiel = première partie
190
			$tableau['referentiel'] = (strstr($tableau['referentiel'],':',true)) ?: $tableau['referentiel'];
3228 delphine 191
		}
3377 idir 192
		$tableau['milieux'] = ($tableau['milieux'] != '') ? explode(';', $tableau['milieux']) : [];
3425 idir 193
		if(!isset($this->parametres['id-obs'])) {
194
			if (isset($this->parametres['dept']) || isset($this->parametres['commune'],$this->parametres['dept']) || isset($this->parametres['code_insee'])) {
195
				$localisation = $this->traiterParamsLocalisation($this->parametres);
196
				// les paramètres dans l'url peuvent surcharger les données de localisation de la bdd
197
				if ($localisation) $tableau['localisation'] = $localisation;
198
			}
199
			if (isset($tableau['localisation'])) $tableau['localisation'] = $this->traiterLocalisation($tableau['localisation']);
200
		}
3278 delphine 201
		if (isset($tableau['motscles'])) {
3543 delphine 202
    		if (isset($tableau['tag-obs'])) {
3544 delphine 203
    		    $tableau['tag-obs'] .= $tableau['motscles'];
3543 delphine 204
		    } else if (isset($tableau['tag-img'])) {
205
		        $tableau['tag-img'] .= $tableau['motscles'];
206
		    } else {
207
		        $tableau['tag-obs'] = $tableau['tag-img'] = $tableau['motscles'];
208
		    }
209
 
3278 delphine 210
		}
3239 idir 211
		$tableau['chpSupp'] = $tab;
3240 idir 212
		$langue_projet_url = ( isset ( $this->parametres['langue'] ) && $this->parametres['langue'] !== 'fr' ) ? '_' . $this->parametres['langue'] : '';
3330 idir 213
		$tableau['chemin_fichiers'] = sprintf( $this->config['chemins']['baseURLAbsoluDyn'], $this->config['manager']['imgProjet'] . $tableau['projet'] . $langue_projet_url . '/' );
3228 delphine 214
		return $tableau;
215
	}
3239 idir 216
 
3228 delphine 217
	/* Recherche si un projet a des champs de saisie supplémentaire */
218
	private function rechercherChampsSupp() {
219
		$retour = array();
3239 idir 220
		$projet = $this->parametres['projet'];
221
		$url = $this->config['manager']['celChpSupTpl'] .'?projet=' . $projet . '&langue=' . $this->parametres['langue'];
3228 delphine 222
		$json = $this->getDao()->consulter($url);
3425 idir 223
		$retour = json_decode($json, true);
3249 idir 224
 
3239 idir 225
		foreach ( $retour[$projet]['champs-supp'] as $key => $chsup ) {
226
 
3249 idir 227
			$retour[$projet]['champs-supp'][$key]['name'] = $this->clean_string( $chsup['name'] );
228
			$retour[$projet]['champs-supp'][$key]['description'] = $this->clean_string( $chsup['description']);
229
			$retour[$projet]['champs-supp'][$key]['unit'] = $this->clean_string( $chsup['unit'] );
3239 idir 230
 
231
			if ( isset( $chsup['fieldValues'] ) ) {
3350 idir 232
				$retour[$projet]['champs-supp'][$key]['fieldValues'] = json_decode( $this->clean_string( $chsup['fieldValues'] ), true );
3239 idir 233
 
234
				if ( isset( $retour[$projet]['champs-supp'][$key]['fieldValues']['listValue'] ) ) {
3347 idir 235
					foreach( $retour[$projet]['champs-supp'][$key]['fieldValues']['listValue'] as $list_key => $list_value_array ) {
3228 delphine 236
						// Obtenir une liste de valeurs utilisables dans les attributs for id ou name par exemple
3350 idir 237
						$retour[$projet]['champs-supp'][$key]['fieldValues']['cleanListValue'][] = ($list_value_array !== 'other') ? 'val-' . preg_replace( '/[^A-Za-z0-9_\-]/', '', $this->remove_accents( strtolower($list_value_array[0] ) ) ) : '';
3228 delphine 238
					}
239
				}
240
			}
3239 idir 241
			$retour[$projet]['champs-supp'][$key]['mandatory'] = intval( $chsup['mandatory'] );
3228 delphine 242
		}
243
		return $retour;
244
	}
3239 idir 245
 
3228 delphine 246
	// remplace certains parametres définis en bd par les parametres définis dans l'url
247
	private function traiterParametres($estProjetDefini, $tableau) {
3278 delphine 248
		$criteres = array('tag-obs', 'tag-img', 'projet', 'titre', 'logo');
3228 delphine 249
		$criteresProjetNonDefini = array('commune', 'num_nom', 'referentiel');
250
		foreach($this->parametres as $nom_critere => $valeur_critere) {
3249 idir 251
			if (($estProjetDefini == false || $tableau['projet'] == 'base') && in_array($nom_critere, $criteresProjetNonDefini)) {
3228 delphine 252
				$tableau[$nom_critere] = $valeur_critere;
253
			} else if (in_array($nom_critere, $criteres)) {
254
				$tableau[$nom_critere] = $valeur_critere;
255
			}
256
		}
257
		return $tableau;
258
	}
3239 idir 259
 
3425 idir 260
	private function traiterLocalisation( $infos_localisation ) {
261
		$infos = explode(';', $infos_localisation);
262
		$tableauTmp = array();
3239 idir 263
		$retour = array();
3425 idir 264
		foreach ($infos as $info) {
265
			$tableauTmp = explode(':', $info);
266
			$retour[$tableauTmp[0]] = $tableauTmp[1];
267
		}
268
		return $retour;
269
	}
270
 
271
	private function traiterParamsLocalisation( $params ) {
272
		$infos = array();
273
		$tableauTmp = array();
274
		$zoom = '12';
275
		if (isset($params['commune'])) {
276
			$url = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_COORD). '?zone='.$params['commune'].'&code='.$params['dept'];
277
		} else if (isset($params['dept'])) {
278
			// pas trouvé de manière simple de déterminer le centroïde du département
279
			// du coup on retrouve le code insee du chef lieu
280
			$params['code_insee'] = $this->recupererChefLieuDept($params['dept']);
281
			$zoom = '9';
282
		}
283
		// quoi qu'il arrive, s'il est défini, on donne la priorité au code insee (plus précis)
284
		if (isset($params['code_insee'])) {
285
			$url = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_COORD). '?code='.$params['code_insee'];
286
		}
287
		if(!empty($url)) $json = $this->getDao()->consulter($url);
288
		if(!empty($json)) $infos = json_decode($json, true);
289
		if(!empty($infos)) {
290
			$infos['lat'] = str_replace(',','.',strval(round($infos['lat'],5)));
291
			$infos['lng'] = str_replace(',','.',strval(round($infos['lng'],5)));
292
			return 'latitude:'.$infos['lat'].';longitude:'.$infos['lng'].';zoom:'.$zoom;
293
		} else {
294
			return false;
295
		}
296
	}
297
 
298
	private function rechercherInfosEspeces( $infos_projets ) {
299
		$retour = array();
3239 idir 300
		$referentiel = $infos_projets['referentiel'];
3228 delphine 301
		$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
3260 idir 302
		$retour['url_ws_autocompletion_ns'] = sprintf( $urlWsNsTpl, self::EFLORE_API_VERSION, $referentiel, self::WS_NOM );;
303
		$retour['url_ws_autocompletion_ns_tpl'] = sprintf( $urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM );
3228 delphine 304
		$retour['ns_referentiel'] = $referentiel;
3239 idir 305
 
306
		if ( isset( $infos_projets['type_especes'] ) ) {
307
 
308
			switch ( $infos_projets['type_especes'] ) {
3249 idir 309
				case 'fixe' :
3377 idir 310
					$info_taxon = explode(':', $referentiel);
311
					if (!empty($info_taxon) && count((array) $info_taxon) === 2) {
312
						$retour = $this->chargerInfosTaxon( $info_taxon[0], $info_taxon[1] );
313
					}
3228 delphine 314
					break;
3257 delphine 315
				case 'referentiel' : break;
3249 idir 316
				case 'liste' :
3256 idir 317
					$retour['taxons'] = $this->recupererListeNomsSci();
3239 idir 318
					break;
3228 delphine 319
			}
3377 idir 320
		} else if ( isset( $referentiel ) ) {
3239 idir 321
			if ( isset($infos_projets['num_nom'] ) ) {
3377 idir 322
				$retour = $this->chargerInfosTaxon( $referentiel, $infos_projets['num_nom'] );
3228 delphine 323
			}
324
		}
325
		return $retour;
326
	}
3239 idir 327
 
3228 delphine 328
	/**
329
	 * Consulte un webservice pour obtenir des informations sur le taxon dont le
330
	 * numéro nomenclatural est $num_nom (ce sont donc plutôt des infos sur le nom
331
	 * et non le taxon?)
332
	 * @param string|int $num_nom
333
	 * @return array
334
	 */
3239 idir 335
	protected function chargerInfosTaxon( $referentiel, $num_nom ) {
336
		$url_service_infos = sprintf( $this->config['chemins']['infosTaxonUrl'], $referentiel, $num_nom );
337
		$infos = json_decode( file_get_contents( $url_service_infos ) );
3228 delphine 338
		// trop de champs injectés dans les infos espèces peuvent
339
		// faire planter javascript
3330 idir 340
		$champs_a_garder = array( 'id', 'nom_sci','nom_sci_complet', 'nom_complet', 'famille','nom_retenu.id', 'nom_retenu_complet', 'num_taxonomique' );
3228 delphine 341
		$resultat = array();
342
		$retour = array();
3239 idir 343
		if ( isset( $infos ) && !empty( $infos ) ) {
344
			$infos = (array) $infos;
3249 idir 345
			if ( isset( $infos['nom_sci'] ) && $infos['nom_sci'] !== '' ) {
3239 idir 346
				$resultat = array_intersect_key( $infos, array_flip($champs_a_garder ) );
3249 idir 347
				$resultat['retenu'] = ( $infos['id'] == $infos['nom_retenu.id'] ) ? 'true' : 'false';
3228 delphine 348
				$retour['espece_imposee'] = true;
349
				$retour['nn_espece_defaut'] = $nnEspeceImposee;
350
				$retour['nom_sci_espece_defaut'] = $resultat['nom_complet'];
3239 idir 351
				$retour['infos_espece'] = $this->array2js( $resultat, true );
3228 delphine 352
			}
353
		}
354
		return $retour;
355
	}
3120 delphine 356
 
3239 idir 357
	protected function getReferentielImpose() {
358
		$referentiel_impose = true;
3249 idir 359
		if (!empty($_GET['referentiel']) && $_GET['referentiel'] !== 'autre') {
3239 idir 360
			$this->ns_referentiel = $_GET['referentiel'];
361
		} else if (isset($this->configProjet['referentiel'])) {
362
			$this->ns_referentiel = $this->configProjet['referentiel'];
363
		} else if (isset($this->configMission['referentiel'])) {
364
			$this->ns_referentiel = $this->configMission['referentiel'];
365
		} else {
366
			$referentiel_impose = false;
367
		}
368
		return $referentiel_impose;
369
	}
3231 delphine 370
 
3228 delphine 371
	/**
3377 idir 372
	 * Trie par nom français les taxons lus dans le fichier csv/tsv
3228 delphine 373
	 */
374
	protected function recupererListeNomsSci() {
375
		$taxons = $this->recupererListeTaxon();
376
		if (is_array($taxons)) {
377
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
378
		}
379
		return $taxons;
380
	}
3239 idir 381
 
3228 delphine 382
	/**
383
	 * @TODO documenter
384
	 * @return array
385
	 */
386
	protected function recupererListeNoms() {
387
		$taxons = $this->recupererListeTaxon();
388
		$nomsAAfficher = array();
389
		$nomsSpeciaux = array();
390
		if (is_array($taxons)) {
391
			foreach ($taxons as $taxon) {
392
				$nomSciTitle = $taxon['nom_ret'].
393
				($taxon['nom_fr'] != '' ? ' - '.$taxon['nom_fr'] : '' ).
394
				($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
395
				$nomFrTitle = $taxon['nom_sel'].
396
				($taxon['nom_ret'] != $taxon['nom_sel']? ' - '.$taxon['nom_ret'] : '' ).
397
				($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
3239 idir 398
 
3228 delphine 399
				if ($taxon['groupe'] == 'special') {
400
					$nomsSpeciaux[] = array(
401
							'num_nom' => $taxon['num_nom_sel'],
402
							'nom_a_afficher' => $taxon['nom_fr'],
403
							'nom_a_sauver' => $taxon['nom_sel'],
404
							'nom_title' => $nomSciTitle,
405
							'nom_type' => 'nom-special');
406
				} else {
407
					$nomsAAfficher[] = array(
408
							'num_nom' => $taxon['num_nom_sel'],
409
							'nom_a_afficher' => $taxon['nom_sel'],
410
							'nom_a_sauver' => $taxon['nom_sel'],
411
							'nom_title' => $nomSciTitle,
412
							'nom_type' => 'nom-sci');
413
					$nomsAAfficher[] = array(
414
							'num_nom' => $taxon['num_nom_sel'],
415
							'nom_a_afficher' => $taxon['nom_fr'],
416
							'nom_a_sauver' => $taxon['nom_fr'],
417
							'nom_title' => $nomFrTitle,
418
							'nom_type' => 'nom-fr');
419
				}
420
			}
421
			$nomsAAfficher = self::trierTableauMd($nomsAAfficher, array('nom_a_afficher' => SORT_ASC));
422
			$nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
423
		}
424
		return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
425
	}
3239 idir 426
 
3228 delphine 427
	/**
3377 idir 428
	 * Lit une liste de taxons depuis un fichier csv ou tsv fourni
3228 delphine 429
	 */
430
	protected function recupererListeTaxon() {
431
		$taxons = array();
3256 idir 432
		$langue_projet_url = ( isset ( $this->parametres['langue'] ) && $this->parametres['langue'] !== 'fr' ) ? '_' . $this->parametres['langue'] : '';
433
		$chemin_images = dirname(__FILE__) . self::DS . '..' . self::DS . 'manager' . self::DS . 'squelettes' . self::DS . 'img' . self::DS . 'images_projets' . self::DS;
3425 idir 434
		$nom_fichier = ($this->parametres['squelette'] === 'lichens') ? 'lichens_taxons' : 'especes';
435
		$chemin_taxon = $chemin_images . $this->parametres['projet'] . $langue_projet_url . self::DS . $nom_fichier. '.';
3257 delphine 436
 
3377 idir 437
		if ( file_exists( $chemin_taxon . 'csv' ) && is_readable( $chemin_taxon . 'csv' ) ) {
438
			$taxons = $this->decomposerFichierCsv( $chemin_taxon . 'csv' );
439
		} else if ( file_exists( $chemin_taxon . 'tsv' ) && is_readable( $chemin_taxon . 'tsv' ) ) {
440
			$taxons = $this->decomposerFichierCsv( $chemin_taxon . 'tsv' );
3228 delphine 441
		} else {
3425 idir 442
			$this->debug[] = "Impossible d'ouvrir le fichier '$nom_fichier'.";
3228 delphine 443
		}
444
		return $taxons;
445
	}
3239 idir 446
 
3228 delphine 447
	/**
3425 idir 448
	 * Lit une liste de taxons depuis un fichier csv ou tsv fourni
449
	 */
450
	protected function recupererChefLieuDept($dept) {
451
		$infosDepts = array();
452
		$code_insee = '';
453
		$chemin_fichier = dirname(__FILE__).self::DS.'squelettes'.self::DS.'dept.csv';
454
 
455
		if (file_exists($chemin_fichier ) && is_readable($chemin_fichier)) {
456
			$infosDepts = $this->decomposerFichierCsv( $chemin_fichier,"," );
457
			if(!empty($infosDepts) && is_array($infosDepts)) {
458
				foreach ($infosDepts as $key => $infosDept) {
459
					if($dept == $infosDept['dep']) {
460
						return $code_insee = $infosDept['cheflieu'];
461
					}
462
				}
463
			}
464
		} else {
465
			$this->debug[] = "Impossible d'ouvrir le fichier '$nom_fichier'.";
466
		}
467
	}
468
 
469
	/**
3377 idir 470
	 * Découpe un fihcier csv/tsv
3228 delphine 471
	 */
3256 idir 472
	protected function decomposerFichierCsv($fichier, $delimiter = "\t"){
3228 delphine 473
		$header = null;
474
		$data = array();
3256 idir 475
		if (($handle = fopen($fichier, "r")) !== FALSE) {
3228 delphine 476
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
477
				if (!$header) {
478
					$header = $row;
479
				} else {
480
					$data[] = array_combine($header, $row);
481
				}
482
			}
483
			fclose($handle);
484
		}
485
		return $data;
486
	}
3239 idir 487
 
3228 delphine 488
	/**
489
	 * Convertit un tableau PHP en Javascript - @WTF pourquoi ne pas faire un json_encode ?
490
	 * @param array $array
491
	 * @param boolean $show_keys
492
	 * @return une portion de JSON représentant le tableau
493
	 */
494
	protected function array2js($array,$show_keys) {
495
		$tableauJs = '{}';
496
		if (!empty($array)) {
497
			$total = count($array) - 1;
498
			$i = 0;
499
			$dimensions = array();
500
			foreach ($array as $key => $value) {
501
				if (is_array($value)) {
502
					$dimensions[$i] = array2js($value,$show_keys);
503
					if ($show_keys) {
3260 idir 504
						$dimensions[$i] = '\"'.$key.'\":'.$dimensions[$i];
3228 delphine 505
					}
506
				} else {
3260 idir 507
					$dimensions[$i] = '\"'.addslashes($value).'\"';
3228 delphine 508
					if ($show_keys) {
3260 idir 509
						$dimensions[$i] = '\"'.$key.'\":'.$dimensions[$i];
3228 delphine 510
					}
511
				}
512
				if ($i == 0) {
513
					$dimensions[$i] = '{'.$dimensions[$i];
514
				}
515
				if ($i == $total) {
516
					$dimensions[$i].= '}';
517
				}
518
				$i++;
519
			}
520
			$tableauJs = implode(',', $dimensions);
521
		}
522
		return $tableauJs;
523
	}
3120 delphine 524
}
3239 idir 525
?>