Subversion Repositories eFlore/Applications.del

Rev

Rev 984 | Rev 991 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
983 aurelien 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Service affichant les dernières photo publiques du CEL ouvrable sous forme de diaporama.
5
 * Encodage en entrée : utf8
6
 * Encodage en sortie : utf8
7
 *
8
 * Cas d'utilisation et documentation :
9
 * @link http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=AideCELWidgetPhoto
10
 *
11
 * Paramètres :
12
 * ===> extra = booléen (1 ou 0)  [par défaut : 1]
13
 * Affiche / Cache la vignette en taille plus importante au bas du widget.
14
 * ===> vignette = [0-9]+,[0-9]+  [par défaut : 4,3]
15
 * Indique le nombre de vignette par ligne et le nombre de ligne.
16
 *
17
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
18
 * @license	GPL v3 <http://www.gnu.org/licenses/gpl.txt>
19
 * @license	CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
20
 * @version	$Id$
21
 * @copyright	Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
22
 */
23
class Vote extends WidgetCommun {
24
 
25
	const DS = DIRECTORY_SEPARATOR;
26
	const SERVICE_DEFAUT = 'photo';
27
	private $ce_img_url_tpl = null;
28
	private $del_url_service_tpl = null;
29
 
986 aurelien 30
	private $id_observation_suivant = null;
31
	private $id_observation_precedent = null;
32
	private $id_observation = null;
33
 
983 aurelien 34
	/**
35
	 * Méthode appelée par défaut pour charger ce widget.
36
	 */
37
	public function executer() {
38
		$this->del_url_service_tpl = $this->config['vote']['baseURLServicesDelTpl'];
39
		$this->ce_img_url_tpl = $this->config['vote']['celImgUrlTpl'];
40
 
986 aurelien 41
		$this->collecterParametres();
42
		$ids_observations = $this->obtenirIdsObservation();
43
		$donnees = $this->obtenirDonnees($this->id_observation);
983 aurelien 44
 
45
		$widget['donnees']['titre'] = $this->construireTitreImage($donnees);
46
		$widget['donnees']['url'] = $this->obtenirUrlImage(&$donnees['images']);
47
		$widget['donnees']['images'] = $donnees['images'];
48
		$widget['donnees']['liste_votes_determination'] = $this->formaterDeterminations($donnees);
49
		$widget['donnees']['url_css'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], 'modules/vote/squelettes/css/');
50
		$widget['donnees']['url_js'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], 'modules/vote/squelettes/scripts/');
984 aurelien 51
		$widget['donnees']['votes_js'] = $this->convertirTableauVotesEnJs($donnees['commentaires']);
986 aurelien 52
		$widget['donnees']['url_service_votes'] = $this->del_url_service_tpl.'observations/'.$this->id_observation;
984 aurelien 53
		$widget['donnees']['url_service_utilisateurs'] = $this->del_url_service_tpl.'utilisateurs/';
986 aurelien 54
		$widget['donnees']['urls_navigation'] = $this->construireUrlsSuivantesEtPrecedentes($ids_observations);
983 aurelien 55
 
56
		$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'vote.tpl.html';
57
		$contenu = $this->traiterSquelettePhp($squelette, $widget['donnees']);
58
 
59
		$this->envoyer($contenu);
60
	}
61
 
986 aurelien 62
	private function collecterParametres() {
63
		$this->id_observation = (isset($_GET['id_observation'])) ? $_GET['id_observation'] : null;
64
	}
65
 
66
	private function obtenirIdsObservation() {
67
		$ids = json_decode(file_get_contents($this->del_url_service_tpl.'observations?retour.format=widget'), true);
68
		if($this->id_observation == null) {
69
			$this->id_observation = $ids['resultats'][0];
70
		}
71
		return $ids['resultats'];
72
	}
73
 
74
	private function obtenirUrlBaseWidget() {
75
		return $this->config['chemins']['baseURL'].'vote';
76
	}
77
 
