| 1 |
<?php
|
| 2 |
// $Id: node_quick_find.module,v 1.2 2008/04/05 23:54:36 njt1982 Exp $
|
| 3 |
|
| 4 |
/*
|
| 5 |
* @file
|
| 6 |
* The Node Quick Find module provides a block which contains an autocompleting textfield which matches user input
|
| 7 |
* against the beginning of node titles. This means if a user enters 'test', NQF will match 'test node' but NOT 'node test'.
|
| 8 |
*
|
| 9 |
* Once a user selects the node title and submits, they are taken to the node.
|
| 10 |
*
|
| 11 |
* This is a handy module if the user knows exactly what they're looking for.
|
| 12 |
*/
|
| 13 |
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_block().
|
| 17 |
*/
|
| 18 |
function node_quick_find_block($op = 'list', $delta = 0, $edit = array()) {
|
| 19 |
switch ($op) {
|
| 20 |
case 'list' :
|
| 21 |
return array(
|
| 22 |
array('info' => t('Node Quick Find'))
|
| 23 |
);
|
| 24 |
|
| 25 |
case 'configure' :
|
| 26 |
return _node_quick_find_block_configuration($delta);
|
| 27 |
|
| 28 |
|
| 29 |
case 'save' :
|
| 30 |
$edit['settings']['node_types'] = array_filter($edit['settings']['node_types']);
|
| 31 |
variable_set('node_quick_find_'. $delta, $edit['settings']);
|
| 32 |
break;
|
| 33 |
|
| 34 |
case 'view' :
|
| 35 |
return array(
|
| 36 |
'subject' => t('Quick Find'),
|
| 37 |
'content' => drupal_get_form('node_quick_find_block_contents_form', $delta),
|
| 38 |
);
|
| 39 |
}
|
| 40 |
}
|
| 41 |
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Implementation of hook_menu().
|
| 45 |
*/
|
| 46 |
function node_quick_find_menu() {
|
| 47 |
$items = array();
|
| 48 |
|
| 49 |
$items['node_quick_find/autocomplete'] = array(
|
| 50 |
'title' => 'Node autocomplete',
|
| 51 |
'type' => MENU_CALLBACK,
|
| 52 |
'access arguments' => array('access content'),
|
| 53 |
'page callback' => 'node_quick_find_autocomplete',
|
| 54 |
);
|
| 55 |
|
| 56 |
return $items;
|
| 57 |
}
|
| 58 |
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Wrapper function to build the settings form
|
| 62 |
*
|
| 63 |
* @param mixed $delta
|
| 64 |
* The $delta represents the unique ID of the block from this module
|
| 65 |
*
|
| 66 |
* @return array
|
| 67 |
* Array for use with the Drupal FAPI
|
| 68 |
*/
|
| 69 |
function _node_quick_find_block_configuration($delta) {
|
| 70 |
$form = array();
|
| 71 |
$settings = variable_get('node_quick_find_'. $delta, array());
|
| 72 |
|
| 73 |
$form['settings'] = array(
|
| 74 |
'#type' => 'fieldset',
|
| 75 |
'#title' => t('NQF Settings'),
|
| 76 |
'#tree' => TRUE,
|
| 77 |
);
|
| 78 |
|
| 79 |
$form['settings']['node_types'] = array(
|
| 80 |
'#type' => 'checkboxes',
|
| 81 |
'#title' => t('Node Type Filter'),
|
| 82 |
'#required' => TRUE,
|
| 83 |
'#options' => node_get_types('names'),
|
| 84 |
'#default_value' => isset($settings['node_types']) ? $settings['node_types'] : array(),
|
| 85 |
);
|
| 86 |
|
| 87 |
$form['settings']['field_title'] = array(
|
| 88 |
'#type' => 'textfield',
|
| 89 |
'#title' => t('Text Field Title'),
|
| 90 |
'#size' => 16,
|
| 91 |
'#default_value' => isset($settings['field_title']) ? $settings['field_title'] : 'Title',
|
| 92 |
);
|
| 93 |
|
| 94 |
$form['settings']['field_size'] = array(
|
| 95 |
'#type' => 'textfield',
|
| 96 |
'#title' => t('Text Field Size'),
|
| 97 |
'#size' => 3,
|
| 98 |
'#default_value' => isset($settings['field_size']) ? $settings['field_size'] : 16,
|
| 99 |
);
|
| 100 |
|
| 101 |
$form['settings']['field_error'] = array(
|
| 102 |
'#type' => 'textfield',
|
| 103 |
'#title' => t('Error Message'),
|
| 104 |
'#size' => 25,
|
| 105 |
'#default_value' => isset($settings['field_error']) ? $settings['field_error'] : 'Page Not Found',
|
| 106 |
);
|
| 107 |
|
| 108 |
return $form;
|
| 109 |
}
|
| 110 |
|
| 111 |
|
| 112 |
/**
|
| 113 |
* Block Contents Form. This provides the quick-find autocompelte area
|
| 114 |
*/
|
| 115 |
function node_quick_find_block_contents_form(&$form_state, $delta) {
|
| 116 |
$form = array();
|
| 117 |
$settings = variable_get('node_quick_find_'. $delta, array());
|
| 118 |
|
| 119 |
$form['title'] = array(
|
| 120 |
'#title' => t(isset($settings['field_title']) ? check_plain($settings['field_title']) : 'Title'),
|
| 121 |
'#type' => 'textfield',
|
| 122 |
'#autocomplete_path' => 'node_quick_find/autocomplete/'. $delta,
|
| 123 |
'#size' => isset($settings['field_size']) ? $settings['field_size'] : 16,
|
| 124 |
);
|
| 125 |
|
| 126 |
$form['delta'] = array('#type' => 'value', '#value' => $delta);
|
| 127 |
|
| 128 |
$form['submit'] = array(
|
| 129 |
'#type' => 'submit',
|
| 130 |
'#value' => t('Submit'),
|
| 131 |
);
|
| 132 |
|
| 133 |
return $form;
|
| 134 |
}
|
| 135 |
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Submit handler for node_quick_find_block form above
|
| 139 |
*/
|
| 140 |
function node_quick_find_block_contents_form_submit($form, &$form_state) {
|
| 141 |
$settings = variable_get('node_quick_find_'. $delta, array());
|
| 142 |
|
| 143 |
if (empty($settings['node_types'])) {
|
| 144 |
$nid = db_result(db_query('SELECT nid FROM {node} WHERE title = "%s"', $form_state['values']['title']));
|
| 145 |
}
|
| 146 |
else {
|
| 147 |
$nid = db_result(db_query('SELECT nid FROM {node} WHERE type IN('. db_placeholders($settings['node_types'], 'varchar') .') AND title = "%s"', array_marge($settings['node_types'], array($form_state['values']['title']))));
|
| 148 |
}
|
| 149 |
|
| 150 |
|
| 151 |
if ($nid) {
|
| 152 |
$form_state['redirect'] = 'node/'. $nid;
|
| 153 |
return;
|
| 154 |
}
|
| 155 |
else {
|
| 156 |
$settings['field_error'] = empty($settings['field_error']) ? 'Page Not Found' : $settings['field_error'];
|
| 157 |
drupal_set_message(t($settings['field_error'] .': %title', array('%title' => $form_state['values']['title'])), 'error');
|
| 158 |
drupal_not_found();
|
| 159 |
exit();
|
| 160 |
}
|
| 161 |
}
|
| 162 |
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Autocomplete callback
|
| 166 |
*
|
| 167 |
* @param mixed $delta
|
| 168 |
* Provides the unqiue ID for the block provided by this module
|
| 169 |
* @param string $string
|
| 170 |
* The string to lookup
|
| 171 |
*/
|
| 172 |
function node_quick_find_autocomplete($delta = 0, $string = '') {
|
| 173 |
$settings = variable_get('node_quick_find_'. $delta, array());
|
| 174 |
|
| 175 |
$matches = array();
|
| 176 |
if ($string) {
|
| 177 |
$string = drupal_strtolower($string);
|
| 178 |
if (empty($settings['node_types'])) {
|
| 179 |
$result = db_query_range('SELECT title FROM {node} WHERE title LIKE "%s%%"', $string, 0, 10);
|
| 180 |
}
|
| 181 |
else {
|
| 182 |
$result = db_query_range('SELECT title FROM {node} WHERE type IN('. db_placeholders($settings['node_types'], 'varchar') .') AND title LIKE "%s%%"', array_merge($settings['node_types'], array($string)), 0, 10);
|
| 183 |
}
|
| 184 |
while ($node = db_fetch_object($result)) {
|
| 185 |
$matches[$node->title] = check_plain($node->title);
|
| 186 |
}
|
| 187 |
}
|
| 188 |
print drupal_to_js($matches);
|
| 189 |
exit();
|
| 190 |
}
|