Subversion Repositories eFlore/Applications.coel

Rev

Rev 1528 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1497 jpm 1
<?php
2
/** Paramêtres indiquant que l'on est en français pour permettre l'affichage des dates en français. */
3
setlocale(LC_TIME, 'fr_FR.UTF-8');
4
/**
5
 * Service fournissant des informations concernant COEL au format RSS1, RSS2 ou ATOM.
6
 * Encodage en entrée : utf8
7
 * Encodage en sortie : utf8
8
 *
9
 * Notes : pour les requêtes sur la table d'historique cela peut être assez compliqué!
10
 * Voir : http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/
11
 *
12
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
13
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
14
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
15
 * @version $Id: CoelSyndication.php 381 2010-05-17 17:10:37Z jpm $
16
 * @copyright 2009
17
 */
18
class CoelSyndication extends Coel {
19
 
20
	private $format = null;
21
	private $service = null;
22
	private $squelette = null;
23
	private $squelette_dossier = null;
24
	private $squelette_diff = null;
25
	private $flux = array();
26
 
27
	/**
28
	 * Méthode appelée avec une requête de type GET.
29
	 */
30
	public function getElement($param = array()) {
31
		// Initialisation des variables
32
		$info = array();
33
		$contenu = '';
34
 
35
		// Pré traitement des paramêtres
36
		$pour_bdd = false;
37
		$p = $this->traiterParametresUrl(array('service', 'format'), $param, $pour_bdd);
38
 
39
		// Récupération de la liste des flux
40
		$this->chargerListeDesFlux();
41
 
42
		// Chargement du bon type de service demandé
43
		if (isset($p['service'])) {
44
			$this->service = strtolower($p['service']);
45
			$methode = $this->getNomMethodeService();
46
			if (method_exists($this, $methode)) {
47
				if ($this->service != 'liste_des_flux') {
48
					if (isset($p['format']) && preg_match('/^(?:rss1|rss2|atom)$/i', $p['format'])) {
49
						// Multiplication par deux de la limite car nous récupérons deux lignes par item
50
						$this->limit = $this->limit*2;
51
						// Mise en minuscule de l'indication du format
52
						$this->format = strtolower($p['format']);
53
						// Définition du fichier squelette demandé
54
						$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
55
						$this->squelette = $this->squelette_dossier.$this->format.'.tpl.xml';
56
						$this->squelette_diff = $this->squelette_dossier.'diff.tpl.html';
57
					} else {
58
						$this->format = '';
59
						$this->messages[] = "Le service COEL Syndication nécessite d'indiquer en second paramètre le format : rss1, rss2 ou atom.";
60
					}
61
				}
62
				// Récupération du contenu à renvoyer
63
				$contenu = $this->$methode();
64
			} else {
65
				$this->messages[] = "Le type d'information demandé '$this->service' n'est pas disponible.";
66
			}
67
		} else {
68
			$this->messages[] = "Le service COEL Syndication nécessite d'indiquer en premier paramètre le type d'information demandé.";
69
		}
70
 
71
		// Envoie sur la sortie standard
72
		$encodage = 'utf-8';
73
		$mime = $this->getTypeMime();
74
		$formatage_json = $this->getFormatageJson();
75
		$this->envoyer($contenu, $mime, $encodage, $formatage_json);
76
	}
77
 
78
	private function getUrlServiceBase() {
79
		$url_service = $this->config['coel']['urlBaseJrest'].'CoelSyndication/'.$this->service.'/'.$this->format;
80
		return $url_service;
81
	}
82
 
83
	private function getNomMethodeService() {
84
		$methode = '';
85
		$service_formate = str_replace(' ', '', ucwords(implode(' ', explode('_', $this->service))));
86
		$methode = 'getService'.$service_formate;
87
		return $methode;
88
	}
89
 
90
	private function getTypeMime() {
91
		$mime = '';
92
		switch ($this->format) {
93
			case 'atom' :
94
				$mime = 'application/atom+xml';
95
				break;
96
			case 'rss1' :
97
			case 'rss2' :
98
				$mime = 'application/rss+xml';
99
				break;
100
			default:
101
				$mime = 'text/html';
102
		}
103
		return $mime;
104
	}
105
 
106
	private function getFormatageJson() {
107
		$json = false;
108
		switch ($this->service) {
109
			case 'liste_des_flux' :
110
				$json = true;
111
				break;
112
			default:
113
				$json = false;
114
		}
115
		return $json;
116
	}
117
 
118
	private function getFlux($nom) {
119
		$nom = strtolower($nom);
120
		return isset($this->flux[$nom]) ? $this->flux[$nom] : array();
121
	}
122
 
123
	private function setFlux($nom, $titre, $description) {
124
		$url_base = $this->config['coel']['urlBaseJrest'].'CoelSyndication/';
125
		$formats = array('atom', 'rss2', 'rss1');
126
		$flux = array();
127
		foreach ($formats as $format) {
128
			$url = $url_base.$nom.'/'.$format;
129
			$flux[$format] = $url;
130
		}
131
		$this->flux[$nom] = array('titre' => $titre, 'description' => $description, 'urls' => $flux);
132
	}
133
 
134
	private function chargerListeDesFlux() {
135
 
136
		$this->setFlux('projet', 'Flux de syndication des projets',
137
			'Ce flux fournit des informations sur les mises à jour des projets saisis dans COEL.');
138
		$this->setFlux('structure','Flux de syndication des institutions',
139
			'Ce flux fournit des informations sur les mises à jour des institutions saisies dans COEL.');
140
		$this->setFlux('collection', 'Flux de syndication des Collections',
141
			'Ce flux fournit des informations sur les mises à jour des collections saisies dans COEL.');
142
		$this->setFlux('personne', 'Flux de syndication des personnes',
143
			'Ce flux fournit des informations sur les mises à jour des personnes saisies dans COEL.');
144
		$this->setFlux('publication', 'Flux de syndication des publications',
145
			'Ce flux fournit des informations sur les mises à jour des publications saisies dans COEL.');
146
		$this->setFlux('commentaire', 'Flux de syndication des notes',
147
			'Ce flux fournit des informations sur les mises à jour des notes saisies dans COEL.');
148
	}
149
 
150
	private function getServiceListeDesFlux() {
151
		return $this->flux;
152
	}
153
 
154
	private function getServiceProjet() {
155
		// Construction de la requête
156
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' '.
157
			'	h1.cmhl_id_historique_ligne, h1.cmhl_ce_table, h1.cmhl_cle_ligne, '.
158
			'	h1.cmhl_enregistrement, '.
159
			'	h1.cmhl_date_modification, h1.cmhl_ce_etat, h1.cmhl_ip, '.
160
			'	h1.cmhl_cle_ligne AS guid, '.
161
			"	'prj' AS guid_type, ".
162
			'	cp_fmt_nom_complet AS modifier_par '.
163
			'FROM coel_meta_historique_ligne AS h1 '.
164
    		'	LEFT JOIN coel_meta_historique_ligne AS h2  '.
165
    		'	ON (h1.cmhl_ce_table = h2.cmhl_ce_table '.
166
			'		AND h1.cmhl_cle_ligne = h2.cmhl_cle_ligne '.
167
			'		AND h1.cmhl_date_modification <= h2.cmhl_date_modification ) '.
168
			'	LEFT JOIN coel_personne ON (h1.cmhl_ce_modifier_par = cp_id_personne) '.
169
			'WHERE h1.cmhl_ce_table = 115 %s '.
170
			'GROUP BY h1.cmhl_cle_ligne, h1.cmhl_date_modification, h1.cmhl_date_modification '.
171
			'HAVING COUNT(*) = %s '.
172
			'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'h1.cmhl_date_modification DESC').' '.
173
			"LIMIT $this->start,$this->limit ";
174
 
175
		$elements = $this->executerRequeteHistorique($requete);
176
 
177
		// Création du contenu
178
		$contenu = $this->executerService('cpr_nom', $elements);
179
		return $contenu;
180
	}
181
 
182
	private function getServiceStructure() {
183
		$elements = array();
184
		$elements = array_merge($elements, $this->getHistoriqueTableStructure(120));
185
		$elements = array_merge($elements, $this->getHistoriqueTableStructure(122));
186
		$elements = array_merge($elements, $this->getHistoriqueTableStructure(123));
187
		$elements = array_merge($elements, $this->getHistoriqueStructureAPersonne());
188
		krsort($elements);
189
		$elements = array_slice($elements, 0, ($this->limit/2));
190
 
191
		// Création du contenu
192
		$contenu = $this->executerService('cs_nom', $elements);
193
		return $contenu;
194
	}
195
 
196
	private function getHistoriqueTableStructure($table_id) {
197
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' '.
198
			'	h1.cmhl_id_historique_ligne, h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_enregistrement, '.
199
			'	h1.cmhl_date_modification, h1.cmhl_ce_etat, h1.cmhl_ip, '.
200
			'	h1.cmhl_cle_ligne AS guid, '.
201
			"	'str' AS guid_type, ".
202
			(($table_id != 120) ? '	CONCAT(IF (h1.cmhl_ce_table = 122, "Conservation", "Valorisation"), " - ", cs_nom) AS titre, ' : '').
203
			'	cp_fmt_nom_complet AS modifier_par '.
204
			'FROM coel_meta_historique_ligne AS h1 '.
205
    		'	LEFT JOIN coel_meta_historique_ligne AS h2  '.
206
    		'	ON (h1.cmhl_ce_table = h2.cmhl_ce_table '.
207
			'		AND h1.cmhl_cle_ligne = h2.cmhl_cle_ligne '.
208
			'		AND h1.cmhl_date_modification <= h2.cmhl_date_modification ) '.
209
			'	LEFT JOIN coel_personne ON (h1.cmhl_ce_modifier_par = cp_id_personne) '.
210
			(($table_id != 120) ? '	LEFT JOIN coel_structure ON (cs_id_structure = h1.cmhl_cle_ligne) ' : '').
211
			"WHERE h1.cmhl_ce_table = $table_id %s ".
212
			(($table_id != 120) ? '	AND h1.cmhl_ce_etat != 3 ' : '').
213
			'GROUP BY h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_date_modification '.
214
			'HAVING COUNT(*) = %s '.
215
			'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'h1.cmhl_date_modification DESC').' '.
216
			"LIMIT $this->start,$this->limit ";
217
 
218
		$elements = $this->executerRequeteHistorique($requete);
219
		return $elements;
220
	}
221
 
222
	private function getHistoriqueStructureAPersonne() {
223
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' '.
224
			'	h1.cmhl_id_historique_ligne, h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_enregistrement, '.
225
			'	h1.cmhl_date_modification, h1.cmhl_ce_etat, h1.cmhl_ip, '.
226
			'	h1.cmhl_cle_ligne AS guid, '.
227
			"	'csap' AS guid_type, ".
228
			'	p1.cp_fmt_nom_complet AS modifier_par, '.
229
			'	CONCAT(IF(h1.cmhl_ce_etat = 1, "Ajout", IF (h1.cmhl_ce_etat = 3, "Suppression", "Modification")), " d\'une personne liée à «", s1.cs_nom, "»") AS titre, '.
230
			'	CONCAT("Personne «", p2.cp_fmt_nom_complet, "» liée à «", s1.cs_nom, "» avec rôle «", lv1.cmlv_nom, "»") AS description '.
231
			'FROM coel_meta_historique_ligne AS h1 '.
232
    		'	LEFT JOIN coel_meta_historique_ligne AS h2  '.
233
    		'	ON (h1.cmhl_ce_table = h2.cmhl_ce_table '.
234
			'		AND h1.cmhl_cle_ligne = h2.cmhl_cle_ligne '.
235
			'		AND h1.cmhl_date_modification <= h2.cmhl_date_modification ) '.
236
			'	LEFT JOIN coel_personne AS p1 ON (h1.cmhl_ce_modifier_par = p1.cp_id_personne) '.
237
		  	'	LEFT JOIN coel_personne AS p2 '.
238
			"		ON (SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', 1) = p2.cp_id_personne) ".
239
  			'	LEFT JOIN coel_structure AS s1 '.
240
			"		ON (SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', -1) = s1.cs_id_structure) ".
241
  			'	LEFT JOIN coel_meta_liste_valeur AS lv1 '.
242
			"		ON (SUBSTRING_INDEX(SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', 2), '-', -1) = lv1.cmlv_id_valeur) ".
243
			'WHERE h1.cmhl_ce_table = 121 %s '.
244
			'GROUP BY h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_date_modification '.
245
			'HAVING COUNT(*) = %s '.
246
			'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'h1.cmhl_date_modification DESC').' '.
247
			"LIMIT $this->start,$this->limit ";
248
 
249
		$elements = $this->executerRequeteHistorique($requete);
250
		return $elements;
251
	}
252
 
253
	private function getServiceCollection() {
254
		$elements = array();
255
		$elements = array_merge($elements, $this->getHistoriqueTableCollection(101));
256
		$elements = array_merge($elements, $this->getHistoriqueTableCollection(106));
257
		$elements = array_merge($elements, $this->getHistoriqueCollectionAPersonne());
258
		$elements = array_merge($elements, $this->getHistoriqueCollectionAPublication());
259
		$elements = array_merge($elements, $this->getHistoriqueCollectionACommentaire());
260
		krsort($elements);
261
		$elements = array_slice($elements, 0, ($this->limit/2));
262
		//echo '<pre>'.print_r($elements, true).'</pre>';
263
		// Création du contenu
264
		$contenu = $this->executerService('cc_nom', $elements);
265
		return $contenu;
266
	}
267
 
268
	private function getHistoriqueTableCollection($table_id) {
269
		// Reque générale avec paramêtres
270
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' '.
271
					'	h1.cmhl_id_historique_ligne, h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_enregistrement, '.
272
					'	h1.cmhl_date_modification, h1.cmhl_ce_etat, h1.cmhl_ip, '.
273
					'	h1.cmhl_cle_ligne AS guid, '.
274
					"	'col' AS guid_type, ".
275
					(($table_id == 106) ? '	CONCAT("Botanique", " - ", cc_nom) AS titre, ' : '').
276
					'	cp_fmt_nom_complet AS modifier_par '.
277
					'FROM coel_meta_historique_ligne AS h1 '.
278
    				'	LEFT JOIN coel_meta_historique_ligne AS h2  '.
279
    				'	ON (h1.cmhl_ce_table = h2.cmhl_ce_table '.
280
					'		AND h1.cmhl_cle_ligne = h2.cmhl_cle_ligne '.
281
					'		AND h1.cmhl_date_modification <= h2.cmhl_date_modification ) '.
282
					'	LEFT JOIN coel_personne ON (h1.cmhl_ce_modifier_par = cp_id_personne) '.
283
					(($table_id == 106) ? '	LEFT JOIN coel_collection ON (cc_id_collection = h1.cmhl_cle_ligne) ' : '').
284
					"WHERE h1.cmhl_ce_table = $table_id %s ".
285
					(($table_id == 106) ? '	AND h1.cmhl_ce_etat != 3 ' : '').
286
					'GROUP BY h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_date_modification '.
287
					'HAVING COUNT(*) = %s '.
288
					'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'h1.cmhl_date_modification DESC').' '.
289
					"LIMIT $this->start,$this->limit ";
290
		$elements = $this->executerRequeteHistorique($requete);
291
		return $elements;
292
	}
293
 
294
	private function getHistoriqueCollectionAPersonne() {
295
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' '.
296
			'	h1.cmhl_id_historique_ligne, h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_enregistrement, '.
297
			'	h1.cmhl_date_modification, h1.cmhl_ce_etat, h1.cmhl_ip, '.
298
			'	p1.cp_fmt_nom_complet AS modifier_par, '.
299
			'	h1.cmhl_cle_ligne AS guid, '.
300
			"	'ccap' AS guid_type, ".
301
			'	CONCAT(IF(h1.cmhl_ce_etat = 1, "Ajout", IF (h1.cmhl_ce_etat = 3, "Suppression", "Modification")), " d\'une personne liée à «", c1.cc_nom, "»") AS titre, '.
302
			'	CONCAT("Personne «", p2.cp_fmt_nom_complet, "» liée à «", c1.cc_nom, "» avec rôle «", lv1.cmlv_nom, "»") AS description '.
303
			'FROM coel_meta_historique_ligne AS h1 '.
304
    		'	LEFT JOIN coel_meta_historique_ligne AS h2  '.
305
    		'	ON (h1.cmhl_ce_table = h2.cmhl_ce_table '.
306
			'		AND h1.cmhl_cle_ligne = h2.cmhl_cle_ligne '.
307
			'		AND h1.cmhl_date_modification <= h2.cmhl_date_modification ) '.
308
			'	LEFT JOIN coel_personne AS p1 ON (h1.cmhl_ce_modifier_par = p1.cp_id_personne) '.
309
		  	'	LEFT JOIN coel_personne AS p2 '.
310
			"		ON (SUBSTRING_INDEX(SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', 2), '-', -1) = p2.cp_id_personne) ".
311
  			'	LEFT JOIN coel_collection AS c1 '.
312
			"		ON (SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', 1) = c1.cc_id_collection) ".
313
  			'	LEFT JOIN coel_meta_liste_valeur AS lv1 '.
314
			"		ON (SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', -1) = lv1.cmlv_id_valeur) ".
315
			'WHERE h1.cmhl_ce_table = 103 %s '.
316
			'GROUP BY h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_date_modification '.
317
			'HAVING COUNT(*) = %s '.
318
			'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'h1.cmhl_date_modification DESC').' '.
319
			"LIMIT $this->start,$this->limit ";
320
 
321
		$elements = $this->executerRequeteHistorique($requete);
322
		return $elements;
323
	}
324
 
325
	private function getHistoriqueCollectionAPublication() {
326
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' '.
327
			'	h1.cmhl_id_historique_ligne, h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_enregistrement, '.
328
			'	h1.cmhl_date_modification, h1.cmhl_ce_etat, h1.cmhl_ip, '.
329
			'	p1.cp_fmt_nom_complet AS modifier_par, '.
330
			'	h1.cmhl_cle_ligne AS guid, '.
331
			"	'ccapu' AS guid_type, ".
332
			'	CONCAT(IF(h1.cmhl_ce_etat = 1, "Ajout", IF (h1.cmhl_ce_etat = 3, "Suppression", "Modification")), " d\'une publication liée à «", c1.cc_nom, "»") AS titre, '.
333
			'	CONCAT("Publication «", p2.cpu_fmt_nom_complet, "» liée à «", c1.cc_nom, "»") AS description '.
334
			'FROM coel_meta_historique_ligne AS h1 '.
335
    		'	LEFT JOIN coel_meta_historique_ligne AS h2  '.
336
    		'	ON (h1.cmhl_ce_table = h2.cmhl_ce_table '.
337
			'		AND h1.cmhl_cle_ligne = h2.cmhl_cle_ligne '.
338
			'		AND h1.cmhl_date_modification <= h2.cmhl_date_modification ) '.
339
			'	LEFT JOIN coel_personne AS p1 ON (h1.cmhl_ce_modifier_par = p1.cp_id_personne) '.
340
		  	'	LEFT JOIN coel_publication AS p2 '.
341
			"		ON (SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', -1) = p2.cpu_id_publication) ".
342
  			'	LEFT JOIN coel_collection AS c1 '.
343
			"		ON (SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', 1) = c1.cc_id_collection) ".
344
			'WHERE h1.cmhl_ce_table = 104 %s '.
345
			'GROUP BY h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_date_modification '.
346
			'HAVING COUNT(*) = %s '.
347
			'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'h1.cmhl_date_modification DESC').' '.
348
			"LIMIT $this->start,$this->limit ";
349
 
350
		$elements = $this->executerRequeteHistorique($requete);
351
		return $elements;
352
	}
353
 
354
	private function getHistoriqueCollectionACommentaire() {
355
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' '.
356
			'	h1.cmhl_id_historique_ligne, h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_enregistrement, '.
357
			'	h1.cmhl_date_modification, h1.cmhl_ce_etat, h1.cmhl_ip, '.
358
			'	p1.cp_fmt_nom_complet AS modifier_par, '.
359
			'	h1.cmhl_cle_ligne AS guid, '.
360
			"	'ccacm' AS guid_type, ".
361
			'	CONCAT(IF(h1.cmhl_ce_etat = 1, "Ajout", IF (h1.cmhl_ce_etat = 3, "Suppression", "Modification")), " d\'un commentaire lié à «", c1.cc_nom, "»") AS titre, '.
362
			'	CONCAT("Commentaire «", c.ccm_titre, "» liée à «", c1.cc_nom, "»") AS description '.
363
			'FROM coel_meta_historique_ligne AS h1 '.
364
    		'	LEFT JOIN coel_meta_historique_ligne AS h2  '.
365
    		'	ON (h1.cmhl_ce_table = h2.cmhl_ce_table '.
366
			'		AND h1.cmhl_cle_ligne = h2.cmhl_cle_ligne '.
367
			'		AND h1.cmhl_date_modification <= h2.cmhl_date_modification ) '.
368
			'	LEFT JOIN coel_personne AS p1 ON (h1.cmhl_ce_modifier_par = p1.cp_id_personne) '.
369
		  	'	LEFT JOIN coel_commentaire AS c '.
370
			"		ON (SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', -1) = c.ccm_id_commentaire) ".
371
  			'	LEFT JOIN coel_collection AS c1 '.
372
			"		ON (SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', 1) = c1.cc_id_collection) ".
373
			'WHERE h1.cmhl_ce_table = 102 %s '.
374
			'GROUP BY h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_date_modification '.
375
			'HAVING COUNT(*) = %s '.
376
			'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'h1.cmhl_date_modification DESC').' '.
377
			"LIMIT $this->start,$this->limit ";
378
 
379
		$elements = $this->executerRequeteHistorique($requete);
380
		return $elements;
381
	}
382
 
383
	private function getServicePersonne() {
384
		$elements = array();
385
		$elements = array_merge($elements, $this->getHistoriqueTable(113, 'per'));
386
		$elements = array_merge($elements, $this->getHistoriquePublicationAPersonne());
387
		krsort($elements);
388
		$elements = array_slice($elements, 0, ($this->limit/2));
389
		//echo '<pre>'.print_r($elements, true).'</pre>';
390
		// Création du contenu
391
		$contenu = $this->executerService('cp_fmt_nom_complet', $elements);
392
		return $contenu;
393
	}
394
 
395
	private function getHistoriquePublicationAPersonne() {
396
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' '.
397
			'	h1.cmhl_id_historique_ligne, h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_enregistrement, '.
398
			'	h1.cmhl_date_modification, h1.cmhl_ce_etat, h1.cmhl_ip, '.
399
			'	p1.cp_fmt_nom_complet AS modifier_par, '.
400
			'	h1.cmhl_cle_ligne AS guid, '.
401
			"	'cpap' AS guid_type, ".
402
			'	CONCAT(IF(h1.cmhl_ce_etat = 1, "Ajout", IF (h1.cmhl_ce_etat = 3, "Suppression", "Modification")), " d\'une publication liée à «", p2.cp_fmt_nom_complet, "»") AS titre, '.
403
			'	CONCAT("Publication «", pu.cpu_fmt_nom_complet, "» liée à «", p2.cp_fmt_nom_complet, "» avec rôle «", lv1.cmlv_nom, "»") AS description '.
404
			'FROM coel_meta_historique_ligne AS h1 '.
405
    		'	LEFT JOIN coel_meta_historique_ligne AS h2  '.
406
    		'		ON (h1.cmhl_ce_table = h2.cmhl_ce_table '.
407
			'			AND h1.cmhl_cle_ligne = h2.cmhl_cle_ligne '.
408
			'			AND h1.cmhl_date_modification <= h2.cmhl_date_modification ) '.
409
			'	LEFT JOIN coel_personne AS p1 ON (h1.cmhl_ce_modifier_par = p1.cp_id_personne) '.
410
		  	'	LEFT JOIN coel_publication AS pu '.
411
			"		ON (SUBSTRING_INDEX(SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', 2), '-', -1) = pu.cpu_id_publication) ".
412
  			'	LEFT JOIN coel_personne AS p2 '.
413
			"		ON (SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', 1) = p2.cp_id_personne) ".
414
			'	LEFT JOIN coel_meta_liste_valeur AS lv1 '.
415
			"		ON (SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', -1) = lv1.cmlv_id_valeur) ".
416
			'WHERE h1.cmhl_ce_table = 119 '.
417
			'	%s '.
418
			"	AND SUBSTRING_INDEX(h1.cmhl_cle_ligne, '-', -1) IN (2361,2362,2363) ".
419
			'GROUP BY h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_date_modification '.
420
			'HAVING COUNT(*) = %s '.
421
			'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'h1.cmhl_date_modification DESC').' '.
422
			"LIMIT $this->start,$this->limit ";
423
 
424
		$elements = $this->executerRequeteHistorique($requete);
425
		return $elements;
426
	}
427
 
428
	private function getServicePublication() {
429
		$elements = array();
430
		$elements = $this->getHistoriqueTable(118, 'pub');
431
		krsort($elements);
432
		$elements = array_slice($elements, 0, ($this->limit/2));
433
		//echo '<pre>'.print_r($elements, true).'</pre>';
434
		// Création du contenu
435
		$contenu = $this->executerService('cpu_fmt_nom_complet', $elements);
436
		return $contenu;
437
	}
438
 
439
	private function getServiceCommentaire() {
440
		$elements = array();
441
		$elements = $this->getHistoriqueTable(107, 'com');
442
		krsort($elements);
443
		$elements = array_slice($elements, 0, ($this->limit/2));
444
		//echo '<pre>'.print_r($elements, true).'</pre>';
445
		// Création du contenu
446
		$contenu = $this->executerService('ccm_titre', $elements);
447
		return $contenu;
448
	}
449
 
450
	private function getHistoriqueTable($table_id, $guid_type) {
451
		// Reque générale avec paramêtres
452
		$requete = 	(($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' '.
453
					'	h1.cmhl_id_historique_ligne, h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_enregistrement, '.
454
					'	h1.cmhl_date_modification, h1.cmhl_ce_etat, h1.cmhl_ip, '.
455
					'	h1.cmhl_cle_ligne AS guid, '.
456
					"	'$guid_type' AS guid_type, ".
457
					'	cp_fmt_nom_complet AS modifier_par '.
458
					'FROM coel_meta_historique_ligne AS h1 '.
459
    				'	LEFT JOIN coel_meta_historique_ligne AS h2  '.
460
    				'	ON (h1.cmhl_ce_table = h2.cmhl_ce_table '.
461
					'		AND h1.cmhl_cle_ligne = h2.cmhl_cle_ligne '.
462
					'		AND h1.cmhl_date_modification <= h2.cmhl_date_modification ) '.
463
					'	LEFT JOIN coel_personne ON (h1.cmhl_ce_modifier_par = cp_id_personne) '.
464
					"WHERE h1.cmhl_ce_table = $table_id %s ".
465
					'GROUP BY h1.cmhl_ce_table, h1.cmhl_cle_ligne, h1.cmhl_date_modification '.
466
					'HAVING COUNT(*) = %s '.
467
					'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby  : 'h1.cmhl_date_modification DESC').' '.
468
					"LIMIT $this->start,$this->limit ";
469
		$elements = $this->executerRequeteHistorique($requete);
470
		return $elements;
471
	}
472
 
473
	private function executerRequete($requete) {
474
		try {
475
			$infos = $this->bdd->query($requete)->fetchAll(PDO::FETCH_ASSOC);
476
			if ($infos === false) {
477
				$this->messages[] = "La requête a retourné aucun résultat.";
478
			}
479
		} catch (PDOException $e) {
480
			$this->messages[] = sprintf($this->getTxt('sql_erreur'), $e->getFile(), $e->getLine(), $e->getMessage());
481
		}
482
		return $infos;
483
	}
484
 
485
	private function fusionnerEnregistrements($infos) {
486
		// Fusion des lignes
487
		$elements = array();
488
		foreach ($infos as $info) {
489
			$id = $info['cmhl_ce_table'].'|'.$info['cmhl_cle_ligne'];
490
			if (!isset($elements[$id])) {
491
				$elements[$id] = $info;
492
			} else {
493
				if ($elements[$id]['cmhl_date_modification'] < $info['cmhl_date_modification']) {
494
					$elements[$id] = $this->traiterInfosPrecedentes($info, $elements[$id]);
495
				} else {
496
					$elements[$id] = $this->traiterInfosPrecedentes($elements[$id], $info);
497
				}
498
			}
499
		}
500
 
501
		// Nettoyage et utilisation de la date pour pouvoir trier le tableau
502
		$sortie = array();
503
		foreach ($elements as $id => $element) {
504
			$element = $this->nettoyerNomChamps($element);
505
			$id_avec_date = $element['cmhl_date_modification'].'|'.$id;
506
			$sortie[$id_avec_date] = $element;
507
		}
508
 
509
		return $sortie;
510
	}
511
 
512
	private function executerRequeteHistorique($requete) {
513
		$elements = array();
514
 
515
		// Récupération des 1er éléments
516
		$requete_elements_1er = sprintf($requete, '', '1');
517
		$infos_elements_1er = $this->executerRequete($requete_elements_1er);
518
 
519
		// Construction de la requête pour récupérer les second éléments
520
		$elements_1er_cle_ligne = array();
521
		foreach ($infos_elements_1er as $info) {
522
			$elements_1er_cle_ligne[] = $info['cmhl_cle_ligne'];
523
		}
524
		$chaine_1er_elements = 'AND h1.cmhl_cle_ligne IN ('.implode(',', $elements_1er_cle_ligne).') ';
525
		$requete_elements_2nd = sprintf($requete, $chaine_1er_elements, '2');
526
 
527
		// Récupération des 2nd éléments
528
		$infos_elements_2nd = $this->executerRequete($requete_elements_2nd);
529
 
530
		// Fusion des 1er et 2nd éléments
531
		$infos = array_merge($infos_elements_1er, $infos_elements_2nd);
532
 
533
		$elements = $this->fusionnerEnregistrements($infos);
534
		return $elements;
535
	}
536
 
537
	private function executerService($champ_titre, $elements) {
538
		// Prétraitement des données
539
		$donnees = $this->construireDonneesCommunesAuFlux($elements);
540
		foreach ($elements as $element) {
541
			$xml = $this->getXmlHisto($element);
542
			$enrg = $this->getTableauDepuisXmlHisto($xml);
543
			$diff = $this->getDiffInfos($element);
544
			$diff['differences'] = $this->getDiff($element);
545
			$diff_html = (!is_null($diff['differences'])) ? Coel::traiterSquelettePhp($this->squelette_diff, $diff) : '';
546
 
547
			$item = $this->construireDonneesCommunesAuxItems($element);
548
			$item['titre'] = $this->creerTitre($champ_titre, $element, $enrg);
549
			$item['guid'] = sprintf($this->config['coel']['guid'], 'coel', $element['guid_type'].$element['guid']);
550
			$item['lien'] = $this->config['coel']['urlBaseCoel'].'#'.urlencode($item['guid']);
551
			$item['description'] = '<p>'.$this->getMessageModif($item).'</p>';
552
			$item['description'] .= $this->creerDescription($element, $enrg);
553
			$item['description'] .= $diff_html;
554
			$item['description'] = $this->nettoyerTexte($item['description']);
555
			$item['description_encodee'] = htmlspecialchars($item['description']);
556
 
557
			$donnees['items'][] = $item;
558
		}
559
 
560
		// Création du contenu à partir d'un template PHP
561
		$contenu = Coel::traiterSquelettePhp($this->squelette, $donnees);
562
 
563
		return $contenu;
564
	}
565
 
566
	private function creerTitre($champ, $element, $enrg) {
567
		$titre = '';
568
		if (isset($element['titre'])) {
569
			$titre = $element['titre'];
570
		} else if (isset($element[$champ])) {
571
			$titre = $element[$champ];
572
		} else if (isset($enrg[$champ])) {
573
			$titre = $enrg[$champ];
574
		}
575
		$titre = $this->nettoyerTexte($titre);
576
		return $titre;
577
	}
578
 
579
	private function creerDescription($element, $enrg) {
580
		$description = '';
581
		if (isset($element['description'])) {
582
			$description = $element['description'];
583
		}
584
		return $description;
585
	}
586
 
587
	private function nettoyerNomChamps($infos) {
588
		$sortie = array();
589
		foreach ($infos as $champ => $valeur) {
590
			if (preg_match('/^__(.+)$/', $champ, $match)) {
591
				$sortie[$match[1]] = $valeur;
592
			} else {
593
				$sortie[$champ] = $valeur;
594
			}
595
		}
596
		return $sortie;
597
	}
598
 
599
	private function traiterInfosPrecedentes($infos_courantes, $infos_precedentes) {
600
		$infos_precedentes_traitees = array();
601
		foreach ($infos_precedentes as $champ => $valeur) {
602
			if ($champ == 'cmhl_date_modification') {
603
				$infos_precedentes_traitees['date_prec'] = $valeur;
604
			} else if ($champ == 'cmhl_enregistrement') {
605
				$infos_precedentes_traitees['enrg_prec'] = $valeur;
606
			} else if (preg_match('/^__(.+)$/', $champ, $match)) {
607
				$infos_precedentes_traitees[$match[1].'_prec'] = $valeur;
608
			}
609
		}
610
		$sortie = array_merge($infos_courantes, $infos_precedentes_traitees);
611
		return $sortie;
612
	}
613
 
614
	private function nettoyerTexte($txt) {
615
		$txt = preg_replace('/&(?!amp;)/i', '&amp;', $txt, -1);
616
		return $txt;
617
	}
618
 
619
	private function getMessageModif($item) {
620
		$message = $item['etat'].' le '.$item['date_maj_simple'].' par '.$item['modifier_par'].' depuis l\'IP '.$item['ip'];
621
		return $message;
622
	}
623
 
624
	private function construireDonneesCommunesAuxItems($info) {
625
		$item = array();
626
		$date_modification_timestamp = strtotime($info['cmhl_date_modification']);
627
		$item['date_maj_simple'] = strftime('%A %d %B %Y à %H:%M', $date_modification_timestamp);
628
		$item['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
629
		$item['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
630
		$item['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
631
		$item['guid'] = $info['cmhl_id_historique_ligne'];
632
		$item['cle'] = $info['cmhl_cle_ligne'];
633
		$item['ip'] = $info['cmhl_ip'];
634
		$item['modifier_par'] = $info['modifier_par'];
635
		$item['etat'] = isset($info['cmhl_ce_etat']) ? $this->getTexteEtat($info['cmhl_ce_etat']) : '';
636
 
637
		return $item;
638
	}
639
 
640
	private function getXmlHisto($info) {
641
		$xml = '';
642
		if ($info['cmhl_ce_etat'] == '3') {
643
			$xml = $info['enrg_prec'];
644
		} else {
645
			$xml = $info['cmhl_enregistrement'];
646
		}
647
		return $xml;
648
	}
649
 
650
	private function getDiff($info) {
651
		$diff = null;
652
		if ($info['cmhl_ce_etat'] == '1') {
653
			$nouveau = $this->getTableauDepuisXmlHisto($info['cmhl_enregistrement']);
654
			foreach ($nouveau as $cle => $valeur) {
655
				$diff[$cle] = array('type' => 'A', 'type_txt' => 'Ajout', 'nouveau' => $valeur, 'ancien' => ' ');
656
			}
657
		} else if ($info['cmhl_ce_etat'] == '2') {
658
			$nouveau = $this->getTableauDepuisXmlHisto($info['cmhl_enregistrement']);
659
			$ancien = $this->getTableauDepuisXmlHisto($info['enrg_prec']);
660
			foreach ($nouveau as $cle => $valeur) {
661
				if (!isset($ancien[$cle])) {
662
					$diff[$cle] = array('type' => 'A', 'type_txt' => 'Ajout', 'nouveau' => $valeur, 'ancien' => ' ');
663
				} else if (isset($ancien[$cle]) && $ancien[$cle] != $valeur) {
664
					$diff[$cle] = array('type' => 'M', 'type_txt' => 'Modification', 'nouveau' => $valeur, 'ancien' => $ancien[$cle]);
665
				}
666
			}
667
			foreach ($ancien as $cle => $valeur) {
668
				if (!isset($nouveau[$cle])) {
669
					$diff[$cle] = array('type' => 'S', 'type_txt' => 'Suppression', 'nouveau' => ' ', 'ancien' => $valeur);
670
				}
671
			}
672
		}
673
		return $diff;
674
	}
675
 
676
	private function getDiffInfos($info) {
677
		$diff = null;
678
		$format = '%d/%m/%Y à %H:%M:%S';
679
		if ($info['cmhl_ce_etat'] == '1') {
680
			$diff['date_nouvelle'] = strftime($format, strtotime($info['cmhl_date_modification']));
681
			$diff['etat'] = 'A';
682
		} else if ($info['cmhl_ce_etat'] == '2') {
683
			$diff['date_nouvelle'] = strftime($format, strtotime($info['cmhl_date_modification']));
684
			$diff['date_ancienne'] = strftime($format, strtotime($info['date_prec']));
685
			$diff['etat'] = 'M';
686
		} else if ($info['cmhl_ce_etat'] == '3') {
687
			$diff['etat'] = 'S';
688
		}
689
		return $diff;
690
	}
691
 
692
	private function getTexteEtat($code) {
693
		$etat = '';
694
		switch ($code) {
695
			case '1' :
696
				$etat = 'Ajouté';
697
				break;
698
			case '2' :
699
				$etat = 'Modifié';
700
				break;
701
			case '3' :
702
				$etat = 'Supprimé';
703
				break;
704
			default :
705
				$etat = '!Problème!';
706
				$e = "Le champ cmhl_ce_etat possède une valeur innatendue : $code";
707
				$this->messages[] = $e;
708
		}
709
		return $etat;
710
	}
711
 
712
	private function construireDonneesCommunesAuFlux($infos) {
713
		$donnees = $this->getFlux($this->service);
714
		$donnees['guid'] = $this->getUrlServiceBase();
715
		$donnees['lien_service'] = $this->creerUrlService();
716
		$donnees['lien_coel'] = $this->config['coel']['urlBaseCoel'];
717
		$donnees['editeur'] = $this->config['coel']['editeur'];
718
		$derniere_info_en_date = reset($infos);
719
		$date_modification_timestamp = strtotime($derniere_info_en_date['cmhl_date_modification']);
720
		$donnees['date_maj_RSS'] = date(DATE_RSS, $date_modification_timestamp);
721
		$donnees['date_maj_ATOM'] = date(DATE_ATOM, $date_modification_timestamp);
722
		$donnees['date_maj_W3C'] = date(DATE_W3C, $date_modification_timestamp);
723
		$donnees['annee_courante'] = date('Y');
724
		$donnees['generateur'] = 'COEL - Jrest';
725
		preg_match('/([0-9]+)/', '$Revision: 381 $', $match);
726
		$donnees['generateur_version'] = $match[1];
727
		return $donnees;
728
	}
729
 
730
	private function creerUrlService() {
731
		$url_service = $this->getUrlServiceBase();
732
		if (isset($this->start) || isset($this->limit)) {
733
			$arguments = array();
734
			if (isset($this->start) && isset($_GET['start'])) {
735
				$arguments[] = 'start='.$this->start;
736
			}
737
			if (isset($this->limit) && isset($_GET['limit'])) {
738
				$arguments[] = 'limit='.($this->limit/2);
739
			}
740
			if (count($arguments) > 0) {
741
				$url_service .= '?'.implode('&', $arguments);
742
			}
743
		}
744
		return $url_service;
745
	}
746
}