| 424 | jpm | 1 | <?php
 | 
        
           | 575 | jpm | 2 | // declare(encoding='UTF-8');
 | 
        
           | 424 | jpm | 3 | /**
 | 
        
           |  |  | 4 |  * Service fournissant une carte dynamique des obsertions publiques du CEL.
 | 
        
           |  |  | 5 |  * Encodage en entrée : utf8
 | 
        
           |  |  | 6 |  * Encodage en sortie : utf8
 | 
        
           |  |  | 7 |  *
 | 
        
           | 575 | jpm | 8 |  * Cas d'utilisation et documentation :
 | 
        
           |  |  | 9 |  * @link http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=AideCELWidgetCarto
 | 
        
           | 424 | jpm | 10 |  *
 | 
        
           | 575 | jpm | 11 |  * Paramètres :
 | 
        
           |  |  | 12 |  * ===> utilisateur = identifiant
 | 
        
           |  |  | 13 |  * Affiche seulement les observations d'un utilisateur donné. L'identifiant correspond au courriel de
 | 
        
           |  |  | 14 |  * l'utilisateur avec lequel il s'est inscrit sur Tela Botanica.
 | 
        
           |  |  | 15 |  * ===> dept = code_du_département
 | 
        
           |  |  | 16 |  * Affiche seulement les observations pour le département français métropolitain indiqué. Les codes de département utilisables
 | 
        
           |  |  | 17 |  * sont : 01 à 19, 2A, 2B et 21 à 95.
 | 
        
           |  |  | 18 |  * ===> projet = mot-clé
 | 
        
           |  |  | 19 |  * Affiche seulement les observations pour le projet d'observations indiqué. Dans l'interface du CEL, vous pouvez taguer vos
 | 
        
           |  |  | 20 |  * observations avec un mot-clé de projet. Si vous voulez regrouper des observations de plusieurs utilisateurs, communiquez un
 | 
        
           |  |  | 21 |  * mot-clé de projet à vos amis et visualisez les informations ainsi regroupées.
 | 
        
           |  |  | 22 |  * ===> num_taxon = num_taxon
 | 
        
           |  |  | 23 |  * Affiche seulement les observations pour la plante indiquée. Le num_taxon correspond au numéro taxonomique de la plante.
 | 
        
           |  |  | 24 |  * Ce numéro est disponible dans les fiches d'eFlore. Par exemple, pour "Poa bulbosa L." le numéro taxonomique vaut 7080.
 | 
        
           |  |  | 25 |  *
 | 
        
           |  |  | 26 |  * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
 | 
        
           |  |  | 27 |  * @license	GPL v3 <http://www.gnu.org/licenses/gpl.txt>
 | 
        
           |  |  | 28 |  * @license	CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
 | 
        
           |  |  | 29 |  * @version	$Id$
 | 
        
           |  |  | 30 |  * @copyright	Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
 | 
        
           | 424 | jpm | 31 |  */
 | 
        
           |  |  | 32 | class Carto extends WidgetCommun {
 | 
        
           | 464 | delphine | 33 | 	const DS = DIRECTORY_SEPARATOR;
 | 
        
           | 836 | jpm | 34 | 	const SERVICE_CARTO_NOM = 'CelWidgetMap';
 | 
        
           |  |  | 35 | 	const SERVICE_CARTO_ACTION_DEFAUT = 'carte-defaut';
 | 
        
           | 424 | jpm | 36 |   | 
        
           | 836 | jpm | 37 | 	private $carte = null;
 | 
        
           |  |  | 38 | 	private $utilisateur = null;
 | 
        
           |  |  | 39 | 	private $projet = null;
 | 
        
           |  |  | 40 | 	private $dept = null;
 | 
        
           |  |  | 41 | 	private $num_taxon = null;
 | 
        
           |  |  | 42 | 	private $station = null;
 | 
        
           |  |  | 43 | 	private $format = null;// Format des obs pour les stations (tableau/liste)
 | 
        
           |  |  | 44 |   | 
        
           | 424 | jpm | 45 | 	/**
 | 
        
           | 575 | jpm | 46 | 	 * Méthode appelée par défaut pour charger ce widget.
 | 
        
           | 424 | jpm | 47 | 	 */
 | 
        
           |  |  | 48 | 	public function executer() {
 | 
        
           |  |  | 49 | 		$retour = null;
 | 
        
           | 836 | jpm | 50 | 		$this->extraireParametres();
 | 
        
           |  |  | 51 | 		$methode = $this->traiterNomMethodeExecuter($this->carte);
 | 
        
           | 471 | jpm | 52 | 		if (method_exists($this, $methode)) {
 | 
        
           |  |  | 53 | 			$retour = $this->$methode();
 | 
        
           | 424 | jpm | 54 | 		} else {
 | 
        
           | 471 | jpm | 55 | 			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
 | 
        
           | 424 | jpm | 56 | 		}
 | 
        
           |  |  | 57 | 		if (is_null($retour)) {
 | 
        
           |  |  | 58 | 			$info = 'Un problème est survenu : '.print_r($this->messages, true);
 | 
        
           |  |  | 59 | 			$this->envoyer($info);
 | 
        
           |  |  | 60 | 		} else {
 | 
        
           | 543 | jpm | 61 | 			$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$retour['squelette'].'.tpl.html';
 | 
        
           | 424 | jpm | 62 | 			$html = $this->traiterSquelettePhp($squelette, $retour['donnees']);
 | 
        
           |  |  | 63 | 			$this->envoyer($html);
 | 
        
           |  |  | 64 | 		}
 | 
        
           |  |  | 65 | 	}
 | 
        
           | 836 | jpm | 66 |   | 
        
           |  |  | 67 | 	public function extraireParametres() {
 | 
        
           |  |  | 68 | 		extract($this->parametres);
 | 
        
           |  |  | 69 | 		$this->carte = (isset($carte) ? $carte : self::SERVICE_CARTO_ACTION_DEFAUT);
 | 
        
           |  |  | 70 | 		$this->utilisateur = (isset($utilisateur) ? $utilisateur : '*');
 | 
        
           |  |  | 71 | 		$this->projet = (isset($projet) ? $projet : '*');
 | 
        
           | 939 | jpm | 72 | 		$this->tag = (isset($tag) ? $tag : '*');
 | 
        
           | 836 | jpm | 73 | 		$this->dept = (isset($dept) ? $dept : '*');
 | 
        
           | 837 | jpm | 74 | 		$this->commune = (isset($commune) ? $commune : '*');
 | 
        
           | 836 | jpm | 75 | 		$this->num_taxon = (isset($num_taxon) ? $num_taxon : '*');
 | 
        
           | 939 | jpm | 76 | 		$this->date = (isset($date) ? $date : '*');
 | 
        
           |  |  | 77 | 		$this->taxon = (isset($taxon) ? $taxon : '*');
 | 
        
           | 940 | jpm | 78 | 		$this->commentaire = (isset($commentaire) ? $commentaire : null);
 | 
        
           | 836 | jpm | 79 | 		$this->station = (isset($station) ? $station : null);
 | 
        
           |  |  | 80 | 		$this->format = (isset($format) ? $format : null);
 | 
        
           | 841 | jpm | 81 | 		$this->start = (isset($start) ? $start : null);
 | 
        
           |  |  | 82 | 		$this->limit = (isset($limit) ? $limit : null);
 | 
        
           | 836 | jpm | 83 | 	}
 | 
        
           | 424 | jpm | 84 |   | 
        
           |  |  | 85 | 	/**
 | 
        
           |  |  | 86 | 	 * Carte par défaut
 | 
        
           |  |  | 87 | 	 */
 | 
        
           |  |  | 88 | 	public function executerCarteDefaut() {
 | 
        
           |  |  | 89 | 		$widget = null;
 | 
        
           | 939 | jpm | 90 | 		$url_stations = $this->contruireUrlServiceCarto('stations');
 | 
        
           | 424 | jpm | 91 | 		$url_base = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 | 		// Création des infos du widget
 | 
        
           | 836 | jpm | 94 | 		$widget['donnees']['url_cel_carto'] = $this->contruireUrlServiceCarto();
 | 
        
           | 939 | jpm | 95 | 		$widget['donnees']['url_stations'] = $url_stations;
 | 
        
           | 424 | jpm | 96 | 		$widget['donnees']['url_base'] = $url_base;
 | 
        
           | 836 | jpm | 97 | 		$widget['donnees']['utilisateur'] = $this->utilisateur;
 | 
        
           |  |  | 98 | 		$widget['donnees']['projet'] = $this->projet;
 | 
        
           | 939 | jpm | 99 | 		$widget['donnees']['tag'] = $this->tag;
 | 
        
           | 836 | jpm | 100 | 		$widget['donnees']['dept'] = $this->dept;
 | 
        
           | 837 | jpm | 101 | 		$widget['donnees']['commune'] = $this->commune;
 | 
        
           | 836 | jpm | 102 | 		$widget['donnees']['num_taxon'] = $this->num_taxon;
 | 
        
           | 939 | jpm | 103 | 		$widget['donnees']['date'] = $this->date;
 | 
        
           |  |  | 104 | 		$widget['donnees']['taxon'] = $this->taxon;
 | 
        
           | 940 | jpm | 105 | 		$widget['donnees']['commentaire'] = $this->commentaire;
 | 
        
           | 939 | jpm | 106 | 		$widget['donnees']['url_limites_communales'] = $this->obtenirUrlsLimitesCommunales();
 | 
        
           |  |  | 107 |   | 
        
           | 424 | jpm | 108 | 		$widget['squelette'] = 'carte_defaut';
 | 
        
           | 464 | delphine | 109 |   | 
        
           | 424 | jpm | 110 | 		return $widget;
 | 
        
           |  |  | 111 | 	}
 | 
        
           | 464 | delphine | 112 |   | 
        
           | 836 | jpm | 113 | 	private function contruireUrlServiceCarto($action = null) {
 | 
        
           |  |  | 114 | 		// Création url données json
 | 
        
           |  |  | 115 | 		$url = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::SERVICE_CARTO_NOM);
 | 
        
           |  |  | 116 | 		if ($action) {
 | 
        
           |  |  | 117 | 			$url .= "/$action";
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 | 			$parametres_retenus = array();
 | 
        
           | 939 | jpm | 120 | 			$parametres_a_tester = array('station', 'utilisateur', 'projet', 'tag', 'dept', 'commune',
 | 
        
           | 940 | jpm | 121 | 				'num_taxon', 'taxon', 'date', 'commentaire',
 | 
        
           | 939 | jpm | 122 | 				'start', 'limit');
 | 
        
           | 836 | jpm | 123 | 			foreach ($parametres_a_tester as $param) {
 | 
        
           | 841 | jpm | 124 | 				if (isset($this->$param) && $this->$param != '*') {
 | 
        
           | 836 | jpm | 125 | 					$parametres_retenus[$param] = $this->$param;
 | 
        
           |  |  | 126 | 				}
 | 
        
           |  |  | 127 | 			}
 | 
        
           |  |  | 128 | 			if (count($parametres_retenus) > 0) {
 | 
        
           |  |  | 129 | 				$parametres_url = array();
 | 
        
           |  |  | 130 | 				foreach ($parametres_retenus as $cle => $valeur) {
 | 
        
           |  |  | 131 | 					$parametres_url[] = $cle.'='.$valeur;
 | 
        
           |  |  | 132 | 				}
 | 
        
           |  |  | 133 | 				$url .= '?'.implode('&', $parametres_url);
 | 
        
           |  |  | 134 | 			}
 | 
        
           |  |  | 135 | 		}
 | 
        
           |  |  | 136 | 		return $url;
 | 
        
           |  |  | 137 | 	}
 | 
        
           |  |  | 138 |   | 
        
           | 939 | jpm | 139 | 	private function obtenirUrlsLimitesCommunales() {
 | 
        
           |  |  | 140 | 		$urls = null;
 | 
        
           |  |  | 141 | 		if (isset($this->dept)) {
 | 
        
           |  |  | 142 | 			// si on veut afficher les limites départemmentales on va compter et chercher les noms de fichiers
 | 
        
           |  |  | 143 | 			$fichiersKml = $this->chercherFichierKml();
 | 
        
           |  |  | 144 | 			if (count($fichiersKml) > 0) {
 | 
        
           |  |  | 145 | 				foreach ($fichiersKml as $kml => $dossier){
 | 
        
           |  |  | 146 | 					$url_limites_communales = sprintf($this->config['carto']['limitesCommunaleUrlTpl'], $dossier, $kml);
 | 
        
           |  |  | 147 | 					$urls[] = $url_limites_communales;
 | 
        
           | 490 | jpm | 148 | 				}
 | 
        
           |  |  | 149 | 			}
 | 
        
           |  |  | 150 | 		}
 | 
        
           | 939 | jpm | 151 | 		$urls = json_encode($urls);
 | 
        
           |  |  | 152 | 		return $urls;
 | 
        
           | 490 | jpm | 153 | 	}
 | 
        
           |  |  | 154 |   | 
        
           | 836 | jpm | 155 | 	private function chercherFichierKml(){
 | 
        
           | 939 | jpm | 156 | 		$fichiers = array();
 | 
        
           | 574 | jpm | 157 | 		$chemins = explode(',', $this->config['carto']['communesKmzChemin']);
 | 
        
           | 836 | jpm | 158 | 		$departements = explode(',', $this->dept);// plrs code de départements peuvent être demandés séparés par des virgules
 | 
        
           | 599 | jpm | 159 | 		$departements_trouves = array();
 | 
        
           | 574 | jpm | 160 | 		foreach ($chemins as $dossier_chemin) {
 | 
        
           |  |  | 161 | 			if ($dossier_ressource = opendir($dossier_chemin)) {
 | 
        
           |  |  | 162 | 				while ($element = readdir($dossier_ressource)) {
 | 
        
           |  |  | 163 | 					if ($element != '.' && $element != '..') {
 | 
        
           | 599 | jpm | 164 | 						foreach ($departements as $departement) {
 | 
        
           |  |  | 165 | 							$nom_dossier = basename($dossier_chemin);
 | 
        
           |  |  | 166 | 							if (!isset($departements_trouves[$departement]) || $departements_trouves[$departement] == $nom_dossier) {
 | 
        
           |  |  | 167 | 								$dept_protege = preg_quote($departement);
 | 
        
           |  |  | 168 | 								if (!is_dir($dossier_chemin.'/'.$element) && preg_match("/^$dept_protege(?:_[0-9]+|)\.km[lz]$/", $element)) {
 | 
        
           |  |  | 169 | 									$fichiers[$element] = $nom_dossier;
 | 
        
           |  |  | 170 | 									$departements_trouves[$departement] = $nom_dossier;
 | 
        
           |  |  | 171 | 								}
 | 
        
           |  |  | 172 | 							}
 | 
        
           | 574 | jpm | 173 | 						}
 | 
        
           | 543 | jpm | 174 | 					}
 | 
        
           | 464 | delphine | 175 | 				}
 | 
        
           | 574 | jpm | 176 | 				closedir($dossier_ressource);
 | 
        
           | 464 | delphine | 177 | 			}
 | 
        
           |  |  | 178 | 		}
 | 
        
           | 599 | jpm | 179 |   | 
        
           | 574 | jpm | 180 | 		return $fichiers;
 | 
        
           | 471 | jpm | 181 | 	}
 | 
        
           |  |  | 182 |   | 
        
           |  |  | 183 | 	/**
 | 
        
           | 501 | jpm | 184 | 	 * Afficher message d'avertissement.
 | 
        
           |  |  | 185 | 	 */
 | 
        
           |  |  | 186 | 	public function executerAvertissement() {
 | 
        
           |  |  | 187 | 		$widget = null;
 | 
        
           |  |  | 188 |   | 
        
           |  |  | 189 | 		// Création des infos du widget
 | 
        
           |  |  | 190 | 		$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
 | 
        
           |  |  | 191 | 		$widget['squelette'] = 'avertissement';
 | 
        
           |  |  | 192 |   | 
        
           |  |  | 193 | 		return $widget;
 | 
        
           |  |  | 194 | 	}
 | 
        
           | 424 | jpm | 195 | }
 |