Subversion Repositories eFlore/Applications.del

Rev

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

<?php
// declare(encoding='UTF-8');
/**
 * Service fournissant des informations concernant les votes sur les images de DEL en fonction d'un protocole
 * 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 Votes {

        private $categorie = 'Vote protocole';
        private $conteneur;
        private $bdd;
        private $navigation;
        private $syndication;
        private $mapping = array();

        public function __construct(Conteneur $conteneur) {
                $this->conteneur = $conteneur;
                $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->getDerniersVotesImage();
                $commentaires_formates = $this->formaterPourRss($donnees_brutes) ;
                return $commentaires_formates;
        }

        /**
         * Formater les données pour mettre en page le RSS
         * */
        private function formaterPourRss($elements) {
                $derniere_info_en_date = reset($elements);
                $donnees = $this->syndication->construireDonneesCommunesAuFlux('vote', $derniere_info_en_date['date_vote']);
                foreach ($elements as $element) {
                        $donnees['items'][] = $this->construireDonneesCommunesAuxItems($element);
                }
                return $donnees;
        }

        /**
         * Générer les données communes & spécifiques à chaque item
         * */
        private function construireDonneesCommunesAuxItems($info) {
                $item = array();
                $date_modification_timestamp = strtotime($info['date_vote']);
                $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->creerVotant($info);
                return $item;
        }

        private function creerGuidItem($element) {
                $guid = sprintf($this->conteneur->getParametre('syndication.vote_guid_tpl'), $element['id_vote']);
                return $guid;
        }

        private function creerLienItem($element) {
                $lien = sprintf($this->conteneur->getParametre('img_fiche_tpl'), $element['ce_image']);
                return $lien;
        }

        private function creerTitre($element) {
                $noteVote = $element['valeur'];
                $nomSci = htmlspecialchars($element['nom_sel']);
                $votant = htmlspecialchars($this->creerVotant($element));
                $observateur = htmlspecialchars($this->creerObservateur($element));

                $titre = "Vote $noteVote par $votant pour $nomSci de $observateur";
                return $titre;
        }

        private function creerDescription($donnees, $item) {
                $idVote = htmlspecialchars($donnees['id_vote']);
                $idObs = htmlspecialchars($donnees['id_observation']);
                $idImg = htmlspecialchars($donnees['ce_image']);
                $urlImg = $this->syndication->getUrlImage($donnees['ce_image']);
                $miniatureUrl = $this->syndication->getUrlImage($donnees['ce_image'], 'CRS');
                $nomSelActuel = htmlspecialchars($donnees['nom_sel']);
                $dateObs = htmlspecialchars(str_replace(' 00:00:00', '', $donnees['date_observation']));
                $lieuObs = htmlspecialchars($donnees['zone_geo']);
                $protocole = htmlspecialchars($donnees['intitule']);
                $votant = htmlspecialchars($this->creerVotant($donnees));
                $dateVote = htmlspecialchars(strftime('%A %d %B %Y à %H:%M', strtotime($donnees['date_vote'])));
                $observateur = htmlspecialchars($this->creerObservateur($donnees));

                $description = '<style>.champ{color:grey} .gauche{float:left;padding:0 20px 0 0;} ul{list-style-type:none;padding:0;}</style>'.
                        '<h2>'."Vote pictoFlora #$idVote pour l'image #$idImg de l'observation #$idObs".'</h2>'.
                        '<div class="gauche">'.
                        '       <a href="'.$urlImg.'">'.
                        '               <img src="'.$miniatureUrl.'" alt="Img #'.$idImg.'"/>'.
                        '       </a>'.
                        '</div>'.
                        '<div class="gauche">'.
                        "       <h3>Image #$idImg de l'observation #$idObs</h3>".
                        '       <ul>'.
                        '               <li><span class="champ">'."Auteur de l'image :</span> $observateur</li>".
                        '               <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>".
                        '       </ul>'.
                        '</div>'.
                        '<div class="gauche">'.
                        "       <h3>Vote #$idVote</h3>".
                        '       <ul>'.
                        '               <li><span class="champ">'."Protocole :</span> <strong>$protocole</strong></li>".
                        '               <li><span class="champ">'."Valeur :</span> <strong>{$donnees['valeur']}</strong>/5</li>".
                        '               <li><span class="champ">'."Votant :</span> $votant</li>".
                        '               <li><span class="champ">'."À voté le :</span> $dateVote</li>".
                        '       </ul>'.
                        '</div>';
                return $description;
        }

        private function creerVotant($info) {
                $votant = 'Anonyme';
                if (isset($info['votant_prenom']) && isset($info['votant_nom'])) {
                        $votant = $info['votant_prenom'].' '.$info['votant_nom'];
                }
                return $votant;
        }

        private function creerObservateur($info) {
                $observateur = 'Anonyme';
                if ($info['observateur_prenom'] != '' && $info['observateur_nom'] != '') {
                        $observateur = $info['observateur_prenom'].' '.$info['observateur_nom'];
                }
                return $observateur;
        }



        /**
         * Retrouver les derniers votes image
         * */
        private function getDerniersVotesImage() {
                $elements = array();
                $idsVotes = $this->getIdsDerniersVotesImage();
                if (!empty($idsVotes)) {
                        $idsVotesConcat = implode(', ', $idsVotes);
                        $requete =  'SELECT DISTINCT id_vote, ce_image, valeur, divo.date AS date_vote, '.
                                        '       duo.prenom AS observateur_prenom, duo.nom AS observateur_nom, '.
                                        '       duv.prenom AS votant_prenom, duv.nom AS votant_nom, '.
                                        '       do.id_observation, do.nom_sel, do.zone_geo, do.date_observation, dip.intitule '.
                                        'FROM del_image_vote AS divo '.
                                        '       INNER JOIN del_image AS di '.
                                        '               ON divo.ce_image = di.id_image '.
                                        '       INNER JOIN del_observation AS do '.
                                        '               ON di.ce_observation = do.id_observation '.
                                        '       INNER JOIN del_image_protocole AS dip '.
                                        '               ON ce_protocole = id_protocole '.
                                        '       LEFT JOIN del_utilisateur_infos AS duo '.
                                        '               ON do.ce_utilisateur = duo.id_utilisateur '.
                                        '       LEFT JOIN del_utilisateur_infos AS duv '.
                                        '               ON if((CHAR_LENGTH(divo.ce_utilisateur) <> 32),CAST(divo.ce_utilisateur AS unsigned),0) '.
                                        '                       = duv.id_utilisateur '.
                                        "WHERE id_vote IN ($idsVotesConcat) ".
                                        'ORDER BY divo.date DESC '.
                                        'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().
                                        ' -- '.__FILE__.' : '.__LINE__;
                        $elements = $this->bdd->recupererTous($requete);
                }
                return $elements;
        }

        /**
         * Retrouver les ids des derniers votes image
         * */
        private function getIdsDerniersVotesImage() {
                $clauseWhere = $this->chargerClauseWhere();
                $requete =  'SELECT DISTINCT id_vote '.
                                'FROM del_image_vote '.
                                ($clauseWhere != '' ? "WHERE $clauseWhere " : '').
                                'ORDER BY date DESC '.
                                'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().
                                ' -- '.__FILE__.' : '.__LINE__;
                $resultats = $this->bdd->recupererTous($requete);

                $idsVotes = array();
                if ($resultats != false && is_array($resultats)) {
                        foreach ($resultats as $infos) {
                                $idsVotes[] = $infos['id_vote'];
                        }
                }
                return $idsVotes;
        }

        private function chargerClauseWhere() {
                $where = array();
                $filtres = $this->navigation->getFiltre();
                if (!empty($filtres)) {
                        $filtrePossibles = $this->conteneur->getParametreTableau('syndication.vote_filtres');
                        foreach ($filtres as $cleFiltre => $valeur) {
                                if (in_array($cleFiltre, $filtrePossibles)) {
                                        $champ = $this->mapping[$cleFiltre];
                                        $valeurP = $this->bdd->proteger($valeur);
                                        $where[] = " $champ = $valeurP ";
                                }
                        }
                }

                return (!empty($where)) ? implode('AND', $where) : '';
        }
}