New file |
0,0 → 1,366 |
<?php |
// declare(encoding='UTF-8'); |
/** |
* Le web service observations récupère toutes les observations et, pour chacune d'elle, les |
* images qui lui sont associées. |
* |
* @category php 5.2 |
* @package del |
* @subpackage images |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org> |
* @copyright Copyright (c) 2012, Tela Botanica (accueil@tela-botanica.org) |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL |
* @version $Id: Bdd.php 403 2012-02-22 14:35:20Z gduche $ |
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=ApiIdentiplante01Observations |
*/ |
|
// http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire |
class Observation { |
|
private $conteneur; |
private $gestionBdd; |
private $bdd; |
private $id_observation; |
private $imageIds; |
|
public function __construct(Conteneur $conteneur = null) { |
$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur; |
$this->conteneur->chargerConfiguration('config_votes.ini'); |
$this->conteneur->chargerConfiguration('config_mapping_votes.ini'); |
$this->conteneur->chargerConfiguration('config_mapping_commentaires.ini'); |
$this->gestionBdd = $conteneur->getGestionBdd(); |
$this->bdd = $this->gestionBdd->getBdd(); |
} |
|
/** |
* Méthode principale de la classe. |
* Lance la récupération des images dans la base et les place dans un objet ResultatService |
* pour l'afficher. |
* @param array $ressources les ressources situées après l'url de base (ex : http://url/ressource1/ressource2) |
* @param array $parametres les paramètres situés après le ? dans l'url |
* */ |
public function consulter($ressources, $parametres) { |
|
// Gestion des configuration du script |
$this->configurer($ressources); |
$this->verifierConfiguration(); |
|
|
// Lancement du service |
$liaisons = $this->chargerLiaisons(); |
$observation = $this->chargerObservation($liaisons); |
$observation = $this->chargerImages($observation); |
$observation = $this->chargerCommentaires($observation); |
|
// Mettre en forme le résultat et l'envoyer pour affichage |
$resultat = new ResultatService(); |
$resultat->corps = $observation; |
return $resultat; |
} |
|
/*------------------------------------------------------------------------------- |
CONFIGURATION DU SERVICE |
--------------------------------------------------------------------------------*/ |
/** |
* Configuration du service en fonction du fichier de config config_del.ini |
* */ |
private function configurer($ressources) { |
$this->mappingObservation = $this->conteneur->getParametre('mapping_observation'); |
$this->mappingVotes = $this->conteneur->getParametre('mapping_votes'); |
$this->mappingCommentaire = $this->conteneur->getParametre('mapping_commentaire'); |
if (empty($ressources) || sizeof($ressources) > 1 ) { |
$e = 'Le service observation accepete 1 et 1 seule ressource'; |
throw new Exception($e, RestServeur::HTTP_CODE_ERREUR); |
} else { |
$this->id_observation = $ressources[0]; |
} |
} |
|
/** |
* Vérifier que le service est bien configuré |
* */ |
private function verifierConfiguration() { |
|
$erreurs = array(); |
$tableauImages = $this->conteneur->getParametre('observations'); |
if (empty($tableauImages)) { |
$erreurs[] = '- le fichier de configuration ne contient pas le tableau [images] ou celui-ci est vide ;'; |
} else { |
if ($this->conteneur->getParametre('url_service') == null) { |
$erreurs[] = '- paramètre "url_service" manquant ;'; |
} |
|
if ($this->conteneur->getParametre('url_images') == null) { |
$erreurs[] = '- paramètre "url_images" manquant ;'; |
} |
|
} |
|
if (empty($this->mappingObservation)) { |
$erreurs[] = '- le fichier de configuration ne contient pas le tableau [mapping_observation] ou celui-ci est vide ;'; |
} else { |
$champsMappingObs = array('id_observation', 'date_observation', 'date_transmission', 'famille', 'nom_sel', 'nom_sel_nn', 'nt', |
'ce_zone_geo', 'zone_geo', 'lieudit', 'station', 'courriel', 'ce_utilisateur', 'nom', 'prenom'); |
|
foreach ($champsMappingObs as $champ) { |
if (!isset($this->mappingObservation[$champ])) { |
$erreurs[] = '- le mapping du champ "'.$champ.'" pour l\'observation est manquant ;'; |
} |
} |
} |
|
if (empty($this->mappingCommentaire)) { |
$erreurs[] = '- le fichier de configuration ne contient pas le tableau [mapping_commentaire] ou celui-ci est vide ;'; |
} else { |
$champsMappingCom = array('id_commentaire', 'texte', 'ce_utilisateur', 'utilisateur_nom', 'utilisateur_prenom', 'utilisateur_courriel', 'date'); |
foreach ($champsMappingCom as $champ) { |
if (!isset($this->mappingCommentaire[$champ])) { |
$erreurs[] = '- le mapping du champ "'.$champ.'" pour le commentaire est manquant ;'; |
} |
} |
} |
|
if (!empty($erreurs)) { |
$e = 'Erreur lors de la configuration : '."\n"; |
$e .= implode("\n", $erreurs); |
throw new Exception($e, RestServeur::HTTP_CODE_ERREUR); |
} |
} |
|
/*------------------------------------------------------------------------------- |
CHARGEMENT DES OBSERVATIONS |
--------------------------------------------------------------------------------*/ |
/** |
* Chargement depuis la bdd de toutes les liaisons entre images et observations |
* */ |
private function chargerLiaisons() { |
|
$requeteLiaisons = 'SELECT *, dob.commentaire as dob_commentaire '. |
'FROM '.$this->gestionBdd->formaterTable('del_observation', 'dob'). |
'INNER JOIN '.$this->gestionBdd->formaterTable('del_obs_image', 'doi'). |
'ON doi.id_observation = dob.id_observation '. |
'LEFT JOIN del_utilisateur du '. |
'ON du.id_utilisateur = dob.ce_utilisateur '. |
'WHERE doi.id_observation = '.$this->id_observation; |
$requeteLiaisons .= ' GROUP BY doi.id_observation'; |
$requeteLiaisons .= ' ORDER BY date_transmission DESC '; |
$requeteLiaisons .= $this->gestionBdd->getLimitSql(); |
|
return $this->bdd->recuperer($requeteLiaisons); |
} |
|
/** |
* Retourner un tableau d'images formaté en fonction des liaisons trouvées |
* @param $liaisons les liaisons de la table del_obs_images |
* */ |
private function chargerObservation($liaison) { |
$observation = $this->formaterObservation($liaison); |
return $observation; |
} |
|
/** |
* Sélectionner toutes les images de chaque observation |
* @param array $observations la liste des observations |
* */ |
private function chargerImages($observation) { |
$requeteImages = 'SELECT * FROM '. $this->gestionBdd->formaterTable('del_obs_image', 'doi'). |
'INNER JOIN '.$this->gestionBdd->formaterTable('del_image', 'di'). |
'ON doi.id_image = di.id_image '. |
'WHERE doi.id_observation = '.$observation['id_observation']; |
|
$images = $this->bdd->recupererTous($requeteImages); |
$images = $this->formaterImages($images); |
$images = $this->chargerVotesImage($images); |
$observation['images'] = $images; |
|
return $observation; |
} |
|
/** |
* Charger les votes pour chaque image |
* */ |
private function chargerVotesImage($images) { |
$requeteVotes = 'SELECT v.*, p.* FROM '. |
$this->gestionBdd->formaterTable('del_image_vote', 'v'). |
' INNER JOIN del_image_protocole p '. |
'ON v.ce_protocole = p.id_protocole '. |
$this->chargerClauseWhereVotesImage(); |
$resultatsVotes = $this->bdd->recupererTous($requeteVotes); |
|
$votes = $this->formaterVotesImages($resultatsVotes); |
|
foreach ($images as $id => $image) { |
if (isset($votes[$image['id_image']])) { |
foreach($votes[$image['id_image']] as $id_vote => $vote_image) { |
$images[$id]['protocoles_votes'][$id_vote] = $vote_image; |
} |
} |
} |
|
return $images; |
} |
|
private function chargerClauseWhereVotesImage() { |
if (sizeof($this->imageIds) > 0) { |
$chaineImageIds = implode(',', $this->imageIds); |
$where[] = 'v.ce_image IN ('.$chaineImageIds.')'; |
} |
if (isset($this->parametres['protocole'])) { |
$where[] = 'v.ce_protocole = '.$this->proteger($this->parametres['protocole']); |
} |
|
$where = (!empty($where)) ? 'WHERE '.implode(' AND ', $where) : ''; |
return $where; |
} |
|
/** |
* Récupérer tous les commentaires au total |
* @param array $observations la liste des observations à mettre à jour |
* */ |
private function chargerCommentaires($observation) { |
|
$requeteCommentaires = 'SELECT * FROM '.$this->gestionBdd->formaterTable('del_commentaire', 'dc'). |
'WHERE ce_observation = '.$observation['id_observation']; |
$commentaires = $this->bdd->recupererTous($requeteCommentaires); |
|
$commentaires_formates = array(); |
foreach ($commentaires as $commentaire) { |
$commentaire = $this->formaterCommentaire($commentaire); |
if (isset($commentaire['nom_sel']) && $commentaire['nom_sel'] != null) { |
$commentaire['votes'] = $this->chargerVotes($commentaire['id_commentaire']); |
} |
$commentaires_formates[$commentaire['id_commentaire']] = $commentaire; |
} |
$observation['commentaires'] = $commentaires_formates; |
|
return $observation; |
} |
|
private function chargerVotes($id_commentaire) { |
$requeteVotes = 'SELECT * FROM '. |
$this->gestionBdd->formaterTable('del_commentaire_vote'). |
'WHERE ce_proposition = '.$this->proteger($id_commentaire); |
$resultatsVotes = $this->bdd->recupererTous($requeteVotes); |
$votes = array(); |
foreach ($resultatsVotes as $vote) { |
$id_vote = $vote['id_vote']; |
$votes[$id_vote] = $this->formaterVote($vote); |
} |
return $votes; |
} |
|
|
/*------------------------------------------------------------------------------- |
FORMATER ET METTRE EN FORME |
--------------------------------------------------------------------------------*/ |
|
/** |
* Formater les images d'une observation |
* @param array $images les images de l'observation |
* */ |
private function formaterImages($images) { |
$imagesRetour = array(); |
foreach ($images as $image) { |
|
$this->imageIds[] = $image['id_image']; |
$imageCourante = array(); |
$imageCourante['id_image'] = $image['id_image']; |
$imageCourante['date'] = $image['date_prise_de_vue']; |
$imageCourante['binaire.href'] = $this->formaterLienImage($image['id_image']); |
$imageCourante['hauteur'] = $image['hauteur']; |
$imageRetour['largeur'] = $image['largeur']; |
|
$imagesRetour[] = $imageCourante; |
} |
|
return $imagesRetour; |
} |
|
/** |
* Formater une observation depuis une ligne liaison |
* @param $liaison liaison issue de la recherche |
* @return $observation l'observation mise en forme |
* */ |
private function formaterObservation($liaison) { |
$observation = array(); |
|
foreach ($this->mappingObservation as $nomOriginal => $nomFinal) { |
$observation[$nomFinal] = $liaison[$nomOriginal]; |
} |
|
$observation['images'] = array(); |
|
return $observation; |
} |
|
/** |
* Formater le lien de l'image en fonction du fichier de config et de l'identifiant de l'image |
* */ |
private function formaterLienImage($idImage) { |
$idImage = sprintf('%09s', $idImage); |
$url = $this->conteneur->getParametre('url_images'); |
$urlImage = str_replace('%s', $idImage, $url); |
return $urlImage; |
} |
|
private function proteger($valeur) { |
if (is_array($valeur)) { |
return $this->bdd->protegerTableau($valeur); |
} else { |
return $this->bdd->proteger($valeur); |
} |
} |
|
/** |
* |
* Formate un commentaire en fonction du fichier de configuration |
*/ |
private function formaterCommentaire($commentaire) { |
$commentaire_formate = array(); |
foreach ($this->mappingCommentaire as $nomOriginal => $nomFinal) { |
if (isset($commentaire[$nomOriginal])) { |
$commentaire_formate[$nomFinal] = $commentaire[$nomOriginal]; |
} |
} |
return $commentaire_formate; |
} |
|
/** |
* Formater un vote en fonction du fichier de configuration config_votes.ini |
* @param $votes array() |
* */ |
private function formaterVote($vote) { |
$retour = array(); |
foreach ($vote as $param => $valeur) { |
$retour[$this->mappingVotes[$param]] = $valeur; |
} |
return $retour; |
} |
|
/** |
* Formater une observation depuis une ligne liaison |
* @param $liaison liaison issue de la recherche |
* @return $observation l'observation mise en forme |
* */ |
private function formaterVotesImages($votes) { |
$retour = array(); |
foreach ($votes as $vote) { |
$retour_vote = array(); |
foreach ($vote as $param=>$valeur) { |
if (strpos($this->mappingVotes[$param], 'protocole.') === 0) { |
$retour_protocole[$this->mappingVotes[$param]] = $valeur; |
} else { |
$retour_vote[$this->mappingVotes[$param]] = $valeur; |
} |
} |
if (!isset($retour[$vote['ce_image']][$vote['ce_protocole']])) { |
$retour[$vote['ce_image']][$vote['ce_protocole']] = $retour_protocole; |
} |
$retour[$vote['ce_image']][$vote['ce_protocole']]['votes'][$vote['id_vote']] = $retour_vote; |
} |
|
return $retour; |
} |
} |
?> |