Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev Author Line No. Line
302 gduche 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
* Classe implémentant l'API d'eFlore Publications pour le projet Biblio bota.
5
*
6
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=EfloreApi01Publications
7
*
8
* @package eFlore/services
9
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
10
* @author Aurélien PERONNET <aurelien@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 1.0
14
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
15
*/
16
// TODO : Config et Outils sont des classes statiques qui doivent poser des pb pour les tests...
536 gduche 17
class Publications extends Commun{
302 gduche 18
 
536 gduche 19
	protected $parametres;
20
	protected $ressources;
302 gduche 21
	private $Bdd;
22
	private $config;
23
	private $nbrePublications;
24
	private $masque_taxon;
536 gduche 25
	protected $service = 'publications';
302 gduche 26
 
27
	public function __construct(Bdd $bdd = null, Array $config = null, Utilisateurs $utilisateurs = null, Url $url = null) {
28
		$this->Bdd = is_null($bdd) ? new Bdd() : $bdd;
29
		$this->UrlNavigation = is_null($url) ? new Url($this->config['url_service']) : $url;
30
	}
31
 
32
	public function consulter($ressources, $parametres) {
33
 
34
		$resultat = new ResultatService();
35
		$this->parametres = $parametres;
36
		$this->ressources = $ressources;
37
 
38
		$this->definirValeurParDefautDesParametres();
39
		$this->verifierParametres();
40
		$this->recupererParametresMasque();
41
		$this->chargerNbrePublicationsTotal();
42
 
43
		$publications = $this->getPublications($this->masque_taxon);
44
		$resultat->corps = $this->formaterPublications($publications);
45
 
46
		return $resultat;
47
	}
48
 
49
	public function getPublications($cherche) {
50
 
51
		$depart = $this->parametres['navigation.depart'];
52
		$limite = $this->parametres['navigation.limite'];
53
 
54
		$tab_mots = preg_split('~ ~', $cherche, -1, PREG_SPLIT_NO_EMPTY);
55
		$sql = 	'SELECT DISTINCT B_C_CRAI, B_C_NOMCOMPLET, B_C_ABREGE, ' .
56
				'	B_F_NUMERO, B_F_TITRE, B_F_DATE, B_F_CRAICOLL, '.
57
				'	B_SER_SOUSTITRE, '.
58
				'	B_A_PAGEDEBUT, B_A_PAGEFIN, B_A_CRAIFASC, B_A_CRAISERIE, ' .
59
				'	B_D_ID, B_D_LABEL, ' .
60
				'	B_S_IDSTR, B_S_NOM, ' .
61
				'	B_AS_LIBELLE, '.
62
				'	biblio_serie.*, biblio_item.*, biblio_item_typlog.*, biblio_item_typphy.* '.
63
				'FROM biblio_item '.
64
				'	LEFT JOIN biblio_aut_saisie ON (b_i_auteursaisie = b_as_id) '.
65
				'	LEFT JOIN biblio_article ON (b_i_iditem = b_a_idart) '.
66
				'	LEFT JOIN biblio_fasc ON '.
67
				'		(B_F_CRAICOLL = B_A_CRAICOLL '.
68
				'		AND B_F_CRAISERIE = B_A_CRAISERIE '.
69
				'		AND B_F_NUMERO = B_A_CRAIFASC)' .
70
				'	LEFT JOIN biblio_serie ON ' .
71
				'		(b_f_craicoll = b_ser_craicoll ' .
72
				'		AND b_f_craiserie = b_ser_idserie) '.
73
				'	LEFT JOIN biblio_collection ON (b_ser_craicoll = b_c_crai) '.
74
				'	LEFT JOIN biblio_str ON (B_C_LKSTR = B_S_IDSTR) '.
75
				'	LEFT JOIN biblio_domaine_lier ON (b_i_iditem = b_dl_iditem) ' .
76
				'	LEFT JOIN biblio_domaine ON (b_dl_iddom = b_d_id) ' .
77
				'	LEFT JOIN biblio_item_typphy ON (b_i_typphy = b_ip_id) ' .
78
				'	LEFT JOIN biblio_item_typlog ON (b_i_typlog = b_il_id) ' .
79
				'	WHERE B_I_TYPLOG = 1 AND B_I_TYPPHY = 2  AND B_I_CACHER = 0 '.
80
				'	AND B_D_ID = 1 ';
81
 
82
		foreach ($tab_mots as $mot) {
83
			$sql .= "	AND (B_I_TITRE LIKE '%$mot%' ".
84
					"		OR B_I_RESUMCLE LIKE '%$mot%' ".
85
					"		OR B_I_COMMENT LIKE '%$mot%') ";
86
		}
87
		$sql .= 'ORDER BY B_I_AUTEURS LIMIT '.$depart.', '.$limite;
88
 
89
		$publications = $this->Bdd->recupererTous($sql);
90
		return $publications;
91
	}
92
 
93
	function formaterPublications($publications) {
94
 
95
		$resultats = array();
96
 
97
		foreach($publications as $publication) {
98
 
99
			$id_a = $publication['B_I_IDITEM'];
100
			$titre = $publication['B_I_TITRE'];
101
			$auteurs = $publication['B_I_AUTEURS'];
102
			$geo = $publication['B_I_GEO'];
103
			$langue = $publication['B_I_LANGUE'];
104
			$resum = $publication['B_I_RESUMCLE'];
105
			$image = $publication['B_I_IMAGE'];
106
			$comment = $publication['B_I_COMMENT'];
107
			// Table Auteur Saisie
108
			$saisie = $publication['B_AS_LIBELLE'];
109
			// Table Domaine
110
			$domaine_id = $publication['B_D_ID'];
111
			$domaine_nom = $publication['B_D_LABEL'];
112
			// Table Collection
113
			$coll_nom = $publication['B_C_NOMCOMPLET'];
114
			$coll_abreviation = $publication['B_C_ABREGE'];
115
			// Table S�rie
116
			$serie_titre = $publication['B_SER_SOUSTITRE'];
117
			// Table Fascicule
118
			$craicoll = $publication['B_F_CRAICOLL'];
119
			$date = $publication['B_F_DATE'];
120
			$fascicule_titre = $publication['B_F_TITRE'];
121
			// Table Article
122
			$craifasc = $publication['B_A_CRAIFASC'];
123
			$craiserie = $publication['B_A_CRAISERIE'];
124
			$page_debut = $publication['B_A_PAGEDEBUT'];
125
			$page_fin = $publication['B_A_PAGEFIN'];
126
			// Table Structure
127
			$nomstr =  $publication['B_S_NOM'];
128
 
129
			// Formatage de la chaine de publication
130
			$chainePublication = "";
131
			if (isset($domaine_nom)) {
132
				$chainePublication .= '<span class="texte_inactif">['.$domaine_nom.']</span>';
133
			}
134
 
135
			if (isset($auteurs)) {
136
				$chainePublication .= $auteurs;
137
			}
138
 
139
			if (isset($titre)) {
140
				$chainePublication .= " - <strong>$titre</strong>";
141
			}
142
 
143
			if (isset($date)) {
144
				$chainePublication .= " - $date";
145
			}
146
 
147
			if (isset($mots_cles)) {
148
				$chainePublication .= " - <em>$mots_cles</em>";
149
			}
150
 
151
			if (isset($pages)) {
152
				$chainePublication .= ", p. $pages";
153
			}
154
 
155
			if (isset($geo)) {
156
				$chainePublication .= " - Départ./Région : <em>$geo</em>";
157
			}
158
 
159
			if (isset($structure)) {
160
				$chainePublication .= " - $structure";
161
			}
162
 
163
			if (isset($collection)) {
164
				$chainePublication .= ", $collection";
165
			}
166
 
167
			if (isset($serie)) {
168
				$chainePublication .= ", $serie";
169
			}
170
 
171
			if (isset($lien)) {
172
				$chainePublication .= ", <a class=\"lien_ext\" href=\"$lien_url\">$fascicule_titre</a>";
173
			} else {
174
				$chainePublication .= ", $fascicule_titre";
175
			}
176
 
177
			if (isset($saisie_auteur)) {
178
				$chainePublication .= " - <span class=\"texte_inactif\">Saisie : $saisie_auteur - Art. n°$article_id</span>";
179
			}
180
 
181
			$resultats[$id_a]['reference_html'] = $chainePublication;
182
		}
183
 
184
		$retour = array('entete' => $this->construireEntete(), 'resultats' => $resultats);
185
		return $retour;
186
 
187
	}
188
 
189
	private function definirValeurParDefautDesParametres() {
190
		if (isset($this->parametres['navigation.depart']) == false) {
191
			$this->parametres['navigation.depart'] = 0;
192
		}
193
		if (isset($this->parametres['navigation.limite']) == false) {
194
			$this->parametres['navigation.limite'] = 10;
195
		}
196
	}
197
 
198
	private function verifierParametres() {
199
		$erreurs = array();
200
 
201
		if ($this->verifierValeurParametreNavigationDepart() == false) {
202
			$erreurs[] = "Le paramètre 'navigation.depart' doit possèder un valeur numérique.";
203
		}
204
 
205
		if ($this->verifierValeurParametreNavigationLimite() == false) {
206
			$erreurs[] = "Le paramètre 'navigation.limite' doit possèder un valeur numérique supérieure à 0.";
207
		}
208
 
209
		if (count($erreurs) > 0) {
210
			$message = implode('<br />', $erreurs);
211
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
212
			throw new Exception($message, $code);
213
		}
214
	}
215
 
216
	private function verifierValeurParametreNavigationDepart() {
217
		$depart = $this->parametres['navigation.depart'];
218
		$ok = is_numeric($depart) ? true : false;
219
		return $ok;
220
	}
221
 
222
	private function verifierValeurParametreNavigationLimite() {
223
		$limite = $this->parametres['navigation.limite'];
224
		$ok = (is_numeric($limite) && $limite != 0) ? true : false;
225
		return $ok;
226
	}
227
 
228
	private function recupererParametresMasque() {
229
		if (isset($this->parametres['masque.taxon'])) {
230
			$this->masque_taxon = $this->parametres['masque.taxon'];
231
		} else {
232
			$this->masque_taxon = "";
233
		}
234
	}
235
 
236
	private function chargerNbrePublicationsTotal() {
237
		$tab_mots = preg_split('~ ~', $this->masque_taxon, -1, PREG_SPLIT_NO_EMPTY);
238
		$requete = 'SELECT COUNT(B_I_IDITEM) AS nbre '.
239
				'FROM biblio_item '.
240
				'	LEFT JOIN biblio_aut_saisie ON (b_i_auteursaisie = b_as_id) '.
241
				'	LEFT JOIN biblio_article ON (b_i_iditem = b_a_idart) '.
242
				'	LEFT JOIN biblio_fasc ON '.
243
				'		(B_F_CRAICOLL = B_A_CRAICOLL '.
244
				'		AND B_F_CRAISERIE = B_A_CRAISERIE '.
245
				'		AND B_F_NUMERO = B_A_CRAIFASC)' .
246
				'	LEFT JOIN biblio_serie ON ' .
247
				'		(b_f_craicoll = b_ser_craicoll ' .
248
				'		AND b_f_craiserie = b_ser_idserie) '.
249
				'	LEFT JOIN biblio_collection ON (b_ser_craicoll = b_c_crai) '.
250
				'	LEFT JOIN biblio_str ON (B_C_LKSTR = B_S_IDSTR) '.
251
				'	LEFT JOIN biblio_domaine_lier ON (b_i_iditem = b_dl_iditem) ' .
252
				'	LEFT JOIN biblio_domaine ON (b_dl_iddom = b_d_id) ' .
253
				'	LEFT JOIN biblio_item_typphy ON (b_i_typphy = b_ip_id) ' .
254
				'	LEFT JOIN biblio_item_typlog ON (b_i_typlog = b_il_id) ' .
255
				'	WHERE B_I_TYPLOG = 1 AND B_I_TYPPHY = 2  AND B_I_CACHER = 0 '.
256
				'	AND B_D_ID = 1 ';
257
		foreach ($tab_mots as $mot) {
258
			$requete .= "	AND (B_I_TITRE LIKE '%$mot%' ".
259
							"		OR B_I_RESUMCLE LIKE '%$mot%' ".
260
							"		OR B_I_COMMENT LIKE '%$mot%') ";
261
		}
262
		$resultats = $this->Bdd->recuperer($requete);
263
		$this->nbrePublications = (int) $resultats['nbre'];
264
	}
265
 
266
	private function construireEntete() {
267
		$entete = array('masque' => '', 'depart' => 0, 'limite' => 10, 'total' => 0);
268
 
269
		$entete['masque'] = $this->recupererMasque();
270
		$entete['depart'] = (int) $this->parametres['navigation.depart'];
271
		$entete['limite'] = (int) $this->parametres['navigation.limite'];
272
		$entete['total'] = $this->nbrePublications;
273
		if ($hrefPrecedent = $this->recupererHrefPrecedent()) {
274
			$entete['href.precedent'] = $hrefPrecedent;
275
		}
276
		if ($hrefSuivant = $this->recupererHrefSuivant()) {
277
			$entete['href.suivant'] = $hrefSuivant;
278
		}
279
 
280
		return $entete;
281
	}
282
 
283
	private function recupererMasque() {
284
		$masqueEntete = '';
285
		if ($this->masque_taxon) {
286
			$masqueEntete = "masque.taxon=$this->masque_taxon";
287
		}
288
		return $masqueEntete;
289
	}
290
 
291
	private function recupererHrefPrecedent() {
292
		$departActuel = $this->parametres['navigation.depart'];
293
		$limite = $this->parametres['navigation.limite'];
294
		$departPrecedent = $departActuel - $limite;
295
		$url = null;
296
		if ($departPrecedent >= 0) {
297
			$url = $this->obtenirUrlNavigation($departPrecedent, $limite, array('masque.taxon' => $this->masque_taxon));
298
		}
299
		return $url;
300
	}
301
 
302
	private function recupererHrefSuivant() {
303
		$departActuel = $this->parametres['navigation.depart'];
304
		$limite = $this->parametres['navigation.limite'];
305
		$departSuivant = $departActuel + $limite;
306
 
307
		$url = null;
308
		if ($departSuivant < $this->nbrePublications) {
309
			$url = $this->obtenirUrlNavigation($departSuivant, $limite, array('masque.taxon' => $this->masque_taxon));
310
		}
311
		return $url;
312
	}
313
 
314
	private function obtenirUrlNavigation($depart, $limite, $parametresAdditionnels) {
315
		$parametres = array(
316
			'navigation.depart' => $depart,
317
			'navigation.limite' => $limite);
318
 
319
		if ($parametresAdditionnels != null) {
320
			$parametres = array_merge($parametres, $parametresAdditionnels);
321
		}
322
		$this->UrlNavigation->setRequete($parametres);
323
		$url = $this->UrlNavigation->getURL();
324
		return $url;
325
	}
326
}
327
?>