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 {
|
1876 |
jpm |
24 |
|
983 |
aurelien |
25 |
const DS = DIRECTORY_SEPARATOR;
|
|
|
26 |
const SERVICE_DEFAUT = 'photo';
|
1876 |
jpm |
27 |
|
983 |
aurelien |
28 |
private $ce_img_url_tpl = null;
|
|
|
29 |
private $del_url_service_tpl = null;
|
1876 |
jpm |
30 |
|
986 |
aurelien |
31 |
private $id_observation = null;
|
1876 |
jpm |
32 |
|
983 |
aurelien |
33 |
/**
|
|
|
34 |
* Méthode appelée par défaut pour charger ce widget.
|
|
|
35 |
*/
|
1876 |
jpm |
36 |
public function executer() {
|
983 |
aurelien |
37 |
$this->del_url_service_tpl = $this->config['vote']['baseURLServicesDelTpl'];
|
|
|
38 |
$this->ce_img_url_tpl = $this->config['vote']['celImgUrlTpl'];
|
1876 |
jpm |
39 |
|
986 |
aurelien |
40 |
$this->collecterParametres();
|
|
|
41 |
$ids_observations = $this->obtenirIdsObservation();
|
|
|
42 |
$donnees = $this->obtenirDonnees($this->id_observation);
|
983 |
aurelien |
43 |
|
992 |
aurelien |
44 |
$widget['donnees']['id_observation'] = $this->id_observation;
|
983 |
aurelien |
45 |
$widget['donnees']['titre'] = $this->construireTitreImage($donnees);
|
991 |
aurelien |
46 |
$widget['donnees']['url'] = $this->obtenirUrlImage($donnees['images']);
|
983 |
aurelien |
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);
|
1876 |
jpm |
55 |
|
993 |
delphine |
56 |
if (isset($this->parametres['mode']) && $this->parametres['mode'] == 'ajax') {
|
|
|
57 |
$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'vote_ajax.tpl.html';
|
|
|
58 |
} else {
|
|
|
59 |
$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'vote.tpl.html';
|
|
|
60 |
}
|
1876 |
jpm |
61 |
|
983 |
aurelien |
62 |
$contenu = $this->traiterSquelettePhp($squelette, $widget['donnees']);
|
1876 |
jpm |
63 |
|
983 |
aurelien |
64 |
$this->envoyer($contenu);
|
|
|
65 |
}
|
1876 |
jpm |
66 |
|
986 |
aurelien |
67 |
private function collecterParametres() {
|
|
|
68 |
$this->id_observation = (isset($_GET['id_observation'])) ? $_GET['id_observation'] : null;
|
991 |
aurelien |
69 |
unset($_GET['id_observation']);
|
|
|
70 |
$this->parametres = $_GET;
|
986 |
aurelien |
71 |
}
|
1876 |
jpm |
72 |
|
999 |
delphine |
73 |
private function convertirParametresEnChaineRequeteService() {
|
1876 |
jpm |
74 |
$parametresWs = array();
|
|
|
75 |
$mapping = $this->transformerEnTableau($this->config['vote']['mapping_masque']);
|
|
|
76 |
if (!empty($this->parametres)) {
|
|
|
77 |
foreach ($this->parametres as $parametre => $valeur) {
|
999 |
delphine |
78 |
if (isset($mapping[$parametre])) {
|
1876 |
jpm |
79 |
$parametresWs[] = $mapping[$parametre].'='.urlencode($valeur);
|
999 |
delphine |
80 |
}
|
|
|
81 |
}
|
|
|
82 |
}
|
1876 |
jpm |
83 |
$requete = (!empty($parametresWs)) ? '?'.implode('&', $parametresWs) : '';
|
999 |
delphine |
84 |
return $requete;
|
|
|
85 |
}
|
1876 |
jpm |
86 |
|
991 |
aurelien |
87 |
private function convertirParametresEnChaineRequete() {
|
|
|
88 |
$requete = str_replace('masque_','masque.',http_build_query($this->parametres));
|
|
|
89 |
if(!empty($this->parametres)) {
|
|
|
90 |
$requete = '&'.$requete;
|
|
|
91 |
}
|
|
|
92 |
return $requete;
|
|
|
93 |
}
|
1876 |
jpm |
94 |
|
986 |
aurelien |
95 |
private function obtenirIdsObservation() {
|
1876 |
jpm |
96 |
$url = $this->del_url_service_tpl.'observations'.$this->convertirParametresEnChaineRequeteService();
|
|
|
97 |
//exit('<pre>'.print_r($url, true).'</pre>');
|
|
|
98 |
$infos = json_decode(file_get_contents($url), true);
|
|
|
99 |
//exit('<pre>'.print_r($infos, true).'</pre>');
|
|
|
100 |
$ids = array();
|
|
|
101 |
foreach ($infos['resultats'] as $resultat) {
|
|
|
102 |
if ($this->id_observation == null) {
|
|
|
103 |
$this->id_observation = $resultat['id_observation'];
|
|
|
104 |
}
|
|
|
105 |
$ids[] = $resultat['id_observation'];
|
986 |
aurelien |
106 |
}
|
1876 |
jpm |
107 |
return $ids;
|
986 |
aurelien |
108 |
}
|
1876 |
jpm |
109 |
|
986 |
aurelien |
110 |
private function obtenirUrlBaseWidget() {
|
|
|
111 |
return $this->config['chemins']['baseURL'].'vote';
|
|
|
112 |
}
|
1876 |
jpm |
113 |
|
986 |
aurelien |
114 |
private function construireUrlsSuivantesEtPrecedentes($tableau_id_observation) {
|
|
|
115 |
$indices_id_obs = array_flip($tableau_id_observation);
|
|
|
116 |
$indice_obs_courante = $indices_id_obs[$this->id_observation];
|
|
|
117 |
$url_base = $this->obtenirUrlBaseWidget();
|
1876 |
jpm |
118 |
|
986 |
aurelien |
119 |
$indice_predecent = isset($tableau_id_observation[$indice_obs_courante - 1]) ? $tableau_id_observation[$indice_obs_courante - 1] : '';
|
|
|
120 |
$indice_suivant = isset($tableau_id_observation[$indice_obs_courante + 1]) ? $tableau_id_observation[$indice_obs_courante + 1] : '';
|
1876 |
jpm |
121 |
|
991 |
aurelien |
122 |
$url_suivant = isset($tableau_id_observation[$indice_obs_courante + 1]) ? $url_base.'?id_observation='.$tableau_id_observation[$indice_obs_courante + 1].$this->convertirParametresEnChaineRequete() : null;
|
|
|
123 |
$url_precedent = isset($tableau_id_observation[$indice_obs_courante - 1]) ? $url_base.'?id_observation='.$tableau_id_observation[$indice_obs_courante - 1].$this->convertirParametresEnChaineRequete() : null;
|
|
|
124 |
|
1876 |
jpm |
125 |
return array('url_suivant' => $url_suivant, 'url_precedent' => $url_precedent);
|
986 |
aurelien |
126 |
}
|
1876 |
jpm |
127 |
|
986 |
aurelien |
128 |
private function obtenirDonnees($id_observation) {
|
|
|
129 |
$donnees = json_decode(file_get_contents($this->del_url_service_tpl.'observations/'.$id_observation), true);
|
1876 |
jpm |
130 |
//exit('<pre>'.print_r($donnees, true).'</pre>');
|
983 |
aurelien |
131 |
return $donnees;
|
|
|
132 |
}
|
1876 |
jpm |
133 |
|
983 |
aurelien |
134 |
private function construireTitreImage($donnees) {
|
991 |
aurelien |
135 |
return 'Pris par '.$donnees['auteur.prenom'].' '.$donnees['auteur.nom'].' à '.$donnees['zone_geo'].' le '.$this->formaterDate($donnees['date_observation'],'%d/%m/%Y');
|
983 |
aurelien |
136 |
}
|
1876 |
jpm |
137 |
|
983 |
aurelien |
138 |
private function obtenirUrlImage($images) {
|
991 |
aurelien |
139 |
return $images[0]['binaire.href'];
|
983 |
aurelien |
140 |
}
|
1876 |
jpm |
141 |
|
983 |
aurelien |
142 |
private function formaterDeterminations($observation) {
|
|
|
143 |
$liste_determinations = $observation['commentaires'];
|
|
|
144 |
$liste_determinations_formatees = array();
|
|
|
145 |
$creerPropositionAPartirObservation = true;
|
1876 |
jpm |
146 |
|
|
|
147 |
foreach ($liste_determinations as $determination) {
|
|
|
148 |
if ($determination['nom_sel'] != null && $determination['nom_sel'] != '') {
|
|
|
149 |
$liste_determinations_formatees[] = $this->formaterDetermination($determination);
|
983 |
aurelien |
150 |
}
|
1876 |
jpm |
151 |
|
|
|
152 |
if ($determination['nom_sel'] == $observation['determination.ns'] &&
|
983 |
aurelien |
153 |
$determination['auteur.id'] == $observation['auteur.id']) {
|
|
|
154 |
$creerPropositionAPartirObservation = false;
|
|
|
155 |
}
|
|
|
156 |
}
|
1876 |
jpm |
157 |
|
|
|
158 |
if ($creerPropositionAPartirObservation) {
|
983 |
aurelien |
159 |
$liste_determinations_formatees[] = $this->creerDeterminationAPartirObservation($observation);
|
|
|
160 |
}
|
1876 |
jpm |
161 |
|
983 |
aurelien |
162 |
return $liste_determinations_formatees;
|
|
|
163 |
}
|
1876 |
jpm |
164 |
|
|
|
165 |
private function formaterDetermination($infos) {
|
|
|
166 |
//exit('<pre>'.print_r($infos, true).'</pre>');
|
|
|
167 |
$determination = array(
|
|
|
168 |
"id_commentaire" => $infos['id_commentaire'],
|
|
|
169 |
"observation" => $infos['observation'],
|
|
|
170 |
"proposition" => $infos['proposition'],
|
|
|
171 |
"id_parent" => $infos['id_parent'],
|
|
|
172 |
"texte" => $infos['texte'],
|
|
|
173 |
"date" => $infos['date_observation'],
|
|
|
174 |
"nom_sel" => $infos['nom_sel'],
|
|
|
175 |
"auteur_intitule" => $this->formaterIntituleAuteur($infos),
|
|
|
176 |
"votes" => $this->formaterVotes($infos['votes'])
|
|
|
177 |
);
|
|
|
178 |
return $determination;
|
|
|
179 |
}
|
|
|
180 |
|
983 |
aurelien |
181 |
private function creerDeterminationAPartirObservation($observation) {
|
1876 |
jpm |
182 |
$determination = array(
|
|
|
183 |
"id_commentaire" => "0",
|
|
|
184 |
"observation" => $observation['id_observation'],
|
|
|
185 |
"proposition" => "0",
|
|
|
186 |
"id_parent" => "0",
|
|
|
187 |
"texte" => null,
|
|
|
188 |
"date" => $observation['date_observation'],
|
|
|
189 |
"nom_sel" => $observation['determination.ns'],
|
|
|
190 |
"auteur_intitule" => $this->formaterIntituleAuteur($observation),
|
|
|
191 |
"votes" => array('oui' => 0, 'non' => 0)
|
983 |
aurelien |
192 |
);
|
1876 |
jpm |
193 |
return $determination;
|
983 |
aurelien |
194 |
}
|
1876 |
jpm |
195 |
|
|
|
196 |
private function formaterIntituleAuteur($donnees) {
|
|
|
197 |
$intitule = 'Anonyme';
|
|
|
198 |
// TODO : si auteur.id existe, appelé le WS de l'annuaire...
|
|
|
199 |
if (!empty($donnees['auteur.prenom']) && !empty($donnees['auteur.nom'])) {
|
|
|
200 |
$intitule = $donnees['auteur.prenom'].' '.$donnees['auteur.nom'];
|
|
|
201 |
} else if (!empty($donnees['auteur.courriel'])) {
|
|
|
202 |
$intitule = strstr($donnees['auteur.courriel'], '@').'@...';
|
|
|
203 |
}
|
|
|
204 |
return $intitule;
|
|
|
205 |
}
|
|
|
206 |
|
983 |
aurelien |
207 |
private function formaterVotes($liste_votes) {
|
|
|
208 |
$liste_votes_formates = array('oui' => 0, 'non' => 0);
|
|
|
209 |
foreach($liste_votes as $vote) {
|
|
|
210 |
if($vote['vote'] == 1) {
|
|
|
211 |
$liste_votes_formates['oui']++;
|
|
|
212 |
} else {
|
|
|
213 |
$liste_votes_formates['non']++;
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
return $liste_votes_formates;
|
|
|
217 |
}
|
1876 |
jpm |
218 |
|
984 |
aurelien |
219 |
private function convertirTableauVotesEnJs($liste_commentaires) {
|
|
|
220 |
$tableau_js = "var g_votes = new Array();";
|
|
|
221 |
foreach($liste_commentaires as $commentaire) {
|
|
|
222 |
$tableau_js .= 'g_votes["'.$commentaire['id_commentaire'].'"] = new Array();';
|
|
|
223 |
if(isset($commentaire['votes'])) {
|
|
|
224 |
foreach($commentaire['votes'] as $vote) {
|
|
|
225 |
$tableau_js .= 'g_votes["'.$commentaire['id_commentaire'].'"]["'.$vote['auteur.id'].'"] = '.$vote['vote'].';';
|
|
|
226 |
}
|
|
|
227 |
}
|
|
|
228 |
}
|
|
|
229 |
return $tableau_js;
|
|
|
230 |
}
|
1876 |
jpm |
231 |
}
|