diff options
author | plegall <plg@piwigo.org> | 2004-12-05 11:47:40 +0000 |
---|---|---|
committer | plegall <plg@piwigo.org> | 2004-12-05 11:47:40 +0000 |
commit | e4ce82fc27e1e10d697815928ef06c692a3f56da (patch) | |
tree | 4f344ae6e3d7a186846e83f870bd9c6a7ef03d28 | |
parent | b3336f57d5d61eaa5d465b816962b12ea16c24ae (diff) |
- search modified : possible to partially fill the date, allwords are
separated from more specific search, separation of HTML (in template) and
PHP code)
git-svn-id: http://piwigo.org/svn/trunk@634 68402e56-0260-453c-a942-63ccdbb3a9ee
-rw-r--r-- | include/functions_category.inc.php | 106 | ||||
-rw-r--r-- | search.php | 187 | ||||
-rw-r--r-- | template/default/search.tpl | 47 |
3 files changed, 219 insertions, 121 deletions
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']; + { + $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'; + } - // 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']) ) + if (!empty($_POST['end_year'])) { - $end_date = $_POST['end_year'].'.'.$_POST['end_month'].'.'.$_POST['end_day']; - $search['fields'][$type_date]['mode'] = $end_date; + $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 = '<select name="start_day">'; -for ($i=0; $i <= 31; $i++) +/** + * instantiate number list for days in a template block + * + * @param string blockname + * @param string selection + */ +function get_day_list($blockname, $selection) { - $start_day .= '<option value="' . $i . '" >' . ( ($i == 0) ? ' -- ' : str_pad($i, 2, '0', STR_PAD_LEFT) ) . '</option>'; + global $template; + + $template->assign_block_vars( + $blockname, array('SELECTED' => '', 'VALUE' => 0, 'OPTION' => '--')); + + for ($i = 1; $i <= 31; $i++) + { + $selected = ''; + if ($i == (int)$selection) + { + $selected = 'selected="selected"'; + } + $template->assign_block_vars( + $blockname, array('SELECTED' => $selected, + 'VALUE' => $i, + 'OPTION' => str_pad($i, 2, '0', STR_PAD_LEFT))); + } } -$start_day .= '</select>'; -// month list -$start_month = '<select name="start_month">'; -$start_month .= '<option value="0"> ------------ </option>'; -for ($i=1; $i <= 12; $i++) +/** + * instantiate month list in a template block + * + * @param string blockname + * @param string selection + */ +function get_month_list($blockname, $selection) { - $start_month .= '<option value="' . $i . '">' . $lang['month'][$i] . '</option>'; -} -$start_month .= '</select>'; + global $template, $lang; + + $template->assign_block_vars( + $blockname, array('SELECTED' => '', + 'VALUE' => 0, + 'OPTION' => '------------')); -// day list -$end_day = '<select name="end_day">'; -for ($i=0; $i <= 31; $i++) -{ - $end_day .= '<option value="' . $i . '" >' . ( ($i == 0) ? ' -- ' : str_pad($i, 2, '0', STR_PAD_LEFT) ) . '</option>'; + for ($i = 1; $i <= 12; $i++) + { + $selected = ''; + if ($i == (int)$selection) + { + $selected = 'selected="selected"'; + } + $template->assign_block_vars( + $blockname, array('SELECTED' => $selected, + 'VALUE' => $i, + 'OPTION' => $lang['month'][$i])); + } } -$end_day .= '</select>'; -// month list -$end_month = '<select name="end_month">'; -$end_month .= '<option value="0"> ------------ </option>'; -for ($i=1; $i <= 12; $i++) -{ - $end_month .= '<option value="' . $i . '">' . $lang['month'][$i] . '</option>'; -} -$end_month .= '</select>'; +// 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 @@ <tr> <td colspan="2"><b>{L_SEARCH_DATE} :</b><br /><span class="small">{L_SEARCH_DATE_HINT}</span></td> <td colspan="2" valign="middle"> - <table><tr><td> - {L_SEARCH_DATE_FROM} :</td><td> - {S_CALENDAR_DAY} {S_CALENDAR_MONTH} <input name="start_year" type="text" size="4" maxlength="4"> - <a href="#" name="#" onClick="document.post.start_day.value={TODAY_DAY};document.post.start_month.value={TODAY_MONTH};document.post.start_year.value={TODAY_YEAR};" />{L_TODAY}</a> - </tr><tr><td> - {L_SEARCH_DATE_TO} :</td><td> - {E_CALENDAR_DAY} {E_CALENDAR_MONTH} <input name="end_year" type="text" size="4" maxlength="4"> - <a href="#" name="#" onClick="document.post.end_day.value={TODAY_DAY};document.post.end_month.value={TODAY_MONTH};document.post.end_year.value={TODAY_YEAR};" />{L_TODAY}</a> - </td></tr></table> - </td> + <table> + <tr> + <td>{L_SEARCH_DATE_FROM} :</td> + <td> + <select name="start_day"> + <!-- BEGIN start_day --> + <option {start_day.SELECTED} value="{start_day.VALUE}">{start_day.OPTION}</option> + <!-- END start_day --> + </select> + <select name="start_month"> + <!-- BEGIN start_month --> + <option {start_month.SELECTED} value="{start_month.VALUE}">{start_month.OPTION}</option> + <!-- END start_month --> + </select> + <input name="start_year" type="text" size="4" maxlength="4"> + <a href="#" name="#" onClick="document.post.start_day.value={TODAY_DAY};document.post.start_month.value={TODAY_MONTH};document.post.start_year.value={TODAY_YEAR};" />{L_TODAY}</a> + </td> + </tr> + <tr> + <td>{L_SEARCH_DATE_TO} :</td> + <td> + <select name="end_day"> + <!-- BEGIN end_day --> + <option {end_day.SELECTED} value="{end_day.VALUE}">{end_day.OPTION}</option> + <!-- END end_day --> + </select> + <select name="end_month"> + <!-- BEGIN end_month --> + <option {end_month.SELECTED} value="{end_month.VALUE}">{end_month.OPTION}</option> + <!-- END end_month --> + </select> + <input name="end_year" type="text" size="4" maxlength="4"> + <a href="#" name="#" onClick="document.post.end_day.value={TODAY_DAY};document.post.end_month.value={TODAY_MONTH};document.post.end_year.value={TODAY_YEAR};" />{L_TODAY}</a> + </td> + </tr> + </table> + </td> </tr> <tr class="admin"> <th colspan="4">{L_SEARCH_OPTIONS}</th> |