source: trunk/search.php @ 28708

Last change on this file since 28708 was 28708, checked in by plg, 10 years ago

feature 2563: do not display all tags as checkboxes on search.php. Use jQuery Selectize instead.

  • Property svn:eol-style set to LF
File size: 7.7 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    $drop_char_match = array(
53      '-','^','$',';','#','&','(',')','<','>','`','\'','"','|',',','@','_',
54      '?','%','~','.','[',']','{','}',':','\\','/','=','\'','!','*');
55    $drop_char_replace = array(
56      ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','','',' ',' ',' ',' ','',' ',
57      ' ',' ',' ',' ',' ',' ',' ',' ','' ,' ',' ',' ',' ',' ');
58
59    // Split words
60    $search['fields']['allwords'] = array(
61      'words' => array_unique(
62        preg_split(
63          '/\s+/',
64          str_replace(
65            $drop_char_match,
66            $drop_char_replace,
67            $_POST['search_allwords']
68            )
69          )
70        ),
71      'mode' => $_POST['mode'],
72      );
73  }
74
75  if (isset($_POST['tags']))
76  {
77    check_input_parameter('tags', $_POST, true, PATTERN_ID);
78    check_input_parameter('tag_mode', $_POST, false, '/^(OR|AND)$/');
79   
80    $search['fields']['tags'] = array(
81      'words' => $_POST['tags'],
82      'mode'  => $_POST['tag_mode'],
83      );
84  }
85
86  if (isset($_POST['authors']) and is_array($_POST['authors']) and count($_POST['authors']) > 0)
87  {
88    $authors = array();
89
90    foreach ($_POST['authors'] as $author)
91    {
92      $authors[] = strip_tags($author);
93    }
94   
95    $search['fields']['author'] = array(
96      'words' => $authors,
97      'mode' => 'OR',
98      );
99  }
100
101  if (isset($_POST['cat']))
102  {
103    check_input_parameter('cat', $_POST, true, PATTERN_ID);
104   
105    $search['fields']['cat'] = array(
106      'words'   => $_POST['cat'],
107      'sub_inc' => ($_POST['subcats-included'] == 1) ? true : false,
108      );
109  }
110
111  // dates
112  $type_date = $_POST['date_type'];
113
114  if (!empty($_POST['start_year']))
115  {
116    $search['fields'][$type_date.'-after'] = array(
117      'date' => sprintf(
118        '%d-%02d-%02d',
119        $_POST['start_year'],
120        $_POST['start_month'] != 0 ? $_POST['start_month'] : '01',
121        $_POST['start_day']   != 0 ? $_POST['start_day']   : '01'
122        ),
123      'inc' => true,
124      );
125  }
126
127  if (!empty($_POST['end_year']))
128  {
129    $search['fields'][$type_date.'-before'] = array(
130      'date' => sprintf(
131        '%d-%02d-%02d',
132        $_POST['end_year'],
133        $_POST['end_month'] != 0 ? $_POST['end_month'] : '12',
134        $_POST['end_day']   != 0 ? $_POST['end_day']   : '31'
135      ),
136      'inc' => true,
137      );
138  }
139
140  if (!empty($search))
141  {
142    // default search mode : each clause must be respected
143    $search['mode'] = 'AND';
144
145    // register search rules in database, then they will be available on
146    // thumbnails page and picture page.
147    $query ='
148INSERT INTO '.SEARCH_TABLE.'
149  (rules, last_seen)
150  VALUES
151  (\''.serialize($search).'\', NOW())
152;';
153    pwg_query($query);
154
155    $search_id = pwg_db_insert_id(SEARCH_TABLE);
156  }
157  else
158  {
159    $page['errors'][] = l10n('Empty query. No criteria has been entered.');
160  }
161}
162//----------------------------------------------------------------- redirection
163if (isset($_POST['submit']) and count($page['errors']) == 0)
164{
165  redirect(
166    make_index_url(
167      array(
168        'section' => 'search',
169        'search'  => $search_id,
170        )
171      )
172    );
173}
174//----------------------------------------------------- template initialization
175
176//
177// Start output of page
178//
179$title= l10n('Search');
180$page['body_id'] = 'theSearchPage';
181
182$template->set_filename('search' ,'search.tpl' );
183
184$month_list = $lang['month'];
185$month_list[0]='------------';
186ksort($month_list);
187
188$template->assign(
189  array(
190    'F_SEARCH_ACTION' => 'search.php',
191    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=search',
192
193    'month_list' => $month_list,
194    'START_DAY_SELECTED' => @$_POST['start_day'],
195    'START_MONTH_SELECTED' => @$_POST['start_month'],
196    'END_DAY_SELECTED' => @$_POST['end_day'],
197    'END_MONTH_SELECTED' => @$_POST['end_month'],
198    )
199  );
200
201$available_tags = get_available_tags();
202
203if (count($available_tags) > 0)
204{
205  usort( $available_tags, 'tag_alpha_compare');
206
207  $template->assign('TAGS', $available_tags);
208}
209
210// authors
211$query = '
212SELECT
213    author,
214    COUNT(*) AS counter
215  FROM '.IMAGES_TABLE.' AS i
216    JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.image_id = i.id
217  '.get_sql_condition_FandF(
218    array(
219      'forbidden_categories' => 'category_id',
220      'visible_categories' => 'category_id',
221      'visible_images' => 'ic.image_id'
222      ),
223    ' WHERE '
224    ).'
225    AND author IS NOT NULL
226  GROUP BY author
227  ORDER BY author
228;';
229$authors = query2array($query);
230
231$template->assign('AUTHORS', $authors);
232
233//------------------------------------------------------------- categories form
234$query = '
235SELECT id,name,global_rank,uppercats
236  FROM '.CATEGORIES_TABLE.'
237'.get_sql_condition_FandF
238  (
239    array
240      (
241        'forbidden_categories' => 'id',
242        'visible_categories' => 'id'
243      ),
244    'WHERE'
245  ).'
246;';
247display_select_cat_wrapper($query, array(), 'category_options', false);
248
249
250// include menubar
251$themeconf = $template->get_template_vars('themeconf');
252if (!isset($themeconf['hide_menu_on']) OR !in_array('theSearchPage', $themeconf['hide_menu_on']))
253{
254  include( PHPWG_ROOT_PATH.'include/menubar.inc.php');
255}
256
257//------------------------------------------------------------ html code display
258include(PHPWG_ROOT_PATH.'include/page_header.php');
259trigger_notify('loc_end_search');
260flush_page_messages();
261$template->pparse('search');
262include(PHPWG_ROOT_PATH.'include/page_tail.php');
263?>
Note: See TracBrowser for help on using the repository browser.