Subversion Repositories eFlore/Applications.del

Compare Revisions

Ignore whitespace Rev 1820 → Rev 1821

/trunk/services/modules/0.1/syndication/Commentaires.php/SyndicationCommentaires.php
14,26 → 14,28
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
*/
class SyndicationCommentaires {
class Commentaires {
 
private $mappingFiltre = array();
private $conteneur = null;
private $bdd = null;
private $navigation = null;
private $categorie = 'Commentaires';
private $conteneur;
private $contexte;
private $bdd;
private $navigation;
private $mapping = array();
private $syndication;
 
public function __construct(Conteneur $conteneur = null) {
$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
$this->conteneur->chargerConfiguration('config_syndication_commentaires.ini');
$this->mappingFiltre = $this->conteneur->getParametre('mapping_masque');
$this->contexte = $this->conteneur->getContexte();
$this->bdd = $this->conteneur->getBdd();
$this->navigation = $this->conteneur->getNavigation();
$this->syndication = $this->conteneur->getSyndicationOutils();
$this->mapping = $this->conteneur->getParametreTableau('syndication.mapping');
}
 
public function consulter($params = array()) {
$this->verifierConfiguration();
 
if ($this->fluxAdminDemande()) {
$this->demanderAutorisationAdmin();
public function consulter() {
if ($this->syndication->fluxAdminDemande()) {
$this->syndication->demanderAutorisationAdmin();
}
 
$donnees_brutes = $this->getDerniersCommentaires();
41,73 → 43,18
return $commentaires_formates;
}
 
/**
* Vérifier que le service est bien configuré
* */
public function verifierConfiguration() {
$erreurs = array();
 
if (empty($this->mappingFiltre)) {
$erreurs[] = 'Le fichier de configuration ne contient pas le tableau [mapping_masque] ou celui-ci est vide.';
}
 
if (!empty($erreurs)) {
$e = 'Erreur lors de la configuration : '."\n";
$e .= implode("\n", $erreurs);
throw new Exception($e, RestServeur::HTTP_CODE_ERREUR);
}
}
 
/**
* Verifier si le flux admin est demande
*/
private function fluxAdminDemande() {
return $this->conteneur->getParametre('admin') != null && $this->conteneur->getParametre('admin') == 1;
}
 
private function demanderAutorisationAdmin() {
$verification = $this->conteneur->getControleAcces();
$verification->demanderAuthentificationAdmin();
}
 
private function formaterPourRss($elements) {
$donnees = $this->construireDonneesCommunesAuFlux($elements);
$derniere_info_en_date = reset($elements);
$donnees = $this->syndication->construireDonneesCommunesAuFlux('commentaire', $derniere_info_en_date['date']);
foreach ($elements as $element) {
$identifiants[$element['id_commentaire']] = $element['id_commentaire'];
}
foreach ($elements as $element) {
$donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
}
return $donnees;
}
 
private function construireDonneesCommunesAuFlux($infos) {
$donnees = array();
$donnees['guid'] = htmlspecialchars($this->creerUrlService());
$donnees['titre'] = 'identiPlante : commentaires et propositions';
$donnees['description'] = 'Ce flux regroupe les dernières déterminations et commentaires rédigés dans l\'application identiPlante';
$donnees['lien_service'] = htmlspecialchars($this->creerUrlService());
$donnees['lien_del'] = $this->conteneur->getParametre('obsAppliLien');
$donnees['editeur'] = $this->conteneur->getParametre('editeur');
$derniere_info_en_date = reset($infos);
$date_modification_timestamp = $this->convertirDateHeureMysqlEnTimestamp($derniere_info_en_date['date']);
$donnees['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
$donnees['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
$donnees['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
$donnees['annee_courante'] = date('Y');
$donnees['generateur'] = 'DEL - SyndicationCommentaire';
$donnees['generateur_version'] = (preg_match('/([0-9]+)/', '$Revision$', $match)) ? $match[1] : '0';
return $donnees;
}
 
private function creerUrlService() {
$url_service = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
return $url_service;
}
 
private function construireDonneesCommunesAuxItems($info) {
$item = array();
$date_modification_timestamp = $this->convertirDateHeureMysqlEnTimestamp($info['date']);
$date_modification_timestamp = $this->syndication->convertirDateHeureMysqlEnTimestamp($info['date']);
$item['date_maj_simple'] = strftime('%A %d %B %Y à %H:%M', $date_modification_timestamp);
$item['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
115,7 → 62,7
$item['titre'] = $this->creerTitre($info);
$item['guid'] = $this->creerGuidItem($info);
$item['lien'] = $this->creerLienItem($info);
$item['categorie'] = $this->creerCategorie($item);
$item['categorie'] = htmlentities($this->categorie);
$item['description'] = $this->creerDescription($info, $item);
$item['description_encodee'] = htmlspecialchars($this->creerDescription($info, $item));
$item['modifier_par'] = $this->creerAuteur($info);
122,19 → 69,6
return $item;
}
 
protected function convertirDateHeureMysqlEnTimestamp($date_heure_mysql){
$timestamp = 0;
// Le date de 1970-01-01 pose problème dans certains lecteur de Flux, on met donc la date de création de Tela
$date_heure_mysql = ($date_heure_mysql == '0000-00-00 00:00:00') ? '1999-12-14 00:00:00' : $date_heure_mysql;
if ($date_heure_mysql != '0000-00-00 00:00:00') {
$val = explode(' ', $date_heure_mysql);
$date = explode('-', $val[0]);
$heure = explode(':', $val[1]);
$timestamp = mktime((int) $heure[0], (int) $heure[1], (int) $heure[2], (int) $date[1], (int) $date[2], (int) $date[0]);
}
return $timestamp;
}
 
private function creerTitre($element) {
$nomPropose = htmlspecialchars($element['nom_sel']);
$intitule = ($element['nom_sel'] != '') ? "Proposition $nomPropose" : 'Commentaire';
156,32 → 90,26
}
 
private function creerGuidItem($element) {
$guid = sprintf($this->conteneur->getParametre('commentaireGuidTpl'), $element['id_commentaire']);
$guid = sprintf($this->conteneur->getParametre('syndication.commentaire_guid_tpl'), $element['id_commentaire']);
return $guid;
}
 
private function creerLienItem($element) {
$lien = sprintf($this->conteneur->getParametre('obsFicheTpl'), $element['dob_id_observation']);
$lien = sprintf($this->conteneur->getParametre('obs_fiche_tpl'), $element['dob_id_observation']);
return $lien;
}
 
private function creerCategorie($element) {
$categorie = 'Commentaires';
$categorie = htmlentities($categorie);
return $categorie;
}
 
private function creerDescription($donnees, $item) {
$idCommentaire = $donnees['id_commentaire'];
$idObs = $donnees['dob_id_observation'];
$nomPropose = ($donnees['nom_sel'] != '') ? htmlspecialchars($donnees['nom_sel']) : '';
$dateCommentaire = htmlspecialchars(strftime('%A %d %B %Y à %H:%M', $this->convertirDateHeureMysqlEnTimestamp($donnees['date'])));
$dateCommentaire = htmlspecialchars(strftime('%A %d %B %Y à %H:%M', $this->syndication->convertirDateHeureMysqlEnTimestamp($donnees['date'])));
$nomSelActuel = htmlspecialchars($donnees['dob_nom_sel']);
$etreProposition = ($nomPropose != '') ? true : false;
$intitule = ($etreProposition) ? 'Proposition' : 'Commentaire';
$txt = ($donnees['texte'] != '') ? htmlspecialchars($donnees['texte']) : '';
$auteur = htmlspecialchars($this->creerAuteur($donnees)).
($this->fluxAdminDemande() ? ' ('.$donnees['utilisateur_courriel'].')' : '');
($this->syndication->fluxAdminDemande() ? ' ('.$donnees['utilisateur_courriel'].')' : '');
$lieuObs = htmlspecialchars((($donnees['dob_zone_geo'] != '') ? $donnees['dob_zone_geo'] : '?'));
$dateObs = htmlspecialchars(str_replace(' 00:00:00', '', $donnees['dob_date_observation']));
$observateur = htmlspecialchars($this->creerObservateur($donnees));
227,6 → 155,7
}
 
private function getDerniersCommentaires() {
$clauseWhere = $this->chargerClauseWhere();
$requete = 'SELECT DISTINCT dc.*, '.
' dob.id_observation AS dob_id_observation, dob.ce_zone_geo AS dob_ce_zone_geo, '.
' dob.zone_geo AS dob_zone_geo, dob.date_observation AS dob_date_observation, dob.nom_sel AS dob_nom_sel, '.
237,9 → 166,10
' LEFT JOIN del_utilisateur AS duo '.
' ON dob.ce_utilisateur = duo.id_utilisateur '.
'WHERE proposition_initiale != 1 '.
$this->chargerClauseWhere().' '.
($clauseWhere != '' ? "AND $clauseWhere " : '').
'ORDER BY dc.date DESC '.
'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().' ';
'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().' '.
' -- '.__FILE__.' : '.__LINE__;
 
$elements = $this->bdd->recupererTous($requete);
return $elements;
250,33 → 180,30
* */
private function chargerClauseWhere() {
$where = array();
 
$tableauMasque = $this->navigation->getFiltre();
if (!empty($tableauMasque)) {
foreach ($tableauMasque as $idMasque => $valeurMasque) {
$idMasque = str_replace('masque.', '', $idMasque);
$champ = $this->mappingFiltre[$idMasque];
$valeurMasquePattern = $this->bdd->proteger($valeurMasque.'%');
$valeurMasqueProtegee = $this->bdd->proteger($valeurMasque);
 
switch ($idMasque) {
case 'espece':
$where[] = " dob.$champ LIKE $valeurMasquePattern OR dc.$champ LIKE $valeurMasquePattern ";
break;
case 'auteur':
$where[] = $this->creerFiltreAuteur($valeurMasque);
break;
default:
$where[] = " $champ = $valeurMasqueProtegee ";
$filtres = $this->navigation->getFiltre();
if (!empty($filtres)) {
$filtrePossibles = $this->conteneur->getParametreTableau('syndication.commentaire_filtres');
foreach ($filtres as $cleFiltre => $valeur) {
if (in_array($cleFiltre, $filtrePossibles)) {
if (isset($this->mapping[$cleFiltre])) {
$champ = $this->mapping[$cleFiltre];
}
switch ($cleFiltre) {
case 'espece':
$valeurP = $this->bdd->proteger($valeur.'%');
$where[] = " dob.$champ LIKE $valeurP OR dc.$champ LIKE $valeurP ";
break;
case 'auteur':
$where[] = $this->creerFiltreAuteur($valeur);
break;
default:
$valeurP = $this->bdd->proteger($valeur);
$where[] = " $champ = $valeurP ";
}
}
}
}
 
$whereSql = '';
if (!empty($where)) {
$whereSql = ' AND '.implode('AND', $where);
}
return $whereSql;
return (!empty($where)) ? implode('AND', $where) : '';
}
 
private function creerFiltreAuteur($auteurId) {
286,9 → 213,13
} else {
$auteurIdMotif = $this->bdd->proteger($auteurId.'%');
 
if (strpos($auteurId, '@') === false) {
if (strpos($auteurId, '@') !== false) {
$whereAuteur = " dc.utilisateur_courriel LIKE $auteurIdMotif ";
} else {
$tableauNomPrenom = explode(' ', $auteurId, 2);
if (count($tableauNomPrenom) == 2) {
if (count($tableauNomPrenom) != 2) {
$whereAuteur = "(dc.utilisateur_nom LIKE $auteurIdMotif OR dc.utilisateur_prenom LIKE $auteurIdMotif) ";
} else {
// on teste potentiellement un nom prenom ou bien un prénom nom
$nomMotif = $this->bdd->proteger($tableauNomPrenom[0].'%');
$prenomMotif = $this->bdd->proteger($tableauNomPrenom[1].'%');
298,14 → 229,9
'OR '.
"(dc.utilisateur_nom LIKE $nomMotif AND dc.utilisateur_prenom LIKE $prenomMotif) ".
') ';
} else {
$whereAuteur = "(dc.utilisateur_nom LIKE $auteurIdMotif OR dc.utilisateur_prenom LIKE $auteurIdMotif) ";
}
} else {
$whereAuteur = " dob.utilisateur_courriel LIKE $auteurIdMotif ";
}
}
return $whereAuteur;
}
}
?>
}