* @author Jean-Pascal MILCENT * @author Aurelien PERONNET * @license GPL v3 * @license CECILL v2 * @copyright 1999-2014 Tela Botanica */ class Commentaires { 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->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() { if ($this->syndication->fluxAdminDemande()) { $this->syndication->demanderAutorisationAdmin(); } $donnees_brutes = $this->getDerniersCommentaires(); $commentaires_formates = $this->formaterPourRss($donnees_brutes) ; return $commentaires_formates; } private function formaterPourRss($elements) { $derniere_info_en_date = reset($elements); $donnees = $this->syndication->construireDonneesCommunesAuFlux('commentaire', $derniere_info_en_date['date']); foreach ($elements as $element) { $donnees['items'][] = $this->construireDonneesCommunesAuxItems($element); } return $donnees; } private function construireDonneesCommunesAuxItems($info) { $item = array(); $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); $item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp); $item['titre'] = $this->creerTitre($info); $item['guid'] = $this->creerGuidItem($info); $item['lien'] = $this->creerLienItem($info); $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); return $item; } private function creerTitre($element) { $nomPropose = htmlspecialchars($element['nom_sel']); $intitule = ($element['nom_sel'] != '') ? "Proposition $nomPropose" : 'Commentaire'; $auteur = htmlspecialchars($this->creerAuteur($element)); $nomSelActuel = htmlspecialchars($element['dob_nom_sel']); $zoneGeo = htmlspecialchars((($element['dob_zone_geo'] != '') ? $element['dob_zone_geo'] : null)); $dateObs = null; if (strpos($element['dob_date_observation'], '0000') === false) { $dateObs = htmlspecialchars(strftime('%d %B %Y', strtotime($element['dob_date_observation']))); } $titre = "$intitule par $auteur pour $nomSelActuel"; $titre .= ($zoneGeo != null) ? " à $zoneGeo" : ""; $titre .= ($dateObs != null) ? " le $dateObs" : ""; return $titre; } private function creerAuteur($info) { $auteur = 'Anonyme'; if ($info['utilisateur_prenom'] != '' && $info['utilisateur_nom'] != '') { $auteur = $info['utilisateur_prenom'].' '.$info['utilisateur_nom']; } return $auteur; } private function creerGuidItem($element) { $guid = sprintf($this->conteneur->getParametre('syndication.commentaire_guid_tpl'), $element['id_commentaire']); return $guid; } private function creerLienItem($element) { $lien = sprintf($this->conteneur->getParametre('obs_fiche_tpl'), $element['dob_id_observation']); return $lien; } 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->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->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)); $contenuCommentaire = ''; if ($etreProposition) { $contenuCommentaire = '
  • '."Nom proposé : $nomPropose
  • ". ((!empty($txt)) ? '
  • '."Argumentaire : $txt
  • " : ''). '
  • '."Auteur de la proposition : $auteur
  • ". '
  • '."Proposé le : $dateCommentaire
  • "; } else { $contenuCommentaire = '
  • '."Commentaire #$idCommentaire :
    $txt
  • ". '
  • '."Auteur du commentaire : $auteur
  • ". '
  • '."Commenté le : $dateCommentaire
  • "; } $description = ''. '

    '."$intitule identiPlante #$idCommentaire pour l'observation #$idObs".'

    '. '
    '. "

    Observation #$idObs

    ". '
      '. '
    • '."Nom saisi actuel : $nomSelActuel
    • ". '
    • '."Lieu : $lieuObs
    • ". '
    • '."Date : $dateObs
    • ". '
    • '."Auteur : $observateur
    • ". '
    '. '
    '. '
    '. "

    $intitule #$idCommentaire

    ". "
      $contenuCommentaire
    ". '
    '; return $description; } private function creerObservateur($info) { $observateur = 'Anonyme'; if ($info['observateur_prenom'] != '' && $info['observateur_nom'] != '') { $observateur = $info['observateur_prenom'].' '.$info['observateur_nom']; } return $observateur; } private function getDerniersCommentaires() { $commentaires = array(); $idsCommentaires = $this->getIdsDerniersCommentaires(); if (!empty($idsCommentaires)) { $idsCommentairesconcat = implode(', ', $idsCommentaires); $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, '. ' dob.prenom_utilisateur AS observateur_prenom, dob.nom_utilisateur AS observateur_nom, dob.courriel_utilisateur AS observateur_courriel '. 'FROM del_commentaire AS dc '. ' INNER JOIN del_observation AS dob '. ' ON dob.id_observation = dc.ce_observation '. "WHERE id_commentaire IN ($idsCommentairesconcat) ". 'ORDER BY dc.date DESC '. 'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().' '. ' -- '.__FILE__.' : '.__LINE__; $commentaires = $this->bdd->recupererTous($requete); } return $commentaires; } private function getIdsDerniersCommentaires() { $clauseWhere = $this->chargerClauseWhere(); $requete = 'SELECT DISTINCT dc.id_commentaire '. 'FROM del_commentaire AS dc '. ' INNER JOIN del_observation AS dob ON (dc.ce_observation = dob.id_observation) '. 'WHERE proposition_initiale != 1 '. 'AND dc.date >= DATE_ADD(CURDATE(), INTERVAL -7 DAY) '. ($clauseWhere != '' ? "AND $clauseWhere " : ''). 'ORDER BY dc.date DESC '. 'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().' '. ' -- '.__FILE__.' : '.__LINE__; //echo $requete; $resultats = $this->bdd->recupererTous($requete); $idsCommentaires = array(); if ($resultats != false && is_array($resultats)) { foreach ($resultats as $infos) { $idsCommentaires[] = $infos['id_commentaire']; } } return $idsCommentaires; } /** * Charger la clause WHERE en fonction des paramètres de masque * */ private function chargerClauseWhere() { $where = array(); $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 "; } } } } return (!empty($where)) ? implode(' AND ', $where) : ''; } private function creerFiltreAuteur($auteurId) { $whereAuteur = ''; if (is_numeric($auteurId)) { $whereAuteur = " dc.ce_utilisateur = $auteurId "; } else { $auteurIdMotif = $this->bdd->proteger($auteurId.'%'); if (strpos($auteurId, '@') !== false) { $whereAuteur = " dc.utilisateur_courriel LIKE $auteurIdMotif "; } else { $tableauNomPrenom = explode(' ', $auteurId, 2); if (count($tableauNomPrenom) == 1) { $whereAuteur = " (dc.utilisateur_nom LIKE $auteurIdMotif OR dc.utilisateur_prenom LIKE $auteurIdMotif) "; } else if (count($tableauNomPrenom) == 2) { // on teste potentiellement un nom prenom ou bien un prénom nom $nomMotif = $this->bdd->proteger($tableauNomPrenom[0].'%'); $prenomMotif = $this->bdd->proteger($tableauNomPrenom[1].'%'); $whereAuteur = ' ('. "(dc.utilisateur_nom LIKE $nomMotif AND dc.utilisateur_prenom LIKE $prenomMotif) ". 'OR '. "(dc.utilisateur_nom LIKE $prenomMotif AND dc.utilisateur_prenom LIKE $nomMotif) ". ') '; } } } return $whereAuteur; } }