739 |
delphine |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Service affichant les dernières observations publiques du CEL.
|
|
|
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=AideCELWidget
|
|
|
10 |
*
|
|
|
11 |
* Paramètres :
|
|
|
12 |
* ===> vignette = [0-9]+,[0-9]+ [par défaut : 4,3]
|
|
|
13 |
* Indique le nombre de vignette par ligne et le nombre de ligne.
|
|
|
14 |
*
|
|
|
15 |
* @author Delphine <jpm@tela-botanica.org>
|
|
|
16 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
17 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
18 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
19 |
* @version $Id$
|
|
|
20 |
* @copyright Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
|
|
|
21 |
*/
|
|
|
22 |
class Observation extends WidgetCommun {
|
|
|
23 |
|
|
|
24 |
const DS = DIRECTORY_SEPARATOR;
|
|
|
25 |
const SERVICE_DEFAUT = 'observation';
|
|
|
26 |
private $flux_rss_url = null;
|
|
|
27 |
private $eflore_url_tpl = null;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Méthode appelée par défaut pour charger ce widget.
|
|
|
31 |
*/
|
|
|
32 |
public function executer() {
|
|
|
33 |
$retour = null;
|
|
|
34 |
// Pour la création de l'id du cache nous ne tenons pas compte du paramètre de l'url callback
|
|
|
35 |
unset($this->parametres['callback']);
|
|
|
36 |
extract($this->parametres);
|
|
|
37 |
|
|
|
38 |
if (!isset($mode)) {
|
|
|
39 |
$mode = self::SERVICE_DEFAUT;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
$this->eflore_url_tpl = $this->config['observation']['efloreUrlTpl'];
|
|
|
43 |
$this->flux_rss_url = $this->config['observation']['fluxRssUrl'];
|
|
|
44 |
|
|
|
45 |
$cache_activation = $this->config['observation.cache']['activation'];
|
|
|
46 |
$cache_stockage = $this->config['observation.cache']['stockageDossier'];
|
|
|
47 |
$ddv = $this->config['observation.cache']['dureeDeVie'];
|
|
|
48 |
$cache = new Cache($cache_stockage, $ddv, $cache_activation);
|
|
|
49 |
$id_cache = 'observation-'.hash('adler32', print_r($this->parametres, true));
|
|
|
50 |
if (! $contenu = $cache->charger($id_cache)) {
|
|
|
51 |
$methode = $this->traiterNomMethodeExecuter($mode);
|
|
|
52 |
if (method_exists($this, $methode)) {
|
|
|
53 |
$retour = $this->$methode();
|
|
|
54 |
} else {
|
|
|
55 |
$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
if (is_null($retour)) {
|
|
|
59 |
$contenu = 'Un problème est survenu : '.print_r($this->messages, true);
|
|
|
60 |
} else {
|
|
|
61 |
$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$retour['squelette'].'.tpl.html';
|
|
|
62 |
$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
|
|
|
63 |
$cache->sauver($id_cache, $contenu);
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
if (isset($_GET['callback'])) {
|
|
|
67 |
$this->envoyerJsonp(array('contenu' => $contenu));
|
|
|
68 |
} else {
|
|
|
69 |
$this->envoyer($contenu);
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
private function executerAjax() {
|
|
|
74 |
$widget = $this->executerObservation();
|
|
|
75 |
$widget['squelette'] = 'observation_ajax';
|
|
|
76 |
return $widget;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
private function executerObservation() {
|
|
|
80 |
$widget = null;
|
|
|
81 |
extract($this->parametres);
|
|
|
82 |
$max_obs = (isset($max_obs) && preg_match('/^[0-9]+,[0-9]+$/', $max_obs)) ? $max_obs : '10';
|
|
|
83 |
$this->flux_rss_url .= $this->traiterParametres();
|
|
|
84 |
$xml = file_get_contents($this->flux_rss_url);
|
|
|
85 |
if ($xml) {
|
|
|
86 |
try {
|
|
|
87 |
$flux = new XmlFeedParser($xml);
|
|
|
88 |
|
|
|
89 |
$widget['donnees']['flux_rss_url'] = $this->flux_rss_url;
|
|
|
90 |
$widget['donnees']['url_css'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], 'modules/observation/squelettes/css/');
|
|
|
91 |
$widget['donnees']['colonne'] = 1;
|
|
|
92 |
$num = 0;
|
|
|
93 |
foreach ($flux as $entree) {
|
|
|
94 |
if ($num == $max_obs) {
|
|
|
95 |
break;
|
|
|
96 |
}
|
|
|
97 |
$item = array();
|
|
|
98 |
// Formatage date
|
|
|
99 |
$date = $entree->updated ? $entree->updated : null;
|
|
|
100 |
$date = $entree->pubDate ? $entree->pubDate : $date;
|
|
|
101 |
$item['date'] = strftime('%A %d %B %Y', $date);
|
|
|
102 |
// Formatage titre
|
|
|
103 |
$item['titre'] = $entree->title;
|
|
|
104 |
$item['nn'] = '';
|
|
|
105 |
$item['eflore_url'] = '#';
|
|
|
106 |
if (preg_match('/\[nn([0-9]+)\]/', $entree->title, $match)) {
|
|
|
107 |
$item['nn'] = $match[1];
|
|
|
108 |
$item['eflore_url'] = sprintf($this->eflore_url_tpl, $item['nn']);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
// Récupération du GUID
|
|
|
112 |
if (preg_match('/urn:lsid:tela-botanica.org:cel:obs([0-9]+)$/', $entree->id, $match)) {
|
|
|
113 |
$item['guid'] = (int) $match[1];
|
|
|
114 |
} else {
|
|
|
115 |
$item['guid'] = $entree->id;
|
|
|
116 |
}
|
|
|
117 |
$item['description'] = $entree->content;
|
|
|
118 |
|
|
|
119 |
$widget['donnees']['items'][$num++] = $item;
|
|
|
120 |
}
|
|
|
121 |
$widget['squelette'] = 'observation';
|
|
|
122 |
} catch (XmlFeedParserException $e) {
|
|
|
123 |
trigger_error('Flux invalide : '.$e->getMessage(), E_USER_WARNING);
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
return $widget;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
private function traiterParametres() {
|
|
|
130 |
$parametres_flux = '?';
|
|
|
131 |
$criteres = array(
|
|
|
132 |
'utilisateur' => 'auteur',
|
|
|
133 |
'commune' => 'commune',
|
|
|
134 |
'departement' => 'departement',
|
|
|
135 |
'taxon' => 'taxon',
|
|
|
136 |
'commentaire' => 'commentaire',
|
|
|
137 |
'date' => 'date',
|
|
|
138 |
'tag' => 'projet');
|
|
|
139 |
foreach($this->parametres as $nom_critere => $valeur_critere) {
|
|
|
140 |
if (isset($criteres[$nom_critere])) {
|
|
|
141 |
$parametres_flux .= $criteres[$nom_critere].'='.$valeur_critere.'&';
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
if ($parametres_flux == '?') {
|
|
|
146 |
$parametres_flux = '';
|
|
|
147 |
} else {
|
|
|
148 |
$parametres_flux = rtrim($parametres_flux, '&');
|
|
|
149 |
}
|
|
|
150 |
return $parametres_flux;
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
?>
|