Subversion Repositories eFlore/Applications.bibliobota

Rev

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

<?php
/*vim: set expandtab tabstop=4 shiftwidth=4: */ 
// +------------------------------------------------------------------------------------------------------+
// | PHP version 4.1                                                                                      |
// +------------------------------------------------------------------------------------------------------+
// | Copyright (C) 2004 Tela Botanica (accueil@tela-botanica.org)                                         |
// +------------------------------------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or                                        |
// | modify it under the terms of the GNU Lesser General Public                                           |
// | License as published by the Free Software Foundation; either                                         |
// | version 2.1 of the License, or (at your option) any later version.                                   |
// |                                                                                                      |
// | This library is distributed in the hope that it will be useful,                                      |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                                    |
// | Lesser General Public License for more details.                                                      |
// |                                                                                                      |
// | You should have received a copy of the GNU Lesser General Public                                     |
// | License along with this library; if not, write to the Free Software                                  |
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
// +------------------------------------------------------------------------------------------------------+
// CVS : $Id: bbc_requete_media.fonct.php,v 1.1 2005-11-23 10:22:25 jp_milcent Exp $
/**
* Fonctions de création de la requête sur un média.
*
* Contient une fonction créant la requête d'intérogation de la base de données.
* Valable pour le paramètre "media".
*
*@package BiblioBota-Consultation
*@subpackage Fonctions
//Auteur original :
*@author        Jean-Charles GRANGER <tela@vecteur.org>
//Autres auteurs :
*@author        Jean-Pascal MILCENT <jpm@clapas.org>
*@copyright     Tela-Botanica 2000-2004
*@version       $Revision: 1.1 $ $Date: 2005-11-23 10:22:25 $
// +------------------------------------------------------------------------------------------------------+
*/

// +------------------------------------------------------------------------------------------------------+
// |                                            ENTETE du PROGRAMME                                       |
// +------------------------------------------------------------------------------------------------------+


// +------------------------------------------------------------------------------------------------------+
// |                                           LISTE de FONCTIONS                                         |
// +------------------------------------------------------------------------------------------------------+

