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)) {
|
3005 |
mathias |
59 |
$this->messages[] = 'Aucune observation';
|
|
|
60 |
//$contenu = 'Un problème est survenu : '.print_r($this->messages, true);
|
739 |
delphine |
61 |
} else {
|
|
|
62 |
$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$retour['squelette'].'.tpl.html';
|
|
|
63 |
$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
|
|
|
64 |
$cache->sauver($id_cache, $contenu);
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
if (isset($_GET['callback'])) {
|
|
|
68 |
$this->envoyerJsonp(array('contenu' => $contenu));
|
|
|
69 |
} else {
|
|
|
70 |
$this->envoyer($contenu);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
private function executerAjax() {
|
|
|
75 |
$widget = $this->executerObservation();
|
|
|
76 |
$widget['squelette'] = 'observation_ajax';
|
|
|
77 |
return $widget;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
private function executerObservation() {
|
|
|
81 |
$widget = null;
|
|
|
82 |
extract($this->parametres);
|
|
|
83 |
$max_obs = (isset($max_obs) && preg_match('/^[0-9]+,[0-9]+$/', $max_obs)) ? $max_obs : '10';
|
1478 |
aurelien |
84 |
$icone_rss = (isset($_GET['rss']) && $_GET['rss'] != 1) ? false : true;
|
739 |
delphine |
85 |
$this->flux_rss_url .= $this->traiterParametres();
|
1478 |
aurelien |
86 |
$titre = isset($titre) ? htmlentities(rawurldecode($titre)) : '';
|
3005 |
mathias |
87 |
|
|
|
88 |
$xml = @file_get_contents($this->flux_rss_url);
|
|
|
89 |
if ($xml !== false) {
|
788 |
delphine |
90 |
if ($xml) {
|
|
|
91 |
try {
|
|
|
92 |
$flux = new XmlFeedParser($xml);
|
|
|
93 |
|
|
|
94 |
$widget['donnees']['flux_rss_url'] = $this->flux_rss_url;
|
|
|
95 |
$widget['donnees']['url_css'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], 'modules/observation/squelettes/css/');
|
|
|
96 |
$widget['donnees']['colonne'] = 1;
|
1478 |
aurelien |
97 |
$widget['donnees']['icone_rss'] = $icone_rss;
|
|
|
98 |
$widget['donnees']['titre'] = $titre;
|
788 |
delphine |
99 |
$num = 0;
|
|
|
100 |
foreach ($flux as $entree) {
|
|
|
101 |
if ($num == $max_obs) {
|
|
|
102 |
break;
|
|
|
103 |
}
|
|
|
104 |
$item = array();
|
|
|
105 |
// Formatage date
|
|
|
106 |
$date = $entree->updated ? $entree->updated : null;
|
|
|
107 |
$date = $entree->pubDate ? $entree->pubDate : $date;
|
|
|
108 |
$item['date'] = strftime('%A %d %B %Y', $date);
|
|
|
109 |
// Formatage titre
|
|
|
110 |
$item['titre'] = $entree->title;
|
|
|
111 |
$item['nn'] = '';
|
|
|
112 |
$item['eflore_url'] = '#';
|
|
|
113 |
if (preg_match('/\[nn([0-9]+)\]/', $entree->title, $match)) {
|
|
|
114 |
$item['nn'] = $match[1];
|
1669 |
aurelien |
115 |
$item['eflore_url'] = $entree->link;
|
788 |
delphine |
116 |
}
|
|
|
117 |
|
|
|
118 |
// Récupération du GUID
|
|
|
119 |
if (preg_match('/urn:lsid:tela-botanica.org:cel:obs([0-9]+)$/', $entree->id, $match)) {
|
|
|
120 |
$item['guid'] = (int) $match[1];
|
|
|
121 |
} else {
|
|
|
122 |
$item['guid'] = $entree->id;
|
|
|
123 |
}
|
|
|
124 |
$item['description'] = $entree->content;
|
|
|
125 |
|
|
|
126 |
$widget['donnees']['items'][$num++] = $item;
|
739 |
delphine |
127 |
}
|
788 |
delphine |
128 |
$widget['squelette'] = 'observation';
|
|
|
129 |
} catch (XmlFeedParserException $e) {
|
|
|
130 |
trigger_error('Flux invalide : '.$e->getMessage(), E_USER_WARNING);
|
739 |
delphine |
131 |
}
|
788 |
delphine |
132 |
} else {
|
3005 |
mathias |
133 |
// si on arrive ici c'est qu'il n'y a aucune obs correspondant
|
|
|
134 |
// à la requête, mais il n'y a rien d'invalide là-dedans
|
|
|
135 |
//$this->messages[] = "Fichier xml invalide.";
|
739 |
delphine |
136 |
}
|
788 |
delphine |
137 |
} else {
|
|
|
138 |
$this->messages[] = "L'URI, $this->flux_rss_url, est invalide.";
|
739 |
delphine |
139 |
}
|
|
|
140 |
return $widget;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
private function traiterParametres() {
|
|
|
144 |
$parametres_flux = '?';
|
3570 |
delphine |
145 |
$this->parametre['standard'] = (!isset($this->parametre['standard'])) ? '1' : $this->parametre['standard'];
|
|
|
146 |
$criteres = array('utilisateur', 'commune', 'standard', 'dept', 'taxon', 'commentaire', 'date', 'projet', 'motcle', 'num_taxon', 'groupe_zones_geo');
|
739 |
delphine |
147 |
foreach($this->parametres as $nom_critere => $valeur_critere) {
|
741 |
delphine |
148 |
if (in_array($nom_critere, $criteres)) {
|
|
|
149 |
$valeur_critere = str_replace(' ', '%20', $valeur_critere);
|
|
|
150 |
$parametres_flux .= $nom_critere.'='.$valeur_critere.'&';
|
739 |
delphine |
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
if ($parametres_flux == '?') {
|
|
|
155 |
$parametres_flux = '';
|
|
|
156 |
} else {
|
|
|
157 |
$parametres_flux = rtrim($parametres_flux, '&');
|
|
|
158 |
}
|
|
|
159 |
return $parametres_flux;
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
?>
|