diff --git a/include/functions_category.inc.php b/include/functions_category.inc.php index 34025a704..7542e337a 100644 --- a/include/functions_category.inc.php +++ b/include/functions_category.inc.php @@ -415,30 +415,52 @@ function initialize_category( $calling_page = 'category' ) // SQL where clauses are stored in $clauses array during query // construction - $clauses = $temp_clauses = array(); - if (isset($search['fields']['allwords'])) - { - $textfields = array('file', 'name', 'comment', 'keywords', 'author'); - foreach ($textfields as $textfield) + $clauses = array(); + + $textfields = array('file', 'name', 'comment', 'keywords', 'author'); + foreach ($textfields as $textfield) + { + if (isset($search['fields'][$textfield])) { $local_clauses = array(); - foreach ($search['fields']['allwords']['words'] as $word) + foreach ($search['fields'][$textfield]['words'] as $word) { array_push($local_clauses, $textfield." LIKE '%".$word."%'"); } // adds brackets around where clauses array_walk($local_clauses,create_function('&$s','$s="(".$s.")";')); - array_push($temp_clauses, - implode(' '.$search['fields']['allwords']['mode'].' ', + array_push($clauses, + implode(' '.$search['fields'][$textfield]['mode'].' ', $local_clauses)); } - array_push($clauses, implode(' OR ', $temp_clauses)); } - - if (isset($search['fields']['author'])) - { - array_push($clauses, "author LIKE '%".$search['fields']['author']['words'][0]."%'"); - } + + if (isset($search['fields']['allwords'])) + { + $fields = array('file', 'name', 'comment', 'keywords'); + // in the OR mode, request bust be : + // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') + // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) + // + // in the AND mode : + // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') + // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) + $word_clauses = array(); + foreach ($search['fields']['allwords']['words'] as $word) + { + $field_clauses = array(); + foreach ($fields as $field) + { + array_push($field_clauses, $field." LIKE '%".$word."%'"); + } + // adds brackets around where clauses + array_push($word_clauses, implode(' OR ', $field_clauses)); + } + array_walk($word_clauses, create_function('&$s','$s="(".$s.")";')); + array_push($clauses, + implode(' '.$search['fields']['allwords']['mode'].' ', + $word_clauses)); + } $datefields = array('date_available', 'date_creation'); foreach ($datefields as $datefield) @@ -446,27 +468,41 @@ function initialize_category( $calling_page = 'category' ) $key = $datefield; if (isset($search['fields'][$key])) { - $local_clause = $datefield." "; - if (isset($search['fields'][$key]['mode'])) - { - $local_clause .=">"; - } - $local_clause .="= '"; - $local_clause.= str_replace('.', '-', + $local_clause = $datefield." = '"; + $local_clause.= str_replace('.', '-', $search['fields'][$key]['words'][0]); - $local_clause.= "'"; - array_push($clauses, $local_clause); - - if (isset($search['fields'][$key]['mode'])) - { - $end_sql_date = str_replace('.', '-', - $search['fields'][$key]['mode']); - $local_clause = $datefield." <= '".$end_sql_date."'"; - array_push($clauses, $local_clause); - } - } - } - + $local_clause.= "'"; + array_push($clauses, $local_clause); + } + + foreach (array('after','before') as $suffix) + { + $key = $datefield.'-'.$suffix; + if (isset($search['fields'][$key])) + { + $local_clause = $datefield; + if ($suffix == 'after') + { + $local_clause.= ' >'; + } + else + { + $local_clause.= ' <'; + } + if (isset($search['fields'][$key]['mode']) + and $search['fields'][$key]['mode'] == 'inc') + { + $local_clause.= '='; + } + $local_clause.= " '"; + $local_clause.= str_replace('.', '-', + $search['fields'][$key]['words'][0]); + $local_clause.= "'"; + array_push($clauses, $local_clause); + } + } + } + if (isset($search['fields']['cat'])) { if ($search['fields']['cat']['mode'] == 'sub_inc') @@ -508,7 +544,7 @@ SELECT DISTINCT(id) AS id // adds brackets around where clauses array_walk($clauses, create_function('&$s', '$s = "(".$s.")";')); - $page['where'] = 'WHERE '.implode(' AND ', $clauses); + $page['where'] = 'WHERE '.implode(' '.$search['mode'].' ', $clauses); if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden; $query = ' diff --git a/search.php b/search.php index 1c613817d..47a5ddc77 100644 --- a/search.php +++ b/search.php @@ -35,25 +35,27 @@ $errors = array(); $search = array(); if (isset($_POST['submit'])) { - if ($_POST['search_allwords'] && - !preg_match('/^\s*$/', $_POST['search_allwords'])) + if (isset($_POST['search_allwords']) + and !preg_match('/^\s*$/', $_POST['search_allwords'])) { $local_search = array(); - $search_keywords = $_POST['search_allwords']; - $drop_char_match = array('-', '^', '$', ';', '#', '&', '(', ')', '<', '>', - '`', '\'', '"', '|', ',', '@', '_', '?', '%', '~', '.', '[', ']', '{', '}', - ':', '\\', '/', '=', '\'', '!', '*'); - $drop_char_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - '', '', ' ', ' ', ' ', ' ', '', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' - , ' ', '' , ' ', ' ', ' ', ' ', ' '); - $search_keywords = str_replace($drop_char_match, $drop_char_replace, $search_keywords); + $search_allwords = $_POST['search_allwords']; + $drop_char_match = array( + '-','^','$',';','#','&','(',')','<','>','`','\'','"','|',',','@','_', + '?','%','~','.','[',']','{','}',':','\\','/','=','\'','!','*'); + $drop_char_replace = array( + ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','','',' ',' ',' ',' ','',' ', + ' ',' ',' ',' ',' ',' ',' ',' ','' ,' ',' ',' ',' ',' '); + $search_allwords = str_replace($drop_char_match, + $drop_char_replace, + $search_allwords); - // Split words - $words = preg_split('#\s+#', $search_keywords); + // Split words + $words = preg_split('/\s+/', $search_allwords); $words = array_unique($words); - $search['fields']['allwords'] = array(); - $search['fields']['allwords']['words'] =$words; - $search['fields']['allwords']['mode']= $_POST['mode']; + $search['fields']['allwords'] = array(); + $search['fields']['allwords']['words'] = $words; + $search['fields']['allwords']['mode'] = $_POST['mode']; } if ($_POST['search_author']) @@ -66,48 +68,57 @@ if (isset($_POST['submit'])) { $search['fields']['cat'] = array(); $search['fields']['cat']['words'] = $_POST['cat']; - if (isset($_POST['subcats-included'])) + if ($_POST['subcats-included'] == 1) { $search['fields']['cat']['mode'] = 'sub_inc'; } } + + // dates + $type_date = $_POST['date_type']; if (!empty($_POST['start_year'])) - { - $type_date = $_POST['date_type']; - - // start event - $date = $_POST['start_year'].'.'.$_POST['start_month'].'.'.$_POST['start_day']; - $search['fields'][$type_date]['words'] = array($date); - - // duration - $search_duration = 0; - if ( !empty($date) && !empty( $_POST['end_year']) ) { - $end_date = $_POST['end_year'].'.'.$_POST['end_month'].'.'.$_POST['end_day']; - $search['fields'][$type_date]['mode'] = $end_date; + $year = $_POST['start_year']; + $month = $_POST['start_month'] != 0 ? $_POST['start_month'] : '01'; + $day = $_POST['start_day'] != 0 ? $_POST['start_day'] : '01'; + $date = $year.'-'.$month.'-'.$day; + + $search['fields'][$type_date.'-after']['words'] = array($date); + $search['fields'][$type_date.'-after']['mode'] = 'inc'; } - } + + if (!empty($_POST['end_year'])) + { + $year = $_POST['end_year']; + $month = $_POST['end_month'] != 0 ? $_POST['end_month'] : '12'; + $day = $_POST['end_day'] != 0 ? $_POST['end_day'] : '31'; + $date = $year.'-'.$month.'-'.$day; + + $search['fields'][$type_date.'-before']['words'] = array($date); + $search['fields'][$type_date.'-before']['mode'] = 'inc'; + } + // search string (for URL) creation $search_string = ''; $tokens = array(); if (!empty($search)) { - foreach (array_keys($search['fields']) as $field) - { - $token = $field.':'; - $token.= implode(',', $search['fields'][$field]['words']); - if (isset($search['fields'][$field]['mode'])) + foreach (array_keys($search['fields']) as $field) { - $token.= '~'.$search['fields'][$field]['mode']; + $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.= '|AND'; } - array_push($tokens, $token); - } - $search_string.= implode(';', $tokens); - if (count($tokens) > 1) - { - $search_string.= '|AND'; - } } else { @@ -121,41 +132,69 @@ if (isset($_POST['submit']) and count($errors) == 0) $url = add_session_id($url, true); redirect($url); } - //----------------------------------------------------- template initialization -// day list -$start_day = ''; -// month list -$start_month = ''; + global $template, $lang; + + $template->assign_block_vars( + $blockname, array('SELECTED' => '', + 'VALUE' => 0, + 'OPTION' => '------------')); -// day list -$end_day = ''; -// month list -$end_month = ''; +// start date +get_day_list('start_day', @$_POST['start_day']); +get_month_list('start_month', @$_POST['start_month']); +// end date +get_day_list('end_day', @$_POST['end_day']); +get_month_list('end_month', @$_POST['end_month']); // // Start output of page @@ -198,10 +237,6 @@ $template->assign_vars(array( 'TODAY_DAY' => date('d', time()), 'TODAY_MONTH' => date('m', time()), 'TODAY_YEAR' => date('Y', time()), - 'E_CALENDAR_MONTH' => $end_month, - 'E_CALENDAR_DAY' => $end_day, - 'S_CALENDAR_MONTH' => $start_month, - 'S_CALENDAR_DAY' => $start_day, 'S_SEARCH_ACTION' => add_session_id( 'search.php' ), 'U_HOME' => add_session_id( 'category.php' ) ) @@ -211,11 +246,11 @@ $template->assign_vars(array( $query = ' SELECT name,id,date_last,nb_images,global_rank,uppercats FROM '.CATEGORIES_TABLE; - if ($user['forbidden_categories'] != '') - { - $query.= ' +if ($user['forbidden_categories'] != '') +{ + $query.= ' WHERE id NOT IN ('.$user['forbidden_categories'].')'; - } +} $query.= ' ;'; diff --git a/template/default/search.tpl b/template/default/search.tpl index 6cdb0b924..7c16d06ca 100644 --- a/template/default/search.tpl +++ b/template/default/search.tpl @@ -31,16 +31,43 @@ {L_SEARCH_DATE} :
{L_SEARCH_DATE_HINT} -
- {L_SEARCH_DATE_FROM} : - {S_CALENDAR_DAY} {S_CALENDAR_MONTH}   - {L_TODAY} -
- {L_SEARCH_DATE_TO} : - {E_CALENDAR_DAY} {E_CALENDAR_MONTH}   - {L_TODAY} -
- + + + + + + + + + +
{L_SEARCH_DATE_FROM} : + + +   + {L_TODAY} +
{L_SEARCH_DATE_TO} : + + +   + {L_TODAY} +
+ {L_SEARCH_OPTIONS}