source: tags/2.7.0RC1/search.php @ 29261

Last change on this file since 29261 was 28744, checked in by rvelices, 10 years ago

feature 3094: redesign of search form

  • fix comments page filter form (css changes affected also that .filter form - simplified also css rules)
  • better query for author list (using id for visible_images instead of image_id field)
  • don't show author list if there is only one author
  • Property svn:eol-style set to LF
File size: 7.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24//--------------------------------------------------------------------- include
25define('PHPWG_ROOT_PATH','./');
26include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );
27
28// +-----------------------------------------------------------------------+
29// | Check Access and exit when user status is not ok                      |
30// +-----------------------------------------------------------------------+
31check_status(ACCESS_GUEST);
32
33trigger_notify('loc_begin_search');
34
35//------------------------------------------------------------------ form check
36$search = array();
37if (isset($_POST['submit']))
38{
39  foreach ($_POST as $post_key => $post_value)
40  {
41    if (!is_array($post_value))
42    {
43      $_POST[$post_key] = pwg_db_real_escape_string($post_value);
44    }
45  } 
46 
47  if (isset($_POST['search_allwords'])
48      and !preg_match('/^\s*$/', $_POST['search_allwords']))
49  {
50    check_input_parameter('mode', $_POST, false, '/^(OR|AND)$/');
51
52    $fields = array_intersect($_POST['fields'], array('name', 'comment', 'file'));
53   
54    $drop_char_match = array(
55      '-','^','$',';','#','&','(',')','<','>','`','\'','"','|',',','@','_',
56      '?','%','~','.','[',']','{','}',':','\\','/','=','\'','!','*');
57    $drop_char_replace = array(
58      ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','','',' ',' ',' ',' ','',' ',
59      ' ',' ',' ',' ',' ',' ',' ',' ','' ,' ',' ',' ',' ',' ');
60
61    // Split words
62    $search['fields']['allwords'] = array(
63      'words' => array_unique(
64        preg_split(
65          '/\s+/',
66          str_replace(
67            $drop_char_match,
68            $drop_char_replace,
69            $_POST['search_allwords']
70            )
71          )
72        ),
73      'mode' => $_POST['mode'],
74      'fields' => $fields,
75      );
76  }
77
78  if (isset($_POST['tags']))
79  {
80    check_input_parameter('tags', $_POST, true, PATTERN_ID);
81    check_input_parameter('tag_mode', $_POST, false, '/^(OR|AND)$/');
82   
83    $search['fields']['tags'] = array(
84      'words' => $_POST['tags'],
85      'mode'  => $_POST['tag_mode'],
86      );
87  }
88
89  if (isset($_POST['authors']) and is_array($_POST['authors']) and count($_POST['authors']) > 0)
90  {
91    $authors = array();
92
93    foreach ($_POST['authors'] as $author)
94    {
95      $authors[] = strip_tags($author);
96    }
97   
98    $search['fields']['author'] = array(
99      'words' => $authors,
100      'mode' => 'OR',
101      );
102  }
103
104  if (isset($_POST['cat']))
105  {
106    check_input_parameter('cat', $_POST, true, PATTERN_ID);
107   
108    $search['fields']['cat'] = array(
109      'words'   => $_POST['cat'],
110      'sub_inc' => ($_POST['subcats-included'] == 1) ? true : false,
111      );
112  }
113
114  // dates
115  $type_date = $_POST['date_type'];
116
117  if (!empty($_POST['start_year']))
118  {
119    $search['fields'][$type_date.'-after'] = array(
120      'date' => sprintf(
121        '%d-%02d-%02d',
122        $_POST['start_year'],
123        $_POST['start_month'] != 0 ? $_POST['start_month'] : '01',
124        $_POST['start_day']   != 0 ? $_POST['start_day']   : '01'
125        ),
126      'inc' => true,
127      );
128  }
129
130  if (!empty($_POST['end_year']))
131  {
132    $search['fields'][$type_date.'-before'] = array(
133      'date' => sprintf(
134        '%d-%02d-%02d',
135        $_POST['end_year'],
136        $_POST['end_month'] != 0 ? $_POST['end_month'] : '12',
137        $_POST['end_day']   != 0 ? $_POST['end_day']   : '31'
138      ),
139      'inc' => true,
140      );
141  }
142
143  if (!empty($search))
144  {
145    // default search mode : each clause must be respected
146    $search['mode'] = 'AND';
147
148    // register search rules in database, then they will be available on
149    // thumbnails page and picture page.
150    $query ='
151INSERT INTO '.SEARCH_TABLE.'
152  (rules, last_seen)
153  VALUES
154  (\''.serialize($search).'\', NOW())
155;';
156    pwg_query($query);
157
158    $search_id = pwg_db_insert_id(SEARCH_TABLE);
159  }
160  else
161  {
162    $page['errors'][] = l10n('Empty query. No criteria has been entered.');
163  }
164}
165//----------------------------------------------------------------- redirection
166if (isset($_POST['submit']) and count($page['errors']) == 0)
167{
168  redirect(
169    make_index_url(
170      array(
171        'section' => 'search',
172        'search'  => $search_id,
173        )
174      )
175    );
176}
177//----------------------------------------------------- template initialization
178
179//
180// Start output of page
181//
182$title= l10n('Search');
183$page['body_id'] = 'theSearchPage';
184
185$template->set_filename('search' ,'search.tpl' );
186
187$month_list = $lang['month'];
188$month_list[0]='------------';
189ksort($month_list);
190
191$template->assign(
192  array(
193    'F_SEARCH_ACTION' => 'search.php',
194    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=search',
195
196    'month_list' => $month_list,
197    'START_DAY_SELECTED' => @$_POST['start_day'],
198    'START_MONTH_SELECTED' => @$_POST['start_month'],
199    'END_DAY_SELECTED' => @$_POST['end_day'],
200    'END_MONTH_SELECTED' => @$_POST['end_month'],
201    )
202  );
203
204$available_tags = get_available_tags();
205
206if (count($available_tags) > 0)
207{
208  usort( $available_tags, 'tag_alpha_compare');
209
210  $template->assign('TAGS', $available_tags);
211}
212
213// authors
214$query = '
215SELECT
216    author,
217    COUNT(*) AS counter
218  FROM '.IMAGES_TABLE.' AS i
219    JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.image_id = i.id
220  '.get_sql_condition_FandF(
221    array(
222      'forbidden_categories' => 'category_id',
223      'visible_categories' => 'category_id',
224      'visible_images' => 'id'
225      ),
226    ' WHERE '
227    ).'
228    AND author IS NOT NULL
229  GROUP BY author
230  ORDER BY author
231;';
232$authors = query2array($query);
233
234$template->assign('AUTHORS', $authors);
235
236//------------------------------------------------------------- categories form
237$query = '
238SELECT id,name,global_rank,uppercats
239  FROM '.CATEGORIES_TABLE.'
240'.get_sql_condition_FandF
241  (
242    array
243      (
244        'forbidden_categories' => 'id',
245        'visible_categories' => 'id'
246      ),
247    'WHERE'
248  ).'
249;';
250display_select_cat_wrapper($query, array(), 'category_options', true);
251
252// include menubar
253$themeconf = $template->get_template_vars('themeconf');
254if (!isset($themeconf['hide_menu_on']) OR !in_array('theSearchPage', $themeconf['hide_menu_on']))
255{
256  include( PHPWG_ROOT_PATH.'include/menubar.inc.php');
257}
258
259//------------------------------------------------------------ html code display
260include(PHPWG_ROOT_PATH.'include/page_header.php');
261trigger_notify('loc_end_search');
262flush_page_messages();
263$template->pparse('search');
264include(PHPWG_ROOT_PATH.'include/page_tail.php');
265?>
Note: See TracBrowser for help on using the repository browser.