Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php
// declare(encoding='UTF-8');
/**
* Classe implémentant l'API d'eFlore Publications pour le projet Biblio bota.
*
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=EfloreApi01Publications
*
* @package eFlore/services
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
* @version 1.0
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
*/
// TODO : Config et Outils sont des classes statiques qui doivent poser des pb pour les tests...
class Publications extends Commun{

        protected $parametres;
        protected $ressources;
        private $Bdd;
        private $config;
        private $nbrePublications;
        private $masque_taxon;
        protected $service = 'publications';
        
        public function __construct(Bdd $bdd = null, Array $config = null, Utilisateurs $utilisateurs = null, Url $url = null) {
                $this->Bdd = is_null($bdd) ? new Bdd() : $bdd;
                $this->UrlNavigation = is_null($url) ? new Url($this->config['url_service']) : $url;
        }

        public function consulter($ressources, $parametres) {
                
                $resultat = new ResultatService();
                $this->parametres = $parametres;
                $this->ressources = $ressources;

                $this->definirValeurParDefautDesParametres();
                $this->verifierParametres();
                $this->recupererParametresMasque();
                $this->chargerNbrePublicationsTotal();
                
                $publications = $this->getPublications($this->masque_taxon);
                $resultat->corps = $this->formaterPublications($publications);

                return $resultat;
        }

        public function getPublications($cherche) {
                
                $depart = $this->parametres['navigation.depart'];
                $limite = $this->parametres['navigation.limite'];
                
                $tab_mots = preg_split('~ ~', $cherche, -1, PREG_SPLIT_NO_EMPTY);
                $sql =  'SELECT DISTINCT B_C_CRAI, B_C_NOMCOMPLET, B_C_ABREGE, ' .
                                '       B_F_NUMERO, B_F_TITRE, B_F_DATE, B_F_CRAICOLL, '.
                                '       B_SER_SOUSTITRE, '.
                                '       B_A_PAGEDEBUT, B_A_PAGEFIN, B_A_CRAIFASC, B_A_CRAISERIE, ' .
                                '       B_D_ID, B_D_LABEL, ' .
                                '       B_S_IDSTR, B_S_NOM, ' .
                                '       B_AS_LIBELLE, '.
                                '       biblio_serie.*, biblio_item.*, biblio_item_typlog.*, biblio_item_typphy.* '.
                                'FROM biblio_item '.
                                '       LEFT JOIN biblio_aut_saisie ON (b_i_auteursaisie = b_as_id) '.
                                '       LEFT JOIN biblio_article ON (b_i_iditem = b_a_idart) '. 
                                '       LEFT JOIN biblio_fasc ON '.
                                '               (B_F_CRAICOLL = B_A_CRAICOLL '.
                                '               AND B_F_CRAISERIE = B_A_CRAISERIE '.
                                '               AND B_F_NUMERO = B_A_CRAIFASC)' .
                                '       LEFT JOIN biblio_serie ON ' .
                                '               (b_f_craicoll = b_ser_craicoll ' .
                                '               AND b_f_craiserie = b_ser_idserie) '.
                                '       LEFT JOIN biblio_collection ON (b_ser_craicoll = b_c_crai) '.
                                '       LEFT JOIN biblio_str ON (B_C_LKSTR = B_S_IDSTR) '.
                                '       LEFT JOIN biblio_domaine_lier ON (b_i_iditem = b_dl_iditem) ' .
                                '       LEFT JOIN biblio_domaine ON (b_dl_iddom = b_d_id) ' .
                                '       LEFT JOIN biblio_item_typphy ON (b_i_typphy = b_ip_id) ' .
                                '       LEFT JOIN biblio_item_typlog ON (b_i_typlog = b_il_id) ' .
                                '       WHERE B_I_TYPLOG = 1 AND B_I_TYPPHY = 2  AND B_I_CACHER = 0 '.
                                '       AND B_D_ID = 1 ';       
                                        
                foreach ($tab_mots as $mot) {
                        $sql .= "       AND (B_I_TITRE LIKE '%$mot%' ".
                                        "               OR B_I_RESUMCLE LIKE '%$mot%' ".
                                        "               OR B_I_COMMENT LIKE '%$mot%') ";
                }
                $sql .= 'ORDER BY B_I_AUTEURS LIMIT '.$depart.', '.$limite;

                $publications = $this->Bdd->recupererTous($sql);
                return $publications;
        }
        
