Subversion Repositories eFlore/Projets.eflore-projets

Compare Revisions

Ignore whitespace Rev 448 → Rev 449

/trunk/services/modules/0.1/baseflor/CommunGraphiques.php
New file
0,0 → 1,313
<?php
 
/**
* Classe CommunGraphiques.php regroupement de fonctions pour les graphiques
* graphiques/#typegraphique/#bdnt.nn:#num_nomen --> renvoie un graphique avec les données connues
*
*
* @package eflore-projets
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @author Mathilde SALTHUN-LASSALLE <mathilde@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 1.0
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
*/
 
abstract class CommunGraphiques extends Commun{
protected $parametres = array();
protected $ressources = array();
protected $Bdd;
protected $config;
protected $cache;
protected $metadonnees;
protected $version;
protected $serviceNom = 'Graphiques';
protected $graduations_id = array();
protected $champs_ontologiques = array();
protected $nomGraphique = array();
protected $table = '';
protected $requete_condition = "";
protected $requete_champs;
protected $convertisseur;
protected $cheminGraphBase;
protected $valeurs_en_pourcentage = array();
protected $dom;
protected $largeurSVG = "500";
protected $valeurs_champs;
public function consulter($ressources, $parametres) {
$this->ressources = $ressources;
$this->parametres = $parametres;
$this->initialiserConfiguration();
$resultats = '';
$this->traiterRessources();
$requete = $this->assemblerLaRequete();
$resultat = $this->resultat = $this->Bdd->recupererTous($requete);
$versionResultat = $this->obtenirResultat($resultat);
return $versionResultat;
}
 
//+--------------------------initialisation de paramètres -------------------------------------------+
private function initialiserConfiguration() {
$conteneur = new Conteneur();
$this->Bdd = $conteneur->getBdd();
$this->config = $conteneur->getParametre('Graphiques');
$this->convertisseur = $this->config['convertisseur'];
$this->graduations_id = $this->getParametreTableau('graduations_id');
$this->champs_ontologiques = $this->getParametreTableau('champs_ontologiques');
$this->cheminGraphBase = $this->config['chemin'];
$cacheOptions = array('mise_en_cache' => $this->config['cache']['miseEnCache'],
'stockage_chemin' => $this->config['cache']['stockageChemin'],
'duree_de_vie' => $this->config['cache']['dureeDeVie']);
$this->cache = $conteneur->getCacheSimple($cacheOptions);
$this->definirVersion();
$this->definirFormat();
$this->definirTable($this->version);
}
abstract function definirTable($version);
private function getParametreTableau($cle) {
$tableau = array();
$parametre = Config::get($cle);
if (empty($parametre) === false) {
$tableauPartiel = explode(',', $parametre);
$tableauPartiel = array_map('trim', $tableauPartiel);
foreach ($tableauPartiel as $champ) {
if (strpos($champ, '=') !== false && strlen($champ) >= 3) {
list($cle, $val) = explode('=', $champ);
$tableau[trim($cle)] = trim($val);
} else {
$tableau[] = trim($champ);
}
}
}
return $tableau;
}
//+--------------------------traitement ressources ou paramètres -------------------------------------------+
//+---- paramètres ----+
//on n'affiche qu'une version de graphique à la fois ( la dernière ou celle demandée )
private function definirVersion() {
$this->chargerVersions();
if( (!isset($this->parametres['version.projet']) ) || ((isset($this->parametres['version.projet']) )&&
(($this->parametres['version.projet'] == '+') || ($this->parametres['version.projet'] == '')))){
$this->version = $this->metadonnees[0]['version'];
} else {
$this->version = $this->parametres['version.projet'];
}
}
private function definirFormat() {
if (isset($this->parametres['retour.format']) ){
if (preg_match("/^[0-9]+$/", $this->parametres['retour.format'])){
$this->largeurSVG = $this->parametres['retour.format'];
}else {
$erreur = "Erreur : Entrez la largeur voulue (en pixels) pour le paramètre retour.format.";
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
throw new Exception($erreur, $code);
}
}
if (!isset($this->parametres['retour']) ){
$this->parametres['retour'] = 'image/svg+xml';
}else {
if (( $this->parametres['retour'] != 'image/svg+xml')&& ( $this->parametres['retour'] != 'image/png')){
$erreur = "Erreur : choisissez le format de retour pour le paramètre retour : image/svg%2Bxml ou image/png.";
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
throw new Exception($erreur, $code);
}
}
}
private function chargerVersions() {
$requete = "SELECT version ".
"FROM ".Config::get('bdd_table_meta')." ".
"ORDER BY date_creation DESC ";
$resultats = $this->Bdd->recupererTous($requete);
if (!is_array($resultats) || count($resultats) <= 0) {
$message = "Les méta-données n'ont pu être chargée pour la ressource demandée";
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
}
$this->metadonnees = $resultats;
}
//+----- ressources -----+
public function traiterRessources() {
$this->traiterReferentielEtNum();
$this->traiterTypeGraphique();
}
abstract function traiterReferentielEtNum();
abstract function traiterTypeGraphique();
//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
public function assemblerLaRequete() {
$requete = ' SELECT '.$this->requete_champs.' FROM '.$this->table.' '.$this->retournerRequeteCondition();
return $requete;
}
public function retournerRequeteCondition() {
$condition = '';
if ($this->requete_condition !== "") {
$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
}
return $condition;
}
//+-------------------------- formatage du résultat -------------------------------------------+
 
public function obtenirResultat($resultat) {
if ($resultat == ""){
$message = 'La requête SQL formée comporte une erreur!';
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
}elseif ($resultat) {
if ((count($this->ressources)) != 0) {
$Atraiter = array_filter($resultat[0],function($var){return ($var != '');});
if (!empty($Atraiter)) {
$this->traiterValeursEcologiques($Atraiter);
$svg = $this->genererSVG();
$resultat = new ResultatService();
$resultat->corps = ($this->parametres['retour'] == 'image/png') ? $this->convertirEnPNG($svg) : $svg;
$resultat->mime = $this->parametres['retour'];
} else {
$resultat = null;
}
}
} else {
$resultat = null;
}
return $resultat;
}
//+----- modification svg -----+
 
public function traiterValeursEcologiques($valeur){
$this->valeurs_champs = $valeur;
foreach($this->valeurs_champs as $cle => $val){
if (preg_match("/ve_humidite_edaph/", $cle)) {
$this->valeurs_en_pourcentage[$cle] = round($val/13,1);
} elseif (preg_match("/ve_salinite/", $cle)) {
$this->valeurs_en_pourcentage[$cle] = round(($val+1)/11,1);
} else {
$this->valeurs_en_pourcentage[$cle] = round($val/10,1);
}
}
}
public function genererSVG(){
$svg = null;
$this->dom = new DOMDocument('1.0', 'UTF-8');
$this->dom->validateOnParse = true;
$fichierSvg = $this->cheminGraphBase."".$this->nomGraphique.".svg";
$this->dom->load($fichierSvg);
$this->changerValeursSVG();
$svg = $this->dom->saveXML();
return $svg;
}
abstract function changerValeursSVG();
public function recupererOntologies($valeur, $champs){
$url = $this->ajouterHref('ontologies',$this->champs_ontologiques[$champs].':'.urlencode(urlencode($valeur)));
$val = $this->consulterHref($url);
return $val;
}
/// +---- convertir png ----+
public function convertirEnPNG($svg) {
$png = null;
if (isset($this->convertisseur)) {
if ($this->convertisseur == 'imagick') {
if (extension_loaded('imagick')) {
$png = $this->convertirEnPNGAvecImageMagick($svg);
} else {
$message = "Impossible de générer l'image sur le serveur. Extension ImageMagick absente.";
$code = RestServeur::HTTP_CODE_ERREUR;
throw new Exception($message, $code);
}
} else if ($this->convertisseur == 'rsvg') {
$png = $this->convertirEnPNGAvecRsvg($svg);
} else {
$message = "Le convertisseur indiqué '{$this->convertisseur}' ne fait pas partie de la liste ".
"des convertisseurs disponibles : imagick, rsvg.";
$code = RestServeur::HTTP_CODE_ERREUR;
throw new Exception($message, $code);
}
} else {
$message = "Veuillez indiquer le convertisseur de svg à utiliser pour le service.";
$code = RestServeur::HTTP_CODE_ERREUR;
throw new Exception($message, $code);
}
return $png;
}
public function convertirEnPNGAvecImageMagick($svg) {
$convertisseur = new Imagick();
$convertisseur->setBackgroundColor(new ImagickPixel('#F8F8F8'));
$convertisseur->readImageBlob($svg);
$convertisseur->setImageFormat('png32');
$convertisseur->resizeImage($this->largeurSVG, 0 , imagick::FILTER_LANCZOS, 0, true);
$png = $convertisseur->getImageBlob();
$convertisseur->clear();
$convertisseur->destroy();
return $png;
}
public function convertirEnPNGAvecRsvg($svg) {
$idFichier = $this->getIdFichier();
$fichierPng = $this->config['cache']['stockageChemin']."".$idFichier.'.png';
$fichierSvg = $this->config['cache']['stockageChemin']."".$idFichier.'.svg';
file_put_contents($fichierSvg, $svg);
$commande = "rsvg-convert $fichierSvg -b #F8F8F8 -w $this->largeurSVG -a -o $fichierPng";
$rsvg = exec($commande);
$this->indexerFichierPng($fichierPng);
$png = file_get_contents($fichierPng);
return $png;
}
public function indexerFichierPng($fichierPng) {
$img = imagecreatefrompng($fichierPng);
imagetruecolortopalette($img, false, 32);
imagepng($img, $fichierPng, 9, PNG_ALL_FILTERS);
}
public function getIdFichier(){
$idfichier = str_replace(".","-",$this->ressources[1]);
$idfichier = str_replace(':','-',$idfichier);
$idfichier .= "-".$this->ressources[0];
return $idfichier;
}
}
?>