From 3610e7f3609b3e37e83a94770d25e7d08bd3f784 Mon Sep 17 00:00:00 2001 From: z0rglub Date: Mon, 26 Jul 2004 20:43:46 +0000 Subject: new search form : finer with search on each field independantly git-svn-id: http://piwigo.org/svn/trunk@455 68402e56-0260-453c-a942-63ccdbb3a9ee --- search.php | 388 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 363 insertions(+), 25 deletions(-) (limited to 'search.php') diff --git a/search.php b/search.php index b0770c0fd..2747794d7 100644 --- a/search.php +++ b/search.php @@ -25,38 +25,205 @@ // | USA. | // +-----------------------------------------------------------------------+ -//----------------------------------------------------------- include +//------------------------------------------------------------------- functions +// date_display displays 3 select input fields. The first one is the +// day of the month, from 0 to 31. The second is the month of the year, +// from 01 to 12. The last one is the year. The years displayed are the +// ones given by get_available_years (see function description in +// ./include/functions.inc.php). +function display_date($fieldname, $datefield) +{ + global $template; + + // years + for ($i = 1990; $i < 2006; $i++) + { + $selected = ''; + $key = $datefield.':year'; + if (isset($_POST[$key]) and $i == $_POST[$key]) + { + $selected = ' selected="selected"'; + } + + $template->assign_block_vars( + $fieldname.'year_option', + array('OPTION'=>$i, + 'SELECTED'=>$selected + )); + } + // months of year + for ($i = 1; $i <= 12; $i++) + { + $selected = ''; + $key = $datefield.':month'; + if (isset($_POST[$key]) and $i == $_POST[$key]) + { + $selected = ' selected="selected"'; + } + + $template->assign_block_vars( + $fieldname.'month_option', + array('OPTION'=>sprintf('%02s', $i), + 'SELECTED'=>$selected + )); + } + // days of the month + for ($i = 1; $i <= 31; $i++) + { + $selected = ''; + $key = $datefield.':day'; + if (isset($_POST[$key]) and $i == $_POST[$key]) + { + $selected = ' selected="selected"'; + } + + $template->assign_block_vars( + $fieldname.'day_option', + array('OPTION'=>sprintf('%02s', $i), + 'SELECTED'=>$selected + )); + } +} + +function display_3dates($fieldname) +{ + display_date('datefield.', $fieldname); + display_date('datefield.after_', $fieldname.'-after'); + display_date('datefield.before_', $fieldname.'-before'); +} +//--------------------------------------------------------------------- include define('PHPWG_ROOT_PATH','./'); include_once( PHPWG_ROOT_PATH.'include/common.inc.php' ); //-------------------------------------------------- access authorization check check_login_authorization(); -//----------------------------------------------------------------- redirection -$error = array(); -if ( isset( $_POST['search'] ) ) +//----------------------------------------------------------------- form fields +$textfields = array('file', 'name', 'comment', 'keywords', 'author'); +$datefields = array('date_available', 'date_creation'); +//------------------------------------------------------------------ form check +$errors = array(); +$search = array(); +$search['fields'] = array(); +if (isset($_POST['submit'])) { - $redirect = true; - $search = array(); - $words = preg_split( '/\s+/', $_POST['search'] ); - foreach ( $words as $i => $word ) { - if ( strlen( $word ) > 2 and !preg_match( '/[,;:\']/', $word ) ) + $search['mode'] = $_POST['mode']; + + foreach ($textfields as $textfield) + { + if (isset($_POST[$textfield.'-content']) + and !preg_match('/^\s*$/', $_POST[$textfield.'-content'])) { - array_push( $search, $word ); + $local_search = array(); + $words = preg_split('/\s+/', $_POST[$textfield.'-content']); + foreach ($words as $i => $word) + { + if (strlen($word) > 2 and !preg_match('/[,;:\']/', $word)) + { + array_push($local_search, $word); + } + else + { + array_push($errors, $lang['invalid_search']); + } + } + $local_search = array_unique($local_search); + $search['fields'][$textfield] = array(); + $search['fields'][$textfield]['words'] = $local_search; + if (count($local_search) > 1) + { + $search['fields'][$textfield]['mode'] = $_POST[$textfield.'-mode']; + } } - else + } + foreach ($datefields as $datefield) + { + $suffixes = array('','-after','-before'); + foreach ($suffixes as $suffix) + { + $field = $datefield.$suffix; + if (isset($_POST[$field.'-check'])) + { + $year = $_POST[$field.':year']; + $month = $_POST[$field.':month']; + $day = $_POST[$field.':day']; + $date = $year.'.'.$month.'.'.$day; + if (!checkdate($month, $day, $year)) + { + array_push($errors, $date.$lang['search_wrong_date']); + } + $search['fields'][$field] = array(); + $search['fields'][$field]['words'] = array($date); + if ($suffix == '-after' or $suffix == '-before') + { + if (isset($_POST[$field.'-included'])) + { + $search['fields'][$field]['mode'] = 'inc'; + } + } + } + } + if ($search['mode'] == 'AND') { - $redirect = false; - array_push( $error, $lang['invalid_search'] ); - break; + // before date must be superior to after date + if (isset($search['fields'][$datefield.'-before']) + and isset($search['fields'][$datefield.'-after'])) + { + $after = $search['fields'][$datefield.'-after']['words'][0]; + $before = $search['fields'][$datefield.'-before']['words'][0]; + if ($after >= $before) + { + array_push($errors, $lang['search_wrong_date_order']); + } + } + // having "search is" and ("search is after" or "search is before") is + // not coherent + if (isset($search['fields'][$datefield]) + and (isset($search['fields'][$datefield.'-before']) + or isset($search['fields'][$datefield.'-after']))) + { + array_push($errors, $lang['search_incoherent_date_search']); + } } } - $search = array_unique( $search ); - $search = implode( ',', $search ); - if ( $redirect ) + if (isset($_POST['categories-check'])) { - $url = 'category.php?cat=search&search='.$search.'&mode='.$_POST['mode']; - $url = add_session_id( $url, true ); - redirect( $url ); + $field = 'cat'; + $search['fields'][$field] = array(); + $search['fields'][$field]['words'] = $_POST['cat']; + if (isset($_POST['subcats-included'])) + { + $search['fields'][$field]['mode'] = 'sub_inc'; + } } + // search string (for URL) creation + $search_string = ''; + $tokens = array(); + foreach (array_keys($search['fields']) as $field) + { + $token = $field.':'; + $token.= implode(',', $search['fields'][$field]['words']); + if (isset($search['fields'][$field]['mode'])) + { + $token.= '~'.$search['fields'][$field]['mode']; + } + array_push($tokens, $token); + } + $search_string.= implode(';', $tokens); + if (count($tokens) > 1) + { + $search_string.= '|'.$search['mode']; + } + + if (count($tokens) == 0) + { + array_push($errors, $lang['search_one_clause_at_least']); + } +} +//----------------------------------------------------------------- redirection +if (isset($_POST['submit']) and count($errors) == 0) +{ + $url = 'category.php?cat=search&search='.$search_string; + $url = add_session_id($url, true); + redirect($url); } //----------------------------------------------------- template initialization // @@ -71,24 +238,195 @@ $template->assign_vars(array( 'L_COMMENTS' => $lang['search_comments'], 'L_RETURN' => $lang['search_return_main_page'], 'L_SUBMIT' => $lang['submit'], - 'L_SEARCH'=>$lang['search_field_search'].' *', 'L_SEARCH_OR'=>$lang['search_mode_or'], 'L_SEARCH_AND'=>$lang['search_mode_and'], + 'L_SEARCH_OR_CLAUSES'=>$lang['search_or_clauses'], + 'L_SEARCH_AND_CLAUSES'=>$lang['search_and_clauses'], + 'L_SEARCH_CATEGORIES'=>$lang['search_categories'], + 'L_SEARCH_SUBCATS_INCLUDED'=>$lang['search_subcats_included'], + 'L_SEARCH_DATE_INCLUDED'=> $lang['search_date_included'], + 'L_SEARCH_DATE_IS'=>$lang['search_date_is'], + 'L_SEARCH_DATE_IS_AFTER'=>$lang['search_date_is_after'], + 'L_SEARCH_DATE_IS_BEFORE'=>$lang['search_date_is_before'], 'F_ACTION' => add_session_id( 'search.php' ), - 'F_TEXT_VALUE' => isset($_POST['search'])?$_POST['search']:'', 'U_HOME' => add_session_id( 'category.php' ) ) ); +//------------------------------------------------------------ text fields form +foreach ($textfields as $textfield) +{ + if (isset($_POST[$textfield.'-mode'])) + { + if ($_POST[$textfield.'-mode'] == 'AND') + { + $and_checked = 'checked="checked"'; + $or_checked = ''; + } + else + { + $or_checked = 'checked="checked"'; + $and_checked = ''; + } + } + else + { + $or_checked = 'checked="checked"'; + $and_checked = ''; + } + + $value = ''; + if (isset($_POST[$textfield.'-content'])) + { + $value = $_POST[$textfield.'-content']; + } + + $template->assign_block_vars( + 'textfield', + array('NAME'=>$lang['search_'.$textfield], + 'L_NAME'=>$textfield, + 'VALUE'=>$value, + 'OR_CHECKED'=>$or_checked, + 'AND_CHECKED'=>$and_checked + )); +} +//------------------------------------------------------------- date field form +foreach ($datefields as $datefield) +{ + $checked = ''; + if (isset($_POST[$datefield.'-check'])) + { + $checked = ' checked="checked"'; + } + + $after_checked = ''; + if (isset($_POST[$datefield.'-after-check'])) + { + $after_checked = ' checked="checked"'; + } + + $before_checked = ''; + if (isset($_POST[$datefield.'-before-check'])) + { + $before_checked = ' checked="checked"'; + } + + $after_included_check = ''; + if (isset($_POST[$datefield.'-after-included'])) + { + $after_included_check = ' checked="checked"'; + } + + $before_included_check = ''; + if (isset($_POST[$datefield.'-before-included'])) + { + $before_included_check = ' checked="checked"'; + } + + $template->assign_block_vars( + 'datefield', + array('NAME'=>$datefield, + 'L_NAME'=>$datefield, + 'CHECKED'=>$checked, + 'AFTER_CHECKED'=>$after_checked, + 'BEFORE_CHECKED'=>$before_checked, + 'AFTER_INCLUDED_CHECKED'=>$after_included_check, + 'BEFORE_INCLUDED_CHECKED'=>$before_included_check + )); + display_3dates($datefield); +} +//------------------------------------------------------------- categories form +function display_search_categories($categories, $indent, $selecteds) +{ + global $template,$user; + + foreach ( $categories as $category ) + { + if (!in_array($category['id'], $user['restrictions'])) + { + $selected = ''; + if (in_array($category['id'], $selecteds)) + { + $selected = ' selected="selected"'; + } + + $template->assign_block_vars( + 'category_option', + array('SELECTED'=>$selected, + 'VALUE'=>$category['id'], + 'OPTION'=>$indent.'- '.$category['name'] + )); + + display_search_categories( $category['subcats'], + $indent.str_repeat(' ',3), + $selecteds ); + } + } +} +include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); +$page['plain_structure'] = get_plain_structure(true); +$structure = create_structure(''); + +$selecteds = array(); +if (isset($_POST['submit'])) +{ + $selecteds = $_POST['cat']; +} +display_search_categories( $structure, ' ', $selecteds ); + +$categories_selected = ''; +if (isset($_POST['categories-check'])) +{ + $categories_selected = 'checked="checked"'; +} + +$categories_subcats_selected = ''; +if (isset($_POST['subcats-included'])) +{ + $categories_subcats_selected = 'checked="checked"'; +} + +$template->assign_vars( + array( + 'CATEGORIES_SELECTED'=>$categories_selected, + 'CATEGORIES_SUBCATS_SELECTED'=>$categories_subcats_selected + ) + ); +//---------------------------------------------------------------------- OR/AND +if (isset($_POST['mode'])) +{ + if ($_POST['mode'] == 'AND') + { + $and_checked = 'checked="checked"'; + $or_checked = ''; + } + else + { + $or_checked = 'checked="checked"'; + $and_checked = ''; + } +} +else +{ + $or_checked = 'checked="checked"'; + $and_checked = ''; +} + +$template->assign_vars( + array( + 'OR_CHECKED'=>$or_checked, + 'AND_CHECKED'=>$and_checked + ) + ); //-------------------------------------------------------------- errors display -if ( sizeof( $error ) != 0 ) +if (sizeof($errors) != 0) { $template->assign_block_vars('errors',array()); - for ( $i = 0; $i < sizeof( $error ); $i++ ) + foreach ($errors as $error) { - $template->assign_block_vars('errors.error',array('ERROR'=>$error[$i])); + $template->assign_block_vars('errors.error',array('ERROR'=>$error)); } } //------------------------------------------------------------ log informations -- cgit v1.2.3