Subversion Repositories eFlore/Applications.bibliobota

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 jp_milcent 1
<?php
2
/*vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +------------------------------------------------------------------------------------------------------+
4
// | PHP version 4.1                                                                                      |
5
// +------------------------------------------------------------------------------------------------------+
6
// | Copyright (C) 2004 Tela Botanica (accueil@tela-botanica.org)                                         |
7
// +------------------------------------------------------------------------------------------------------+
8
// | This library is free software; you can redistribute it and/or                                        |
9
// | modify it under the terms of the GNU Lesser General Public                                           |
10
// | License as published by the Free Software Foundation; either                                         |
11
// | version 2.1 of the License, or (at your option) any later version.                                   |
12
// |                                                                                                      |
13
// | This library is distributed in the hope that it will be useful,                                      |
14
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
15
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                                    |
16
// | Lesser General Public License for more details.                                                      |
17
// |                                                                                                      |
18
// | You should have received a copy of the GNU Lesser General Public                                     |
19
// | License along with this library; if not, write to the Free Software                                  |
20
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
21
// +------------------------------------------------------------------------------------------------------+
22
// CVS : $Id: bbc_requete_structure.fonct.php,v 1.1 2005-11-23 10:22:25 jp_milcent Exp $
23
/**
24
* Fonctions de création de la requête sur une structure.
25
*
26
* Contient une fonction créant la requête d'intérogation de la base de données.
27
* Valable pour le paramètre "structure".
28
* Ajouts :
29
* - 27/06/02 : recherche aussi dans la table des collections + jointure coll/str
30
*
31
*@package BiblioBota-Consultation
32
*@subpackage Fonctions
33
//Auteur original :
34
*@author        Jean-Charles GRANGER <tela@vecteur.org>
35
//Autres auteurs :
36
*@author        Jean-Pascal MILCENT <jpm@clapas.org>
37
*@copyright     Tela-Botanica 2000-2004
38
*@version       $Revision: 1.1 $ $Date: 2005-11-23 10:22:25 $
39
// +------------------------------------------------------------------------------------------------------+
40
*/
41
 
42
// +------------------------------------------------------------------------------------------------------+
43
// |                                            ENTETE du PROGRAMME                                       |
44
// +------------------------------------------------------------------------------------------------------+
45
 
46
 
47
// +------------------------------------------------------------------------------------------------------+
48
// |                                           LISTE de FONCTIONS                                         |
49
// +------------------------------------------------------------------------------------------------------+
50
 
