21,23 → 21,22 |
* @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() { |
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); |
53,37 → 52,38 |
$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) { |
$parametresWs = array(); |
$mapping = $this->transformerEnTableau($this->config['vote']['mapping_masque']); |
if (!empty($this->parametres)) { |
foreach ($this->parametres as $parametre => $valeur) { |
if (isset($mapping[$parametre])) { |
$requete .= '&'.$mapping[$parametre].'='.urlencode($valeur); |
$parametresWs[] = $mapping[$parametre].'='.urlencode($valeur); |
} |
} |
} |
$requete = (!empty($parametresWs)) ? '?'.implode('&', $parametresWs) : ''; |
return $requete; |
} |
|
|
private function convertirParametresEnChaineRequete() { |
$requete = str_replace('masque_','masque.',http_build_query($this->parametres)); |
if(!empty($this->parametres)) { |
91,93 → 91,119 |
} |
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]; |
$url = $this->del_url_service_tpl.'observations'.$this->convertirParametresEnChaineRequeteService(); |
//exit('<pre>'.print_r($url, true).'</pre>'); |
$infos = json_decode(file_get_contents($url), true); |
//exit('<pre>'.print_r($infos, true).'</pre>'); |
$ids = array(); |
foreach ($infos['resultats'] as $resultat) { |
if ($this->id_observation == null) { |
$this->id_observation = $resultat['id_observation']; |
} |
$ids[] = $resultat['id_observation']; |
} |
return $ids['resultats']; |
return $ids; |
} |
|
|
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 |
); |
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); |
//exit('<pre>'.print_r($donnees, true).'</pre>'); |
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; |
|
foreach ($liste_determinations as $determination) { |
if ($determination['nom_sel'] != null && $determination['nom_sel'] != '') { |
$liste_determinations_formatees[] = $this->formaterDetermination($determination); |
} |
|
if($determination['nom_sel'] == $observation['determination.ns'] && |
|
if ($determination['nom_sel'] == $observation['determination.ns'] && |
$determination['auteur.id'] == $observation['auteur.id']) { |
$creerPropositionAPartirObservation = false; |
} |
} |
|
if($creerPropositionAPartirObservation) { |
|
if ($creerPropositionAPartirObservation) { |
$liste_determinations_formatees[] = $this->creerDeterminationAPartirObservation($observation); |
} |
|
|
return $liste_determinations_formatees; |
} |
|
|
private function formaterDetermination($infos) { |
//exit('<pre>'.print_r($infos, true).'</pre>'); |
$determination = array( |
"id_commentaire" => $infos['id_commentaire'], |
"observation" => $infos['observation'], |
"proposition" => $infos['proposition'], |
"id_parent" => $infos['id_parent'], |
"texte" => $infos['texte'], |
"date" => $infos['date_observation'], |
"nom_sel" => $infos['nom_sel'], |
"auteur_intitule" => $this->formaterIntituleAuteur($infos), |
"votes" => $this->formaterVotes($infos['votes']) |
); |
return $determination; |
} |
|
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) |
$determination = array( |
"id_commentaire" => "0", |
"observation" => $observation['id_observation'], |
"proposition" => "0", |
"id_parent" => "0", |
"texte" => null, |
"date" => $observation['date_observation'], |
"nom_sel" => $observation['determination.ns'], |
"auteur_intitule" => $this->formaterIntituleAuteur($observation), |
"votes" => array('oui' => 0, 'non' => 0) |
); |
|
return $proposition; |
return $determination; |
} |
|
|
private function formaterIntituleAuteur($donnees) { |
$intitule = 'Anonyme'; |
// TODO : si auteur.id existe, appelé le WS de l'annuaire... |
if (!empty($donnees['auteur.prenom']) && !empty($donnees['auteur.nom'])) { |
$intitule = $donnees['auteur.prenom'].' '.$donnees['auteur.nom']; |
} else if (!empty($donnees['auteur.courriel'])) { |
$intitule = strstr($donnees['auteur.courriel'], '@').'@...'; |
} |
return $intitule; |
} |
|
private function formaterVotes($liste_votes) { |
$liste_votes_formates = array('oui' => 0, 'non' => 0); |
foreach($liste_votes as $vote) { |
187,10 → 213,9 |
$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) { |
201,8 → 226,6 |
} |
} |
} |
|
return $tableau_js; |
} |
} |
?> |
} |