Subversion Repositories eFlore/Projets.eflore-projets

Compare Revisions

Ignore whitespace Rev 5 → Rev 6

/trunk/services/modules/0.1/coste/Textes.php
New file
0,0 → 1,449
<?php
 
/**
* Description :
* Classe Textes.php est une classe qui contient les méthodes permettant la consultation des textes (clé de détermination
* ou description). Les identifiants des textes correspondent au tag de type CleCoste1937TXXPXXNomSci pour le projet
* coste.
* Encodage en entrée : utf8
* Encodage en sortie : utf8
* @package framework-v3
* @author Jennifer Dhé <jennifer.dhe@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-${year} Tela Botanica (accueil@tela-botanica.org)
*/
 
 
class Textes extends Commun {
protected $limite_requete = array('depart' => 0, 'limite' => 100);
protected $requete_champ = ' t1.resource AS tag, t1.value AS num_nom, t2.value AS titre, p.body AS texte';
protected $requete_condition = null;
protected $requete_group_by = ' ORDER BY CAST( t1.value AS DECIMAL )';
protected $table_retour;
protected $retour_format = 'max';
protected $format_reponse = 'textes';
protected $table_param = array();
protected $total_resultat;
protected $table_ressources = array();
protected $html = 'txt';
protected $recherche = 'stricte';
protected $service = 'textes';
protected $masque = null;
public function consulter($ressources, $parametres) {
$resultat_formate = '';
$this->table = array(Config::get("bdd_triples")." t1", Config::get("bdd_triples")." t2", Config::get("bdd_pages")." p");
$this->traiterParametres($parametres);
$this->traiterRessources($ressources);
if ($this->corps_http == '' && $this->entete_http == '') {
$requete = $this->assemblerLaRequete();
$resultat = $this->getBdd()->recupererTous($requete);
$this->testerResultat($resultat, $resultat_formate, $requete);
}
return $this->formerReponseHTTP($resultat_formate);
}
public function testerResultat($resultat, &$resultat_formate, $requete) {
if ($resultat == '') { //cas ou coste/services/ ou la requete comporte des erreurs
$e = 'La requête SQL formée comporte une erreur!';
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE,$e);
Debug::printr($requete);
} elseif ($resultat) {
$resultat_formate = $this->retournerResultatFormate($resultat);
} else {
if ($this->format_reponse = 'textes/relations') {
$resultat_formate = 'null';
} else {
$m = 'Données introuvables dans la base';
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE,$m);
Debug::printr($requete);
}
}
}
//---------------------------------TRAITER RESSOURCES-----------------------------------------------------------------
public function traiterRessources(&$ressources) {
if (isset($ressources) && !empty($ressources)) {
$this->table = Config::get('bdd_triples');
$this->table_ressources = $ressources;
if ($ressources[0] == 'relations') {
$this->traiterRelationsProjet($ressources);
$this->format_reponse .= '/relations';
} elseif (isset($this->table_ressources[0]) && !empty($this->table_ressources[0])) {
$this->traiterRessourceId($this->table_ressources[0]);
if (isset($this->table_ressources[1]) && !empty($this->table_ressources[1])) {
$this->format_reponse .= '/champ';
}
} else {
$e = "Erreur dans votre requête. <br/> Les ressources disponibles sont :
<li> /textes/#id (id correspondant au tag de la page </li>
<li> /textes/#id/#champ+#champ </li>
<li> /textes/relations/bdtfx/#id (id correspondant au num_nom du projet bdtfx </li>";
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
}
} else {
$this->traiterRessourcesListe();
}
$this->requete_condition[] = "p.latest = 'Y'";
}
public function traiterRessourceId($id) {
$this->format_reponse .= '/id';
if ($this->recherche == 'etendue') {
$id = '%'.str_replace(' ','%', $id).'%';
}
$this->requete_champ = ' t1.resource AS tag, t1.value AS num_nom, t2.value AS titre, p.body AS texte';
$this->requete_condition[] = 't1.resource LIKE '.$this->getBdd()->proteger($id);
$this->requete_condition[] = 't1.property = "num_nom"';
$this->requete_condition[] = '(t1.resource = t2.resource AND t2.property = "titre")';
$this->requete_condition[] = 't1.resource = p.tag';
$this->table = array(Config::get("bdd_triples").' t1',
Config::get("bdd_triples").' t2',
Config::get("bdd_pages").' p');
$this->ajouterRechercheFloue('t1.resource', $id);
}
public function traiterRessourcesListe() {
$this->requete_condition[] = "t1.property = 'num_nom'";
$this->requete_condition[] = "(t1.resource = t2.resource AND t2.property = 'titre')";
$this->requete_condition[] = "t1.resource = p.tag";
}
public function traiterRelationsProjet($tab_ress) {
$this->table = array(Config::get("bdd_triples")." t1", Config::get("bdd_pages")." p", Config::get("bdd_table").'_v0_1 c');
$projet = $tab_ress[1];
$num_nom = $tab_ress[2];
$nom_champ_projet = "flore_$projet"."_num";
if (isset($tab_ress[3])) {
$type = $tab_ress[3];
if (!in_array($type, array('cle', 'dsc'))) {
$e = "Le type de texte ne peut etre que clé ou description";
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
}
}
$this->requete_condition[] = "c.flore_bdnff_num = ".$this->getBdd()->proteger($num_nom);
$this->requete_condition[] = "t1.value = c.num_nom";
$this->requete_condition[] = "t1.property = 'num_nom'";
$this->requete_condition[] = "t1.resource = p.tag";
$this->requete_champ = 't1.resource AS tag, t1.value AS num_nom, p.body AS texte';
if (isset($type)) $this->requete_condition[] = "t1.resource LIKE ".$this->getBdd()->proteger(ucfirst($type.'%'));
}
public function ajouterRechercheFloue($nom_champ, $id) {
if ($this->recherche == 'floue') {
$id = $this->getBdd()->proteger($id);
$this->requete_condition[array_search("$nom_champ LIKE $id",
$this->requete_condition)] =
"($nom_champ LIKE $id)".
" OR ( SOUNDEX($nom_champ) = SOUNDEX($id))".
" OR SOUNDEX(REVERSE($nom_champ)) = SOUNDEX(REVERSE($id))))";
}
}
//---------------------------------------TRAITER PARAMETRES-------------------------------------------------------------
public function traiterParametres($parametres) {
if (isset($parametres) && !empty($parametres)) {
$this->table_param = $parametres;
if (isset($parametres['recherche']) && $parametres['recherche'] != '') {
$this->recherche = $parametres['recherche'];
}
foreach ($parametres as $param => $val) {
switch ($param) {
case 'retour_format' : $this->retour_format = $val; break;
case 'masque_titre' : $this->ajouterLeFiltreMasque('titre', $val); break;
case 'masque_famille' : $this->ajouterLeFiltreMasque('famille', $val); break;
case 'masque_ns' : $this->ajouterLeFiltreMasque('nom_sci', $val); break;
case 'masque_type' : $this->ajouterLeFiltreMasque('type', $val); break;
case 'masque_txt' : $this->ajouterLeFiltreMasque('texte', $val); break;
case 'masque_page' : $this->ajouterLeFiltreMasque('page',$val); break;
case 'masque_tome' : $this->ajouterLeFiltreMasque('tome',$val); break;
case 'navigation_depart': $this->limite_requete['depart'] = $val; break;
case 'navigation_limite': $this->limite_requete['limite'] = $val; break;
case 'recherche' : break;
default :
$e = "Erreur dans les paramètres de recherche de votre requête : </br> Le parametre $param n\'existe pas.";
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
break;
}
}
}
}
public function ajouterLeFiltreMasque($masque, $valeur) {
if ($this->recherche == 'etendue') {
$valeur = '%'.str_replace(' ','%', $valeur).'%';
}
$valeur = $this->getBdd()->proteger($valeur);
$this->masque[] = "$masque=$valeur";
switch ($masque) {
case 'famille' :
break;
case 'nom_sci' :
$this->requete_condition[] = "t1.value = c.num_nom AND nom_sci LIKE $valeur)";
$this->ajouterRechercheFloue('nom_sci', $valeur);
$this->table[] = Config::get("bdd_table").'_v0_1 c';
break;
case 'type' :
$type = ($this->getBdd()->proteger($valeur) == '"cle"') ? 'Cle%' : 'Dsc%';
$this->requete_condition[] = 't1.resource LIKE '.$this->getBdd()->proteger($type);
break;
case 'texte' :
$this->requete_condition[] = "p.body LIKE $valeur";
$this->ajouterRechercheFloue('p.body', $valeur);
break;
case 'titre' :
$this->requete_condition[] = "t2.value LIKE $valeur";
$this->ajouterRechercheFloue('t2.value', $valeur);
break;
case 'page' :
$this->requete_condition[] = "t1.value = c.num_nom AND c.page = $valeur";
$this->table[] = Config::get("bdd_table").'_v0_1 c';
break;
case 'tome' :
$this->requete_condition[] = "t1.value = c.num_nom AND c.tome = $valeur";
$this->table[] = Config::get("bdd_table").'_v0_1 c';
break;
default :
break;
}
}
//-----------------------------FONCTIONS DASSEMBLAGE DE LA REQUETE-----------------------------------------------------
public function assemblerLaRequete() {
$requete = ' SELECT '.$this->requete_champ.
' FROM '.implode(', ', $this->table)
.$this->retourneRequeteCondition()
.$this->requete_group_by
.$this->formerRequeteLimite();
return $requete;
}
 
