t('search'),
'description' => t('Search Terms: Posts that match a content search.'),
'include' => 'search',
'prefix' => t('Search for "'),
'suffix' => t('"'),
'weight' => -10,
'form' => TRUE,
'label' => t('Search Terms'),
'help' => t('You may select from previous searches, or run a new search below.'),
'search' => FALSE,
'threshold' => SEARCH_THRESHOLD
);
if ($get_options) {
$type['options'] = mysite_type_search_options();
}
return $type;
}
}
/**
* Set the options for this type
*/
function mysite_type_search_options() {
// add this to the form, even if no options have been saved
$options = array('true');
// this will come later
$data = mysite_get_custom('search', NULL, TRUE);
// since $all = TRUE, this returns an array
if (is_array($data) && !empty($data)) {
foreach ($data as $search) {
if ($i < SEARCH_THRESHOLD) {
$options['group'][] = '';
$options['name'][] = $search->title;
$options['type_id'][] = $search->myid;
$options['type'][] = 'search';
$i++;
}
}
}
return $options;
}
/**
* Return the title if given an id
*/
function mysite_type_search_title($type_id = NULL, $title = NULL) {
if(!empty($type_id)) {
if(is_null($title)) {
$data = mysite_get_custom('search', $type_id, FALSE);
$title = $data->title;
}
$type = mysite_type_search();
$title = $type['prefix'] . $title . $type['suffix'];
$title = trim(rtrim($title));
return $title;
}
drupal_set_message(t('Could not find title'), 'error');
return;
}
/**
* Return the page data for a requested id
*/
function mysite_type_search_data($type_id = NULL) {
// we need to track positive search hits so we can wipe the temporary tables
global $mysite_search_loops;
if(!empty($type_id)) {
$tids = '';
$data = mysite_get_custom('search', $type_id);
$keywords = $data->type_key;
// search.module creates temporary tables, we must explicitly DROP them
// to perform more than one search on a single page view.
if ($mysite_search_loops > 0) {
if (($count = db_result(db_query('SELECT COUNT(*) FROM temp_search_results'))) > 0) {
db_query_temporary("DROP table temp_search_results");
}
if (($count = db_result(db_query('SELECT COUNT(*) FROM temp_search_sids'))) > 0) {
db_query_temporary("DROP table temp_search_sids");
}
}
// since we are searching nodes, better to use node_search instead of do_search
$result = node_search('search', $keywords);
// if matches, then increment the counter so we can wipe the tables
if (count($result) > 0) {
$mysite_search_loops++;
}
// slice the array to limit results
$nodes = array_slice($result, 0, variable_get('mysite_elements', 5));
$data = array(
'base' => 'taxonomy/term/' . $data->type_key,
'xml' => 'taxonomy/term/' . $data->type_key . '/0/feed'
);
$items = array();
$i = 0;
// we can't use node_view() here, because aggregator doesn't use nodes
// and we need the same data structure for all content
foreach ($nodes as $nid) {
// node_search returns an array, not an object
$node = (object) $nid;
$items[$i]['type'] = $node->type;
$items[$i]['link'] = l($node->title, 'node/' . $node->nid);
$items[$i]['title'] = check_plain($node->title);
$items[$i]['subtitle'] = NULL;
$items[$i]['date'] = $node->changed;
$items[$i]['uid'] = $node->uid;
$items[$i]['author'] = strip_tags($node->user); // we need this as plain text
$items[$i]['teaser'] = $node->snippet; // check_markup has been handled by node.module
$items[$i]['nid'] = $node->nid;
$i++;
}
$data['items'] = $items;
return $data;
}
drupal_set_message(t('Could not find data'), 'error');
return;
}
/**
* Return block settings for this type
*/
function mysite_type_search_block($arg, $op = 'view') {
global $user;
// search-by-taxo uses the syntax /taxonomy/term/1,2,4
$terms = explode(',', $arg[2]);
$name = '';
if (user_access('access content') && $arg[0] == 'taxonomy' && $arg[1] == 'term' && count($terms) > 1) {
foreach ($terms as $term) {
$taxo[$term] = taxonomy_get_term($term);
$name .= $taxo[$term]->name . ', ';
}
$name = rtrim($name, ', ');
$myid = mysite_get_myid('search', $arg[2], $name);
$data = array();
$data['uid'] = $user->uid;
$data['type'] = 'search';
$data['type_id'] = $myid;
$data['title'] = mysite_type_search_title($myid, NULL);
$content = mysite_block_handler($data);
return $content;
}
}
/**
* Cron cleanup function
*/
// not used
/**
* Form element distinct to this type
* Add an XML feed direcly through mysite
*/
function mysite_type_search_form($owner) {
if(empty($owner)) {
global $user;
$owner = $user;
}
$output .= drupal_get_form('mysite_type_search_custom_form', $owner);
return $output;
}
/**
* FormsAPI for mysite_type_feed
*/
function mysite_type_search_custom_form($owner) {
$form['new_search'] = array(
'#type' => 'fieldset',
'#title' => t('Add New Search'),
'#weight' => 5,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['new_search']['keywords'] = array('#type' => 'textfield',
'#title' => t('Keywords'),
'#maxlength' => 64,
'#description' => t('Enter your search terms.'),
'#required' => FALSE,
);
$form['new_search']['logic'] = array('#type' => 'textfield',
'#title' => t('Search Logic'),
'#default_value' => 'any',
'#type' => 'radios',
'#options' => array('any' => 'Containing any of the words', 'all' => 'Containing all the words'),
'#description' => t('Find any or all keywords?'),
'#required' => FALSE,
);
$form['new_search']['uid'] = array(
'#type' => 'hidden', '#value' => $owner->uid
);
$form['new_search']['submit'] = array('#type' => 'submit', '#value' =>t('Submit New Search'));
return $form;
}
/**
* Validation for mysite_type_search_custom_form
*/
function mysite_type_search_custom_form_validate($form_id, $form_values) {
$keywords = search_simplify($form_values['keywords']);
$min = variable_get('minimum_word_size', 3);
if (strlen($keywords) < $min) {
form_set_error('keywords', t('The search terms must be at least @min characters.', array('@min' => $min)));
}
}
/**
* Submit for mysite_type_search_custom_form
*/
function mysite_type_search_custom_form_submit($form_id, $form_values) {
$keywords = search_simplify($form_values['keywords']);
$search['keywords'] = search_index_split($keywords);
$search['type'] = 'all';
if ($form_values['logic'] == 'any' || count($search['keywords']) == 1) {
$search['type'] = 'any';
}
$myid = mysite_get_myid('search', $keywords, $keywords, serialize($search));
if ($myid > 0) {
drupal_set_message(t('New content group added.'));
mysite_content_add($form_values['uid'], 'search', $myid, mysite_type_search_title($myid, $keywords));
}
else {
drupal_set_message(t('Insert failed'), 'error');
}
}