New file |
0,0 → 1,164 |
<?php |
/** |
* |
* Service Cartes |
* |
* Service renvoyant la carte de flore probable issue des données sophy |
* (très simple, convertir un num tax en numéro fournier, puis construit l'url |
* de la carte) |
* |
* Encodage en entrée : utf8 |
* Encodage en sortie : utf8 |
* @package eflore-projets |
* @author Aurélien PERONNET <jpm@tela-botanica.org> |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt> |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt> |
* @version 0.1 |
* @copyright 2014 Tela Botanica (accueil@tela-botanica.org) |
*/ |
class SophyCartes { |
|
const MIME_TEXT = 'text/plain'; |
const MIME_PNG = 'image/png'; |
const MIME_JSON = 'application/json'; |
|
private $types_cartes_autorises = array('flore-probable'); |
|
private $bdd; |
private $format_retour; |
private $type_num_demande; |
private $num_demande; |
|
public function consulter($ressources, $param) { |
// Initialisation des variables |
$this->ressources = $ressources; |
$this->param = $param; |
|
$this->traiterRessources(); |
$this->traiterParametres(); |
|
if($this->type_num_demande == "nn") { |
//TODO: obtenir nt grâce aux autres services |
$this->num_demande = $this->obtenirCorrespondanceNtPourNn($this->num_demande); |
$this->type_num_demande = "nt"; |
} |
|
$resultat = $this->obtenirCarteFloreProbable($this->num_demande); |
return $resultat; |
} |
|
private function obtenirCarteFloreProbable($nt) { |
$num_fournier = $this->obtenirCorrespondanceFournier($nt); |
|
if($num_fournier == null) { |
$message = "Aucune carte n'existe pour le numéro demandé"; |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE; |
throw new Exception($message, $code); |
} |
return $this->formerCarteFloreProbable($num_fournier); |
} |
|
public function formerCarteFloreProbable($num_fournier) { |
|
$resultat = new ResultatService(); |
$url = sprintf(Config::get('chemin_cartes_tpl'), $num_fournier); |
|
$resultat->corps = ($this->format_retour == self::MIME_PNG) ? self::getCartePng($url) : array('binaire.href' => $url); |
$resultat->mime = ($this->format_retour == self::MIME_PNG) ? self::MIME_PNG : self::MIME_JSON; |
|
return $resultat; |
} |
|
private static function getCartePng($url){ |
$ch = curl_init(); |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
curl_setopt($ch, CURLOPT_URL, $url); |
$data = curl_exec($ch); |
curl_close($ch); |
return $data; |
} |
|
private function traiterRessources() { |
|
if(!in_array($this->ressources[0], $this->types_cartes_autorises)) { |
$message = 'Erreur dans les parametres de requête : </br> '. |
'Le type de carte '.$this->ressources[0].' est inconnu, <br />'. |
'Les type autorisés sont : '.implode(',', $this->types_cartes_autorises); |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE; |
throw new Exception($message, $code); |
} |
|
$matches = array(); |
if(preg_match("/([a-z]*).(nn|nt):([0-9]*)/", $this->ressources[1], $matches)) { |
//TODO: quoi faire du référentiel |
$this->type_num_demande = $matches[2]; |
$this->num_demande = $matches[3]; |
} else { |
$message = 'Erreur dans les parametres de requête : </br> '. |
'Le service s\'interroge sous la forme sophy/cartes/type_carte/bdtfx.nn:XXXX ou '. |
'sophy/cartes/type_carte/bdtfx.nt:XXXX '; |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE; |
throw new Exception($message, $code); |
} |
} |
|
private function traiterParametres() { |
$formats = array('image/png', 'text/plain'); |
$this->format_retour = self::MIME_PNG; |
if(isset($this->param['retour.format']) && |
!in_array($this->param['retour.format'], $formats)) { |
$message = 'Erreur dans les parametres de recherche de votre requête : </br> '. |
"Le paramètre retour.format ne peut prendre que les valeurs suivantes : ".implode(',', $formats)." "; |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE; |
throw new Exception($message, $code); |
} else { |
if(isset($this->param['retour.format'])) { |
$this->format_retour = $this->param['retour.format']; |
} else { |
$this->format_retour = self::MIME_TEXT; |
} |
} |
} |
|
private function obtenirCorrespondanceFournier($num_tax) { |
$requete = "SELECT * FROM sophy_fournier_bdnff ". |
"WHERE sfb_id_num_bdnff = ".$this->getBdd()->proteger($num_tax); |
|
$resultat = $this->getBdd()->recuperer($requete); |
|
$num_fournier = null; |
if(!empty($resultat)) { |
$num_fournier = $resultat['sfb_id_num_fournier']; |
} |
return $num_fournier; |
} |
|
private function obtenirCorrespondanceNtPourNn($num_nom) { |
// 90% de chances que ces cartes ne soient utilisées que bdtfx |
// donc on se permet une requete directe dans la table |
$requete = "SELECT num_taxonomique FROM ".Config::get('bdd_table_taxons')." ". |
"WHERE num_nom = ".$this->getBdd()->proteger($num_nom); |
|
$resultat = $this->getBdd()->recuperer($requete); |
|
$num_tax = null; |
if(!empty($resultat)) { |
$num_tax = $resultat['num_taxonomique']; |
} |
return $num_tax; |
} |
|
//+----------------------------------------------------------------------------------------------------------------+ |
// Méthodes d'accès aux objets du Framework |
/** |
* Méthode de connection à la base de données sur demande. |
* Tous les services web n'ont pas besoin de s'y connecter. |
*/ |
protected function getBdd() { |
if (! isset($this->bdd)) { |
$this->bdd = new Bdd(); |
} |
return $this->bdd; |
} |
} |
?> |