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();
|
788 |
delphine |
84 |
if (file_get_contents($this->flux_rss_url, false) != false) {
|
|
|
85 |
$xml = file_get_contents($this->flux_rss_url);
|
|
|
86 |
if ($xml) {
|
|
|
87 |
try {
|
|
|
88 |
$flux = new XmlFeedParser($xml);
|
|
|
89 |
|
|
|
90 |
$widget['donnees']['flux_rss_url'] = $this->flux_rss_url;
|
|
|
91 |
$widget['donnees']['url_css'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], 'modules/observation/squelettes/css/');
|
|
|
92 |
$widget['donnees']['colonne'] = 1;
|
|
|
93 |
$num = 0;
|
|
|
94 |
foreach ($flux as $entree) {
|
|
|
95 |
if ($num == $max_obs) {
|
|
|
96 |
break;
|
|
|
97 |
}
|
|
|
98 |
$item = array();
|
|
|
99 |
// Formatage date
|
|
|
100 |
$date = $entree->updated ? $entree->updated : null;
|
|
|
101 |
$date = $entree->pubDate ? $entree->pubDate : $date;
|
|
|
102 |
$item['date'] = strftime('%A %d %B %Y', $date);
|
|
|
103 |
// Formatage titre
|
|
|
104 |
$item['titre'] = $entree->title;
|
|
|
105 |
$item['nn'] = '';
|
|
|
106 |
$item['eflore_url'] = '#';
|
|
|
107 |
if (preg_match('/\[nn([0-9]+)\]/', $entree->title, $match)) {
|
|
|
108 |
$item['nn'] = $match[1];
|
|
|
109 |
$item['eflore_url'] = sprintf($this->eflore_url_tpl, $item['nn']);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// Récupération du GUID
|
|
|
113 |
if (preg_match('/urn:lsid:tela-botanica.org:cel:obs([0-9]+)$/', $entree->id, $match)) {
|
|
|
114 |
$item['guid'] = (int) $match[1];
|
|
|
115 |
} else {
|
|
|
116 |
$item['guid'] = $entree->id;
|
|
|
117 |
}
|
|
|
118 |
$item['description'] = $entree->content;
|
|
|
119 |
|
|
|
120 |
$widget['donnees']['items'][$num++] = $item;
|
739 |
delphine |
121 |
}
|
788 |
delphine |
122 |
$widget['squelette'] = 'observation';
|
|
|
123 |
} catch (XmlFeedParserException $e) {
|
|
|
124 |
trigger_error('Flux invalide : '.$e->getMessage(), E_USER_WARNING);
|
739 |
delphine |
125 |
}
|
788 |
delphine |
126 |
} else {
|
|
|
127 |
$this->messages[] = "Fichier xml invalide.";
|
739 |
delphine |
128 |
}
|
788 |
delphine |
129 |
} else {
|
|
|
130 |
$this->messages[] = "L'URI, $this->flux_rss_url, est invalide.";
|
739 |
delphine |
131 |
}
|
|
|
132 |
return $widget;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
private function traiterParametres() {
|
|
|
136 |
$parametres_flux = '?';
|
741 |
delphine |
137 |
$criteres = array('utilisateur', 'commune', 'dept', 'taxon', 'commentaire', 'date', 'projet');
|
739 |
delphine |
138 |
foreach($this->parametres as $nom_critere => $valeur_critere) {
|
741 |
delphine |
139 |
if (in_array($nom_critere, $criteres)) {
|
|
|
140 |
$valeur_critere = str_replace(' ', '%20', $valeur_critere);
|
|
|
141 |
$parametres_flux .= $nom_critere.'='.$valeur_critere.'&';
|
739 |
delphine |
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 |
?>
|