        function formaterPublications($publications) {
                
                $resultats = array();
                
                foreach($publications as $publication) {
                        
                        $id_a = $publication['B_I_IDITEM'];
                        $titre = $publication['B_I_TITRE'];
                        $auteurs = $publication['B_I_AUTEURS'];
                        $geo = $publication['B_I_GEO'];
                        $langue = $publication['B_I_LANGUE'];
                        $resum = $publication['B_I_RESUMCLE'];
                        $image = $publication['B_I_IMAGE'];
                        $comment = $publication['B_I_COMMENT'];
                        // Table Auteur Saisie
                        $saisie = $publication['B_AS_LIBELLE'];
                        // Table Domaine
                        $domaine_id = $publication['B_D_ID'];
                        $domaine_nom = $publication['B_D_LABEL'];
                        // Table Collection
                        $coll_nom = $publication['B_C_NOMCOMPLET'];
                        $coll_abreviation = $publication['B_C_ABREGE'];
                        // Table S�rie
                        $serie_titre = $publication['B_SER_SOUSTITRE'];
                        // Table Fascicule
                        $craicoll = $publication['B_F_CRAICOLL'];
                        $date = $publication['B_F_DATE'];
                        $fascicule_titre = $publication['B_F_TITRE'];
                        // Table Article
                        $craifasc = $publication['B_A_CRAIFASC'];
                        $craiserie = $publication['B_A_CRAISERIE'];
                        $page_debut = $publication['B_A_PAGEDEBUT'];
                        $page_fin = $publication['B_A_PAGEFIN'];
                        // Table Structure
                        $nomstr =  $publication['B_S_NOM'];
                        
                        // Formatage de la chaine de publication 
                        $chainePublication = "";
                        if (isset($domaine_nom)) {
                                $chainePublication .= '<span class="texte_inactif">['.$domaine_nom.']</span>';  
                        }
                        
                        if (isset($auteurs)) {
                                $chainePublication .= $auteurs;
                        }
                        
                        if (isset($titre)) {
                                $chainePublication .= " - <strong>$titre</strong>";
                        }
                        
                        if (isset($date)) {
                                $chainePublication .= " - $date";
                        }

                        if (isset($mots_cles)) {
                                $chainePublication .= " - <em>$mots_cles</em>";
                        }
                        
                        if (isset($page_debut)) {
                                $chainePublication .= ", p. $page_debut";
                                if (isset($page_fin)) {
                                        $chainePublication .= "- $page_fin";
                                }
                        }
                        
                        if (isset($geo)) {
                                $chainePublication .= " - Départ./Région : <em>$geo</em>";
                        }
                        
                        if (isset($structure)) {
                                $chainePublication .= " - $structure";
                        }
                        
                        if (isset($coll_nom)) {
                                $chainePublication .= ", $coll_nom";
                        }
                        
                        if (isset($craiserie)) {
                                $chainePublication .= ", $craiserie";
                        }
                        
                        if (isset($lien)) {
                                $chainePublication .= ", <a class=\"lien_ext\" href=\"$lien_url\">$fascicule_titre</a>";
                        } else {
                                $chainePublication .= ", $fascicule_titre";
                        }
                        
                        if (isset($saisie_auteur)) {
                                $chainePublication .= " - <span class=\"texte_inactif\">Saisie : $saisie_auteur - Art. n°$article_id</span>";
                        }
                        
                        $resultats[$id_a]['reference_html'] = $chainePublication;
                }
                
                $retour = array('entete' => $this->construireEntete(), 'resultats' => $resultats);
                return $retour;
                        
        }
        
        private function definirValeurParDefautDesParametres() {
                if (isset($this->parametres['navigation.depart']) == false) {
                        $this->parametres['navigation.depart'] = 0;
                }
                if (isset($this->parametres['navigation.limite']) == false) {
                        $this->parametres['navigation.limite'] = 10;
                }
        }

        private function verifierParametres() {
                $erreurs = array();
                
                if ($this->verifierValeurParametreNavigationDepart() == false) {
                        $erreurs[] = "Le paramètre 'navigation.depart' doit possèder un valeur numérique.";
                }
                
                if ($this->verifierValeurParametreNavigationLimite() == false) {
                        $erreurs[] = "Le paramètre 'navigation.limite' doit possèder un valeur numérique supérieure à 0.";
                }

                if (count($erreurs) > 0) {
                        $message = implode('<br />', $erreurs);
                        $code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
                        throw new Exception($message, $code);
                }
        }

        private function verifierValeurParametreNavigationDepart() {
                $depart = $this->parametres['navigation.depart'];
                $ok = is_numeric($depart) ? true : false;
                return $ok;
        }

        private function verifierValeurParametreNavigationLimite() {
                $limite = $this->parametres['navigation.limite'];
                $ok = (is_numeric($limite) && $limite != 0) ? true : false;
                return $ok;
        }

