/[drupal]/contributions/modules/highlight/highlight.module
ViewVC logotype

Contents of /contributions/modules/highlight/highlight.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Thu Dec 20 17:29:37 2007 UTC (23 months ago) by arthuregg
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +10 -5 lines
File MIME type: text/x-php
applied patch #65577 from guardian
1 <?php
2
3 /* $Id: highlight.module,v 1.4 2006/12/11 02:43:11 arthuregg Exp $ */
4
5 /* This module creates a link on search results pages and if highlighting is
6 enabled for the field (check your input types), it will search and replace
7 the search term in that field with an admin defined string
8 */
9
10
11 /*
12 * @TODO add back the "first,second,third" functionality into the replace function
13 * @TODO add admin options for the set message function that the 4.6 version had
14 */
15
16
17 /* *********************************************** */
18 /* Drupal Hooks */
19 /* *********************************************** */
20
21 /**
22 * Implementation of hook_help
23 */
24 function highlight_help($section) {
25 switch ($section) {
26 case 'admin/modules#description':
27 $output .= t('A helper module to highlight terms on pages, as in search results.');
28 break;
29 }
30 return $output;
31 }
32
33 function highlight_info() {
34 return array(
35 'highlight' => array(
36 'name' => t('highlight'),
37 'module' => 'highlight',
38 'description' => t("A helper module to highlight terms on pages, as in search results."),
39 )
40 );
41 }
42
43
44 /**
45 * Implementation of hook_menu
46 */
47 function highlight_menu($may_cache){
48 $items[] = array(
49 'path' => 'admin/settings/highlight',
50 'title' => t('Highlight'),
51 'description' => t('Configure the highlight module'),
52 'callback' => 'drupal_get_form',
53 'callback arguments' => 'highlight_admin_settings',
54 'access' => user_access('administer site configuration'),
55 'type' => MENU_NORMAL_ITEM, // optional
56 );
57
58
59 return $items;
60 }
61
62
63 /**
64 * Implementation of hook_perm
65 */
66 function highlight_perm() {
67 return array('administer search_highlight');
68 }
69
70 /**
71 * Implementation of admin settings
72 */
73 function highlight_admin_settings(){
74 $form['highlight'] = array(
75 '#type' => 'fieldset',
76 '#title' => t('Search Highlight Configuration'),
77 );
78 $form['highlight']['hl_replace'] = array(
79 '#type' => 'textfield',
80 '#title' => t('Replacement string'),
81 '#default_value' => variable_get('hl_replace', '<strong class="highlight">%key%</strong>'),
82 '#description' => t('This string will be used to replace the found value <strong>key</strong> with. <strong>%key%</strong> is variable.'),
83 );
84 $form['highlight']['hl_allow_external'] = array(
85 '#type' => 'checkbox',
86 '#title' => t('Allow external sites to highlight'),
87 '#default_value' => variable_get('hl_allow_external', false),
88 '#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>'),
89 );
90 $form['highlight']['sh_use_css'] = array(
91 '#type' => 'checkbox',
92 '#title' => t('Use Module CSS'),
93 '#default_value' => variable_get('sh_use_css', true),
94 '#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.'),
95 );
96 return system_settings_form($form);
97 }
98
99
100 // This is the old highlight function, replacing this with the filter version
101 /*
102 function highlight_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
103 static $message_given = false;
104 switch ($op) {
105 case 'view':
106 if (!empty($_REQUEST['highlight'])) {
107 $highlights = explode(',', $_REQUEST['highlight']);
108 $count = 0;
109 foreach ($highlights as $highlight) {
110 if($highlight != '') {
111 if ($count == 3) {
112 break;
113 }
114 if (stristr($node->body, $highlight) || stristr($node->title, $highlight)) {
115 $key[] = $highlight;
116 $key[] = ucfirst($highlight);
117 $found[] = '<strong class="highlight' . $count . '">' . $highlight . '</strong>';
118 $replace[] = '<strong class="highlight' . $count . '">' . $highlight . '</strong>';
119 $replace[] = '<strong class="highlight' . $count . '">' . ucfirst($highlight) . '</strong>';
120 $count++;
121 }
122 }
123 }
124 if (count($key) > 0) {
125 $node->body = str_replace($key, $replace, $node->body);
126 $node->title = str_replace($key, $replace, $node->title);
127 if(!($message_given)) {
128 $message = count($key) > 1 ? t('The following terms have been highlighted: ') : t('The following term has been highlighted: ');
129 drupal_set_message($message . implode(' ', $found));
130 $message_given = true;
131 highlight_set_css();
132 }
133 }
134 }
135 }
136 }
137 */
138
139 /**
140 * Implemnatation of highlight
141 */
142 function highlight_filter_tips($delta, $format, $long = FALSE) {
143 switch ($delta){
144 case 0:
145 switch ($long) {
146 case 0:
147 return t('Highlight terms in this textarea.');
148 }
149 }
150 }
151
152
153 /**
154 * Implementation of hook_filter
155 */
156 function highlight_filter($op, $delta = 0, $format = -1, $text = '') {
157 // list filters
158 if ($op == 'list') {
159 return array(
160 0 => t('Highlight search results'));
161 }
162
163 if ($op == "description"){
164 return array(t("Highlight search terms in this content area"));
165 }
166
167 // All operations besides "list" provide a $delta argument so we know which
168 switch ($delta) {
169
170 // First we define the simple string substitution filter.
171 case 0:
172
173 switch ($op) {
174
175 // no caching
176 case 'no cache':
177 return TRUE;
178
179 // describe the filter
180 case 'description':
181 return t('Highlights search terms in the text.');
182
183 // We don't need the "prepare" operation for this filter, but it's required
184 // to at least return the input text as-is.
185 case 'prepare':
186 return $text;
187
188 // process the filter
189 case 'process':
190 if (highlight_check_display() ){
191 return highlight_process($text, $_GET['highlight']);
192 } else {
193 return $text;
194 }
195 }
196 break;
197 }
198 }
199
200 /* *********************************************** */
201 /* Module functions */
202 /* *********************************************** */
203
204
205 /**
206 * controls the conditions for displaying highlights
207 * returns true if highlight should be active, false otherwise
208 *
209 */
210 function highlight_check_display(){
211 global $base_url;
212 static $allow_external_highlight;
213
214 $allow_external_highlight = variable_get('hl_allow_external', false);
215
216 // we don't need to check the url if we allow external highlights
217 if ($allow_external_highlight) {
218 $referer_is_local = true;
219 } else {
220 // parse the referring url to make sure it's local
221 if ( strpos($_SERVER['HTTP_REFERER'], $base_url) == 0 ){
222 $referer_is_local = true;
223 }
224 }
225 if ($allow_external_highlight || $referer_is_local){
226 if ( $_GET['highlight'] ) {
227 return true;
228 }
229 if (strstr($_SERVER['HTTP_REFERER'], "search/node") ) {
230 return true;
231 }
232 } else {
233 return false;
234 }
235 }
236
237 /**
238 * Takes input text and a key and replaces all instances of the
239 * key in the text with highlight code
240 */
241 function highlight_process($text, $keys) {
242 global $base_url;
243 static $replace_text, $use_css;
244
245 $replace_text = variable_get('hl_replace', '<strong class="highlight">%key%</strong>');
246 $use_css = variable_get('sh_use_css', true);
247
248 // add css to header
249 if($use_css) {highlight_set_css(); }
250
251 $string = basename($_SERVER['HTTP_REFERER']);
252 // check if there is a type set
253 if (strstr($string, "+type")){
254 $string = substr($string, 0, strpos($string, "+type"));
255 }
256
257 // replace "+" with spaces to catch all instances
258 $string = str_replace("+", ",", $string);
259
260 if ($_GET['highlight']) {
261 if ($string)
262 $string .= ',' . $_GET['highlight'];
263 else
264 $string = $_GET['highlight'];
265 }
266
267 // strip out any dangerous stuff
268 $pattern = "/[^, a-zA-Z 0-9_\.]/";
269 $keys = preg_replace($pattern, "", $string);
270
271 $keys = explode(",",$keys);
272
273 $replace_text = str_replace("%key%", '$1', $replace_text);
274 foreach($keys as $key) {
275 $text = preg_replace("/($key)/i", $replace_text, $text);
276 }
277
278 return $text;
279 }
280
281
282 /**
283 * adds css to page when highlight is present
284 * do only once
285 */
286 function highlight_set_css() {
287 static $has_displayed;
288
289 if (!($has_displayed)){
290 drupal_set_html_head("
291 <style>
292 strong.highlight {
293 background-color: yellow;
294 }
295 </style>
296 ");
297 }
298 $has_displayed = true;
299 }

  ViewVC Help
Powered by ViewVC 1.1.2