/** Fonction mkquery() - Créé une requête intérogeant la base de donnée.
*
* Fonction créant la requête d'intérogation de la base de données pour le
* paramêtre "media".
*
* @return  string la requête permettant de récupérer les média.
*/
function mkquery()
{
    global $tbl;
    global $TabFinder;
    global $nbr_total;
    global $var_biblio;
    global $query_dom;
    global $plugin_store;
    
    // si limit_domain vaut 1, alors on réduit les recherches au seul domaine botanique
    
    if ($var_biblio['limit_domain'] == 1) {
        $add_from_dom = ', '.$tbl['domaine'].', '.$tbl['domaine_lk'];
        // lien domaines -- table de jointure domaines
        $add_limit_dom = ' AND ('.$tbl['domaine'].'.B_D_ID = '.$tbl['domaine_lk'].'.B_DL_IDDOM)';
        // lien articles -- table de jointure domaines
        $add_limit_dom .= ' AND ('.$tbl['item'].'.B_I_IDITEM = '.$tbl['domaine_lk'].'.B_DL_IDITEM)';
        $add_limit_dom .= ' AND '.$tbl['domaine'].'.B_D_ID = 1';
    } else {
        $add_from_dom = ', '.$tbl['domaine'].', '.$tbl['domaine_lk'];
        // lien domaines -- table de jointure domaines
        $add_limit_dom = ' AND ('.$tbl['domaine'].'.B_D_ID = '.$tbl['domaine_lk'].'.B_DL_IDDOM)';
        // lien articles -- table de jointure domaines
        $add_limit_dom .= ' AND ('.$tbl['item'].'.B_I_IDITEM = '.$tbl['domaine_lk'].'.B_DL_IDITEM)';
    }
    
    /*
    Pour une implémentation d'une recherche sur TOUTE LA TABLE ARTICLES
    il faut une requête spéciale qui fasse une jointure à la fois avec les
    tables structure-coll-serie-fasc-art et les tables str-art-media... gasp... 
    */
    
    /* si la variable de catégorie / type vaut 0, il n'y a pas affinage de la requête par catégorie */
    
    if (BB_ARGUMENT_REGROUPEMENT == 'article') {
        // On demande ici les articles : mise en place des réducteurs
        $add_select = 'B_C_CRAI, '.$tbl['serie'].'.*, B_C_NOMCOMPLET, B_C_ABREGE, B_F_NUMERO, B_F_TITRE, B_F_DATE, B_A_PAGEDEBUT, B_A_PAGEFIN, ';
        // $add_select = $tbl['collection'].".B_C_CRAI, ".$tbl['serie'].".*, ".$tbl['collection'].".B_C_NOMCOMPLET, ".$tbl['collection'].".B_C_ABREGE, ".$tbl['fascicule'].".B_F_TITRE, ".$tbl['fascicule'].".B_F_DATE, ".$tbl['article'].".B_A_PAGEDEBUT, ".$tbl['article'].".B_A_PAGEFIN, ";
        $add_from = $tbl['collection'].' LEFT JOIN '.$tbl['str'].' ON B_C_LKSTR = B_S_IDSTR, '.$tbl['fascicule'].', '.$tbl['serie'].', '.$tbl['article'].',';
        // jointure coll - serie
        $special_join = ' AND ('.$tbl['collection'].'.B_C_CRAI = '.$tbl['serie'].'.B_SER_CRAICOLL)';
        // jointure serie - fasc
        $special_join .= ' AND ('.$tbl['serie'].'.B_SER_CRAICOLL = '.$tbl['fascicule'].'.B_F_CRAICOLL)';
        $special_join .= ' AND ('.$tbl['serie'].'.B_SER_IDSERIE = '.$tbl['fascicule'].'.B_F_CRAISERIE)';
        // jointure fasc - item
        $special_join .= ' AND ('.$tbl['fascicule'].'.B_F_CRAICOLL = '.$tbl['article'].'.B_A_CRAICOLL)';
        $special_join .= ' AND ('.$tbl['fascicule'].'.B_F_CRAISERIE = '.$tbl['article'].'.B_A_CRAISERIE)';
        $special_join .= ' AND ('.$tbl['fascicule'].'.B_F_NUMERO = '.$tbl['article'].'.B_A_CRAIFASC)';
        // jointure item - article
        $special_join .= ' AND ('.$tbl['item'].'.B_I_IDITEM = '.$tbl['article'].'.B_A_IDART)';
        
        // réducteur de catégorie (collections) categ
        if ($TabFinder['categ'] != '') {
            $add_to_query = ' AND ('.$tbl['article'].'.B_A_CRAICOLL like "'.$TabFinder['categ'].'%")';
        } else {
            $add_to_query = '';
        }
    } else {
        // On demande ici les livres et médias
        $add_select = $tbl['media'].'.*, '.$tbl['monnaie'].'.*, ';
        // Ajout du FROM
        $add_from = $tbl['media'].' LEFT JOIN '.$tbl['str'].' ON B_M_LKSTR = B_S_IDSTR, '.$tbl['monnaie'].', ';
        // lien media -- article
        $special_join = ' AND ('.$tbl['media'].'.B_M_IDMEDIA = '.$tbl['item'].'.B_I_IDITEM)';
        // lien media -- monnaie
        $special_join .= ' AND ('.$tbl['media'].'.B_M_LKMONNAIE = '.$tbl['monnaie'].'.GEN_MON_IDMONNAIE)';
        //pas de réducteur de catégorie pour les livres
        $add_to_query = '';
    }
    
    // Compte Total : comptage du nombre de données de la table
    // d'abord, créer les critères de sélection physiques et logiques.
    if (($TabFinder['categ_3'] == '') || (BB_ARGUMENT_REGROUPEMENT == 'article')) {
        if ((!empty($query_dom['log']))) {
            $create_log = $query_dom['log'];
        } else {
            $create_log = '';
        }
    } else {
        $create_log = 'B_I_TYPLOG = '.$TabFinder['categ_3'];
    }
    
    if ($TabFinder['categ_2'] == '') {
        if ((!empty($query_dom['phy']))) {
            $create_phy = $query_dom['phy'];
        } else {
            $create_phy = '';
        }
    } else {
        $create_phy = 'B_I_TYPPHY = '.$TabFinder['categ_2'];
    }
    
    if (($create_phy != "")&&($create_log != "")) {
        $create_logphy = "$create_log AND $create_phy";
    } else if (($create_phy != "")||($create_log != "")) {
        $create_logphy = "$create_log$create_phy";
    }
    
    // Création du corps de la requête
    
    // Création du SELECT général
    $normal_query = "SELECT DISTINCT $add_select B_D_ID, B_D_LABEL, B_S_IDSTR, B_S_NOM, B_AS_LIBELLE, ".$tbl['item'].".*, ".$tbl['item_typlog'].".*, ".$tbl['item_typphy'].".* ";
    
    // Création du FROM général
    $normal_query .= " FROM $add_from ".$tbl['saisie'].", ".$tbl['item'].", ".$tbl['item_typlog'].", ".$tbl['item_typphy']."$add_from_dom";
    
    // Création du WHERE
    $normal_query .= " WHERE $create_logphy ";
    $normal_query .= " AND B_I_CACHER = 0 ";
    
    if (($TabFinder['typque'] == 0)&&($TabFinder['chaine'] != "")) {
        // le mode de moteur demandé est : expression exacte
        $normal_query .= " AND (";
        // on vérifie si la chaine est entre crochets
        $check_method = ereg("\[*\]",$TabFinder['chaine']);
        if ($check_method == 1) {
            // si oui, on utilise le FULL TEXT
            $TabFinder['chaine'] = ereg_replace("\[","",$TabFinder['chaine']);
            $TabFinder['chaine'] = ereg_replace("\]","",$TabFinder['chaine']);
            
            $normal_query .= "MATCH(B_I_TITRE) against ('".$TabFinder['chaine']."')";
            $normal_query .= " OR MATCH(B_I_RESUMCLE) against ('".$TabFinder['chaine']."')";
            $normal_query .= " OR ".$tbl['item'].".B_I_COMMENT like '%".$TabFinder['chaine']."%'";
        } else {
            // si non, on utilise la methode classique
            $normal_query .= $tbl['item'].".B_I_TITRE like '%".$TabFinder['chaine']."%'";
            $normal_query .= " OR ".$tbl['item'].".B_I_RESUMCLE like '%".$TabFinder['chaine']."%'";
            $normal_query .= " OR ".$tbl['item'].".B_I_COMMENT like '%".$TabFinder['chaine']."%'";
        }
        $normal_query .= ")";
    } else if (($TabFinder['chaine'] != "")&&($TabFinder['typque'] == 1)) {
        // le mode du moteur demandé est : chaque terme
        
        $result_from_ex = explode (" ", $TabFinder['chaine']);
        
        if (($result_from_ex != "")&&(count($result_from_ex)>=1)) {
            $i_ex = 0;
            
            $normal_query .= " AND (";
            while ($i_ex < count($result_from_ex)) {
                // on vérifie si la chaine est entre crochets
                $check_method = ereg("\[*\]",$result_from_ex[$i_ex]);
                
                if ($check_method == 1) {
                    $result_from_ex[$i_ex] = ereg_replace("\[","",$result_from_ex[$i_ex]);
                    $result_from_ex[$i_ex] = ereg_replace("\]","",$result_from_ex[$i_ex]);
                    // si oui, on utilise le FULL TEXT
                    $normal_query .= "MATCH(B_I_TITRE) against ('";
                    if ($result_from_ex[$i_ex] != "") $normal_query .= "$result_from_ex[$i_ex]";
                    $normal_query .= "') OR ";
                    
                    $normal_query .= "MATCH(B_I_RESUMCLE) against ('";
                    if ($result_from_ex[$i_ex] != "") $normal_query .= "$result_from_ex[$i_ex]";
                    $normal_query .= "') OR ";
                    
                    $normal_query .= "B_I_COMMENT like '%";
                    if ($result_from_ex[$i_ex] != "") $normal_query .= "$result_from_ex[$i_ex]";
                    $normal_query .= "%'";
                    
                    if ($i_ex < (count($result_from_ex)-1)) $normal_query .= ") AND (";
                } else {
                    // si non, on procède à une recherche classique
                    $normal_query .= $tbl['item'].".B_I_TITRE like '%";
                    if ($result_from_ex[$i_ex] != "") $normal_query .= "$result_from_ex[$i_ex]";
                    $normal_query .= "%' OR ";
                    $normal_query .= $tbl['item'].".B_I_RESUMCLE like '%";
                    if ($result_from_ex[$i_ex] != "") $normal_query .= "$result_from_ex[$i_ex]";
                    $normal_query .= "%' OR ";
                    
                    $normal_query .= $tbl['item'].".B_I_COMMENT like '%";
                    if ($result_from_ex[$i_ex] != "") $normal_query .= "$result_from_ex[$i_ex]";
                    $normal_query .= "%'";          
                    
                    if ($i_ex < (count($result_from_ex)-1)) $normal_query .= ") AND (";
                }
                
                $i_ex++;
            }
            
            $normal_query .= ") ";
        }
    }
    
    /* ajout, si nécessaire, de la contrainte categ */
    
    if (($TabFinder['categ'] != '') && (BB_ARGUMENT_REGROUPEMENT == 'media')) {
        if ($TabFinder['categ'] == 'VIDE') {
            $normal_query .= ' AND (B_M_EDITEUR is null OR B_M_EDITEUR = "")';
        } else {
            $normal_query .= ' AND (B_M_EDITEUR like "%'.$TabFinder['categ'].'%")';
        }
    }
    
    // Création des jointures
    $normal_query .= " AND (".$tbl['saisie'].".B_AS_ID = ".$tbl['item'].".B_I_AUTEURSAISIE)"; // lien article -- type phy
    $normal_query .= " AND (".$tbl['item'].".B_I_TYPPHY = ".$tbl['item_typphy'].".B_IP_ID)"; // lien article -- type phy
    $normal_query .= " AND (".$tbl['item'].".B_I_TYPLOG = ".$tbl['item_typlog'].".B_IL_ID)"; // lien article -- type log
    $normal_query .= " $add_limit_dom"; // si limitation au domaine principal
    
    $normal_query.= $special_join; 
    
    // Si TabFinder['auteur'] est non vide : on ajoute le critère de sélection 
    
    if ($TabFinder['auteur'] != '') {
        $tmp_auteur = explode(' ', $TabFinder['auteur']);
        
        $normal_query .= ' AND (';
        
        for ($tmp=0;$tmp<count($tmp_auteur);$tmp++) {
            $normal_query .= $tbl['item'].'.B_I_AUTEURS like "%'.$tmp_auteur[$tmp].'%"';
            if ( $tmp < (count($tmp_auteur) - 1) ) {
                $normal_query .= ' AND ';
            }
        }
        $normal_query .= ')';
    }
    
    // Si TabFinder['geo'] est non vide :
    // 1- si TabFinder['chaine'] est vide on n'ajoute pas de AND
    // 2- s'il n'est pas vide on ajoute AND
    
    if ($TabFinder['geo'] != '') {
        if ($TabFinder['geo'] == '1' || $TabFinder['geo'] == '2' || $TabFinder['geo'] == '3' || $TabFinder['geo'] == '4' 
            || $TabFinder['geo'] == '5' || $TabFinder['geo'] == '6' || $TabFinder['geo'] == '7' || $TabFinder['geo'] == '8' || $TabFinder['geo'] == '9' ) {
            $TabFinder['geo'] = '0'.$TabFinder['geo'];
            $TabFinder['geo'] = ereg_replace('00', '0', $TabFinder['geo']);
        }
        
        $normal_query .= " AND (".$tbl['item'].".B_I_GEO like '%".$TabFinder['geo']."%' ";
        
        /* ajout de la jointure dpt */
        
        $comp_geo = "select * from ".$tbl['dpt']." where GFD_NAME like '%".$TabFinder['geo']."%'";
        $comp_do = mysql_query($comp_geo) or die("<B>Erreur de requete GEO :</B> $comp_geo.");
        $comp_num = mysql_num_rows($comp_do);
        
        if ($comp_num > 0) {
            $i = 1;
            
            while ($row = mysql_fetch_object($comp_do)) {
                $tmp_stock = $row->GFD_ID;
                
                if ($tmp_stock != '') {
                    if ($tmp_stock == '1' || $tmp_stock == '2' || $tmp_stock == '3' || $tmp_stock == '4' ||
                        $tmp_stock == '5' || $tmp_stock == '6' || $tmp_stock == '7' || $tmp_stock == '8' || $tmp_stock == '9' ) {
                        $tmp_stock = '0'.$tmp_stock;
                        $tmp_stock = ereg_replace('00', '0', $tmp_stock);
                    }
                    
                    if ($i == 1) {
                        $normal_query .= ' OR (';
                    }
                    $normal_query .= $tbl['item'].'.B_I_GEO like "%'.$tmp_stock.'%"';
                    if ($i < $comp_num) {
                        $normal_query .= ' OR ';
                    }
                    if ($i == $comp_num) {
                        $normal_query .= ') ';
                    }
                    $i++;
                }
            }
        }
        mysql_free_result($comp_do);
        $normal_query .= ') ';
    }
    
    // traitement du réducteur SINCE (fonction de la date)
    
    if ($TabFinder['since'] != '') {
        $since_var = explode('/', $TabFinder['since']);
        
        if (empty($since_var[0])) {
            $since_var[0] = '';
        }
        if (empty($since_var[1])) {
            $since_var[1] = '';
        }
        if (empty($since_var[2])) {
            $since_var[2] = '';
        }
        
        $timestamp = mktime (NULL,NULL,NULL,$since_var[1],$since_var[0],$since_var[2]);
        
        $normal_query .= ' AND B_I_MAJFICHE > '.$timestamp;
    }
    
    // Création de l'ordre de tri
    $order_query = " ORDER BY ".$TabFinder['sort'];
    
    // Affectation de la requête à la variable qui va l'envoyer au moteur
    if (empty($plugin_store['query_add_query'])) {
        $plugin_store['query_add_query'] = '';
    }
    
    $unreq = $normal_query.' '.$add_to_query.' '.$plugin_store['query_add_query'].' '.$order_query;
    
    //echo "$unreq";
    
    return $unreq;
}

/* +--Fin du code ----------------------------------------------------------------------------------------+
*
* $Log: not supported by cvs2svn $
* Revision 1.4  2005/05/17 10:10:08  jpm
* Correction des bogues avant mise en ligne du site v4.
*
* Revision 1.3  2004/09/15 10:32:30  jpm
* Suppression d'une ligne après la balise de fermeture php.
*
* Revision 1.2  2004/09/14 10:18:17  jpm
* Mise en forme et amélioration du code.
* Passage au XHTML strict.
*
* Revision 1.1  2004/09/10 18:44:02  jpm
* Ajout des fichiers de gestion du moteur de recherche des médias.
*
*
* +-- Fin du code ----------------------------------------------------------------------------------------+
*/
?>