Changeset 634


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)
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_category.inc.php

    r630 r634  
    416416        // SQL where clauses are stored in $clauses array during query
    417417        // construction
    418         $clauses = $temp_clauses = array();
    419         if (isset($search['fields']['allwords']))
    420     {
    421           $textfields = array('file', 'name', 'comment', 'keywords', 'author');
    422           foreach ($textfields as $textfield)
     418        $clauses = array();
     419       
     420        $textfields = array('file', 'name', 'comment', 'keywords', 'author');
     421        foreach ($textfields as $textfield)
     422        {
     423          if (isset($search['fields'][$textfield]))
    423424          {
    424425            $local_clauses = array();
    425             foreach ($search['fields']['allwords']['words'] as $word)
     426            foreach ($search['fields'][$textfield]['words'] as $word)
    426427            {
    427428              array_push($local_clauses, $textfield." LIKE '%".$word."%'");
     
    429430            // adds brackets around where clauses
    430431            array_walk($local_clauses,create_function('&$s','$s="(".$s.")";'));
    431             array_push($temp_clauses,
    432                        implode(' '.$search['fields']['allwords']['mode'].' ',
     432            array_push($clauses,
     433                       implode(' '.$search['fields'][$textfield]['mode'].' ',
    433434                               $local_clauses));
    434435          }
    435       array_push($clauses, implode(' OR ', $temp_clauses));
    436         }
    437    
    438     if (isset($search['fields']['author']))
    439     {
    440            array_push($clauses, "author LIKE '%".$search['fields']['author']['words'][0]."%'");
    441     }
     436        }
     437
     438        if (isset($search['fields']['allwords']))
     439        {
     440          $fields = array('file', 'name', 'comment', 'keywords');
     441          // in the OR mode, request bust be :
     442          // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
     443          // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
     444          //
     445          // in the AND mode :
     446          // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
     447          // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
     448          $word_clauses = array();
     449          foreach ($search['fields']['allwords']['words'] as $word)
     450          {
     451            $field_clauses = array();
     452            foreach ($fields as $field)
     453            {
     454              array_push($field_clauses, $field." LIKE '%".$word."%'");
     455            }
     456            // adds brackets around where clauses
     457            array_push($word_clauses, implode(' OR ', $field_clauses));
     458          }
     459          array_walk($word_clauses, create_function('&$s','$s="(".$s.")";'));
     460          array_push($clauses,
     461                     implode(' '.$search['fields']['allwords']['mode'].' ',
     462                               $word_clauses));
     463        }
    442464
    443465        $datefields = array('date_available', 'date_creation');
     
    447469          if (isset($search['fields'][$key]))
    448470          {
    449             $local_clause = $datefield." ";
    450       if (isset($search['fields'][$key]['mode']))
    451       {
    452         $local_clause .=">";
    453       }
    454       $local_clause .="= '";
    455       $local_clause.= str_replace('.', '-',
     471            $local_clause = $datefield." = '";
     472            $local_clause.= str_replace('.', '-',
    456473                                        $search['fields'][$key]['words'][0]);
    457       $local_clause.= "'";
    458       array_push($clauses, $local_clause);
    459      
    460       if (isset($search['fields'][$key]['mode']))
    461       {
    462         $end_sql_date = str_replace('.', '-',
    463                                         $search['fields'][$key]['mode']);
    464         $local_clause = $datefield." <= '".$end_sql_date."'";
    465         array_push($clauses, $local_clause);
    466       }
    467     }
    468   }
    469        
     474            $local_clause.= "'";
     475            array_push($clauses, $local_clause);
     476          }
     477
     478          foreach (array('after','before') as $suffix)
     479          {
     480            $key = $datefield.'-'.$suffix;
     481            if (isset($search['fields'][$key]))
     482            {
     483              $local_clause = $datefield;
     484              if ($suffix == 'after')
     485              {
     486                $local_clause.= ' >';
     487              }
     488              else
     489              {
     490                $local_clause.= ' <';
     491              }
     492              if (isset($search['fields'][$key]['mode'])
     493                  and $search['fields'][$key]['mode'] == 'inc')
     494              {
     495                $local_clause.= '=';
     496              }
     497              $local_clause.= " '";
     498              $local_clause.= str_replace('.', '-',
     499                                          $search['fields'][$key]['words'][0]);
     500              $local_clause.= "'";
     501              array_push($clauses, $local_clause);
     502            }
     503          }
     504        }
     505
    470506        if (isset($search['fields']['cat']))
    471507        {
     
    509545        // adds brackets around where clauses
    510546        array_walk($clauses, create_function('&$s', '$s = "(".$s.")";'));
    511         $page['where'] = 'WHERE '.implode(' AND ', $clauses);
     547        $page['where'] = 'WHERE '.implode(' '.$search['mode'].' ', $clauses);
    512548        if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;
    513549
  • 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;';
  • trunk/template/default/search.tpl

    r630 r634  
    3232    <td colspan="2"><b>{L_SEARCH_DATE} :</b><br /><span class="small">{L_SEARCH_DATE_HINT}</span></td>
    3333    <td colspan="2" valign="middle">
    34         <table><tr><td>
    35           {L_SEARCH_DATE_FROM} :</td><td>
    36           {S_CALENDAR_DAY}&nbsp;{S_CALENDAR_MONTH}&nbsp;<input name="start_year" type="text" size="4" maxlength="4">&nbsp;
    37           <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>
    38           </tr><tr><td>
    39           {L_SEARCH_DATE_TO} :</td><td>
    40           {E_CALENDAR_DAY}&nbsp;{E_CALENDAR_MONTH}&nbsp;<input name="end_year" type="text" size="4" maxlength="4">&nbsp;
    41           <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>
    42           </td></tr></table>
    43         </td>
     34      <table>
     35        <tr>
     36          <td>{L_SEARCH_DATE_FROM} :</td>
     37          <td>
     38            <select name="start_day">
     39              <!-- BEGIN start_day -->
     40              <option {start_day.SELECTED} value="{start_day.VALUE}">{start_day.OPTION}</option>
     41              <!-- END start_day -->
     42            </select>
     43            <select name="start_month">
     44              <!-- BEGIN start_month -->
     45              <option {start_month.SELECTED} value="{start_month.VALUE}">{start_month.OPTION}</option>
     46              <!-- END start_month -->
     47            </select>
     48            <input name="start_year" type="text" size="4" maxlength="4">&nbsp;
     49            <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>
     50          </td>
     51        </tr>
     52        <tr>
     53          <td>{L_SEARCH_DATE_TO} :</td>
     54          <td>
     55            <select name="end_day">
     56              <!-- BEGIN end_day -->
     57              <option {end_day.SELECTED} value="{end_day.VALUE}">{end_day.OPTION}</option>
     58              <!-- END end_day -->
     59            </select>
     60            <select name="end_month">
     61              <!-- BEGIN end_month -->
     62              <option {end_month.SELECTED} value="{end_month.VALUE}">{end_month.OPTION}</option>
     63              <!-- END end_month -->
     64            </select>
     65            <input name="end_year" type="text" size="4" maxlength="4">&nbsp;
     66            <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>
     67          </td>
     68        </tr>
     69      </table>
     70    </td>
    4471  </tr>
    4572  <tr class="admin">
Note: See TracChangeset for help on using the changeset viewer.