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

Contents of /contributions/modules/wordfilter/wordfilter.module

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


Revision 1.8 - (show annotations) (download) (as text)
Tue Jan 1 17:46:30 2008 UTC (22 months, 3 weeks ago) by add1sun
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +4 -4 lines
File MIME type: text/x-php
#166215 thanks to arte.mis and hunmonk, fixing settings form bug.
1 <?php
2 // $Id: wordfilter.module,v 1.7 2007/06/19 12:13:44 add1sun Exp $
3
4 /**
5 * @file
6 * Replaces words inside posts with filtered versions.
7 */
8
9 /**
10 * Implementation of hook_help().
11 *
12 * @param $section
13 * string file path
14 *
15 * @return
16 * string
17 */
18 function wordfilter_help($section = 'admin/help#wordfilter') {
19 switch ($section) {
20 case 'admin/modules#description':
21 return t('Replaces words inside posts with filtered versions.');
22 case 'admin/filters/wordfilter':
23 return t('Here you can edit the words that are filtered by your site.');
24 }
25 }
26
27 /**
28 * Implementation of hook_perm().
29 *
30 * @return
31 * array of permissions
32 */
33 function wordfilter_perm() {
34 return array('administer words filtered');
35 }
36
37 /**
38 * Implementation of hook_menu().
39 *
40 * @param $may_cache
41 * boolean indicating whether cacheable menu items should be returned
42 *
43 * @return
44 * array of menu information
45 */
46 function wordfilter_menu($may_cache) {
47 $items = array();
48
49 if ($may_cache) {
50 $access = user_access('administer words filtered');
51
52 $items[] = array(
53 'path' => 'admin/settings/wordfilter',
54 'title' => t('Word filter'),
55 'description' => t('Replaces words inside posts with filtered versions.'),
56 'callback' => 'wordfilter_admin_list',
57 'access' => $access);
58
59 $items[] = array(
60 'path' => 'admin/settings/wordfilter/list',
61 'title' => t('List'),
62 'type' => MENU_DEFAULT_LOCAL_TASK,
63 'weight' => -10);
64
65 $items[] = array(
66 'path' => 'admin/settings/wordfilter/add',
67 'title' => t('Add'),
68 'callback' => 'drupal_get_form',
69 'callback arguments' => array('wordfilter_admin_form'),
70 'access' => $access,
71 'type' => MENU_LOCAL_TASK);
72
73 $items[] = array(
74 'path' => 'admin/settings/wordfilter/edit',
75 'title' => t('Edit'),
76 'callback' => 'drupal_get_form',
77 'callback arguments' => array('wordfilter_admin_form'),
78 'access' => $access,
79 'type' => MENU_CALLBACK);
80 }
81 return $items;
82 }
83
84 /**
85 * Implementation of hook_settings().
86 *
87 * @return
88 * array of form elements
89 */
90 function wordfilter_settings_form() {
91 $form["wordfilter_default_replacement"] = array(
92 "#type" => "textfield",
93 "#title" => t("Default Word Replacement"),
94 "#default_value" => variable_get("wordfilter_default_replacement", "[filtered word]"),
95 "#required" => true
96 );
97
98 $form["wordfilter_node_title"] = array(
99 "#type" => "checkbox",
100 "#title" => t("Enable Word Filtering On Node Titles"),
101 "#default_value" => variable_get("wordfilter_node_title", true),
102 "#return_value" => true
103 );
104
105 return system_settings_form($form);
106 }
107
108 /**
109 * Implementation of hook_nodeapi().
110 *
111 * @param &$node
112 * editable node object
113 *
114 * @param $op
115 * string of what process we're at
116 *
117 * @param $teaser
118 * boolean if showing teaser
119 *
120 * @param $page
121 * boolean if on full view page of a node
122 *
123 */
124 function wordfilter_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
125 if ($node && ($op == 'submit' || $op == 'update') && variable_get("wordfilter_node_title", true)) {
126 $node->title = wordfilter_filter_process($node->title);
127 }
128 }
129
130 /**
131 * Implementation of hook_block().
132 *
133 * @param $op
134 * string for view type
135 *
136 * @param $delta
137 * int
138 *
139 * @return
140 * array of block information
141 */
142 function wordfilter_block($op = 'list', $delta = 0) {
143 if ($op == 'list') {
144 $blocks[0]['info'] = t('Filtered word lists on submission pages');
145 return $blocks;
146 }
147 else if ($op == 'view') {
148 switch ($delta) {
149 case 0:
150 $block['subject'] = t('Filtered words');
151 $block['content'] = $GLOBALS['display_wordfilter_block'] ? wordfilter_table() : '';
152 return $block;
153 }
154 }
155 }
156
157 /**
158 * @return string
159 */
160 function wordfilter_filter_tips($delta, $format, $long = false) {
161 if ($long) {
162 return t('If you include a word in your post that\'s filtered (usually foul language), it will be replaced by the filtered version of the word.') . '<br />';
163 }
164 else {
165 $GLOBALS['display_wordfilter_block'] = true;
166 return t('Filtered words will be replaced with the filtered version of the word.');
167 }
168 }
169
170 function wordfilter_table() {
171 $content .= '<div align="center">';
172 $content .= '<table border="0" cellspacing="1" cellpadding="0">';
173 $list = _wordfilter_list();
174 foreach ($list as $filtered_word) {
175 $alt = implode(' &nbsp; ', explode(' ', $filtered_word->words));
176 $content .= '<tr><td align="left">'. $filtered_word->replacement .'</td><td align="right">&nbsp;'. $alt .'</td></tr>';
177 }
178 $content .= '</table></div>';
179 return $content;
180 }
181
182 function wordfilter_filter($op, $delta = 0, $format = -1, $text = '') {
183 switch ($op) {
184 case 'list':
185 return array(0 => t('Word filter'));
186 case 'description':
187 return wordfilter_help('admin/modules#description');
188 case 'settings':
189 $form['word_filter'] = array(
190 '#type' => 'fieldset',
191 '#title' => t('Word filter'),
192 '#description' => t('You can define a global list of words to be filtered on the <a href="!url">wordfilter settings page</a>.', array('!url' => url('admin/settings/wordfilter'))),
193 );
194 return $form;
195 case 'process':
196 return wordfilter_filter_process($text);
197 default:
198 return $text;
199 }
200 }
201
202 function wordfilter_filter_process($text) {
203 $text = ' '. $text .' ';
204 $list = _wordfilter_list();
205 foreach ($list as $word) {
206 $words = explode(' ', $word->words);
207 foreach ($words as $a) {
208 // Prevent mysterious empty value from blowing away the node title.
209 if (!empty($a)) {
210 $replacement = ($word->replacement) ? $word->replacement : variable_get("wordfilter_default_replacement", "[filtered word]");
211
212 if ($word->standalone) {
213 $text = eregi_replace("([ ,\.\?!:\(\)\r\n\<\>])". preg_quote($a) ."([ ,\.\?!:\(\)\r\n\<\>])", "\\1". ($replacement) ."\\2", $text);
214 } else {
215 $text = eregi_replace(preg_quote($a), ($replacement), $text);
216 }
217 }
218 }
219 }
220 $text = substr($text, 1, -1);
221
222 return $text;
223 }
224
225 function _wordfilter_list($refresh = 0) {
226 static $list = NULL;
227 if (is_null($list) || $refresh) {
228 $result = db_query('SELECT * FROM {wordfilter}');
229 $list = array();
230 while ($a = db_fetch_object($result)) {
231 $list[] = $a;
232 }
233 }
234 return $list;
235 }
236
237 function wordfilter_admin_list() {
238 $header = array(t('Words'), t('Replacement'), t('Operations'));
239 $rows = array();
240 $list = _wordfilter_list(1);
241 foreach ($list as $word) {
242 $rows[] = array(
243 check_plain($word->words),
244 ($word->replacement) ? $word->replacement : variable_get("wordfilter_default_replacement", "[filtered word]"),
245 l(t('Edit word'), 'admin/settings/wordfilter/edit/'. $word->id)
246 );
247 }
248 $output .= theme('table', $header, $rows);
249
250 $output .= drupal_get_form('wordfilter_settings_form');
251
252 return $output;
253 }
254
255 /**
256 * @return array
257 */
258 function wordfilter_admin_form($word_id = NULL) {
259 if (isset($word_id)) {
260 $result = db_query('SELECT * FROM {wordfilter} WHERE id = %d', $word_id);
261 $word = db_fetch_array($result);
262 }
263
264 $form = array();
265 if ($word['id']) {
266 $form['id'] = array(
267 '#type' => 'hidden',
268 '#value' => $word['id']
269 );
270 }
271
272 $form['words'] = array(
273 '#type' => 'textarea',
274 '#title' => t('Words'),
275 '#default_value' => $word['words'],
276 '#description' => t('Enter a list of words you want to filter, separated by spaces.'),
277 '#required' => true
278 );
279 $form['replacement'] = array(
280 '#type' => 'textfield',
281 '#title' => t('Replacement'),
282 '#default_value' => ($word['replacement']) ? $word['replacement'] : variable_get("wordfilter_default_replacement", "[filtered word]"),
283 '#size' => 50,
284 '#maxlength' => 255,
285 '#description' => t('Enter the filtered version of the word to replace the original words with.')
286 );
287 $form['standalone'] = array(
288 '#type' => 'checkbox',
289 '#title' => t('Stand-alone'),
290 '#default_value' => $word['standalone'],
291 '#description' => t('When checked, the word will only be filtered when found as a separate word (i.e. prefixed and suffixed by spaces or "whitespace"). A period one character after a word will exclude the words from replacement in stand-alone mode. This is useful for preventing accidental word filtering with short or common words.')
292 );
293 $form['Save Word Filter'] = array(
294 '#type' => 'submit',
295 '#value' => t('Save Word Filter')
296 );
297
298 if ($word['id']) {
299 $form['Delete Word Filter'] = array(
300 '#type' => 'submit',
301 '#value' => t('Delete Word Filter')
302 );
303 }
304
305 return $form;
306 }
307
308 function wordfilter_admin_form_validate($form_id, $form_values) {
309 if (trim($form_values['words']) == '') {
310 form_set_error('words', t('Please enter a word to filter.'));
311 }
312 }
313
314 function wordfilter_admin_form_submit($form_id, $form_values) {
315 if ($form_values['id']) {
316 if ($form_values['op'] == t('Delete Word Filter')) {
317 db_query("DELETE FROM {wordfilter} WHERE id = %d", $form_values['id']);
318 $message = t('Deleted filter for: %words', array('%words' => $form_values['words']));
319 }
320 else {
321 db_query("UPDATE {wordfilter} SET words='%s', replacement='%s', standalone=%d WHERE id = %d", $form_values['words'], $form_values['replacement'], $form_values['standalone'], $form_values['id']);
322 $message = t('Updated filter for: %words', array('%words' => $edit['words']));
323 }
324 }
325 else {
326 db_query("INSERT INTO {wordfilter} (words, replacement, standalone) VALUES ('%s', '%s', %d)", $form_values["words"], $form_values["replacement"], $form_values["standalone"]);
327 $message = t('Added filter for: %words', array('%words' => $form_values['words']));
328 }
329 watchdog('regular', $message);
330 drupal_set_message($message);
331 }

  ViewVC Help
Powered by ViewVC 1.1.2