| 1012 | aurelien | 1 | <?php
 | 
        
           |  |  | 2 | /**
 | 
        
           |  |  | 3 | *
 | 
        
           |  |  | 4 | * Service Cartes
 | 
        
           |  |  | 5 | *
 | 
        
           |  |  | 6 | * Service renvoyant la carte de flore probable issue des données sophy
 | 
        
           |  |  | 7 | * (très simple, convertir un num tax en numéro fournier, puis construit l'url
 | 
        
           |  |  | 8 | * de la carte)
 | 
        
           |  |  | 9 | *
 | 
        
           |  |  | 10 | * Encodage en entrée : utf8
 | 
        
           |  |  | 11 | * Encodage en sortie : utf8
 | 
        
           |  |  | 12 | * @package eflore-projets
 | 
        
           |  |  | 13 | * @author Aurélien PERONNET <jpm@tela-botanica.org>
 | 
        
           |  |  | 14 | * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
 | 
        
           |  |  | 15 | * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
 | 
        
           |  |  | 16 | * @version 0.1
 | 
        
           |  |  | 17 | * @copyright 2014 Tela Botanica (accueil@tela-botanica.org)
 | 
        
           |  |  | 18 | */
 | 
        
           |  |  | 19 | class SophyCartes {
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | 	const MIME_TEXT = 'text/plain';
 | 
        
           |  |  | 22 | 	const MIME_PNG = 'image/png';
 | 
        
           | 1014 | aurelien | 23 | 	const MIME_JSON = 'application/json';
 | 
        
           | 1012 | aurelien | 24 |   | 
        
           |  |  | 25 | 	private $types_cartes_autorises = array('flore-probable');
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | 	private $bdd;
 | 
        
           |  |  | 28 | 	private $format_retour;
 | 
        
           |  |  | 29 | 	private $type_num_demande;
 | 
        
           |  |  | 30 | 	private $num_demande;
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 | 	public function consulter($ressources, $param) {
 | 
        
           |  |  | 33 | 		// Initialisation des variables
 | 
        
           |  |  | 34 | 		$this->ressources = $ressources;
 | 
        
           |  |  | 35 | 		$this->param = $param;
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 | 		$this->traiterRessources();
 | 
        
           |  |  | 38 | 		$this->traiterParametres();
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 | 		if($this->type_num_demande == "nn") {
 | 
        
           |  |  | 41 | 			//TODO: obtenir nt grâce aux autres services
 | 
        
           |  |  | 42 | 			$this->num_demande = $this->obtenirCorrespondanceNtPourNn($this->num_demande);
 | 
        
           |  |  | 43 | 			$this->type_num_demande = "nt";
 | 
        
           |  |  | 44 | 		}
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 | 		$resultat = $this->obtenirCarteFloreProbable($this->num_demande);
 | 
        
           |  |  | 47 | 		return $resultat;
 | 
        
           |  |  | 48 | 	}
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 | 	private function obtenirCarteFloreProbable($nt) {
 | 
        
           |  |  | 51 | 		$num_fournier = $this->obtenirCorrespondanceFournier($nt);
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 | 		if($num_fournier == null) {
 | 
        
           |  |  | 54 | 			$message = "Aucune carte n'existe pour le numéro demandé";
 | 
        
           |  |  | 55 | 			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
 | 
        
           |  |  | 56 | 			throw new Exception($message, $code);
 | 
        
           |  |  | 57 | 		}
 | 
        
           |  |  | 58 | 		return $this->formerCarteFloreProbable($num_fournier);
 | 
        
           |  |  | 59 | 	}
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 | 	public function formerCarteFloreProbable($num_fournier) {
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 | 		$resultat = new ResultatService();
 | 
        
           |  |  | 64 | 		$url = sprintf(Config::get('chemin_cartes_tpl'), $num_fournier);
 | 
        
           |  |  | 65 |   | 
        
           | 1014 | aurelien | 66 | 		$resultat->corps = ($this->format_retour == self::MIME_PNG) ? self::getCartePng($url) : array('binaire.href' => $url);
 | 
        
           |  |  | 67 | 		$resultat->mime = ($this->format_retour == self::MIME_PNG) ? self::MIME_PNG : self::MIME_JSON;
 | 
        
           | 1012 | aurelien | 68 |   | 
        
           |  |  | 69 | 		return $resultat;
 | 
        
           |  |  | 70 | 	}
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 | 	private static function getCartePng($url){
 | 
        
           |  |  | 73 |           $ch = curl_init();
 | 
        
           |  |  | 74 |           curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 | 
        
           |  |  | 75 |           curl_setopt($ch, CURLOPT_URL, $url);
 | 
        
           |  |  | 76 |           $data = curl_exec($ch);
 | 
        
           |  |  | 77 |           curl_close($ch);
 | 
        
           |  |  | 78 |           return $data;
 | 
        
           |  |  | 79 |     }
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |     private function traiterRessources() {
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 |     	if(!in_array($this->ressources[0], $this->types_cartes_autorises)) {
 | 
        
           |  |  | 84 |     		$message = 'Erreur dans les parametres de requête : </br> '.
 | 
        
           |  |  | 85 |     	    					'Le type de carte '.$this->ressources[0].' est inconnu, <br />'.
 | 
        
           |  |  | 86 |     	    					'Les type autorisés sont : '.implode(',', $this->types_cartes_autorises);
 | 
        
           |  |  | 87 |     		$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
 | 
        
           |  |  | 88 |     		throw new Exception($message, $code);
 | 
        
           |  |  | 89 |     	}
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |     	$matches = array();
 | 
        
           |  |  | 92 |     	if(preg_match("/([a-z]*).(nn|nt):([0-9]*)/", $this->ressources[1], $matches)) {
 | 
        
           |  |  | 93 |     		//TODO: quoi faire du référentiel
 | 
        
           |  |  | 94 |     		$this->type_num_demande = $matches[2];
 | 
        
           |  |  | 95 |     		$this->num_demande = $matches[3];
 | 
        
           |  |  | 96 |     	} else {
 | 
        
           |  |  | 97 |     		$message = 'Erreur dans les parametres de requête : </br> '.
 | 
        
           |  |  | 98 |     					'Le service s\'interroge sous la forme sophy/cartes/type_carte/bdtfx.nn:XXXX ou '.
 | 
        
           |  |  | 99 |     					'sophy/cartes/type_carte/bdtfx.nt:XXXX ';
 | 
        
           |  |  | 100 |     		$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
 | 
        
           |  |  | 101 |     		throw new Exception($message, $code);
 | 
        
           |  |  | 102 |     	}
 | 
        
           |  |  | 103 |     }
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 | 	private function traiterParametres() {
 | 
        
           |  |  | 106 | 		$formats = array('image/png', 'text/plain');
 | 
        
           |  |  | 107 | 		$this->format_retour = self::MIME_PNG;
 | 
        
           |  |  | 108 | 		if(isset($this->param['retour.format']) &&
 | 
        
           |  |  | 109 | 			!in_array($this->param['retour.format'], $formats)) {
 | 
        
           |  |  | 110 | 			$message = 'Erreur dans les parametres de recherche de votre requête : </br> '.
 | 
        
           |  |  | 111 | 			"Le paramètre retour.format ne peut prendre que les valeurs suivantes : ".implode(',', $formats)." ";
 | 
        
           |  |  | 112 | 			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
 | 
        
           |  |  | 113 | 			throw new Exception($message, $code);
 | 
        
           |  |  | 114 | 		} else {
 | 
        
           |  |  | 115 | 			if(isset($this->param['retour.format'])) {
 | 
        
           |  |  | 116 | 				$this->format_retour = $this->param['retour.format'];
 | 
        
           |  |  | 117 | 			} else {
 | 
        
           |  |  | 118 | 				$this->format_retour = self::MIME_TEXT;
 | 
        
           |  |  | 119 | 			}
 | 
        
           |  |  | 120 | 		}
 | 
        
           |  |  | 121 | 	}
 | 
        
           |  |  | 122 |   | 
        
           |  |  | 123 | 	private function obtenirCorrespondanceFournier($num_tax) {
 | 
        
           |  |  | 124 | 		$requete = "SELECT * FROM sophy_fournier_bdnff ".
 | 
        
           |  |  | 125 | 		           "WHERE sfb_id_num_bdnff = ".$this->getBdd()->proteger($num_tax);
 | 
        
           |  |  | 126 |   | 
        
           |  |  | 127 | 		$resultat = $this->getBdd()->recuperer($requete);
 | 
        
           |  |  | 128 |   | 
        
           |  |  | 129 | 		$num_fournier = null;
 | 
        
           |  |  | 130 | 		if(!empty($resultat)) {
 | 
        
           |  |  | 131 | 			$num_fournier = $resultat['sfb_id_num_fournier'];
 | 
        
           |  |  | 132 | 		}
 | 
        
           |  |  | 133 | 		return $num_fournier;
 | 
        
           |  |  | 134 | 	}
 | 
        
           |  |  | 135 |   | 
        
           |  |  | 136 | 	private function obtenirCorrespondanceNtPourNn($num_nom) {
 | 
        
           |  |  | 137 | 		// 90% de chances que ces cartes ne soient utilisées que bdtfx
 | 
        
           |  |  | 138 | 		// donc on se permet une requete directe dans la table
 | 
        
           |  |  | 139 | 		$requete = "SELECT num_taxonomique FROM ".Config::get('bdd_table_taxons')." ".
 | 
        
           |  |  | 140 | 		           "WHERE num_nom = ".$this->getBdd()->proteger($num_nom);
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 | 		$resultat = $this->getBdd()->recuperer($requete);
 | 
        
           |  |  | 143 |   | 
        
           |  |  | 144 | 		$num_tax = null;
 | 
        
           |  |  | 145 | 		if(!empty($resultat)) {
 | 
        
           |  |  | 146 | 			$num_tax = $resultat['num_taxonomique'];
 | 
        
           |  |  | 147 | 		}
 | 
        
           |  |  | 148 | 		return $num_tax;
 | 
        
           |  |  | 149 | 	}
 | 
        
           |  |  | 150 |   | 
        
           |  |  | 151 | 	//+----------------------------------------------------------------------------------------------------------------+
 | 
        
           |  |  | 152 | 	// Méthodes d'accès aux objets du Framework
 | 
        
           |  |  | 153 | 	/**
 | 
        
           |  |  | 154 | 	* Méthode de connection à la base de données sur demande.
 | 
        
           |  |  | 155 | 	* Tous les services web n'ont pas besoin de s'y connecter.
 | 
        
           |  |  | 156 | 	*/
 | 
        
           |  |  | 157 | 	protected function getBdd() {
 | 
        
           |  |  | 158 | 		if (! isset($this->bdd)) {
 | 
        
           |  |  | 159 | 			$this->bdd = new Bdd();
 | 
        
           |  |  | 160 | 		}
 | 
        
           |  |  | 161 | 		return $this->bdd;
 | 
        
           |  |  | 162 | 	}
 | 
        
           |  |  | 163 | }
 | 
        
           |  |  | 164 | ?>
 |