Subversion Repositories eFlore/Applications.bibliobota

Rev

Rev 2 | 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_structure.fonct.php,v 1.3 2004/09/16 11:05:54 jpm Exp $
/**
* Fonctions de création de la requête sur une structure.
*
* Contient une fonction créant la requête d'intérogation de la base de données.
* Valable pour le paramètre "structure".
* Ajouts :
* - 27/06/02 : recherche aussi dans la table des collections + jointure coll/str
*
*@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.3 $ $Date: 2004/09/16 11:05:54 $
// +------------------------------------------------------------------------------------------------------+
*/

// +------------------------------------------------------------------------------------------------------+
// |                                            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 "structure".
*
* @return  string la requête permettant de récupérer les structures.
*/
function mkquery()
{
    // Définition des variables globales
    global $tbl;
    global $TabFinder;
    
    // Initialisation des variables
    $normal_query = '';
    $add_to_query = '';
    $order_query = '';
    
    // Si la variable de catégorie vaut 'tous', il n'y a pas affinage de la requête par catégorie
    if ($TabFinder['categ'] != 'tous') {
        $add_to_query = ' AND '.$tbl['str'].'.B_S_TYPESTR = '.$TabFinder['categ'].' ';
    }
    
    // Ccréation du corps de la requête
    
    // Création du SELECT
    $normal_query = 'SELECT DISTINCT '.$tbl['str'].'.*, '.$tbl['pays'].'.GC_NAME, '.$tbl['typestr'].'.B_TYPSTR_LABEL ';
    
    // Création du FROM
    $normal_query .= ' FROM '.$tbl['str'].' LEFT JOIN '.$tbl['collection'].' ON '.$tbl['collection'].'.B_C_LKSTR = '.$tbl['str'].'.B_S_IDSTR, '.$tbl['pays'].', '.$tbl['typestr'].' ';
    
    // Création du WHERE
    $normal_query .=    'WHERE B_S_IDSTR > 0 '.
                        'AND B_S_CACHER = 0 '.
                        'AND ';
    
    if (($TabFinder['typque'] != 1) && ($TabFinder['chaine'] != '')) {
        $normal_query .= ' (';
        $normal_query .= $tbl['str'].'.B_S_NOM LIKE "%'.$TabFinder['chaine'].'%"';
        $normal_query .= ' OR '.$tbl['str'].'.B_S_SIGLE LIKE "%'.$TabFinder['chaine'].'%"';
        $normal_query .= ' OR '.$tbl['str'].'.B_S_VOCACTIV LIKE "%'.$TabFinder['chaine'].'%"';
        $normal_query .= ' OR '.$tbl['str'].'.B_S_COMMENT LIKE "%'.$TabFinder['chaine'].'%"';
        $normal_query .= ' OR '.$tbl['str'].'.B_S_SERVICE LIKE "%'.$TabFinder['chaine'].'%"';
        $normal_query .= ' OR '.$tbl['collection'].'.B_C_NOMCOMPLET LIKE "%'.$TabFinder['chaine'].'%"';
        $normal_query .= ' OR '.$tbl['collection'].'.B_C_ABREGE LIKE "%'.$TabFinder['chaine'].'%"';
        $normal_query .= ' OR '.$tbl['collection'].'.B_C_COMMENT LIKE "%'.$TabFinder['chaine'].'%"';
        $normal_query .= ')';
    } else if ($TabFinder['chaine'] != '') {
        $normal_query .= ' (';
        $result_from_ex = explode(' ', $TabFinder['chaine']);
        
        $i_ex = 0;
        while ($i_ex < count($result_from_ex)) {
            $normal_query .= $tbl['str'].'.B_S_NOM LIKE "%';
            if ($result_from_ex[$i_ex] != '') {
                $normal_query .= $result_from_ex[$i_ex];
            }
            $normal_query .= '%" OR ';
            
            $normal_query .= $tbl['str'].'.B_S_VOCACTIV LIKE "%';
            if ($result_from_ex[$i_ex] != '') {
                $normal_query .= $result_from_ex[$i_ex];
            }
            $normal_query .= '%" OR ';
            
            $normal_query .= $tbl['str'].'.B_S_SIGLE LIKE "%';
            if ($result_from_ex[$i_ex] != '') {
                $normal_query .= $result_from_ex[$i_ex];
            }
            $normal_query .= '%" OR ';
            
            $normal_query .= $tbl['str'].'.B_S_COMMENT LIKE "%';
            if ($result_from_ex[$i_ex] != '') {
                $normal_query .= $result_from_ex[$i_ex];
            }
            $normal_query .= '%" OR ';
            
            $normal_query .= $tbl['str'].'.B_S_SERVICE LIKE "%';
            if ($result_from_ex[$i_ex] != '') {
                $normal_query .= $result_from_ex[$i_ex];
            }
            $normal_query .= '%" OR ';
            
            $normal_query .= $tbl['collection'].'.B_C_NOMCOMPLET LIKE "%';
            if ($result_from_ex[$i_ex] != '') {
                $normal_query .= $result_from_ex[$i_ex];
            }
            $normal_query .= '%" OR ';
            
            $normal_query .= $tbl['collection'].'.B_C_ABREGE LIKE "%';
            if ($result_from_ex[$i_ex] != '') {
                $normal_query .= $result_from_ex[$i_ex];
            }
            $normal_query .= '%" OR ';
            
            $normal_query .= $tbl['collection'].'.B_C_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 .= ')';
    }
    
    // Création de la jointure avec les pays et les types
    if ($TabFinder['chaine'] != '') {
        $normal_query .= ' AND ';
    }
    $normal_query .= ' B_S_PAYS = GC_ID ';
    $normal_query .= ' AND GC_LOCALE = "fr" ';
    $normal_query .= ' AND B_S_TYPESTR = B_TYPSTR_ID ';
    
    // 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'] != '') {
        preg_replace('/^(\d)$/', "0$1", $TabFinder['geo']);
        preg_replace('/^00$/', '0', $TabFinder['geo']);
        
        $normal_query .=    ' AND ('.$tbl['str'].'.B_S_CODEPOSTAL LIKE "'.$TabFinder['geo'].'%" '.
                            'OR '.$tbl['str'].'.B_S_VILLE LIKE "%'.$TabFinder['geo'].'%" '.
                            'OR '.$tbl['pays'].'.GC_NAME 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(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $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 != '') {
                    preg_replace('^(\d)$', "0$1", $tmp_stock);
                    preg_replace('^00$', '0', $tmp_stock);
                    
                    if ($i == 1) {
                        $normal_query .= ' OR ';
                    }
                    $normal_query .= $tbl['str'].'.B_S_CODEPOSTAL 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_S_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
    $queries = $normal_query.' '.$add_to_query.' '.$order_query;
    
    return $queries;
}

/* +--Fin du code ----------------------------------------------------------------------------------------+
*
* $Log: bbc_requete_structure.fonct.php,v $
* Revision 1.3  2004/09/16 11:05:54  jpm
* Correction bogue mauvaise requete.
*
* Revision 1.2  2004/09/15 11:21:07  jpm
* Début mise en conformité avec la convention de codage et le standard XHTML Strict.
*
*
* +-- Fin du code ----------------------------------------------------------------------------------------+
*/
?>