Subversion Repositories eFlore/Applications.del

Rev

Rev 2008 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
// declare(encoding='UTF-8');
/**
 * Service fournissant des informations concernant les commentaire de DEL au format RSS1, RSS2 ou ATOM.
 *
 * @category   DEL
 * @package    Services
 * @subpackage Syndication
 * @version    0.1
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
 * @author     Aurelien PERONNET <aurelien@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>
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
 */
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 =
                                '<li><span class="champ">'."Nom proposé :</span> <em>$nomPropose</em></li>".
                                ((!empty($txt)) ? '<li><span class="champ">'."Argumentaire :</span> $txt</li>" : '').
                                '<li><span class="champ">'."Auteur de la proposition :</span> $auteur</li>".
                                '<li><span class="champ">'."Proposé le :</span> $dateCommentaire</li>";
                } else {
                        $contenuCommentaire =
                                '<li><span class="champ">'."Commentaire #$idCommentaire :</span> <pre>$txt</pre></li>".
                                '<li><span class="champ">'."Auteur du commentaire :</span> $auteur</li>".
                                '<li><span class="champ">'."Commenté le :</span> $dateCommentaire</li>";
                }

                $description = '<style>.champ{color:grey} .gauche{float:left;padding:0 20px 0 0;} ul{list-style-type:none;padding:0;}</style>'.
                        '<h2>'."$intitule identiPlante #$idCommentaire pour l'observation #$idObs".'</h2>'.
                        '<div class="gauche">'.
                        "       <h3>Observation #$idObs</h3>".
                        '       <ul>'.
                        '               <li><span class="champ">'."Nom saisi actuel :</span> <em>$nomSelActuel</em></li>".
                        '               <li><span class="champ">'."Lieu :</span> $lieuObs</li>".
                        '               <li><span class="champ">'."Date :</span> $dateObs</li>".
                        '               <li><span class="champ">'."Auteur :</span> $observateur</li>".
                        '       </ul>'.
                        '</div>'.
                        '<div class="gauche">'.
                        "       <h3>$intitule #$idCommentaire</h3>".
                        "       <ul>$contenuCommentaire</ul>".
                        '</div>';
                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, '.
                                '       duo.prenom AS observateur_prenom, duo.nom AS observateur_nom, duo.courriel AS observateur_courriel '.
                                'FROM del_commentaire AS dc '.
                                '       INNER JOIN del_observation AS dob '.
                                '               ON dob.id_observation = dc.ce_observation '.
                                '       LEFT JOIN del_utilisateur AS duo '.
                                '               ON dob.ce_utilisateur = duo.id_utilisateur '.
                                "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) '.
                        '       LEFT JOIN del_utilisateur AS duo ON (dob.ce_utilisateur = duo.id_utilisateur) '.
                        'WHERE proposition_initiale != 1 '.
                        ($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;
        }
}