Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1012 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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