Subversion Repositories Applications.projet

Rev

Rev 431 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
431 mathias 1
<?php
2
/**
3
 * Service fournissant des informations concernant PROJET au format RSS1, RSS2 ou ATOM.
4
 * Encodage en entrée : utf8
5
 * Encodage en sortie : utf8
6
 *
7
 * @author Grégoire Duché <gregoire@tela-botanica.org>
8
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
9
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
10
 * @version $Id: CoelSyndication.php 381 2010-05-17 17:10:37Z jpm $
11
 * @copyright 2009
12
 */
13
 
14
class ProjetSyndication extends ProjetService {
15
 
16
    private $format = null;
17
    private $service = null;
18
    private $squelette = null;
19
    private $squelette_dossier = null;
20
    private $squelette_diff = null;
21
    private $flux = array();
22
 
23
    /**
24
     * Méthode appelée avec une requête de type GET.
25
     */
26
    public function getElement($param = array()) {
27
        // Initialisation des variables
28
        $info = array();
29
        $contenu = '';
30
 
31
        // Pré traitement des paramètres
32
        $pour_bdd = false;
33
        $p = $this->traiterParametresUrl(array('service', 'format'), $param, $pour_bdd);
34
 
35
        // Récupération de la liste des flux
36
        $this->chargerListeDesFlux();
37
 
38
        // Chargement du bon type de service demandé
39
        if (isset($p['service'])) {
40
            $this->service = strtolower($p['service']);
41
            $methode = $this->getNomMethodeService();
42
            if (method_exists($this, $methode)) {
43
                if ($this->service != 'liste_des_flux') {
44
                    if (isset($p['format']) && preg_match('/^(?:rss1|rss2|atom)$/i', $p['format'])) {
45
                        // Multiplication par deux de la limite car nous récupérons deux lignes par item
46
                        $this->limit = $this->limit*2;
47
                        // Mise en minuscule de l'indication du format
48
                        $this->format = strtolower($p['format']);
49
                        // Définition du fichier squelette demandé
50
                        $this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
51
                        $this->squelette = $this->squelette_dossier.$this->format.'.tpl.xml';
52
                        $this->squelette_diff = $this->squelette_dossier.'diff.tpl.html';
53
                    } else {
54
                        $this->format = '';
55
                        $this->messages[] = "Le service Projet Syndication nécessite d'indiquer en second paramètre le format : rss1, rss2 ou atom.";
56
                    }
57
                }
58
                // Récupération du contenu à renvoyer
59
                $contenu = $this->$methode();
60
            } else {
61
                $this->messages[] = "Le type d'information demandé '$this->service' n'est pas disponible.";
62
            }
63
        } else {
64
            $this->messages[] = "Le service Projet Syndication nécessite d'indiquer en premier paramètre le type d'information demandé.";
65
        }
66
 
67
        // Envoie sur la sortie standard
68
        $encodage = 'utf-8';
69
        $mime = $this->getTypeMime();
70
        $formatage_json = $this->getFormatageJson();
71
        $this->envoyer($contenu, $mime, $encodage, $formatage_json);
72
    }
73
 
74
    private function getUrlServiceBase() {
75
        $url_service = $this->config['coel']['urlBaseJrest'].'CoelSyndication/'.$this->service.'/'.$this->format;
76
        return $url_service;
77
    }
78
 
79
    private function getNomMethodeService() {
80
        $methode = '';
81
        $service_formate = str_replace(' ', '', ucwords(implode(' ', explode('_', $this->service))));
82
        $methode = 'getService'.$service_formate;
83
        return $methode;
84
    }
85
 
86
    private function getTypeMime() {
87
        $mime = '';
88
        switch ($this->format) {
89
            case 'atom' :
90
                $mime = 'application/atom+xml';
91
                break;
92
            case 'rss1' :
93
            case 'rss2' :
94
                $mime = 'application/rss+xml';
95
                break;
96
            default:
97
                $mime = 'text/html';
98
        }
99
        return $mime;
100
    }
101
 
102
    private function getFormatageJson() {
103
        $json = false;
104
        switch ($this->service) {
105
            case 'liste_des_flux' :
106
                $json = true;
107
                break;
108
            default:
109
                $json = false;
110
        }
111
        return $json;
112
    }
113
 
114
    private function getFlux($nom) {
115
        $nom = strtolower($nom);
116
        return isset($this->flux[$nom]) ? $this->flux[$nom] : array();
117
    }
118
 
119
    private function setFlux($nom, $titre, $description) {
120
        $url_base = $this->config['appli']['urlBaseJrest'].'ProjetSyndication/';
121
        $formats = array('atom', 'rss2', 'rss1');
122
        $flux = array();
123
        foreach ($formats as $format) {
124
            $url = $url_base.$nom.'/'.$format;
125
            $flux[$format] = $url;
126
        }
127
        $this->flux[$nom] = array('titre' => $titre, 'description' => $description, 'urls' => $flux);
128
    }
129
 
130
    private function chargerListeDesFlux() {
131
        $this->setFlux('actifs', 'Flux de syndication des projets les plus actifs',
132
            'Ce flux fournit des informations sur les  projets les plus actifs de l\'espace projet');
133
        $this->setFlux('derniers_messages', 'Flux de syndication des derniers messages',
134
            'Ce flux fournit des informations sur les  derniers messages de l\'espace projet');
135
    }
136
 
137
    private function getServiceListeDesFlux() {
138
        return $this->flux;
139
    }
140
 
141
    private function getServiceActifs() {
142
        // Construction de la requête
143
        $requete_projets =  ' select p_id, p_titre as titre, p_resume as description, pl_id_liste, plle_id_liste, p_avoir_document, ps_nombre_inscrit, ps_pourcent,'.
144
						' p_wikini, ps_doc_derniere_semaine, p_avoir_document, ps_nombre_inscrit_derniere_semaine, '.
145
						' ps_nombre_membre_yahoo, ps_msg_derniere_semaine, ps_modifwiki_derniere_semaine, p_en_dormance from'.
146
						' projet left join projet_lien_liste on p_id=pl_id_projet'.
147
						' left join projet_lien_liste_externe on p_id=plle_id_projet'.
148
						' left join projet_statistiques on p_id=ps_ce_projet and ps_dernier=1'.
149
						' WHERE p_en_dormance = 0'.
150
						' group by p_id order by ps_pourcent desc';
151
 
152
        $elements = $this->executerRequete($requete_projets);
153
 
154
 
155
        // Création du contenu
156
        $contenu = $this->executerService('cpr_nom', $elements);
157
        return $contenu;
158
    }
159
 
160
    private function getServiceDerniersMessages() {
161
	/*include_once("lib/ezmlmAccessObject.class.php");
162
	$xml_parser = &new ezmlmAccessObject('list_info', 'tela-botanica.org',
163
			'determination_plantes', 'fr', 'http://localhost') ;
164
	$xml_parser->load();
165
	ob_start();
166
	$resultat = $xml_parser->parse() ;
167
	$calendrier = ob_get_contents();
168
	ob_end_clean();*/
169
    }
170
 
171
    private function executerRequete($requete) {
172
        try {
173
            $infos = $this->bdd->query($requete)->fetchAll(PDO::FETCH_ASSOC);
174
            if ($infos === false) {
175
                $this->messages[] = "La requête a retourné aucun résultat.";
176
            }
177
        } catch (PDOException $e) {
178
            $this->messages[] = sprintf($this->getTxt('sql_erreur'), $e->getFile(), $e->getLine(), $e->getMessage());
179
        }
180
        return $infos;
181
    }
182
 
183
 
184
    private function executerService($champ_titre, $elements) {
185
        // Prétraitement des données
186
        $donnees = $this->construireDonneesCommunesAuFlux($elements);
187
        foreach ($elements as $element) {
188
            //$xml = $this->getXmlHisto($element);
189
            //$enrg = $this->getTableauDepuisXmlHisto($xml);
190
            //$diff = $this->getDiffInfos($element);
191
            //$diff['differences'] = $this->getDiff($element);
192
            //$diff_html = (!is_null($diff['differences'])) ? Coel::traiterSquelettePhp($this->squelette_diff, $diff) : '';
193
 
194
            $item = $this->construireDonneesCommunesAuxItems($element);
195
            $item['titre'] = $this->creerTitre($champ_titre, $element, $enrg);
196
            $item['guid'] = sprintf($this->config['appli']['guid'], 'projet', $element['p_id']);
197
            $item['lien'] = $this->config['appli']['url_base_projet'].'?id_projet='.urlencode($element['p_id']);
198
	    $item['description'] = "<h4>".$element['ps_nombre_inscrit'].' inscrits - '.$element['ps_msg_derniere_semaine'].' nouveaux messages</h4>';
199
            $item['description'] .= '<p>'.$this->creerDescription($element, $enrg).'</p>';
200
            $item['description'] .= $diff_html;
201
            $item['description'] = $this->nettoyerTexte($item['description']);
202
            $item['description_encodee'] = htmlspecialchars($item['description']);
203
 
204
            $donnees['items'][] = $item;
205
        }
206
 
207
        // Création du contenu à partir d'un template PHP
208
        $contenu = ProjetService::traiterSquelettePhp($this->squelette, $donnees);
209
 
210
        return $contenu;
211
    }
212
 
213
    private function creerTitre($champ, $element, $enrg) {
214
        $titre = '';
215
        if (isset($element['titre'])) {
216
            $titre = $element['titre'];
217
        } else if (isset($element[$champ])) {
218
            $titre = $element[$champ];
219
        } else if (isset($enrg[$champ])) {
220
            $titre = $enrg[$champ];
221
        }
222
        $titre = $this->nettoyerTexte($titre);
223
        return $titre;
224
    }
225
 
226
    private function creerDescription($element, $enrg) {
227
        $description = '';
228
        if (isset($element['description'])) {
229
            $description = strip_tags($element['description']);
230
        }
231
        return $description;
232
    }
233
 
234
    private function nettoyerNomChamps($infos) {
235
        $sortie = array();
236
        foreach ($infos as $champ => $valeur) {
237
            if (preg_match('/^__(.+)$/', $champ, $match)) {
238
                $sortie[$match[1]] = $valeur;
239
            } else {
240
                $sortie[$champ] = $valeur;
241
            }
242
        }
243
        return $sortie;
244
    }
245
 
246
    private function traiterInfosPrecedentes($infos_courantes, $infos_precedentes) {
247
        $infos_precedentes_traitees = array();
248
        foreach ($infos_precedentes as $champ => $valeur) {
249
            if ($champ == 'cmhl_date_modification') {
250
                $infos_precedentes_traitees['date_prec'] = $valeur;
251
            } else if ($champ == 'cmhl_enregistrement') {
252
                $infos_precedentes_traitees['enrg_prec'] = $valeur;
253
            } else if (preg_match('/^__(.+)$/', $champ, $match)) {
254
                $infos_precedentes_traitees[$match[1].'_prec'] = $valeur;
255
            }
256
        }
257
        $sortie = array_merge($infos_courantes, $infos_precedentes_traitees);
258
        return $sortie;
259
    }
260
 
261
    private function nettoyerTexte($txt) {
262
        $txt = preg_replace('/&(?!amp;)/i', '&amp;', $txt, -1);
263
        return $txt;
264
    }
265
 
266
    private function getMessageModif($item) {
267
        $message = $item['etat'].' le '.$item['date_maj_simple'].' par '.$item['modifier_par'].' depuis l\'IP '.$item['ip'];
268
        return $message;
269
    }
270
 
271
    private function construireDonneesCommunesAuxItems($info) {
272
        $item = array();
273
        $date_modification_timestamp = strtotime($info['cmhl_date_modification']);
274
        $item['date_maj_simple'] = strftime('%A %d %B %Y à %H:%M', $date_modification_timestamp);
275
        $item['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
276
        $item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
277
        $item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
278
        $item['guid'] = $info['cmhl_id_historique_ligne'];
279
        $item['cle'] = $info['cmhl_cle_ligne'];
280
        $item['ip'] = $info['cmhl_ip'];
281
        $item['modifier_par'] = $info['modifier_par'];
282
        $item['etat'] = isset($info['cmhl_ce_etat']) ? $this->getTexteEtat($info['cmhl_ce_etat']) : '';
283
 
284
        return $item;
285
    }
286
 
287
 
288
    private function getTexteEtat($code) {
289
        $etat = '';
290
        switch ($code) {
291
            case '1' :
292
                $etat = 'Ajouté';
293
                break;
294
            case '2' :
295
                $etat = 'Modifié';
296
                break;
297
            case '3' :
298
                $etat = 'Supprimé';
299
                break;
300
            default :
301
                $etat = '!Problème!';
302
                $e = "Le champ cmhl_ce_etat possède une valeur innatendue : $code";
303
                $this->messages[] = $e;
304
        }
305
        return $etat;
306
    }
307
 
308
    private function construireDonneesCommunesAuFlux($infos) {
309
        $donnees = $this->getFlux($this->service);
310
        $donnees['guid'] = $this->getUrlServiceBase();
311
        $donnees['lien_service'] = $this->creerUrlService();
312
        $donnees['lien_projet'] = $this->config['appli']['url_base_projet'];
313
        $donnees['editeur'] = $this->config['coel']['editeur'];
314
        $derniere_info_en_date = reset($infos);
315
        $date_modification_timestamp = strtotime($derniere_info_en_date['cmhl_date_modification']);
316
        $donnees['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
317
        $donnees['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
318
        $donnees['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
319
        $donnees['annee_courante'] = date('Y');
320
        $donnees['generateur'] = 'COEL - Jrest';
321
        preg_match('/([0-9]+)/', '$Revision: 381 $', $match);
322
        $donnees['generateur_version'] = $match[1];
323
        return $donnees;
324
    }
325
 
326
    private function creerUrlService() {
327
        $url_service = $this->getUrlServiceBase();
328
        if (isset($this->start) || isset($this->limit)) {
329
            $arguments = array();
330
            if (isset($this->start) && isset($_GET['start'])) {
331
                $arguments[] = 'start='.$this->start;
332
            }
333
            if (isset($this->limit) && isset($_GET['limit'])) {
334
                $arguments[] = 'limit='.($this->limit/2);
335
            }
336
            if (count($arguments) > 0) {
337
                $url_service .= '?'.implode('&', $arguments);
338
            }
339
        }
340
        return $url_service;
341
    }
342
}