Drupal: Custom search with filters

This CodeLet has a search box with three checkboxes as filters. Depending on the fitler selected, result will be fetched. By default, this CodeLet will dispaly 10 most recent results from groups and users

CodeLet: 

/**
* @file
*
* Provides a page to search for groups, users and service providers
*
* @author DrupalD
*/

/**
* Implemenation of hoook_menu
*
* @author DrupalD
*/
function invest_search_menu($ss__keyword = '') {
$items['directory'] = array(
'title' => t('Directory'),
'menu_name' => 'main-menu',
'page callback' => 'invest_search_page',
'page arguments' => array(1),
'access callback' => TRUE,
'weight' => -5,
);

return $items;
}

/**
* invest_search_page
*/
function invest_search_page($ss__keyword= '') {
if (!empty($ss__keyword)) {
list($key, $type) = explode('type:', $ss__keyword);
}
$build['invest_search_form'] = drupal_get_form('invest_search_form', $ss__keyword);
$ss__keyword = ($type == '') ? $key .'type: group users': $key . 'type:'. $type;
$build['search_result'] = array('' => _get_invest_search_result($ss__keyword));
return drupal_render($build);
}

/**
* invest_search_form
*
* Search form for searching the content
*
* @author DrupalD
*/
function invest_search_form($form, &$form_sate, $ss__keyword = '') {
$form = array();
if (!empty($ss__keyword)) {
list($key, $type) = explode('type:', $ss__keyword);
}

$form[''] = 'directory';
$form['name'] = array(
'' => 'hidden',
'' => 'invest_search_page',
);

$form['search_type'] = array(
'' => 'checkboxes',
'' => t('Search in'),
'' => array('width' => 3),
'' => !empty($type) ? explode(' ', $type) : array('group', 'users'),
'' => array(
'group' => t('Groups'),
'users' => t('Users'),
'provider' => t('Service provider'),
),
'' => -9,
);

$form['search_keywrod'] = array(
'' => 'textfield',
'' => t('Search'),
'' => $key,
'' => -10,
);

$form['search_submit'] = array(
'' => 'submit',
'' => t('Search'),
'' => -8,
);

$form[''] = array('invest_search_submit');
return $form;
}

/**
* invest_search_submit
*
* @author DrupalD
*/
function invest_search_submit($form, &$form_state) {
$ss__keyword = $form_state['values']['search_keywrod'];
$ss__type = implode(' ', $form_state['values']['search_type']);
$keys = $ss__keyword .'type:'. $ss__type;
if ($ss__type == '0 0 0') {
form_set_error('search_type', t('Please select a type to search in.'));
}

$form_state['redirect'] = $form_state['complete form']['']
. '/' . $keys;
}

/**
* _get_invest_search_result
*
* @param unknown_type $ss__keyword
* @author DrupalD
*/
function _get_invest_search_result($ss__keyword) {
$type = '';
$header = array(t('Name'), t('Body'), t('Is service provider?'));

if (!empty($ss__keyword)) {
unset($type);
list($key, $type) = explode('type:', $ss__keyword);
$key = trim($key);
}

if (strstr($type, 'group') != '' || strstr($type, 'provider') != '') {
unset($row);
$group_query = db_select('node', 'n');
$group_query->leftjoin('field_data_body', 'fdr', 'n.nid = fdr.entity_id');
$group_query->leftjoin('field_data_field_service_provider', 'fsp', 'n.nid = fsp.entity_id');
if (strstr($type, 'provider') != '') {
$group_query->condition('field_service_provider_value', '1', '=');
}

$group_query->fields('n', array('nid', 'title'))
->fields('fdr', array('body_value'))
->fields('fsp', array('service_provider' => 'field_service_provider_value'))
->condition('type', 'group', '=')
->condition('fsp.bundle', 'group', '=')
->condition('n.status', '1', '=');
if ($key != '') {
$group_query->where("((title LIKE '%". $key ."%') OR (body_value LIKE '%". $key ."%'))");
}

$group_query->orderBy('title', 'ASC');
$group_result = $group_query->extend('PagerDefault')->limit(5);
$group_result = $group_query->execute();
while($group_record = $group_result->fetchAssoc()) {
$row[] = array(
l($group_record['title'], 'node/'. $group_record['nid']),
$group_record['body_value'],
$group_record['field_service_provider_value'] == 1 ? t('Yes') : t('No'),
);
}
}

if (strstr($type, 'users') != '' || strstr($type, 'provider') != '') {
$user__query = db_select('users', 'u');
$user__query->leftjoin('field_data_field_notes', 'fdn', 'u.uid = fdn.entity_id');
$user__query->leftjoin('field_data_field_service_provider', 'fsp', 'u.uid = fsp.entity_id');
if (strstr($type, 'provider') != '') {
$user__query->condition('field_service_provider_value', '1', '=');
}
$user__query->fields('u', array('name', 'uid'))
->fields('fdn', array('field_notes_value'))
->fields('fsp', array('field_service_provider_value'))
->condition('fsp.bundle', 'user', '=');
if ($key != '') {
$user__query->where("((name LIKE '%". $key ."%') OR (field_notes_value LIKE '%". $key ."%'))");
}
$user__query->orderBy('name', 'ASC');
$user__query = $user__query->extend('PagerDefault')->limit(5);
$user__query = $user__query->execute();
while($user__record = $user__query->fetchAssoc()) {
$row[] = array(
l($user__record['name'], 'user/'. $user__record['uid']),
$user__record['field_notes_value'],
$user__record['field_service_provider_value'] == '1' ? t('Yes') : t('No'),
);
}
}

$output .= theme('table', array('header' => $header, 'rows' => $row, 'empty' => t('No records found'), 'caption' => ''. t('Result') .''));
$output .= theme('pager', array('tags' => array()));
return $output;
}

Drupal: Custom search with filters | Drupal Developer

By DrupalD on Wed, 24/04/2013 - 12:16pm

Add comment

Log in or register to post comments
QR code for this page URL This page URL
Feedback