78
	private function construireUrlsSuivantesEtPrecedentes($tableau_id_observation) {
79
 
80
		$indices_id_obs = array_flip($tableau_id_observation);
81
		$indice_obs_courante = $indices_id_obs[$this->id_observation];
82
		$url_base = $this->obtenirUrlBaseWidget();
83
 
84
		$indice_predecent = isset($tableau_id_observation[$indice_obs_courante - 1]) ? $tableau_id_observation[$indice_obs_courante - 1] : '';
85
		$indice_suivant = isset($tableau_id_observation[$indice_obs_courante + 1]) ? $tableau_id_observation[$indice_obs_courante + 1] : '';
86
 
87
		$url_suivant = isset($tableau_id_observation[$indice_obs_courante + 1]) ? $url_base.'?id_observation='.$tableau_id_observation[$indice_obs_courante + 1] : null;
88
		$url_precedent = isset($tableau_id_observation[$indice_obs_courante - 1]) ? $url_base.'?id_observation='.$tableau_id_observation[$indice_obs_courante - 1] : null;
89
 
90
		return array('url_suivant' => $url_suivant,
91
					'url_precedent' => $url_precedent
92
		);
93
	}
94
 
95
	private function obtenirDonnees($id_observation) {
96
		$donnees = json_decode(file_get_contents($this->del_url_service_tpl.'observations/'.$id_observation), true);
983 aurelien 97
		$donnees = $donnees;
98
		return $donnees;
99
	}
100
 
101
	private function construireTitreImage($donnees) {
102
		return 'Pris par '.$donnees['auteur.prenom'].' '.$donnees['auteur.nom'].' à '.$donnees['zone_geo'];
103
	}
104
 
105
	private function obtenirUrlImage($images) {
106
		$index = 0;
107
		$premiere_image = $images[$index];
108
		return $premiere_image['binaire.href'];
109
	}
110
 
111
	private function formaterDeterminations($observation) {
112
		$liste_determinations = $observation['commentaires'];
113
		$liste_determinations_formatees = array();
114
 
115
		$creerPropositionAPartirObservation = true;
116
 
117
		foreach ($liste_determinations as $determination) {
118
			$determination_formatee = $determination;
119
			if($determination['nom_sel'] != null && $determination['nom_sel'] != '') {
120
				$determination_formatee['votes'] = $this->formaterVotes($determination['votes']);
121
				$liste_determinations_formatees[] = $determination_formatee;
122
			}
123
 
124
			if($determination['nom_sel'] == $observation['determination.ns'] &&
125
				$determination['auteur.id'] == $observation['auteur.id']) {
126
				$creerPropositionAPartirObservation = false;
127
			}
128
		}
129
 
130
		if($creerPropositionAPartirObservation) {
131
			$liste_determinations_formatees[] = $this->creerDeterminationAPartirObservation($observation);
132
		}
133
 
134
		return $liste_determinations_formatees;
135
	}
136
 
137
	private function creerDeterminationAPartirObservation($observation) {
138
		$proposition = array("id_commentaire" =>  "0",
139
		    "observation" => $observation['id_observation'],
140
		    "proposition" => "0",
141
		    "id_parent" => "0",
142
		    "auteur.id" => $observation['auteur.id'],
143
		    "texte" => null,
144
		    "auteur.nom" => $observation['auteur.nom'],
145
		    "auteur.prenom" => $observation['auteur.prenom'],
146
		    "auteur.courriel" => $observation['observateur'],
147
		    "date" => $observation['date_observation'],
148
		    "nom_sel" => $observation['determination.ns'],
149
		    "votes" => array('oui' => 0, 'non' => 0)
150
		);
151
 
152
		return $proposition;
153
	}
154
 
155
	private function formaterVotes($liste_votes) {
156
		$liste_votes_formates = array('oui' => 0, 'non' => 0);
157
		foreach($liste_votes as $vote) {
158
			if($vote['vote'] == 1) {
159
				$liste_votes_formates['oui']++;
160
			} else {
161
				$liste_votes_formates['non']++;
162
			}
163
		}
164
 
165
		return $liste_votes_formates;
166
	}
167
 
984 aurelien 168
	private function convertirTableauVotesEnJs($liste_commentaires) {
169
		$tableau_js = "var g_votes = new Array();";
170
		foreach($liste_commentaires as $commentaire) {
171
			$tableau_js .= 'g_votes["'.$commentaire['id_commentaire'].'"] = new Array();';
172
			if(isset($commentaire['votes'])) {
173
				foreach($commentaire['votes'] as $vote) {
174
					$tableau_js .= 'g_votes["'.$commentaire['id_commentaire'].'"]["'.$vote['auteur.id'].'"] = '.$vote['vote'].';';
175
				}
176
			}
177
		}
178
 
179
		return $tableau_js;
180
	}
983 aurelien 181
}
182
?>