Changeset 634 for trunk/search.php


Ignore:
Timestamp:
Dec 5, 2004, 12:47:40 PM (19 years ago)
Author:
plg
Message:
  • search modified : possible to partially fill the date, allwords are separated from more specific search, separation of HTML (in template) and PHP code)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/search.php

    r629 r634  
    3636if (isset($_POST['submit']))
    3737{
    38   if ($_POST['search_allwords'] &&
    39    !preg_match('/^\s*$/', $_POST['search_allwords']))
     38  if (isset($_POST['search_allwords'])
     39      and !preg_match('/^\s*$/', $_POST['search_allwords']))
    4040  {
    4141    $local_search = array();
    42         $search_keywords = $_POST['search_allwords'];
    43         $drop_char_match =   array('-', '^', '$', ';', '#', '&', '(', ')', '<', '>',
    44           '`', '\'', '"', '|', ',', '@', '_', '?', '%', '~', '.', '[', ']', '{', '}',
    45           ':', '\\', '/', '=', '\'', '!', '*');
    46         $drop_char_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
    47           '',  '',   ' ', ' ', ' ', ' ', '',  ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
    48           , ' ', '' ,  ' ', ' ', ' ',  ' ', ' ');
    49         $search_keywords = str_replace($drop_char_match, $drop_char_replace, $search_keywords);
     42    $search_allwords = $_POST['search_allwords'];
     43    $drop_char_match = array(
     44      '-','^','$',';','#','&','(',')','<','>','`','\'','"','|',',','@','_',
     45      '?','%','~','.','[',']','{','}',':','\\','/','=','\'','!','*');
     46    $drop_char_replace = array(
     47      ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','','',' ',' ',' ',' ','',' ',
     48      ' ',' ',' ',' ',' ',' ',' ',' ','' ,' ',' ',' ',' ',' ');
     49    $search_allwords = str_replace($drop_char_match,
     50                                   $drop_char_replace,
     51                                   $search_allwords);
    5052       
    51         // Split words
    52         $words = preg_split('#\s+#', $search_keywords);
     53    // Split words
     54    $words = preg_split('/\s+/', $search_allwords);
    5355    $words = array_unique($words);
    54         $search['fields']['allwords'] = array();
    55         $search['fields']['allwords']['words'] =$words;
    56         $search['fields']['allwords']['mode']= $_POST['mode'];
     56    $search['fields']['allwords'] = array();
     57    $search['fields']['allwords']['words'] = $words;
     58    $search['fields']['allwords']['mode'] = $_POST['mode'];
    5759  }
    5860 
     
    6769    $search['fields']['cat'] = array();
    6870    $search['fields']['cat']['words'] = $_POST['cat'];
    69     if (isset($_POST['subcats-included']))
     71    if ($_POST['subcats-included'] == 1)
    7072    {
    7173      $search['fields']['cat']['mode'] = 'sub_inc';
    7274    }
    7375  }
     76
     77  // dates
     78  $type_date = $_POST['date_type'];
    7479 
    7580  if (!empty($_POST['start_year']))
    76  {
    77   $type_date = $_POST['date_type'];
    78 
    79   // start event
    80   $date = $_POST['start_year'].'.'.$_POST['start_month'].'.'.$_POST['start_day'];
    81   $search['fields'][$type_date]['words'] = array($date);
    82  
    83   // duration
    84   $search_duration = 0;
    85   if ( !empty($date) && !empty( $_POST['end_year']) )
    86   {
    87         $end_date = $_POST['end_year'].'.'.$_POST['end_month'].'.'.$_POST['end_day'];
    88         $search['fields'][$type_date]['mode'] =  $end_date;
    89   }
    90  }
     81  {
     82    $year = $_POST['start_year'];
     83    $month = $_POST['start_month'] != 0 ? $_POST['start_month'] : '01';
     84    $day = $_POST['start_day'] != 0 ? $_POST['start_day'] : '01';
     85    $date = $year.'-'.$month.'-'.$day;
     86   
     87    $search['fields'][$type_date.'-after']['words'] = array($date);
     88    $search['fields'][$type_date.'-after']['mode'] = 'inc';
     89  }
     90
     91  if (!empty($_POST['end_year']))
     92  {
     93    $year = $_POST['end_year'];
     94    $month = $_POST['end_month'] != 0 ? $_POST['end_month'] : '12';
     95    $day = $_POST['end_day'] != 0 ? $_POST['end_day'] : '31';
     96    $date = $year.'-'.$month.'-'.$day;
     97   
     98    $search['fields'][$type_date.'-before']['words'] = array($date);
     99    $search['fields'][$type_date.'-before']['mode'] = 'inc';
     100  }
     101   
    91102  // search string (for URL) creation
    92103  $search_string = '';
     
    94105  if (!empty($search))
    95106  {
    96   foreach (array_keys($search['fields']) as $field)
    97   {
    98     $token = $field.':';
    99     $token.= implode(',', $search['fields'][$field]['words']);
    100     if (isset($search['fields'][$field]['mode']))
    101     {
    102       $token.= '~'.$search['fields'][$field]['mode'];
    103     }
    104     array_push($tokens, $token);
    105   }
    106   $search_string.= implode(';', $tokens);
    107   if (count($tokens) > 1)
    108   {
    109     $search_string.= '|AND';
    110   }
     107    foreach (array_keys($search['fields']) as $field)
     108    {
     109      $token = $field.':';
     110      $token.= implode(',', $search['fields'][$field]['words']);
     111      if (isset($search['fields'][$field]['mode']))
     112      {
     113        $token.= '~'.$search['fields'][$field]['mode'];
     114      }
     115      array_push($tokens, $token);
     116    }
     117    $search_string.= implode(';', $tokens);
     118    if (count($tokens) > 1)
     119    {
     120      $search_string.= '|AND';
     121    }
    111122  }
    112123  else
     
    122133  redirect($url);
    123134}
    124 
    125135//----------------------------------------------------- template initialization
    126 // day list
    127 $start_day = '<select name="start_day">';
    128 for ($i=0; $i <= 31; $i++)
    129 {
    130         $start_day .= '<option value="' . $i . '" >' . ( ($i == 0) ? ' -- ' : str_pad($i, 2, '0', STR_PAD_LEFT) ) . '</option>';
    131 }
    132 $start_day .= '</select>';
    133 
    134 // month list
    135 $start_month = '<select name="start_month">';
    136 $start_month .= '<option value="0"> ------------ </option>';
    137 for ($i=1; $i <= 12; $i++)
    138 {
    139         $start_month .= '<option value="' . $i . '">' . $lang['month'][$i] . '</option>';
    140 }
    141 $start_month .= '</select>';
    142 
    143 // day list
    144 $end_day = '<select name="end_day">';
    145 for ($i=0; $i <= 31; $i++)
    146 {
    147         $end_day .= '<option value="' . $i . '" >' . ( ($i == 0) ? ' -- ' : str_pad($i, 2, '0', STR_PAD_LEFT) ) . '</option>';
    148 }
    149 $end_day .= '</select>';
    150 
    151 // month list
    152 $end_month = '<select name="end_month">';
    153 $end_month .= '<option value="0"> ------------ </option>';
    154 for ($i=1; $i <= 12; $i++)
    155 {
    156         $end_month .= '<option value="' . $i . '">' . $lang['month'][$i] . '</option>';
    157 }
    158 $end_month .= '</select>';
     136/**
     137 * instantiate number list for days in a template block
     138 *
     139 * @param string blockname
     140 * @param string selection
     141 */
     142function get_day_list($blockname, $selection)
     143{
     144  global $template;
     145 
     146  $template->assign_block_vars(
     147    $blockname, array('SELECTED' => '', 'VALUE' => 0, 'OPTION' => '--'));
     148 
     149  for ($i = 1; $i <= 31; $i++)
     150  {
     151    $selected = '';
     152    if ($i == (int)$selection)
     153    {
     154      $selected = 'selected="selected"';
     155    }
     156    $template->assign_block_vars(
     157      $blockname, array('SELECTED' => $selected,
     158                        'VALUE' => $i,
     159                        'OPTION' => str_pad($i, 2, '0', STR_PAD_LEFT)));
     160  }
     161}
     162
     163/**
     164 * instantiate month list in a template block
     165 *
     166 * @param string blockname
     167 * @param string selection
     168 */
     169function get_month_list($blockname, $selection)
     170{
     171  global $template, $lang;
     172 
     173  $template->assign_block_vars(
     174    $blockname, array('SELECTED' => '',
     175                      'VALUE' => 0,
     176                      'OPTION' => '------------'));
     177
     178  for ($i = 1; $i <= 12; $i++)
     179  {
     180    $selected = '';
     181    if ($i == (int)$selection)
     182    {
     183      $selected = 'selected="selected"';
     184    }
     185    $template->assign_block_vars(
     186      $blockname, array('SELECTED' => $selected,
     187                        'VALUE' => $i,
     188                        'OPTION' => $lang['month'][$i]));
     189  }
     190}
     191
     192// start date
     193get_day_list('start_day', @$_POST['start_day']);
     194get_month_list('start_month', @$_POST['start_month']);
     195// end date
     196get_day_list('end_day', @$_POST['end_day']);
     197get_month_list('end_month', @$_POST['end_month']);
    159198
    160199//
     
    199238  'TODAY_MONTH' => date('m', time()),
    200239  'TODAY_YEAR' => date('Y', time()),
    201   'E_CALENDAR_MONTH' => $end_month,
    202         'E_CALENDAR_DAY' => $end_day,
    203   'S_CALENDAR_MONTH' => $start_month,
    204   'S_CALENDAR_DAY' => $start_day,
    205240  'S_SEARCH_ACTION' => add_session_id( 'search.php' ),   
    206241  'U_HOME' => add_session_id( 'category.php' )
     
    212247SELECT name,id,date_last,nb_images,global_rank,uppercats
    213248  FROM '.CATEGORIES_TABLE;
    214   if ($user['forbidden_categories'] != '')
    215   {
    216     $query.= '
     249if ($user['forbidden_categories'] != '')
     250{
     251  $query.= '
    217252  WHERE id NOT IN ('.$user['forbidden_categories'].')';
    218   }
     253}
    219254$query.= '
    220255;';
Note: See TracChangeset for help on using the changeset viewer.