2007-09-05 08:39:57 +00:00
< ? php
// $Id$
/**
* @ file
* User page callbacks for the search module .
*/
/**
* Menu callback ; presents the search form and / or search results .
*/
function search_view ( $type = 'node' ) {
// Search form submits with POST but redirects to GET. This way we can keep
// the search query URL clean as a whistle:
// search/type/keyword+keyword
if ( ! isset ( $_POST [ 'form_id' ])) {
if ( $type == '' ) {
// Note: search/node can not be a default tab because it would take on the
// path of its parent (search). It would prevent remembering keywords when
// switching tabs. This is why we drupal_goto to it from the parent instead.
drupal_goto ( 'search/node' );
}
$keys = search_get_keys ();
// Only perform search if there is non-whitespace search term:
$results = '' ;
if ( trim ( $keys )) {
2010-01-08 17:06:09 +00:00
// Log the search keys.
$info = search_get_info ();
watchdog ( 'search' , 'Searched %type for %keys.' , array ( '%keys' => $keys , '%type' => $info [ $type ][ 'title' ]), WATCHDOG_NOTICE , l ( t ( 'results' ), 'search/' . $type . '/' . $keys ));
2007-09-05 08:39:57 +00:00
2010-01-08 17:06:09 +00:00
// Collect the search results.
2007-09-05 08:39:57 +00:00
$results = search_data ( $keys , $type );
2009-07-29 06:39:35 +00:00
// Construct the search form.
$build [ 'search_form' ] = drupal_get_form ( 'search_form' , NULL , $keys , $type );
$build [ 'search_results' ] = array (
'#theme' => 'search_results_listing' ,
2009-12-12 20:49:34 +00:00
'#content' => $results ,
2009-07-29 06:39:35 +00:00
);
2007-09-05 08:39:57 +00:00
2009-07-29 06:39:35 +00:00
return $build ;
}
2007-09-05 08:39:57 +00:00
}
2007-12-06 09:51:01 +00:00
return drupal_get_form ( 'search_form' , NULL , empty ( $keys ) ? '' : $keys , $type );
2007-09-05 08:39:57 +00:00
}
2009-04-20 21:28:15 +00:00
/**
* Theme the listing of search results
*
2009-10-09 01:00:08 +00:00
* @ param $variables
* An associative array containing :
* - title : The subject of the listing .
* - content : The content of the listing .
*
2009-04-20 21:28:15 +00:00
* @ return
* A string containing the listing output .
*/
2009-10-09 01:00:08 +00:00
function theme_search_results_listing ( $variables ) {
$output = '<h2 class="title">' . $variables [ 'title' ] . '</h2><div>' . $variables [ 'content' ] . '</div>' ;
2009-04-20 21:28:15 +00:00
return $output ;
}
2007-09-05 08:39:57 +00:00
/**
2007-10-31 18:06:38 +00:00
* Process variables for search - results . tpl . php .
2007-09-05 08:39:57 +00:00
*
2007-10-31 18:06:38 +00:00
* The $variables array contains the following arguments :
* - $results
* - $type
2007-09-05 08:39:57 +00:00
*
2007-10-31 18:06:38 +00:00
* @ see search - results . tpl . php
2007-09-05 08:39:57 +00:00
*/
2007-10-31 18:06:38 +00:00
function template_preprocess_search_results ( & $variables ) {
$variables [ 'search_results' ] = '' ;
foreach ( $variables [ 'results' ] as $result ) {
2009-10-09 01:00:08 +00:00
$variables [ 'search_results' ] .= theme ( 'search_result' , array ( 'result' => $result , 'type' => $variables [ 'type' ]));
2007-09-05 08:39:57 +00:00
}
2009-10-09 01:00:08 +00:00
$variables [ 'pager' ] = theme ( 'pager' , array ( 'tags' => NULL ));
2010-01-13 05:40:03 +00:00
$variables [ 'theme_hook_suggestions' ][] = 'search_results__' . $variables [ 'type' ];
2007-09-05 08:39:57 +00:00
}
/**
2007-10-31 18:06:38 +00:00
* Process variables for search - result . tpl . php .
2007-09-05 08:39:57 +00:00
*
2007-10-31 18:06:38 +00:00
* The $variables array contains the following arguments :
* - $result
* - $type
2007-09-05 08:39:57 +00:00
*
2007-10-31 18:06:38 +00:00
* @ see search - result . tpl . php
2007-09-05 08:39:57 +00:00
*/
2007-10-31 18:06:38 +00:00
function template_preprocess_search_result ( & $variables ) {
$result = $variables [ 'result' ];
$variables [ 'url' ] = check_url ( $result [ 'link' ]);
$variables [ 'title' ] = check_plain ( $result [ 'title' ]);
2007-09-05 08:39:57 +00:00
$info = array ();
2007-10-31 18:06:38 +00:00
if ( ! empty ( $result [ 'type' ])) {
2007-12-05 19:12:59 +00:00
$info [ 'type' ] = check_plain ( $result [ 'type' ]);
2007-09-05 08:39:57 +00:00
}
2007-10-31 18:06:38 +00:00
if ( ! empty ( $result [ 'user' ])) {
$info [ 'user' ] = $result [ 'user' ];
2007-09-05 08:39:57 +00:00
}
2007-10-31 18:06:38 +00:00
if ( ! empty ( $result [ 'date' ])) {
2009-08-25 10:27:15 +00:00
$info [ 'date' ] = format_date ( $result [ 'date' ], 'short' );
2007-09-05 08:39:57 +00:00
}
2007-10-31 18:06:38 +00:00
if ( isset ( $result [ 'extra' ]) && is_array ( $result [ 'extra' ])) {
$info = array_merge ( $info , $result [ 'extra' ]);
2007-09-05 08:39:57 +00:00
}
2007-10-31 18:06:38 +00:00
// Check for existence. User search does not include snippets.
$variables [ 'snippet' ] = isset ( $result [ 'snippet' ]) ? $result [ 'snippet' ] : '' ;
// Provide separated and grouped meta information..
$variables [ 'info_split' ] = $info ;
$variables [ 'info' ] = implode ( ' - ' , $info );
2010-01-13 05:40:03 +00:00
$variables [ 'theme_hook_suggestions' ][] = 'search_result__' . $variables [ 'type' ];
2007-09-05 08:39:57 +00:00
}
/**
* As the search form collates keys from other modules hooked in via
* hook_form_alter , the validation takes place in _submit .
* search_form_validate () is used solely to set the 'processed_keys' form
* value for the basic search form .
*/
function search_form_validate ( $form , & $form_state ) {
2010-03-03 19:46:26 +00:00
form_set_value ( $form [ 'basic' ][ 'processed_keys' ], trim ( $form_state [ 'values' ][ 'keys' ]), $form_state );
2007-09-05 08:39:57 +00:00
}
/**
* Process a search form submission .
*/
function search_form_submit ( $form , & $form_state ) {
$keys = $form_state [ 'values' ][ 'processed_keys' ];
if ( $keys == '' ) {
form_set_error ( 'keys' , t ( 'Please enter some keywords.' ));
// Fall through to the drupal_goto() call.
}
$type = $form_state [ 'values' ][ 'module' ] ? $form_state [ 'values' ][ 'module' ] : 'node' ;
2010-03-12 02:29:23 +00:00
$form_state [ 'redirect' ] = $form_state [ 'action' ] . '/' . $keys ;
2007-09-05 08:39:57 +00:00
return ;
}