Subversion Repositories eFlore/Applications.del

Rev

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

<?php
// declare(encoding='UTF-8');
/**
 * Service affichant les dernières photo publiques du CEL ouvrable sous forme de diaporama.
 * Encodage en entrée : utf8
 * Encodage en sortie : utf8
 *
 * Cas d'utilisation et documentation :
 * @link http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=AideCELWidgetPhoto
 *
 * Paramètres :
 * ===> extra = booléen (1 ou 0)  [par défaut : 1]
 * Affiche / Cache la vignette en taille plus importante au bas du widget.
 * ===> vignette = [0-9]+,[0-9]+  [par défaut : 4,3]
 * Indique le nombre de vignette par ligne et le nombre de ligne.
 *
 * @author              Jean-Pascal MILCENT <jpm@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>
 * @version     $Id$
 * @copyright   Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
 */
class Vote extends WidgetCommun {
        
        const DS = DIRECTORY_SEPARATOR;
        const SERVICE_DEFAUT = 'photo';
        private $ce_img_url_tpl = null;
        private $del_url_service_tpl = null;
        
        private $id_observation_suivant = null;
        private $id_observation_precedent = null;
        private $id_observation = null;
        
        /**
         * Méthode appelée par défaut pour charger ce widget.
         */
        public function executer() {            
                $this->del_url_service_tpl = $this->config['vote']['baseURLServicesDelTpl'];
                $this->ce_img_url_tpl = $this->config['vote']['celImgUrlTpl'];
                
                $this->collecterParametres();
                $ids_observations = $this->obtenirIdsObservation();
                $donnees = $this->obtenirDonnees($this->id_observation);

                $widget['donnees']['id_observation'] = $this->id_observation;
                $widget['donnees']['titre'] = $this->construireTitreImage($donnees);
                $widget['donnees']['url'] = $this->obtenirUrlImage($donnees['images']);
                $widget['donnees']['images'] = $donnees['images'];
                $widget['donnees']['liste_votes_determination'] = $this->formaterDeterminations($donnees);
                $widget['donnees']['url_css'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], 'modules/vote/squelettes/css/');
                $widget['donnees']['url_js'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], 'modules/vote/squelettes/scripts/');
                $widget['donnees']['votes_js'] = $this->convertirTableauVotesEnJs($donnees['commentaires']);
                $widget['donnees']['url_service_votes'] = $this->del_url_service_tpl.'observations/'.$this->id_observation;
                $widget['donnees']['url_service_utilisateurs'] = $this->del_url_service_tpl.'utilisateurs/';
                $widget['donnees']['urls_navigation'] = $this->construireUrlsSuivantesEtPrecedentes($ids_observations);
                
                if (isset($this->parametres['mode']) && $this->parametres['mode'] == 'ajax') {
                        $squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'vote_ajax.tpl.html';
                } else {
                        $squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'vote.tpl.html';
                }
                
                $contenu = $this->traiterSquelettePhp($squelette, $widget['donnees']);
                
                $this->envoyer($contenu);
        }
        
        private function collecterParametres() {
                $this->id_observation = (isset($_GET['id_observation'])) ? $_GET['id_observation'] : null;
                unset($_GET['id_observation']);
                $this->parametres = $_GET;
        }
        
        private function convertirParametresEnChaineRequeteService() {
                $requete = '';
                $mapping = $this->config['mapping_masque'];
                if(!empty($this->parametres)) {
                        foreach ($this->parametres as $parametre=>$valeur) {
                                if (isset($mapping[$parametre])) {
                                        $requete .= '&'.$mapping[$parametre].'='.urlencode($valeur);
                                }
                        }
                }
                return $requete;
        }
        
        private function convertirParametresEnChaineRequete() {
                $requete = str_replace('masque_','masque.',http_build_query($this->parametres));
                if(!empty($this->parametres)) {
                        $requete = '&'.$requete;
                }
                return $requete;
        }
        
        private function obtenirIdsObservation() {
                $ids = json_decode(file_get_contents($this->del_url_service_tpl.'observations?retour.format=widget'.$this->convertirParametresEnChaineRequeteService()), true);
                if($this->id_observation == null) {
                        $this->id_observation = $ids['resultats'][0];
                }
                return $ids['resultats'];
        }
        
        private function obtenirUrlBaseWidget() {
                return $this->config['chemins']['baseURL'].'vote';
        }
        
        private function construireUrlsSuivantesEtPrecedentes($tableau_id_observation) {
                
                $indices_id_obs = array_flip($tableau_id_observation);
                $indice_obs_courante = $indices_id_obs[$this->id_observation];
                $url_base = $this->obtenirUrlBaseWidget();
                
                $indice_predecent = isset($tableau_id_observation[$indice_obs_courante - 1]) ? $tableau_id_observation[$indice_obs_courante - 1] : '';
                $indice_suivant = isset($tableau_id_observation[$indice_obs_courante + 1]) ? $tableau_id_observation[$indice_obs_courante + 1] : '';
                
                $url_suivant = isset($tableau_id_observation[$indice_obs_courante + 1]) ? $url_base.'?id_observation='.$tableau_id_observation[$indice_obs_courante + 1].$this->convertirParametresEnChaineRequete() : null;
                $url_precedent = isset($tableau_id_observation[$indice_obs_courante - 1]) ? $url_base.'?id_observation='.$tableau_id_observation[$indice_obs_courante - 1].$this->convertirParametresEnChaineRequete() : null;

                return array('url_suivant' => $url_suivant,
                                        'url_precedent' => $url_precedent
                );
        }
        
        private function obtenirDonnees($id_observation) {
                $donnees = json_decode(file_get_contents($this->del_url_service_tpl.'observations/'.$id_observation), true);
                return $donnees;
        }
        
        private function construireTitreImage($donnees) {
                return 'Pris par '.$donnees['auteur.prenom'].' '.$donnees['auteur.nom'].' à '.$donnees['zone_geo'].' le '.$this->formaterDate($donnees['date_observation'],'%d/%m/%Y');
        }
        
        private function obtenirUrlImage($images) {
                return $images[0]['binaire.href'];
        }
        
        private function formaterDeterminations($observation) {
                $liste_determinations = $observation['commentaires'];
                $liste_determinations_formatees = array();
                
                $creerPropositionAPartirObservation = true;
                
                foreach ($liste_determinations as $determination) {             
                        $determination_formatee = $determination;
                        if($determination['nom_sel'] != null && $determination['nom_sel'] != '') {
                                $determination_formatee['votes'] = $this->formaterVotes($determination['votes']);
                                $liste_determinations_formatees[] = $determination_formatee;
                        }
                        
                        if($determination['nom_sel'] == $observation['determination.ns'] && 
                                $determination['auteur.id'] == $observation['auteur.id']) {
                                $creerPropositionAPartirObservation = false;
                        }
                }
                
                if($creerPropositionAPartirObservation) {
                        $liste_determinations_formatees[] = $this->creerDeterminationAPartirObservation($observation);
                }
                
                return $liste_determinations_formatees;
        }
        
        private function creerDeterminationAPartirObservation($observation) {
                $proposition = array("id_commentaire" =>  "0",
                    "observation" => $observation['id_observation'],
                    "proposition" => "0",
                    "id_parent" => "0",
                    "auteur.id" => $observation['auteur.id'],
                    "texte" => null,
                    "auteur.nom" => $observation['auteur.nom'],
                    "auteur.prenom" => $observation['auteur.prenom'],
                    "auteur.courriel" => $observation['observateur'],
                    "date" => $observation['date_observation'],
                    "nom_sel" => $observation['determination.ns'],
                    "votes" => array('oui' => 0, 'non' => 0)
                );
                
                return $proposition;
        }
        
        private function formaterVotes($liste_votes) {
                $liste_votes_formates = array('oui' => 0, 'non' => 0);
                foreach($liste_votes as $vote) {
                        if($vote['vote'] == 1) {
                                $liste_votes_formates['oui']++;
                        } else {
                                $liste_votes_formates['non']++;
                        }
                }
                
                return $liste_votes_formates;
        }
        
        private function convertirTableauVotesEnJs($liste_commentaires) {
                $tableau_js = "var g_votes = new Array();";
                foreach($liste_commentaires as $commentaire) {
                        $tableau_js .= 'g_votes["'.$commentaire['id_commentaire'].'"] = new Array();';
                        if(isset($commentaire['votes'])) {
                                foreach($commentaire['votes'] as $vote) {
                                        $tableau_js .= 'g_votes["'.$commentaire['id_commentaire'].'"]["'.$vote['auteur.id'].'"] = '.$vote['vote'].';';
                                }
                        }
                }
                
                return $tableau_js;
        }
}
?>