highlight.module in Highlight 5
Same filename and directory in other branches
File
highlight.moduleView source
<?php
/* This module creates a link on search results pages and if highlighting is
enabled for the field (check your input types), it will search and replace
the search term in that field with an admin defined string
*/
/*
* @TODO add back the "first,second,third" functionality into the replace function
* @TODO add admin options for the set message function that the 4.6 version had
*/
/* *********************************************** */
/* Drupal Hooks */
/* *********************************************** */
/**
* Implementation of hook_help
*/
function highlight_help($section) {
switch ($section) {
case 'admin/modules#description':
$output .= t('A helper module to highlight terms on pages, as in search results.');
break;
}
return $output;
}
function highlight_info() {
return array(
'highlight' => array(
'name' => t('highlight'),
'module' => 'highlight',
'description' => t("A helper module to highlight terms on pages, as in search results."),
),
);
}
/**
* Implementation of hook_menu
*/
function highlight_menu($may_cache) {
$items[] = array(
'path' => 'admin/settings/highlight',
'title' => t('Highlight'),
'description' => t('Configure the highlight module'),
'callback' => 'drupal_get_form',
'callback arguments' => 'highlight_admin_settings',
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implementation of hook_perm
*/
function highlight_perm() {
return array(
'administer search_highlight',
);
}
/**
* Implementation of admin settings
*/
function highlight_admin_settings() {
$form['highlight'] = array(
'#type' => 'fieldset',
'#title' => t('Search Highlight Configuration'),
);
$form['highlight']['hl_replace'] = array(
'#type' => 'textfield',
'#title' => t('Replacement string'),
'#default_value' => variable_get('hl_replace', '<strong class="highlight">%key%</strong>'),
'#description' => t('This string will be used to replace the found value <strong>key</strong> with. <strong>%key%</strong> is variable.'),
);
$form['highlight']['hl_allow_external'] = array(
'#type' => 'checkbox',
'#title' => t('Allow external sites to highlight'),
'#default_value' => variable_get('hl_allow_external', false),
'#description' => t('If this is checked, sites out side of yours will be able to highlight items on your site. <br /> <em>Defaults to off</em>'),
);
$form['highlight']['sh_use_css'] = array(
'#type' => 'checkbox',
'#title' => t('Use Module CSS'),
'#default_value' => variable_get('sh_use_css', true),
'#description' => t('If you have customized your CSS for the <em>highlight</em> class, uncheck this box to prevent the module from overriding your CSS.'),
);
return system_settings_form($form);
}
// This is the old highlight function, replacing this with the filter version
/*
function highlight_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
static $message_given = false;
switch ($op) {
case 'view':
if (!empty($_REQUEST['highlight'])) {
$highlights = explode(',', $_REQUEST['highlight']);
$count = 0;
foreach ($highlights as $highlight) {
if($highlight != '') {
if ($count == 3) {
break;
}
if (stristr($node->body, $highlight) || stristr($node->title, $highlight)) {
$key[] = $highlight;
$key[] = ucfirst($highlight);
$found[] = '<strong class="highlight' . $count . '">' . $highlight . '</strong>';
$replace[] = '<strong class="highlight' . $count . '">' . $highlight . '</strong>';
$replace[] = '<strong class="highlight' . $count . '">' . ucfirst($highlight) . '</strong>';
$count++;
}
}
}
if (count($key) > 0) {
$node->body = str_replace($key, $replace, $node->body);
$node->title = str_replace($key, $replace, $node->title);
if(!($message_given)) {
$message = count($key) > 1 ? t('The following terms have been highlighted: ') : t('The following term has been highlighted: ');
drupal_set_message($message . implode(' ', $found));
$message_given = true;
highlight_set_css();
}
}
}
}
}
*/
/**
* Implemnatation of highlight
*/
function highlight_filter_tips($delta, $format, $long = FALSE) {
switch ($delta) {
case 0:
switch ($long) {
case 0:
return t('Highlight terms in this textarea.');
}
}
}
/**
* Implementation of hook_filter
*/
function highlight_filter($op, $delta = 0, $format = -1, $text = '') {
// list filters
if ($op == 'list') {
return array(
0 => t('Highlight search results'),
);
}
if ($op == "description") {
return array(
t("Highlight search terms in this content area"),
);
}
// All operations besides "list" provide a $delta argument so we know which
switch ($delta) {
// First we define the simple string substitution filter.
case 0:
switch ($op) {
// no caching
case 'no cache':
return TRUE;
// describe the filter
case 'description':
return t('Highlights search terms in the text.');
// We don't need the "prepare" operation for this filter, but it's required
// to at least return the input text as-is.
case 'prepare':
return $text;
// process the filter
case 'process':
if (highlight_check_display()) {
return highlight_process($text, $_GET['highlight']);
}
else {
return $text;
}
}
break;
}
}
/* *********************************************** */
/* Module functions */
/* *********************************************** */
/**
* controls the conditions for displaying highlights
* returns true if highlight should be active, false otherwise
*
*/
function highlight_check_display() {
global $base_url;
static $allow_external_highlight;
$allow_external_highlight = variable_get('hl_allow_external', false);
// we don't need to check the url if we allow external highlights
if ($allow_external_highlight) {
$referer_is_local = true;
}
else {
// parse the referring url to make sure it's local
if (strpos($_SERVER['HTTP_REFERER'], $base_url) == 0) {
$referer_is_local = true;
}
}
if ($allow_external_highlight || $referer_is_local) {
if ($_GET['highlight']) {
return true;
}
if (strstr($_SERVER['HTTP_REFERER'], "search/node")) {
return true;
}
}
else {
return false;
}
}
/**
* Takes input text and a key and replaces all instances of the
* key in the text with highlight code
*/
function highlight_process($text, $keys) {
global $base_url;
static $replace_text, $use_css;
$replace_text = variable_get('hl_replace', '<strong class="highlight">%key%</strong>');
$use_css = variable_get('sh_use_css', true);
// add css to header
if ($use_css) {
highlight_set_css();
}
$string = basename($_SERVER['HTTP_REFERER']);
// check if there is a type set
if (strstr($string, "+type")) {
$string = substr($string, 0, strpos($string, "+type"));
}
// replace "+" with spaces to catch all instances
$string = str_replace("+", ",", $string);
if ($_GET['highlight']) {
$string .= "," . $_GET['highlight'];
}
// strip out any dangerous stuff
$pattern = "/[^, a-zA-Z 0-9_\\.]/";
$keys = preg_replace($pattern, "", $string);
$keys = explode(",", $keys);
foreach ($keys as $key) {
$replacement[] = str_replace("%key%", $key, $replace_text);
}
$text = str_replace($keys, $replacement, $text);
return $text;
}
/**
* adds css to page when highlight is present
* do only once
*/
function highlight_set_css() {
static $has_displayed;
if (!$has_displayed) {
drupal_set_html_head("\n <style>\n strong.highlight {\n background-color: yellow;\n }\n </style>\n ");
}
$has_displayed = true;
}
Functions
Name | Description |
---|---|
highlight_admin_settings | Implementation of admin settings |
highlight_check_display | controls the conditions for displaying highlights returns true if highlight should be active, false otherwise |
highlight_filter | Implementation of hook_filter |
highlight_filter_tips | Implemnatation of highlight |
highlight_help | Implementation of hook_help |
highlight_info | |
highlight_menu | Implementation of hook_menu |
highlight_perm | Implementation of hook_perm |
highlight_process | Takes input text and a key and replaces all instances of the key in the text with highlight code |
highlight_set_css | adds css to page when highlight is present do only once |