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