| 1 |
<?php
|
| 2 |
// $Id: flexisearch.module,v 1.00 2005-12-18 15:32:57 DublinDrupaller Exp $
|
| 3 |
|
| 4 |
|
| 5 |
/**
|
| 6 |
* Implementation of hook_help().
|
| 7 |
*/
|
| 8 |
function flexisearch_help($section = 'admin/help#flexisearch') {
|
| 9 |
switch ($section) {
|
| 10 |
case 'admin/modules#description':
|
| 11 |
return t('Enables keyword and field filter advanced searching of flexinodes. After enabling this module, click on %link to setup your flexisearch settings.', array('%link'=>l('admin/settings/flexisearch', 'admin/settings/flexisearch')));
|
| 12 |
case 'admin/settings/flexisearch':
|
| 13 |
return t('<P>Define which content fields are displayed on the flexisearch page and display some help text for your users.</p>');
|
| 14 |
case 'flexisearch':
|
| 15 |
return variable_get('flexisearch_help', '');
|
| 16 |
}
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implementation of hook_perm().
|
| 21 |
*/
|
| 22 |
function flexisearch_perm() {
|
| 23 |
return array('search flexinodes', 'administer flexisearch');
|
| 24 |
}
|
| 25 |
|
| 26 |
|
| 27 |
/**
|
| 28 |
* this function sets up the admin/settings/flexisearch settings page
|
| 29 |
*
|
| 30 |
*/
|
| 31 |
|
| 32 |
function flexisearch_settings() {
|
| 33 |
|
| 34 |
$group = form_textfield(t('Custom page title & menu name'), 'flexisearch_menu_title', variable_get('flexisearch_menu_title', 'Flexisearch'), 70, 70, t('Specify your own link name and page title for the flexisearch page. The default is Flexisearch.'));
|
| 35 |
$group .= form_textarea(t('Flexisearch help text (optional)'), 'flexisearch_help', variable_get('flexisearch_help', ''), 70, 5, t('This text will be displayed at the top of the flexisearch form. It is useful for helping or instructing your users.'));
|
| 36 |
$group .= form_select(t('Refresh your database search index (optional)'), 'flexisearch_index_reset', variable_get('flexisearch_index_reset', no), drupal_map_assoc(array(no, yes)), t('<p>Select whether you want to update your Search Index. Recommended if you are using flexisearch for the first time.</p><p>If selected, your search index will be updated on the next cron run and may take a few moments depending on how your <em>cron runs</em> are setup under the <a href="admin/settings/search">Search Admin Settings page</a>.</p>'));
|
| 37 |
|
| 38 |
$output = form_group(t('General settings'), $group);
|
| 39 |
$content_types = flexinode_content_types(); //loads up content types
|
| 40 |
foreach ($content_types as $ctype) {
|
| 41 |
$group ='';
|
| 42 |
$ctype = flexinode_load_content_type($ctype->ctype_id);
|
| 43 |
if (count($ctype->fields) > 1) {
|
| 44 |
foreach ($ctype->fields as $field) {
|
| 45 |
if ($field->field_type == 'textfield' || $field->field_type == 'textarea') {
|
| 46 |
$group .= form_checkbox(t($field->label), ('flexisearch_'. $field->field_id), 1, variable_get(('flexisearch_'. $field->field_id .''),0));
|
| 47 |
}
|
| 48 |
}
|
| 49 |
$output .= form_group(t($ctype->name),$group);
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
| 53 |
return $output;
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Implementation of hook_menu().
|
| 58 |
*/
|
| 59 |
function flexisearch_menu($may_cache) {
|
| 60 |
$items = array();
|
| 61 |
|
| 62 |
if ($may_cache) {
|
| 63 |
$items[] = array('path' => 'flexisearch', 'title' => t(variable_get('flexisearch_menu_title', 'Flexisearch')),
|
| 64 |
'callback' => 'flexisearch_view',
|
| 65 |
'access' => user_access('search flexinodes'),
|
| 66 |
'type' => MENU_DYNAMIC_ITEM);
|
| 67 |
|
| 68 |
}
|
| 69 |
return $items;
|
| 70 |
}
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Menu callback; presents the flexisearch form and/or flexisearch results.
|
| 74 |
*/
|
| 75 |
function flexisearch_view() {
|
| 76 |
$edit = ($_POST['edit']) ? $_POST['edit'] : array();
|
| 77 |
$ctype_id = arg(1);
|
| 78 |
$results = array();
|
| 79 |
if (user_access('search flexinodes')) {
|
| 80 |
foreach ($edit as $type => $search_str) {
|
| 81 |
if ((strlen(trim($search_str)) > 0) && (function_exists('do_search'))) {
|
| 82 |
$results[$type] = do_search($search_str, $type);
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
$themed_results = array();
|
| 87 |
// iterate over $results and theme output
|
| 88 |
foreach ($results as $type => $hits) {
|
| 89 |
foreach ($hits as $id) {
|
| 90 |
$node = node_load(array('nid' => $id));
|
| 91 |
$result['link'] = url('node/'.$id);
|
| 92 |
$result['title'] = $node->title;
|
| 93 |
$result['type'] = $node->type;
|
| 94 |
$result['date'] = max($node->created, $node->updated);
|
| 95 |
$field = preg_replace('/flexisearch/', 'flexinode', $type);
|
| 96 |
$result['snippet'] = search_excerpt($search_str, $node->$field);
|
| 97 |
$user = user_load(array('uid' => $node->uid));
|
| 98 |
$result['user'] = l($user->name, 'user/'.$user->uid);
|
| 99 |
$themed_results[] = theme('search_item', $result, $type);
|
| 100 |
}
|
| 101 |
}
|
| 102 |
if ($edit) { // check to see if a search has been run yet or not
|
| 103 |
if ($themed_results) { // if there are results, display them nicely.
|
| 104 |
foreach ($themed_results as $entry) {
|
| 105 |
$output .= $entry;
|
| 106 |
}
|
| 107 |
$output .= '</dl>';
|
| 108 |
$search_results .= theme('box', t('Flexisearch results'), $output);
|
| 109 |
}
|
| 110 |
else if (!$themed_results){
|
| 111 |
$search_results .= theme('box', t('Your search yielded no results'), search_help('search#noresults'));
|
| 112 |
}
|
| 113 |
else if (!isset($search_str)) {
|
| 114 |
form_set_error('keys', t('Please enter some keywords.'));
|
| 115 |
}
|
| 116 |
}
|
| 117 |
$search_results .= flexisearch_form($edit); //display the flexisearch form underneath the results for more intuitive viewing.
|
| 118 |
print theme('page', $search_results);
|
| 119 |
}
|
| 120 |
else {
|
| 121 |
drupal_access_denied();
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
|
| 126 |
/**
|
| 127 |
* Render a flexisearch form.
|
| 128 |
*/
|
| 129 |
function flexisearch_form($edit = null, $type = null) {
|
| 130 |
global $base_url;
|
| 131 |
$output = '';
|
| 132 |
$content_types = flexinode_content_types();
|
| 133 |
|
| 134 |
foreach ($content_types as $ctype) {
|
| 135 |
$ctype = flexinode_load_content_type($ctype->ctype_id);
|
| 136 |
$form = null;
|
| 137 |
foreach ($ctype->fields as $field) {
|
| 138 |
if (variable_get('flexisearch_'. $field->field_id, 0)) {
|
| 139 |
$name = 'flexisearch_'. $field->field_id;
|
| 140 |
$value = ($edit) ? $edit[$name] : '';
|
| 141 |
$form .= form_textfield($field->label, $name, $value, 60, 128);
|
| 142 |
}
|
| 143 |
}
|
| 144 |
if (!is_null($form)) {
|
| 145 |
$form .= form_submit(t('Search'));
|
| 146 |
$uri = request_uri();
|
| 147 |
$action = substr($uri, 0, strpos($uri, 'flexisearch') + strlen('flexisearch/')).'/'.$ctype->ctype_id;
|
| 148 |
$output .= form_group(t($ctype->name), form($form, 'post', $action));
|
| 149 |
}
|
| 150 |
}
|
| 151 |
return $output;
|
| 152 |
}
|
| 153 |
|
| 154 |
function flexisearch_search($op = 'search', $keys = null) {
|
| 155 |
switch ($op) {
|
| 156 |
case 'reset':
|
| 157 |
$last = variable_get('flexisearch_cron_last', 0);
|
| 158 |
return;
|
| 159 |
case 'status':
|
| 160 |
$last = variable_get('flexisearch_cron_last', 0);
|
| 161 |
|
| 162 |
$content_types = flexinode_content_types();
|
| 163 |
$remaining = 0;
|
| 164 |
foreach ($content_types as $ctype) {
|
| 165 |
$remaining += db_result(db_query("SELECT COUNT(*) FROM {node} n WHERE n.type = '%s' AND n.status = 1 AND n.moderate = 0 AND (n.created > %d OR n.changed > %d)", 'flexinode-'.$ctype->ctype_id, $last, $last));
|
| 166 |
}
|
| 167 |
|
| 168 |
$total = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type LIKE 'flexinode%%'"));
|
| 169 |
return array('remaining' => $remaining, 'total' => $total);
|
| 170 |
case 'search':
|
| 171 |
|
| 172 |
return;
|
| 173 |
}
|
| 174 |
}
|
| 175 |
|
| 176 |
function flexisearch_update_index() {
|
| 177 |
if (variable_get('flexisearch_index_reset', no) == "yes") {
|
| 178 |
search_wipe();
|
| 179 |
variable_set('node_cron_last', 0);
|
| 180 |
variable_set('flexisearch_index_reset', no);
|
| 181 |
}
|
| 182 |
$last = variable_get('flexisearch_cron_last', 0);
|
| 183 |
$limit = (int)variable_get('search_cron_limit', 100);
|
| 184 |
watchdog('flexisearch', 'last: '.$last);
|
| 185 |
watchdog('flexisearch', 'limit: '.$limit);
|
| 186 |
$content_types = flexinode_content_types();
|
| 187 |
|
| 188 |
foreach ($content_types as $ctype) {
|
| 189 |
$result = db_query_range("SELECT n.nid FROM {node} n WHERE n.type = '%s' AND n.status = 1 AND n.moderate = 0 AND (n.created > %d OR n.changed > %d) ORDER BY GREATEST(n.created, n.changed) ASC", 'flexinode-'.$ctype->ctype_id, $last, $last, 0, $limit);
|
| 190 |
|
| 191 |
while ($node = db_fetch_object($result)) {
|
| 192 |
$node = node_load(array('nid' => $node->nid));
|
| 193 |
$ctype = flexinode_load_content_type($ctype->ctype_id);
|
| 194 |
|
| 195 |
foreach ($ctype->fields as $field) {
|
| 196 |
$flexid = 'flexinode_'.$field->field_id;
|
| 197 |
if (variable_get('flexisearch_'.$field->field_id, 0)) {
|
| 198 |
$text = check_output($node->$flexid, $node->format);
|
| 199 |
search_index($node->nid, 'flexisearch_'.$field->field_id, $text);
|
| 200 |
}
|
| 201 |
}
|
| 202 |
|
| 203 |
// We update this variable per node in case cron times out, or if the node
|
| 204 |
// cannot be indexed (PHP nodes which call drupal_goto, for example).
|
| 205 |
// In rare cases this can mean a node is only partially indexed, but the
|
| 206 |
// chances of this happening are very small.
|
| 207 |
variable_set('flexisearch_cron_last', max($node->changed, $node->created));
|
| 208 |
}
|
| 209 |
}
|
| 210 |
}
|
| 211 |
|
| 212 |
|
| 213 |
/**
|
| 214 |
* Hook nodeapi
|
| 215 |
*
|
| 216 |
* We use it to clean the search index when nodes subject to flexisearching
|
| 217 |
* are deleted from the database.
|
| 218 |
*/
|
| 219 |
function flexisearch_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
|
| 220 |
if (substr($node->type, 0, 9) == 'flexinode') {
|
| 221 |
switch ($op) {
|
| 222 |
case 'delete':
|
| 223 |
// Remove flexisearch from the search index if needed.
|
| 224 |
$ctype = flexinode_load_content_type(substr($node->type, 10));
|
| 225 |
foreach ($ctype->fields as $field) {
|
| 226 |
$flexid = 'flexinode_'.$field->field_id;
|
| 227 |
if (variable_get('flexisearch_'.$field->field_id, 0)) {
|
| 228 |
if (function_exists('search_wipe')) {
|
| 229 |
search_wipe($node->nid, 'flexisearch_'. $field->field_id);
|
| 230 |
}
|
| 231 |
}
|
| 232 |
}
|
| 233 |
|
| 234 |
break;
|
| 235 |
}
|
| 236 |
}
|
| 237 |
}
|