Subversion Repositories eFlore/Applications.del

Rev

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

Rev 1818 Rev 1820
1
<?php
1
<?php
-
 
2
// declare(encoding='UTF-8');
2
/**
3
/**
3
 * Accès aux sous-services de statistiques pour Identiplante / Pictoflora
4
 * Accès aux sous-services de statistiques pour Identiplante / Pictoflora
4
 *
5
 *
5
 * @see Documentation : <http://www.tela-botanica.org/wikini/DevInformatiques/wakka.php?wiki=AppliDelStats>
6
 * @see Documentation : <http://www.tela-botanica.org/wikini/DevInformatiques/wakka.php?wiki=AppliDelStats>
6
 *
7
 *
7
 * @category   DEL
8
 * @category   DEL
8
 * @package    Services
9
 * @package    Services
9
 * @subpackage Statistiques
10
 * @subpackage Statistiques
10
 * @version    0.1
11
 * @version    0.1
11
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
12
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
12
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
13
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
13
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
14
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
14
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
15
 * @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
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
16
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
17
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
17
 */
18
 */
18
class Statistiques extends RestService {
19
class Statistiques extends RestService {
19
 
20
 
20
	private $parametres = array();
21
	private $parametres = array();
21
	private $ressources = array();
22
	private $ressources = array();
22
	private $methode = null;
23
	private $methode = null;
23
	private $serviceNom = 'statistiques';
24
	private $serviceNom = 'statistiques';
24
	private $sousServiceNom = null;
25
	private $sousServiceNom = null;
25
	private $cheminCourant = null;
26
	private $cheminCourant = null;
26
 
27
 
27
	private $conteneur;
28
	private $conteneur;
28
 
29
 
29
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
30
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
30
	protected $utilisationParametresBruts = true;
31
	protected $utilisationParametresBruts = true;
31
 
32
 
32
	public function __construct() {
33
	public function __construct() {
33
		$this->cheminCourant = dirname(__FILE__).DS;
34
		$this->cheminCourant = dirname(__FILE__).DS;
34
	}
35
	}
35
 
36
 
36
	public function consulter($ressources, $parametres) {
37
	public function consulter($ressources, $parametres) {
37
		$this->methode = 'consulter';
38
		$this->methode = 'consulter';
38
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
39
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
39
		return $this->executerService();
40
		return $this->executerService();
40
	}
41
	}
41
 
42
 
42
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
43
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
43
		$this->ressources = $ressources;
44
		$this->ressources = $ressources;
44
		$this->parametres = $parametres;
45
		$this->parametres = $parametres;
45
	}
46
	}
46
 
47
 
47
	private function executerService() {
48
	private function executerService() {
48
		$reponseHttp = new ReponseHttp();
49
		$reponseHttp = new ReponseHttp();
49
		try {
50
		try {
50
			$this->conteneur = new Conteneur($this->parametres);
51
			$this->conteneur = new Conteneur($this->parametres);
51
			$resultat = $this->traiterRessources();
52
			$resultat = $this->traiterRessources();
52
			$reponseHttp->setResultatService($resultat);
53
			$reponseHttp->setResultatService($resultat);
53
		} catch (Exception $e) {
54
		} catch (Exception $e) {
54
			$reponseHttp->ajouterErreur($e);
55
			$reponseHttp->ajouterErreur($e);
55
		}
56
		}
56
		$reponseHttp->emettreLesEntetes();
57
		$reponseHttp->emettreLesEntetes();
57
		$corps = $reponseHttp->getCorps();
58
		$corps = $reponseHttp->getCorps();
58
		return $corps;
59
		return $corps;
59
	}
60
	}
60
 
61
 
61
	private function traiterRessources() {
62
	private function traiterRessources() {
62
		$this->analyserRessources();
63
		$this->analyserRessources();
63
		$retour = $this->initialiserService();
64
		$retour = $this->initialiserService();
64
		return $retour;
65
		return $retour;
65
	}
66
	}
66
 
67
 
