Changeset 455 for trunk/search.php


Ignore:
Timestamp:
Jul 26, 2004, 10:43:46 PM (20 years ago)
Author:
z0rglub
Message:

new search form : finer with search on each field independantly

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/search.php

    r405 r455  
    2626// +-----------------------------------------------------------------------+
    2727
    28 //----------------------------------------------------------- include
     28//------------------------------------------------------------------- functions
     29// date_display displays 3 select input fields. The first one is the
     30// day of the month, from 0 to 31. The second is the month of the year,
     31// from 01 to 12. The last one is the year. The years displayed are the
     32// ones given by get_available_years (see function description in
     33// ./include/functions.inc.php).
     34function display_date($fieldname, $datefield)
     35{
     36  global $template;
     37
     38  // years
     39  for ($i = 1990; $i < 2006; $i++)
     40  {
     41    $selected = '';
     42    $key = $datefield.':year';
     43    if (isset($_POST[$key]) and $i == $_POST[$key])
     44    {
     45      $selected = ' selected="selected"';
     46    }
     47   
     48    $template->assign_block_vars(
     49      $fieldname.'year_option',
     50      array('OPTION'=>$i,
     51            'SELECTED'=>$selected
     52        ));
     53  }
     54  // months of year
     55  for ($i = 1; $i <= 12; $i++)
     56  {
     57    $selected = '';
     58    $key = $datefield.':month';
     59    if (isset($_POST[$key]) and $i == $_POST[$key])
     60    {
     61      $selected = ' selected="selected"';
     62    }
     63
     64    $template->assign_block_vars(
     65      $fieldname.'month_option',
     66      array('OPTION'=>sprintf('%02s', $i),
     67            'SELECTED'=>$selected
     68        ));
     69  }
     70  // days of the month
     71  for ($i = 1; $i <= 31; $i++)
     72  {
     73    $selected = '';
     74    $key = $datefield.':day';
     75    if (isset($_POST[$key]) and $i == $_POST[$key])
     76    {
     77      $selected = ' selected="selected"';
     78    }
     79
     80    $template->assign_block_vars(
     81      $fieldname.'day_option',
     82      array('OPTION'=>sprintf('%02s', $i),
     83            'SELECTED'=>$selected
     84        ));
     85  }
     86}
     87
     88function display_3dates($fieldname)
     89{
     90  display_date('datefield.', $fieldname);
     91  display_date('datefield.after_', $fieldname.'-after');
     92  display_date('datefield.before_', $fieldname.'-before');
     93}
     94//--------------------------------------------------------------------- include
    2995define('PHPWG_ROOT_PATH','./');
    3096include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );
    3197//-------------------------------------------------- access authorization check
    3298check_login_authorization();
     99//----------------------------------------------------------------- form fields
     100$textfields = array('file', 'name', 'comment', 'keywords', 'author');
     101$datefields = array('date_available', 'date_creation');
     102//------------------------------------------------------------------ form check
     103$errors = array();
     104$search = array();
     105$search['fields'] = array();
     106if (isset($_POST['submit']))
     107{
     108  $search['mode'] = $_POST['mode'];
     109
     110  foreach ($textfields as $textfield)
     111  {
     112    if (isset($_POST[$textfield.'-content'])
     113        and !preg_match('/^\s*$/', $_POST[$textfield.'-content']))
     114    {
     115      $local_search = array();
     116      $words = preg_split('/\s+/', $_POST[$textfield.'-content']);
     117      foreach ($words as $i => $word)
     118      {
     119        if (strlen($word) > 2 and !preg_match('/[,;:\']/', $word))
     120        {
     121          array_push($local_search, $word);
     122        }
     123        else
     124        {
     125          array_push($errors, $lang['invalid_search']);
     126        }
     127      }
     128      $local_search = array_unique($local_search);
     129      $search['fields'][$textfield] = array();
     130      $search['fields'][$textfield]['words'] = $local_search;
     131      if (count($local_search) > 1)
     132      {
     133        $search['fields'][$textfield]['mode'] = $_POST[$textfield.'-mode'];
     134      }
     135    }
     136  }
     137  foreach ($datefields as $datefield)
     138  {
     139    $suffixes = array('','-after','-before');
     140    foreach ($suffixes as $suffix)
     141    {
     142      $field = $datefield.$suffix;
     143      if (isset($_POST[$field.'-check']))
     144      {
     145        $year = $_POST[$field.':year'];
     146        $month = $_POST[$field.':month'];
     147        $day = $_POST[$field.':day'];
     148        $date = $year.'.'.$month.'.'.$day;
     149        if (!checkdate($month, $day, $year))
     150        {
     151          array_push($errors, $date.$lang['search_wrong_date']);
     152        }
     153        $search['fields'][$field] = array();
     154        $search['fields'][$field]['words'] = array($date);
     155        if ($suffix == '-after' or $suffix == '-before')
     156        {
     157          if (isset($_POST[$field.'-included']))
     158          {
     159            $search['fields'][$field]['mode'] = 'inc';
     160          }
     161        }
     162      }
     163    }
     164    if ($search['mode'] == 'AND')
     165    {
     166      // before date must be superior to after date
     167      if (isset($search['fields'][$datefield.'-before'])
     168          and isset($search['fields'][$datefield.'-after']))
     169      {
     170        $after = $search['fields'][$datefield.'-after']['words'][0];
     171        $before = $search['fields'][$datefield.'-before']['words'][0];
     172        if ($after >= $before)
     173        {
     174          array_push($errors, $lang['search_wrong_date_order']);
     175        }
     176      }
     177      // having "search is" and ("search is after" or "search is before") is
     178      // not coherent
     179      if (isset($search['fields'][$datefield])
     180          and (isset($search['fields'][$datefield.'-before'])
     181               or isset($search['fields'][$datefield.'-after'])))
     182      {
     183        array_push($errors, $lang['search_incoherent_date_search']);
     184      }
     185    }
     186  }
     187  if (isset($_POST['categories-check']))
     188  {
     189    $field = 'cat';
     190    $search['fields'][$field] = array();
     191    $search['fields'][$field]['words'] = $_POST['cat'];
     192    if (isset($_POST['subcats-included']))
     193    {
     194      $search['fields'][$field]['mode'] = 'sub_inc';
     195    }
     196  }
     197  // search string (for URL) creation
     198  $search_string = '';
     199  $tokens = array();
     200  foreach (array_keys($search['fields']) as $field)
     201  {
     202    $token = $field.':';
     203    $token.= implode(',', $search['fields'][$field]['words']);
     204    if (isset($search['fields'][$field]['mode']))
     205    {
     206      $token.= '~'.$search['fields'][$field]['mode'];
     207    }
     208    array_push($tokens, $token);
     209  }
     210  $search_string.= implode(';', $tokens);
     211  if (count($tokens) > 1)
     212  {
     213    $search_string.= '|'.$search['mode'];
     214  }
     215 
     216  if (count($tokens) == 0)
     217  {
     218    array_push($errors, $lang['search_one_clause_at_least']);
     219  }
     220}
    33221//----------------------------------------------------------------- redirection
    34 $error = array();
    35 if ( isset( $_POST['search'] ) )
    36 {
    37   $redirect = true;
    38   $search = array();
    39   $words = preg_split( '/\s+/', $_POST['search'] );
    40   foreach ( $words as $i => $word ) {
    41     if ( strlen( $word ) > 2 and !preg_match( '/[,;:\']/', $word ) )
    42     {
    43       array_push( $search, $word );
    44     }
    45     else
    46     {
    47       $redirect = false;
    48       array_push( $error, $lang['invalid_search'] );
    49       break;
    50     }
    51   }
    52   $search = array_unique( $search );
    53   $search = implode( ',', $search );
    54   if ( $redirect )
    55   {
    56     $url = 'category.php?cat=search&search='.$search.'&mode='.$_POST['mode'];
    57     $url = add_session_id( $url, true );
    58     redirect( $url );
    59   }
     222if (isset($_POST['submit']) and count($errors) == 0)
     223{
     224  $url = 'category.php?cat=search&search='.$search_string;
     225  $url = add_session_id($url, true);
     226  redirect($url);
    60227}
    61228//----------------------------------------------------- template initialization
     
    72239  'L_RETURN' => $lang['search_return_main_page'],
    73240  'L_SUBMIT' => $lang['submit'],
    74   'L_SEARCH'=>$lang['search_field_search'].' *',
    75241  'L_SEARCH_OR'=>$lang['search_mode_or'],
    76242  'L_SEARCH_AND'=>$lang['search_mode_and'],
     243  'L_SEARCH_OR_CLAUSES'=>$lang['search_or_clauses'],
     244  'L_SEARCH_AND_CLAUSES'=>$lang['search_and_clauses'],
     245  'L_SEARCH_CATEGORIES'=>$lang['search_categories'],
     246  'L_SEARCH_SUBCATS_INCLUDED'=>$lang['search_subcats_included'],
     247  'L_SEARCH_DATE_INCLUDED'=> $lang['search_date_included'],
     248  'L_SEARCH_DATE_IS'=>$lang['search_date_is'],
     249  'L_SEARCH_DATE_IS_AFTER'=>$lang['search_date_is_after'],
     250  'L_SEARCH_DATE_IS_BEFORE'=>$lang['search_date_is_before'],
    77251 
    78252  'F_ACTION' => add_session_id( 'search.php' ),
    79   'F_TEXT_VALUE' => isset($_POST['search'])?$_POST['search']:'',
    80253   
    81254  'U_HOME' => add_session_id( 'category.php' )
     
    83256);
    84257
     258//------------------------------------------------------------ text fields form
     259foreach ($textfields as $textfield)
     260{
     261  if (isset($_POST[$textfield.'-mode']))
     262  {
     263    if ($_POST[$textfield.'-mode'] == 'AND')
     264    {
     265      $and_checked = 'checked="checked"';
     266      $or_checked  = '';
     267    }
     268    else
     269    {
     270      $or_checked  = 'checked="checked"';
     271      $and_checked = '';
     272    }
     273  }
     274  else
     275  {
     276    $or_checked  = 'checked="checked"';
     277    $and_checked = '';
     278  }
     279
     280  $value = '';
     281  if (isset($_POST[$textfield.'-content']))
     282  {
     283    $value = $_POST[$textfield.'-content'];
     284  }
     285 
     286  $template->assign_block_vars(
     287    'textfield',
     288    array('NAME'=>$lang['search_'.$textfield],
     289          'L_NAME'=>$textfield,
     290          'VALUE'=>$value,
     291          'OR_CHECKED'=>$or_checked,
     292          'AND_CHECKED'=>$and_checked
     293          ));
     294}
     295//------------------------------------------------------------- date field form
     296foreach ($datefields as $datefield)
     297{
     298  $checked = '';
     299  if (isset($_POST[$datefield.'-check']))
     300  {
     301    $checked = ' checked="checked"';
     302  }
     303
     304  $after_checked = '';
     305  if (isset($_POST[$datefield.'-after-check']))
     306  {
     307    $after_checked = ' checked="checked"';
     308  }
     309
     310  $before_checked = '';
     311  if (isset($_POST[$datefield.'-before-check']))
     312  {
     313    $before_checked = ' checked="checked"';
     314  }
     315
     316  $after_included_check = '';
     317  if (isset($_POST[$datefield.'-after-included']))
     318  {
     319    $after_included_check = ' checked="checked"';
     320  }
     321
     322  $before_included_check = '';
     323  if (isset($_POST[$datefield.'-before-included']))
     324  {
     325    $before_included_check = ' checked="checked"';
     326  }
     327 
     328  $template->assign_block_vars(
     329    'datefield',
     330    array('NAME'=>$datefield,
     331          'L_NAME'=>$datefield,
     332          'CHECKED'=>$checked,
     333          'AFTER_CHECKED'=>$after_checked,
     334          'BEFORE_CHECKED'=>$before_checked,
     335          'AFTER_INCLUDED_CHECKED'=>$after_included_check,
     336          'BEFORE_INCLUDED_CHECKED'=>$before_included_check
     337          ));
     338  display_3dates($datefield);
     339}
     340//------------------------------------------------------------- categories form
     341function display_search_categories($categories, $indent, $selecteds)
     342{
     343  global $template,$user;
     344
     345  foreach ( $categories as $category )
     346  {
     347    if (!in_array($category['id'], $user['restrictions']))
     348    {
     349      $selected = '';
     350      if (in_array($category['id'], $selecteds))
     351      {
     352        $selected = ' selected="selected"';
     353      }
     354
     355      $template->assign_block_vars(
     356        'category_option',
     357        array('SELECTED'=>$selected,
     358              'VALUE'=>$category['id'],
     359              'OPTION'=>$indent.'- '.$category['name']
     360              ));
     361     
     362      display_search_categories( $category['subcats'],
     363                                 $indent.str_repeat('&nbsp;',3),
     364                                 $selecteds );
     365    }
     366  }
     367}
     368include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     369$page['plain_structure'] = get_plain_structure(true);
     370$structure = create_structure('');
     371
     372$selecteds = array();
     373if (isset($_POST['submit']))
     374{
     375  $selecteds = $_POST['cat'];
     376}
     377display_search_categories( $structure, '&nbsp;', $selecteds );
     378
     379$categories_selected = '';
     380if (isset($_POST['categories-check']))
     381{
     382  $categories_selected = 'checked="checked"';
     383}
     384
     385$categories_subcats_selected = '';
     386if (isset($_POST['subcats-included']))
     387{
     388  $categories_subcats_selected = 'checked="checked"';
     389}
     390
     391$template->assign_vars(
     392  array(
     393    'CATEGORIES_SELECTED'=>$categories_selected,
     394    'CATEGORIES_SUBCATS_SELECTED'=>$categories_subcats_selected
     395    )
     396  );
     397//---------------------------------------------------------------------- OR/AND
     398if (isset($_POST['mode']))
     399{
     400  if ($_POST['mode'] == 'AND')
     401  {
     402    $and_checked = 'checked="checked"';
     403    $or_checked  = '';
     404  }
     405  else
     406  {
     407    $or_checked  = 'checked="checked"';
     408    $and_checked = '';
     409  }
     410}
     411else
     412{
     413  $or_checked  = 'checked="checked"';
     414  $and_checked = '';
     415}
     416
     417$template->assign_vars(
     418  array(
     419    'OR_CHECKED'=>$or_checked,
     420    'AND_CHECKED'=>$and_checked
     421    )
     422  );
    85423//-------------------------------------------------------------- errors display
    86 if ( sizeof( $error ) != 0 )
     424if (sizeof($errors) != 0)
    87425{
    88426  $template->assign_block_vars('errors',array());
    89   for ( $i = 0; $i < sizeof( $error ); $i++ )
    90   {
    91     $template->assign_block_vars('errors.error',array('ERROR'=>$error[$i]));
     427  foreach ($errors as $error)
     428  {
     429    $template->assign_block_vars('errors.error',array('ERROR'=>$error));
    92430  }
    93431}
Note: See TracChangeset for help on using the changeset viewer.