        private function recupererParametresMasque() {
                if (isset($this->parametres['masque.taxon'])) {
                        $this->masque_taxon = $this->parametres['masque.taxon'];
                } else {
                        $this->masque_taxon = "";
                }
        }

        private function chargerNbrePublicationsTotal() {
                $tab_mots = preg_split('~ ~', $this->masque_taxon, -1, PREG_SPLIT_NO_EMPTY);
                $requete = 'SELECT COUNT(B_I_IDITEM) AS nbre '.
                                'FROM biblio_item '.
                                '       LEFT JOIN biblio_aut_saisie ON (b_i_auteursaisie = b_as_id) '.
                                '       LEFT JOIN biblio_article ON (b_i_iditem = b_a_idart) '. 
                                '       LEFT JOIN biblio_fasc ON '.
                                '               (B_F_CRAICOLL = B_A_CRAICOLL '.
                                '               AND B_F_CRAISERIE = B_A_CRAISERIE '.
                                '               AND B_F_NUMERO = B_A_CRAIFASC)' .
                                '       LEFT JOIN biblio_serie ON ' .
                                '               (b_f_craicoll = b_ser_craicoll ' .
                                '               AND b_f_craiserie = b_ser_idserie) '.
                                '       LEFT JOIN biblio_collection ON (b_ser_craicoll = b_c_crai) '.
                                '       LEFT JOIN biblio_str ON (B_C_LKSTR = B_S_IDSTR) '.
                                '       LEFT JOIN biblio_domaine_lier ON (b_i_iditem = b_dl_iditem) ' .
                                '       LEFT JOIN biblio_domaine ON (b_dl_iddom = b_d_id) ' .
                                '       LEFT JOIN biblio_item_typphy ON (b_i_typphy = b_ip_id) ' .
                                '       LEFT JOIN biblio_item_typlog ON (b_i_typlog = b_il_id) ' .
                                '       WHERE B_I_TYPLOG = 1 AND B_I_TYPPHY = 2  AND B_I_CACHER = 0 '.
                                '       AND B_D_ID = 1 ';
                foreach ($tab_mots as $mot) {
                        $requete .= "   AND (B_I_TITRE LIKE '%$mot%' ".
                                                        "               OR B_I_RESUMCLE LIKE '%$mot%' ".
                                                        "               OR B_I_COMMENT LIKE '%$mot%') ";
                }
                $resultats = $this->Bdd->recuperer($requete);
                $this->nbrePublications = (int) $resultats['nbre'];
        }

        private function construireEntete() {
                $entete = array('masque' => '', 'depart' => 0, 'limite' => 10, 'total' => 0);

                $entete['masque'] = $this->recupererMasque();
                $entete['depart'] = (int) $this->parametres['navigation.depart'];
                $entete['limite'] = (int) $this->parametres['navigation.limite'];
                $entete['total'] = $this->nbrePublications;
                if ($hrefPrecedent = $this->recupererHrefPrecedent()) {
                        $entete['href.precedent'] = $hrefPrecedent;
                }
                if ($hrefSuivant = $this->recupererHrefSuivant()) {
                        $entete['href.suivant'] = $hrefSuivant;
                }
                
                return $entete;
        }

        private function recupererMasque() {
                $masqueEntete = '';
                if ($this->masque_taxon) {
                        $masqueEntete = "masque.taxon=$this->masque_taxon";
                }
                return $masqueEntete;
        }
        
        private function recupererHrefPrecedent() {
                $departActuel = $this->parametres['navigation.depart'];
                $limite = $this->parametres['navigation.limite'];
                $departPrecedent = $departActuel - $limite;
                $url = null;
                if ($departPrecedent >= 0) {
                        $url = $this->obtenirUrlNavigation($departPrecedent, $limite, array('masque.taxon' => $this->masque_taxon));
                }
                return $url;
        }

        private function recupererHrefSuivant() {
                $departActuel = $this->parametres['navigation.depart'];
                $limite = $this->parametres['navigation.limite'];
                $departSuivant = $departActuel + $limite;
                
                $url = null;
                if ($departSuivant < $this->nbrePublications) {
                        $url = $this->obtenirUrlNavigation($departSuivant, $limite, array('masque.taxon' => $this->masque_taxon));
                }
                return $url;
        }

        private function obtenirUrlNavigation($depart, $limite, $parametresAdditionnels) {
                $parametres = array(
                        'navigation.depart' => $depart,
                        'navigation.limite' => $limite);
                
                if ($parametresAdditionnels != null) {
                        $parametres = array_merge($parametres, $parametresAdditionnels);
                }
                $this->UrlNavigation->setRequete($parametres);
                $url = $this->UrlNavigation->getURL();
                return $url;
        }
}
?>