2007-09-05 08:39:57 +00:00
< ? php
/**
* @ file
* User page callbacks for the search module .
*/
/**
* Menu callback ; presents the search form and / or search results .
2010-08-05 07:11:15 +00:00
*
* @ param $module
* Search module to use for the search .
2010-08-11 14:21:39 +00:00
* @ param $keys
* Keywords to use for the search .
2007-09-05 08:39:57 +00:00
*/
2010-08-11 14:21:39 +00:00
function search_view ( $module = NULL , $keys = '' ) {
2010-08-05 07:11:15 +00:00
$info = FALSE ;
$redirect = FALSE ;
2010-08-11 14:21:39 +00:00
$keys = trim ( $keys );
// Also try to pull search keywords out of the $_REQUEST variable to
// support old GET format of searches for existing links.
if ( ! $keys && ! empty ( $_REQUEST [ 'keys' ])) {
$keys = trim ( $_REQUEST [ 'keys' ]);
}
2010-08-05 07:11:15 +00:00
if ( ! empty ( $module )) {
$active_module_info = search_get_info ();
if ( isset ( $active_module_info [ $module ])) {
$info = $active_module_info [ $module ];
}
}
if ( empty ( $info )) {
// No path or invalid path: find the default module. Note that if there
// are no enabled search modules, this function should never be called,
// since hook_menu() would not have defined any search paths.
$info = search_get_default_module_info ();
2010-08-11 14:21:39 +00:00
// Redirect from bare /search or an invalid path to the default search path.
$path = 'search/' . $info [ 'path' ];
if ( $keys ) {
$path .= '/' . $keys ;
}
drupal_goto ( $path );
2010-08-05 07:11:15 +00:00
}
2010-08-18 18:40:50 +00:00
// Default results output is an empty string.
$results = array ( '#markup' => '' );
2010-08-11 14:21:39 +00:00
// Process the search form. Note that if there is $_POST data,
// search_form_submit() will cause a redirect to search/[module path]/[keys],
// which will get us back to this page callback. In other words, the search
// form submits with POST but redirects to GET. This way we can keep
// the search query URL clean as a whistle.
if ( empty ( $_POST [ 'form_id' ]) || $_POST [ 'form_id' ] != 'search_form' ) {
$conditions = NULL ;
2011-12-15 17:33:38 +00:00
if ( isset ( $info [ 'conditions_callback' ])) {
2010-08-11 14:21:39 +00:00
// Build an optional array of more search conditions.
$conditions = call_user_func ( $info [ 'conditions_callback' ], $keys );
2007-09-05 08:39:57 +00:00
}
2010-08-11 14:21:39 +00:00
// Only search if there are keywords or non-empty conditions.
if ( $keys || ! empty ( $conditions )) {
2010-01-08 17:06:09 +00:00
// Log the search keys.
2011-07-04 16:58:33 +00:00
watchdog ( 'search' , 'Searched %type for %keys.' , array ( '%keys' => $keys , '%type' => $info [ 'title' ]), WATCHDOG_NOTICE , l ( t ( 'results' ), 'search/' . $info [ 'path' ] . '/' . $keys ));
2007-09-05 08:39:57 +00:00
2010-01-08 17:06:09 +00:00
// Collect the search results.
2010-08-11 14:21:39 +00:00
$results = search_data ( $keys , $info [ 'module' ], $conditions );
2009-07-29 06:39:35 +00:00
}
2007-09-05 08:39:57 +00:00
}
2010-08-11 14:21:39 +00:00
// The form may be altered based on whether the search was run.
$build [ 'search_form' ] = drupal_get_form ( 'search_form' , NULL , $keys , $info [ 'module' ]);
2010-08-18 18:40:50 +00:00
$build [ 'search_results' ] = $results ;
2007-09-05 08:39:57 +00:00
2010-08-11 14:21:39 +00:00
return $build ;
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 :
2010-11-21 18:42:25 +00:00
* - $results : Search results array .
* - $module : Module the search results came from ( module implementing
* hook_search_info ()) .
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' ] = '' ;
2010-08-18 18:40:50 +00:00
if ( ! empty ( $variables [ 'module' ])) {
$variables [ 'module' ] = check_plain ( $variables [ 'module' ]);
}
2007-10-31 18:06:38 +00:00
foreach ( $variables [ 'results' ] as $result ) {
2010-08-18 18:40:50 +00:00
$variables [ 'search_results' ] .= theme ( 'search_result' , array ( 'result' => $result , 'module' => $variables [ 'module' ]));
2007-09-05 08:39:57 +00:00
}
2009-10-09 01:00:08 +00:00
$variables [ 'pager' ] = theme ( 'pager' , array ( 'tags' => NULL ));
2010-08-18 18:40:50 +00:00
$variables [ 'theme_hook_suggestions' ][] = 'search_results__' . $variables [ 'module' ];
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
2010-08-18 18:40:50 +00:00
* - $module
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 ) {
2010-11-21 20:36:36 +00:00
global $language ;
2007-10-31 18:06:38 +00:00
$result = $variables [ 'result' ];
$variables [ 'url' ] = check_url ( $result [ 'link' ]);
$variables [ 'title' ] = check_plain ( $result [ 'title' ]);
2010-11-21 20:36:36 +00:00
if ( isset ( $result [ 'language' ]) && $result [ 'language' ] != $language -> language && $result [ 'language' ] != LANGUAGE_NONE ) {
2011-11-05 05:13:18 +00:00
$variables [ 'title_attributes_array' ][ 'lang' ] = $result [ 'language' ];
$variables [ 'content_attributes_array' ][ 'lang' ] = $result [ 'language' ];
2010-11-21 20:36:36 +00:00
}
2007-10-31 18:06:38 +00:00
2007-09-05 08:39:57 +00:00
$info = array ();
2010-08-18 18:40:50 +00:00
if ( ! empty ( $result [ 'module' ])) {
$info [ 'module' ] = check_plain ( $result [ 'module' ]);
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-08-18 18:40:50 +00:00
$variables [ 'theme_hook_suggestions' ][] = 'search_result__' . $variables [ 'module' ];
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.' ));
2010-08-05 07:11:15 +00:00
// Fall through to the form redirect.
2007-09-05 08:39:57 +00:00
}
2010-03-12 02:29:23 +00:00
$form_state [ 'redirect' ] = $form_state [ 'action' ] . '/' . $keys ;
2007-09-05 08:39:57 +00:00
return ;
}