51
/** Fonction mkquery() - Créé une requête intérogeant la base de donnée.
52
*
53
* Fonction créant la requête d'intérogation de la base de données pour le
54
* paramêtre "structure".
55
*
56
* @return  string la requête permettant de récupérer les structures.
57
*/
58
function mkquery()
59
{
60
    // Définition des variables globales
61
    global $tbl;
62
    global $TabFinder;
63
 
64
    // Initialisation des variables
65
    $normal_query = '';
66
    $add_to_query = '';
67
    $order_query = '';
68
 
69
    // Si la variable de catégorie vaut 'tous', il n'y a pas affinage de la requête par catégorie
70
    if ($TabFinder['categ'] != 'tous') {
71
        $add_to_query = ' AND '.$tbl['str'].'.B_S_TYPESTR = '.$TabFinder['categ'].' ';
72
    }
73
 
74
    // Ccréation du corps de la requête
75
 
76
    // Création du SELECT
77
    $normal_query = 'SELECT DISTINCT '.$tbl['str'].'.*, '.$tbl['pays'].'.GC_NAME, '.$tbl['typestr'].'.B_TYPSTR_LABEL ';
78
 
79
    // Création du FROM
80
    $normal_query .= ' FROM '.$tbl['str'].' LEFT JOIN '.$tbl['collection'].' ON '.$tbl['collection'].'.B_C_LKSTR = '.$tbl['str'].'.B_S_IDSTR, '.$tbl['pays'].', '.$tbl['typestr'].' ';
81
 
82
    // Création du WHERE
83
    $normal_query .=    'WHERE B_S_IDSTR > 0 '.
84
                        'AND B_S_CACHER = 0 '.
85
                        'AND ';
86
 
87
    if (($TabFinder['typque'] != 1) && ($TabFinder['chaine'] != '')) {
88
        $normal_query .= ' (';
89
        $normal_query .= $tbl['str'].'.B_S_NOM LIKE "%'.$TabFinder['chaine'].'%"';
90
        $normal_query .= ' OR '.$tbl['str'].'.B_S_SIGLE LIKE "%'.$TabFinder['chaine'].'%"';
91
        $normal_query .= ' OR '.$tbl['str'].'.B_S_VOCACTIV LIKE "%'.$TabFinder['chaine'].'%"';
92
        $normal_query .= ' OR '.$tbl['str'].'.B_S_COMMENT LIKE "%'.$TabFinder['chaine'].'%"';
93
        $normal_query .= ' OR '.$tbl['str'].'.B_S_SERVICE LIKE "%'.$TabFinder['chaine'].'%"';
94
        $normal_query .= ' OR '.$tbl['collection'].'.B_C_NOMCOMPLET LIKE "%'.$TabFinder['chaine'].'%"';
95
        $normal_query .= ' OR '.$tbl['collection'].'.B_C_ABREGE LIKE "%'.$TabFinder['chaine'].'%"';
96
        $normal_query .= ' OR '.$tbl['collection'].'.B_C_COMMENT LIKE "%'.$TabFinder['chaine'].'%"';
97
        $normal_query .= ')';
98
    } else if ($TabFinder['chaine'] != '') {
99
        $normal_query .= ' (';
100
        $result_from_ex = explode(' ', $TabFinder['chaine']);
101
 
102
        $i_ex = 0;
103
        while ($i_ex < count($result_from_ex)) {
104
            $normal_query .= $tbl['str'].'.B_S_NOM LIKE "%';
105
            if ($result_from_ex[$i_ex] != '') {
106
                $normal_query .= $result_from_ex[$i_ex];
107
            }
108
            $normal_query .= '%" OR ';
109
 
110
            $normal_query .= $tbl['str'].'.B_S_VOCACTIV LIKE "%';
111
            if ($result_from_ex[$i_ex] != '') {
112
                $normal_query .= $result_from_ex[$i_ex];
113
            }
114
            $normal_query .= '%" OR ';
115
 
116
            $normal_query .= $tbl['str'].'.B_S_SIGLE LIKE "%';
117
            if ($result_from_ex[$i_ex] != '') {
118
                $normal_query .= $result_from_ex[$i_ex];
119
            }
120
            $normal_query .= '%" OR ';
121
 
122
            $normal_query .= $tbl['str'].'.B_S_COMMENT LIKE "%';
123
            if ($result_from_ex[$i_ex] != '') {
124
                $normal_query .= $result_from_ex[$i_ex];
125
            }
126
            $normal_query .= '%" OR ';
127
 
128
            $normal_query .= $tbl['str'].'.B_S_SERVICE LIKE "%';
129
            if ($result_from_ex[$i_ex] != '') {
130
                $normal_query .= $result_from_ex[$i_ex];
131
            }
132
            $normal_query .= '%" OR ';
133
 
134
            $normal_query .= $tbl['collection'].'.B_C_NOMCOMPLET LIKE "%';
135
            if ($result_from_ex[$i_ex] != '') {
136
                $normal_query .= $result_from_ex[$i_ex];
137
            }
138
            $normal_query .= '%" OR ';
139
 
140
            $normal_query .= $tbl['collection'].'.B_C_ABREGE LIKE "%';
141
            if ($result_from_ex[$i_ex] != '') {
142
                $normal_query .= $result_from_ex[$i_ex];
143
            }
144
            $normal_query .= '%" OR ';
145
 
146
            $normal_query .= $tbl['collection'].'.B_C_COMMENT LIKE "%';
147
            if ($result_from_ex[$i_ex] != '') {
148
                $normal_query .= $result_from_ex[$i_ex];
149
            }
150
            $normal_query .= '%"';
151
 
152
            if ($i_ex < (count($result_from_ex) - 1)) {
153
                $normal_query .= ') AND (';
154
            }
155
 
156
            $i_ex++;
157
        }
158
 
159
        $normal_query .= ')';
160
    }
161
 
162
    // Création de la jointure avec les pays et les types
163
    if ($TabFinder['chaine'] != '') {
164
        $normal_query .= ' AND ';
165
    }
166
    $normal_query .= ' B_S_PAYS = GC_ID ';
167
    $normal_query .= ' AND GC_LOCALE = "fr" ';
168
    $normal_query .= ' AND B_S_TYPESTR = B_TYPSTR_ID ';
169
 
170
    // Si TabFinder['geo'] est non vide :
171
    // 1- si TabFinder['chaine'] est vide on n'ajoute pas de AND
172
    // 2- s'il n'est pas vide on ajoute AND
173
    if ($TabFinder['geo'] != '') {
174
        preg_replace('/^(\d)$/', "0$1", $TabFinder['geo']);
175
        preg_replace('/^00$/', '0', $TabFinder['geo']);
176
 
177
        $normal_query .=    ' AND ('.$tbl['str'].'.B_S_CODEPOSTAL LIKE "'.$TabFinder['geo'].'%" '.
178
                            'OR '.$tbl['str'].'.B_S_VILLE LIKE "%'.$TabFinder['geo'].'%" '.
179
                            'OR '.$tbl['pays'].'.GC_NAME LIKE "%'.$TabFinder['geo'].'%" ';
180
 
181
        // Ajout de la jointure dpt
182
        $comp_geo = 'SELECT * '.
183
                    'FROM '.$tbl['dpt'].' '.
184
                    'WHERE GFD_NAME LIKE "%'.$TabFinder['geo'].'%"';
185
        $comp_do = mysql_query($comp_geo) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $comp_geo));
