Subversion Repositories eFlore/Applications.coel

Rev

Rev 1751 | Rev 1771 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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