public function retourneRequeteCondition() {
$condition = '';
if ($this->requete_condition !== null) {
$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
}
return $condition;
}
public function formerRequeteLimite() {
if (in_array($this->format_reponse , array('textes/id', 'textes/id/relations'))) {
$this->requete_limite = '';
} elseif (($depart = $this->limite_requete['depart']) > ($this->total_resultat = $this->recupererTotalResultat())) {
$this->limite_requete['depart'] =
(($this->total_resultat - $this->limite_requete['limite']) < 0) ? 0 : ($this->total_resultat - $this->limite_requete['limite']);
$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
} else {
$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
}
return $this->requete_limite;
}
/**
* Recupere le nombre total de résultat d'une requete lancée.
* Est utilisée lors de l'affichage du total dans le résultat retourné et pr déterminer les limites et départ
* @return nombre total de résultat
*/
public function recupererTotalResultat() {
$total = null;
$requete = 'SELECT count(*) as nombre FROM '
.implode(', ', $this->table).$this->retourneRequeteCondition()
.$this->requete_group_by;
$res = $this->getBdd()->recuperer($requete);
if ($res) {
$total = $res['nombre'];
} else {
$e = 'Données introuvables dans la base ou erreur dans la requête SQL';
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
}
return $total;
}
 
//-------------------FONCTIONS POUR LE FORMATAGE EN JSON----------------------------------------------------------------
public function retournerResultatFormate($resultat) {
$reponse = '';
switch ($this->format_reponse) {
case 'textes' : $reponse = $this->formaterTextes($resultat); break;
case 'textes/relations' : $reponse = $this->formaterTextesRelations($resultat); break;
case 'textes/id' : $reponse = $this->formaterTextesId($resultat[0]); break;
case 'textes/id/champ' : $reponse = $this->formaterTextesIdChamp($resultat[0]); break;
default : break;
}
$reponse = json_encode($reponse);
return $reponse;
}
public function formaterTextes($resultat) {
$this->ajouterEnteteResultat($resultat);
$reponse['entete'] = $this->table_retour;
$this->table_retour = array();
foreach ($resultat as $description) {
$tag = $description['tag'];
$this->afficherDonnees('titre', $description['titre']);
$type = (preg_match('/^Cle.*$/', $tag)) ? '1' : '2' ;
$this->afficherDonnees('type', $type);
$this->table_retour['texte'] = $description['texte'];
$reponse['resultat'][$tag] = $this->table_retour;
$this->table_retour = array();
$reponse['resultat'][$tag]['href'] = $this->ajouterHref('textes', $tag);
}
return $reponse;
}
public function formaterTextesRelations($resultat) {
foreach ($resultat as $description) {
$tag = $description['tag'];
$type = (preg_match('/^Cle.*$/', $tag)) ? '1' : '2' ;
$this->afficherDonnees('type', $type);
$this->table_retour['texte'] = $description['texte'];
$reponse[$tag] = $this->table_retour;
$this->table_retour = array();
$reponse[$tag]['href'] = $this->ajouterHref('textes', $tag);
}
return $reponse;
}
public function ajouterEnteteResultat($resultat) {
if ($this->masque) {
$this->table_retour['masque'] = implode('&', $this->masque);
}
$this->table_retour['depart'] = $this->limite_requete['depart'];
$this->table_retour['limite'] = $this->limite_requete['limite'];
$this->table_retour['total'] = $this->total_resultat;
//formuler les urls
$url = $this->formulerUrl($this->total_resultat, '/'.$this->service);
if ($url['precedent'] != '') { $this->table_retour['href.precedent'] = $url['precedent']; }
if ($url['suivant'] != '') { $this->table_retour['href.suivant'] = $url['suivant']; }
}
public function formaterTextesId($reponse) {
$this->afficherDonnees('titre', $reponse['titre']);
$type = (preg_match('/^Cle.*$/', $reponse['tag'])) ? '2' : '1' ;
$this->afficherDonnees('type', $type);
$this->table_retour['texte'] = $reponse['texte'];
$this->afficherDonnees('id', $reponse['num_nom']);
return $this->table_retour;
}
public function formaterTextesIdChamp($resultat) {
//on recupère tous les resultats possibles
$reponse_id = $this->formaterTextesId($resultat);
$this->table_retour = array();
//on recupère les résultats demandés à partir du tableau de résultat complet
$this->table_retour['id'] = $resultat['tag'];
$champs = explode(' ', $this->table_ressources[1]);
foreach ($champs as $champ) {
if ($this->verifierValiditeChamp($champ, $reponse_id)) {
if (preg_match('/^[^.]+\.\*$/', $champ)) {
$this->afficherPointEtoile($champ, $reponse_id);
} else {
$this->table_retour[$champ] = $reponse_id[$champ];
}
}
}
return $this->table_retour;
}
public function afficherPointEtoile($champ, $reponse_id) {
preg_match('/^([^.]+\.)\*$/', $champ, $match);
foreach ($reponse_id as $chp => $valeur) {
if (strrpos($chp, $match[1]) !== false) {
$this->table_retour[$chp] = $valeur;
}
}
}
public function verifierValiditeChamp($champ, $reponse_id) {
$validite = false;
preg_match('/^([^.]+)(:?\.([^.]+))?$/', $champ, $match);
if (array_key_exists($match[1], $reponse_id)) {
$validite = true;
if (isset($match[2]) && !empty($match[2])) {
if ($match[1] == 'type') {
$suffixes = array('.code', '.href', '.*');
$validite = (in_array($match[2], $suffixes)) ? true : false;
} else {
$validite = false;
}
}
}
if (!$validite) {
$champs = implode('</li><li>', array_keys($reponse_id));
$e = 'Erreur dans votre requête : </br> Le champ "'.$champ.'" n\'existe pas. Les champs disponibles
sont : <li>'.$champs.'</li>';
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
}
return $validite;
}
//------------------------------------------------Fonction d'affichage ------------------------------------------------
public function afficherDonnees($champ, $valeur) {
if ($this->retour_format == 'min') {
$champ = ($champ == 'type') ? $champ.'.code' : $champ;
$this->table_retour[$champ] = $valeur;
} else {
$this->afficherDonneesMax($champ, $valeur);
}
}
public function afficherDonneesMax($champ, $valeur) {
if ($champ == 'id') {
$this->table_retour[$champ] = "coste.".$valeur;
$this->table_retour['nom_sci'] = $this->recupererNomSci($valeur);
$this->table_retour['id.href'] = $this->ajouterHref('noms', $valeur);
} elseif (in_array($champ, array('type'))) {
$this->table_retour[$champ.'.code'] = $valeur;
$this->table_retour[$champ] = $this->recupererSignificationCode($valeur);
$this->table_retour[$champ.'.href'] = $this->ajouterHrefAutreProjet('ontologies', 'texteType:', $valeur, 'commun');
} else {
$this->table_retour[$champ] = $valeur;
}
}
public function recupererSignificationCode($code) {
$url = $this->ajouterHrefAutreProjet('ontologies', 'texteType:', $code.'/nom', 'commun');
$res = $this->consulterHref($url);
return $res->nom;
}
public function recupererNomSci($id) {
$req = 'SELECT nom_sci FROM coste_v0_1 WHERE num_nom = '.$id;
if ($this->html == 'htm') {
$req = str_replace('nom_sci', 'nom_sci_html as nom_sci', $req);
}
$res = $this->getBdd()->recuperer($req);
return $res['nom_sci'];
}
 
}
 
?>