67
	private function analyserRessources() {
68
	private function analyserRessources() {
68
		if ($this->methode == 'consulter') {
69
		if ($this->methode == 'consulter') {
69
			$this->sousServiceNom = 'statistiques-par-annee';
70
			$this->sousServiceNom = 'statistiques-par-annee';
70
		}
71
		}
71
		if ($this->sousServiceNom == null) {
72
		if ($this->sousServiceNom == null) {
72
			$this->lancerMessageErreurRessource();
73
			$this->lancerMessageErreurRessource();
73
		}
74
		}
74
	}
75
	}
75
 
76
 
76
	private function lancerMessageErreurRessource() {
77
	private function lancerMessageErreurRessource() {
77
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
78
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
78
		$message = "La ressource demandée '$ressource' ".
79
		$message = "La ressource demandée '$ressource' ".
79
			"n'est pas disponible pour le service {$this->serviceNom} !\n".
80
			"n'est pas disponible pour le service {$this->serviceNom} !\n".
80
			self::getDoc();
81
			self::getDoc();
81
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
82
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
82
		throw new Exception($message, $code);
83
		throw new Exception($message, $code);
83
	}
84
	}
84
 
85
 
85
	public static function getDoc() {
86
	public static function getDoc() {
86
		return "Les URLs disponibles pour ce service sont :\n".
87
		return "Les URLs disponibles pour ce service sont :\n".
87
			" * en GET :\n".
88
			" * en GET :\n".
88
			" - statistiques/tout\n".
89
			" - statistiques/tout\n".
89
			" - statistiques/moyenneObsSansNomParMois\n".
90
			" - statistiques/moyenneObsSansNomParMois\n".
90
			" - statistiques/moyenneObsIdentifieesParMois\n".
91
			" - statistiques/moyenneObsIdentifieesParMois\n".
91
			" - statistiques/pourcentageObsIdentifieesEnFinDAnnee\n".
92
			" - statistiques/pourcentageObsIdentifieesEnFinDAnnee\n".
92
			" - statistiques/pourcentageObsIdentifieesEnFinDAnneePlusPlus\n".
93
			" - statistiques/pourcentageObsIdentifieesEnFinDAnneePlusPlus\n".
93
			" - statistiques/moyenneActionsParJour\n".
94
			" - statistiques/moyenneActionsParJour\n".
94
			" - statistiques/personnesEnvoyantUnePropositionParMois\n";
95
			" - statistiques/personnesEnvoyantUnePropositionParMois\n";
95
	}
96
	}
96
 
97
 
97
	private function initialiserService() {
98
	private function initialiserService() {
98
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
99
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
99
		$chemins = array();
100
		$chemins = array();
100
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
101
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
101
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
102
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
102
		$retour = '';
103
		$retour = '';
103
		$service = null;
104
		$service = null;
104
 
105
 
105
		foreach ($chemins as $chemin) {
106
		foreach ($chemins as $chemin) {
106
			if (file_exists($chemin)) {
107
			if (file_exists($chemin)) {
107
				require_once $chemin;
108
				require_once $chemin;
108
				$service = new $classe($this->conteneur);
109
				$service = new $classe($this->conteneur);
109
				if ($this->methode == 'consulter') {
110
				if ($this->methode == 'consulter') {
110
					$retour = $service->consulter();
111
					$retour = $service->consulter();
111
				} else {
112
				} else {
112
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
113
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
113
						"ne possède pas de méthode '{$this->methode}' !";
114
						"ne possède pas de méthode '{$this->methode}' !";
114
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
115
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
115
					throw new Exception($message, $code);
116
					throw new Exception($message, $code);
116
				}
117
				}
117
			}
118
			}
118
		}
119
		}
119
 
120
 
120
		if (is_null($service)) {
121
		if (is_null($service)) {
121
			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
122
			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
122
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
123
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
123
				"est introuvable par le service '{$this->serviceNom}' !";
124
				"est introuvable par le service '{$this->serviceNom}' !";
124
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
125
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
125
			throw new Exception($message, $code);
126
			throw new Exception($message, $code);
126
		}
127
		}
127
		return $retour;
128
		return $retour;
128
	}
129
	}
129
 
130
 
130
	private function obtenirNomClasseService($mot) {
131
	private function obtenirNomClasseService($mot) {
131
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
132
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
132
		return $classeNom;
133
		return $classeNom;
133
	}
134
	}
134
}
135
}