source: trunk/search.php @ 602

Last change on this file since 602 was 602, checked in by plg, 19 years ago
  • admin/cat_options page added : manage options for the whole categories tree (uploadable, commentable). status and visible will be soon added
  • admin.php : $conf_link var to avoid lines longer than 79 characters
  • config.upload_available configuration parameter disappear : it's simpler to manage with cat_options
  • config.show_comments idem : new column categories.commentable, each categories can be commentable or not
  • categories.site_id becomes a nullable column : a virtual category does belong to no site
  • function display_select_categories has a new argument : $CSS_classes array to optionnaly assign a CSS class to each category in the select field
  • added informations in include/config.inc.php for setting default value of :
  • categories.visible
  • categories.status
  • categories.uploadable
  • categories.commentable
  • 2 new indexes images_i3(average_rate) and images_i4(hit) : optimizes best rated and most visited categories
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2004 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-11-13 13:43:53 +0000 (Sat, 13 Nov 2004) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 602 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28//------------------------------------------------------------------- functions
29// date_display displays 3 select input fields. The first one is the
30// day of the month, from 0 to 31. The second is the month of the year,
31// from 01 to 12. The last one is the year. The years displayed are the
32// ones given by get_available_years (see function description in
33// ./include/functions.inc.php).
34function display_date($fieldname, $datefield)
35{
36  global $template;
37
38  // years
39  for ($i = 1990; $i < 2006; $i++)
40  {
41    $selected = '';
42    $key = $datefield.':year';
43    if (isset($_POST[$key]) and $i == $_POST[$key])
44    {
45      $selected = ' selected="selected"';
46    }
47   
48    $template->assign_block_vars(
49      $fieldname.'year_option',
50      array('OPTION'=>$i,
51            'SELECTED'=>$selected
52        ));
53  }
54  // months of year
55  for ($i = 1; $i <= 12; $i++)
56  {
57    $selected = '';
58    $key = $datefield.':month';
59    if (isset($_POST[$key]) and $i == $_POST[$key])
60    {
61      $selected = ' selected="selected"';
62    }
63
64    $template->assign_block_vars(
65      $fieldname.'month_option',
66      array('OPTION'=>sprintf('%02s', $i),
67            'SELECTED'=>$selected
68        ));
69  }
70  // days of the month
71  for ($i = 1; $i <= 31; $i++)
72  {
73    $selected = '';
74    $key = $datefield.':day';
75    if (isset($_POST[$key]) and $i == $_POST[$key])
76    {
77      $selected = ' selected="selected"';
78    }
79
80    $template->assign_block_vars(
81      $fieldname.'day_option',
82      array('OPTION'=>sprintf('%02s', $i),
83            'SELECTED'=>$selected
84        ));
85  }
86}
87
88function display_3dates($fieldname)
89{
90  display_date('datefield.', $fieldname);
91  display_date('datefield.after_', $fieldname.'-after');
92  display_date('datefield.before_', $fieldname.'-before');
93}
94//--------------------------------------------------------------------- include
95define('PHPWG_ROOT_PATH','./');
96include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );
97//-------------------------------------------------- access authorization check
98check_login_authorization();
99//----------------------------------------------------------------- form fields
100$textfields = array('file', 'name', 'comment', 'keywords', 'author');
101$datefields = array('date_available', 'date_creation');
102//------------------------------------------------------------------ form check
103$errors = array();
104$search = array();
105$search['fields'] = array();
106if (isset($_POST['submit']))
107{
108  $search['mode'] = $_POST['mode'];
109
110  foreach ($textfields as $textfield)
111  {
112    if (isset($_POST[$textfield.'-content'])
113        and !preg_match('/^\s*$/', $_POST[$textfield.'-content']))
114    {
115      $local_search = array();
116      $words = preg_split('/\s+/', $_POST[$textfield.'-content']);
117      foreach ($words as $i => $word)
118      {
119        if (strlen($word) > 2 and !preg_match('/[,;:\']/', $word))
120        {
121          array_push($local_search, $word);
122        }
123        else
124        {
125          array_push($errors, $lang['invalid_search']);
126        }
127      }
128      $local_search = array_unique($local_search);
129      $search['fields'][$textfield] = array();
130      $search['fields'][$textfield]['words'] = $local_search;
131      if (count($local_search) > 1)
132      {
133        $search['fields'][$textfield]['mode'] = $_POST[$textfield.'-mode'];
134      }
135    }
136  }
137  foreach ($datefields as $datefield)
138  {
139    $suffixes = array('','-after','-before');
140    foreach ($suffixes as $suffix)
141    {
142      $field = $datefield.$suffix;
143      if (isset($_POST[$field.'-check']))
144      {
145        $year = $_POST[$field.':year'];
146        $month = $_POST[$field.':month'];
147        $day = $_POST[$field.':day'];
148        $date = $year.'.'.$month.'.'.$day;
149        if (!checkdate($month, $day, $year))
150        {
151          array_push($errors, $date.$lang['search_wrong_date']);
152        }
153        $search['fields'][$field] = array();
154        $search['fields'][$field]['words'] = array($date);
155        if ($suffix == '-after' or $suffix == '-before')
156        {
157          if (isset($_POST[$field.'-included']))
158          {
159            $search['fields'][$field]['mode'] = 'inc';
160          }
161        }
162      }
163    }
164    if ($search['mode'] == 'AND')
165    {
166      // before date must be superior to after date
167      if (isset($search['fields'][$datefield.'-before'])
168          and isset($search['fields'][$datefield.'-after']))
169      {
170        $after = $search['fields'][$datefield.'-after']['words'][0];
171        $before = $search['fields'][$datefield.'-before']['words'][0];
172        if ($after >= $before)
173        {
174          array_push($errors, $lang['search_wrong_date_order']);
175        }
176      }
177      // having "search is" and ("search is after" or "search is before") is
178      // not coherent
179      if (isset($search['fields'][$datefield])
180          and (isset($search['fields'][$datefield.'-before'])
181               or isset($search['fields'][$datefield.'-after'])))
182      {
183        array_push($errors, $lang['search_incoherent_date_search']);
184      }
185    }
186  }
187  if (isset($_POST['categories-check']))
188  {
189    $field = 'cat';
190    $search['fields'][$field] = array();
191    $search['fields'][$field]['words'] = $_POST['cat'];
192    if (isset($_POST['subcats-included']))
193    {
194      $search['fields'][$field]['mode'] = 'sub_inc';
195    }
196  }
197  // search string (for URL) creation
198  $search_string = '';
199  $tokens = array();
200  foreach (array_keys($search['fields']) as $field)
201  {
202    $token = $field.':';
203    $token.= implode(',', $search['fields'][$field]['words']);
204    if (isset($search['fields'][$field]['mode']))
205    {
206      $token.= '~'.$search['fields'][$field]['mode'];
207    }
208    array_push($tokens, $token);
209  }
210  $search_string.= implode(';', $tokens);
211  if (count($tokens) > 1)
212  {
213    $search_string.= '|'.$search['mode'];
214  }
215 
216  if (count($tokens) == 0)
217  {
218    array_push($errors, $lang['search_one_clause_at_least']);
219  }
220}
221//----------------------------------------------------------------- redirection
222if (isset($_POST['submit']) and count($errors) == 0)
223{
224  $url = 'category.php?cat=search&search='.$search_string;
225  $url = add_session_id($url, true);
226  redirect($url);
227}
228//----------------------------------------------------- template initialization
229//
230// Start output of page
231//
232$title= $lang['search_title'];
233include(PHPWG_ROOT_PATH.'include/page_header.php');
234
235$template->set_filenames( array('search'=>'search.tpl') );
236$template->assign_vars(array(
237  'L_TITLE' => $lang['search_title'],
238  'L_SEARCH_COMMENTS' => $lang['search_comments'],
239  'L_RETURN' => $lang['gallery_index'],
240  'L_SUBMIT' => $lang['submit'],
241  'L_SEARCH_OR'=>$lang['search_mode_or'],
242  'L_SEARCH_AND'=>$lang['search_mode_and'],
243  'L_SEARCH_OR_CLAUSES'=>$lang['search_or_clauses'],
244  'L_SEARCH_AND_CLAUSES'=>$lang['search_and_clauses'],
245  'L_SEARCH_CATEGORIES'=>$lang['categories'],
246  'L_SEARCH_SUBCATS_INCLUDED'=>$lang['search_subcats_included'],
247  'L_SEARCH_DATE_INCLUDED'=> $lang['search_date_included'],
248  'L_SEARCH_DATE_IS'=>$lang['search_date_is'],
249  'L_SEARCH_DATE_IS_AFTER'=>$lang['search_date_is_after'],
250  'L_SEARCH_DATE_IS_BEFORE'=>$lang['search_date_is_before'],
251 
252  'F_ACTION' => add_session_id( 'search.php' ),
253   
254  'U_HOME' => add_session_id( 'category.php' )
255  )
256);
257
258//------------------------------------------------------------ text fields form
259foreach ($textfields as $textfield)
260{
261  if (isset($_POST[$textfield.'-mode']))
262  {
263    if ($_POST[$textfield.'-mode'] == 'AND')
264    {
265      $and_checked = 'checked="checked"';
266      $or_checked  = '';
267    }
268    else
269    {
270      $or_checked  = 'checked="checked"';
271      $and_checked = '';
272    }
273  }
274  else
275  {
276    $or_checked  = 'checked="checked"';
277    $and_checked = '';
278  }
279
280  $value = '';
281  if (isset($_POST[$textfield.'-content']))
282  {
283    $value = $_POST[$textfield.'-content'];
284  }
285 
286  $template->assign_block_vars(
287    'textfield',
288    array('NAME'=>$lang['search_'.$textfield],
289          'L_NAME'=>$textfield,
290          'VALUE'=>$value,
291          'OR_CHECKED'=>$or_checked,
292          'AND_CHECKED'=>$and_checked
293          ));
294}
295//------------------------------------------------------------- date field form
296foreach ($datefields as $datefield)
297{
298  $checked = '';
299  if (isset($_POST[$datefield.'-check']))
300  {
301    $checked = ' checked="checked"';
302  }
303
304  $after_checked = '';
305  if (isset($_POST[$datefield.'-after-check']))
306  {
307    $after_checked = ' checked="checked"';
308  }
309
310  $before_checked = '';
311  if (isset($_POST[$datefield.'-before-check']))
312  {
313    $before_checked = ' checked="checked"';
314  }
315
316  $after_included_check = '';
317  if (isset($_POST[$datefield.'-after-included']))
318  {
319    $after_included_check = ' checked="checked"';
320  }
321
322  $before_included_check = '';
323  if (isset($_POST[$datefield.'-before-included']))
324  {
325    $before_included_check = ' checked="checked"';
326  }
327 
328  $template->assign_block_vars(
329    'datefield',
330    array('NAME'=>$datefield,
331          'L_NAME'=>$lang['search_'.$datefield],
332          'CHECKED'=>$checked,
333          'AFTER_CHECKED'=>$after_checked,
334          'BEFORE_CHECKED'=>$before_checked,
335          'AFTER_INCLUDED_CHECKED'=>$after_included_check,
336          'BEFORE_INCLUDED_CHECKED'=>$before_included_check
337          ));
338  display_3dates($datefield);
339}
340//------------------------------------------------------------- categories form
341// this is a trick : normally, get_user_plain_structure is used to create
342// the categories structure for menu (in category.php) display, but here, we
343// want all categories to be shown...
344$user['expand'] = true;
345$structure = create_user_structure('');
346
347$selecteds = array();
348if (isset($_POST['submit']))
349{
350  $selecteds = $_POST['cat'];
351}
352display_select_categories($structure,
353                          '&nbsp;',
354                          $selecteds,
355                          'category_option',
356                          array());
357
358$categories_selected = '';
359if (isset($_POST['categories-check']))
360{
361  $categories_selected = 'checked="checked"';
362}
363
364$categories_subcats_selected = '';
365if (isset($_POST['subcats-included']))
366{
367  $categories_subcats_selected = 'checked="checked"';
368}
369
370$template->assign_vars(
371  array(
372    'CATEGORIES_SELECTED'=>$categories_selected,
373    'CATEGORIES_SUBCATS_SELECTED'=>$categories_subcats_selected
374    )
375  );
376//---------------------------------------------------------------------- OR/AND
377if (isset($_POST['mode']))
378{
379  if ($_POST['mode'] == 'AND')
380  {
381    $and_checked = 'checked="checked"';
382    $or_checked  = '';
383  }
384  else
385  {
386    $or_checked  = 'checked="checked"';
387    $and_checked = '';
388  }
389}
390else
391{
392  $or_checked  = 'checked="checked"';
393  $and_checked = '';
394}
395
396$template->assign_vars(
397  array(
398    'OR_CHECKED'=>$or_checked,
399    'AND_CHECKED'=>$and_checked
400    )
401  );
402//-------------------------------------------------------------- errors display
403if (sizeof($errors) != 0)
404{
405  $template->assign_block_vars('errors',array());
406  foreach ($errors as $error)
407  {
408    $template->assign_block_vars('errors.error',array('ERROR'=>$error));
409  }
410}
411//------------------------------------------------------------ log informations
412pwg_log( 'search', $title );
413mysql_close();
414$template->pparse('search');
415include(PHPWG_ROOT_PATH.'include/page_tail.php');
416?>
Note: See TracBrowser for help on using the repository browser.