186
        $comp_num = mysql_num_rows($comp_do);
187
 
188
        if ($comp_num > 0) {
189
            $i = 1;
190
            while ($row = mysql_fetch_object($comp_do)) {
191
                $tmp_stock = $row->GFD_ID;
192
 
193
                if ($tmp_stock != '') {
194
                    preg_replace('^(\d)$', "0$1", $tmp_stock);
195
                    preg_replace('^00$', '0', $tmp_stock);
196
 
197
                    if ($i == 1) {
198
                        $normal_query .= ' OR ';
199
                    }
200
                    $normal_query .= $tbl['str'].'.B_S_CODEPOSTAL LIKE "'.$tmp_stock.'%"';
201
                    if ($i < $comp_num) {
202
                        $normal_query .= ' OR ';
203
                    }
204
                    if ($i == $comp_num) {
205
                        $normal_query .= ' ';
206
                    }
207
                    $i++;
208
                }
209
            }
210
        }
211
        mysql_free_result($comp_do);
212
        $normal_query .= ') ';
213
    }
214
 
215
    // Traitement du réducteur SINCE (fonction de la date)
216
    if ($TabFinder['since'] != '') {
217
        $since_var = explode('/', $TabFinder['since']);
218
 
219
        if (empty($since_var[0])) {
220
            $since_var[0] = '';
221
        }
222
        if (empty($since_var[1])) {
223
            $since_var[1] = '';
224
        }
225
        if (empty($since_var[2])) {
226
            $since_var[2] = '';
227
        }
228
 
229
        $timestamp = mktime(NULL, NULL, NULL, $since_var[1], $since_var[0], $since_var[2]);
230
 
231
        $normal_query .= ' AND B_S_MAJFICHE > '.$timestamp;
232
    }
233
 
234
    // Création de l'ordre de tri
235
    $order_query = 'ORDER BY '.$TabFinder['sort'];
236
 
237
    // Affectation de la requête à la variable qui va l'envoyer au moteur
238
    $queries = $normal_query.' '.$add_to_query.' '.$order_query;
239
 
240
    return $queries;
241
}
242
 
243
/* +--Fin du code ----------------------------------------------------------------------------------------+
244
*
245
* $Log: not supported by cvs2svn $
246
* Revision 1.4  2005/05/17 10:10:08  jpm
247
* Correction des bogues avant mise en ligne du site v4.
248
*
249
* Revision 1.3  2004/09/16 11:05:54  jpm
250
* Correction bogue mauvaise requete.
251
*
252
* Revision 1.2  2004/09/15 11:21:07  jpm
253
* Début mise en conformité avec la convention de codage et le standard XHTML Strict.
254
*
255
*
256
* +-- Fin du code ----------------------------------------------------------------------------------------+
257
*/
258
?>