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

Contents of /contributions/modules/username_highlighter/username_highlighter.module

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


Revision 1.6 - (show annotations) (download) (as text)
Fri Jul 24 14:18:15 2009 UTC (4 months ago) by soxofaan
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +5 -2 lines
File MIME type: text/x-php
#476464: add classes for more advanced theming
1 <?php
2 // $Id: username_highlighter.module,v 1.5 2009/07/24 13:58:53 soxofaan Exp $
3
4 /**
5 * @file Implementation of the User name highlighter module.
6 *
7 * Provides an input filter that adds a unique background color to user names
8 * in the filtered text.
9 */
10
11 /**
12 * Implementation of hook_menu().
13 */
14 function username_highlighter_menu() {
15 $items = array();
16 $items['admin/settings/username_highlighter'] = array(
17 'title' => 'User name highlighter',
18 'description' => 'Administer user name highlighting.',
19 'page callback' => 'drupal_get_form',
20 'page arguments' => array('username_highlighter_admin'),
21 'access arguments' => array('administer filters'),
22 'type' => MENU_NORMAL_ITEM,
23 );
24 return $items;
25 }
26
27
28 /**
29 * Implementation of hook_filter().
30 */
31 function username_highlighter_filter($op, $delta = 0, $format = -1, $text = '') {
32 switch ($op) {
33
34 case 'list':
35 return array(0 => t('User name highlighter'));
36
37 case 'description':
38 return t('Highlights each user name with a unique color.');
39
40 case 'settings':
41 return NULL;
42
43 case 'prepare':
44 return $text;
45
46 case 'process':
47 if ($delta == 0) {
48 $username_map = _username_highlighter_username_map();
49 return preg_replace(array_keys($username_map), array_values($username_map), $text);
50 }
51
52 default:
53 return $text;
54 }
55 }
56
57 /**
58 * Implementation of hook_filter_tips().
59 */
60 function username_highlighter_filter_tips($delta, $format, $long = FALSE) {
61 return t('User names will be highlighted.');
62 }
63
64 /**
65 * Administration form builder function.
66 */
67 function username_highlighter_admin() {
68 $form = array();
69
70 // Widget for setting the color saturation.
71 $form['username_highlighter_color_saturation'] = array(
72 '#type' => 'textfield',
73 '#title' => t('Color saturation'),
74 '#description' => t('The color saturation of the highlighting.'),
75 '#default_value' => variable_get('username_highlighter_color_saturation', 50),
76 '#size' => 4,
77 '#maxlength' => 3,
78 '#field_suffix' => '%',
79 );
80 // Widget for setting the color value.
81 $form['username_highlighter_color_value'] = array(
82 '#type' => 'textfield',
83 '#title' => t('Color value'),
84 '#description' => t('The color value of the highlighting, lower means darker.'),
85 '#default_value' => variable_get('username_highlighter_color_value', 100),
86 '#size' => 4,
87 '#maxlength' => 3,
88 '#field_suffix' => '%',
89 );
90
91 // Make it a settings form.
92 $form = system_settings_form($form);
93
94
95 // Add example of highlighting.
96 $text = 'Lorem ipsum';
97 $lorem_ipsum = explode(' ', 'lorem ipsum dolor sit amet consectetuer adipiscing elit integer viverra diam suspendisse aliquam ligula ut justo cras sodales tortor in');
98 $result = db_query_range('SELECT uid, name FROM {users} WHERE name <> "" ORDER BY RAND()', 0, 20);
99 while ($user = db_fetch_object($result)) {
100 $text .= ' '. $user->name;
101 foreach (array_rand($lorem_ipsum, rand(2, 5)) as $i) {
102 $text .= ' '. $lorem_ipsum[$i];
103 }
104 }
105 $text .= '.';
106 $form['username_highlighter_example'] = array(
107 '#type' => 'item',
108 '#title' => t('Example'),
109 '#value' => '<p>'. username_highlighter_filter('process', 0, -1, $text) .'</p>',
110 );
111
112 return $form;
113 }
114
115
116 /**
117 * Validation handler for username_highlighter_admin form.
118 */
119 function username_highlighter_admin_validate($form, &$form_state) {
120 $sat = $form_state['values']['username_highlighter_color_saturation'];
121 if (!is_numeric($sat) || $sat < 0.0 || $sat > 100.0) {
122 form_set_error('username_highlighter_color_saturation', t('Color saturation should be between 0% and 100%'));
123 }
124 $val = $form_state['values']['username_highlighter_color_value'];
125 if (!is_numeric($val) || $val < 0.0 || $val > 100.0) {
126 form_set_error('username_highlighter_color_value', t('Color value should be between 0% and 100%'));
127 }
128 }
129
130
131 /**
132 * Helper function for mapping user id to a color.
133 */
134 function _username_highlighter_uid2color($uid) {
135 $hue = ($uid * 67) % 360;
136 $sat = 0.01 * (float)(variable_get('username_highlighter_color_saturation', 50));
137 $val = 0.01 * (float)(variable_get('username_highlighter_color_value', 100));
138 return _username_highlighter_hsv2rgbhexcode($hue, $sat, $val);
139 }
140
141
142 /**
143 * Helper function for fetching the user name replacements.
144 */
145 function _username_highlighter_username_map() {
146 static $username_map = NULL;
147 if (!$username_map) {
148 $result = db_query('SELECT uid, name FROM {users} WHERE name <> ""');
149 while ($user = db_fetch_object($result)) {
150 $color = _username_highlighter_uid2color($user->uid);
151 $username_map['/\\b'. $user->name .'\\b/'] =
152 l($user->name, 'user/' . $user->uid,
153 array('attributes' => array(
154 'class' => 'user-name-highlighter user-name-highlighter-uid-'. $user->uid,
155 'style' => 'background-color:'. $color .';')
156 )
157 );
158 }
159 }
160 return $username_map;
161 }
162
163
164 /**
165 * Helper function for HSV to RGB hex code conversion.
166 * @param $h the hue, in range [0-360]
167 * @param $s the saturation, in range [0-1]
168 * @param $v the value, in range [0-1]
169 */
170 function _username_highlighter_hsv2rgbhexcode($h, $s, $v) {
171 // Map hue to the piecewise linear R, G, B curves in the range [0-1] first.
172 $r = min(1.0, max(0.0, abs($h-180.0)/60.0 - 1));
173 $g = min(1.0, max(0.0, 2 - abs($h-120.0)/60.0));
174 $b = min(1.0, max(0.0, 2 - abs($h-240.0)/60.0));
175 // Rescale dynamic range, considering value and saturation.
176 $r = min(255, max(0, (int)(256 * ($r*$v + (1.0-$r)*(1.0-$s)*$v))));
177 $g = min(255, max(0, (int)(256 * ($g*$v + (1.0-$g)*(1.0-$s)*$v))));
178 $b = min(255, max(0, (int)(256 * ($b*$v + (1.0-$b)*(1.0-$s)*$v))));
179 return sprintf('#%02X%02X%02X', $r, $g, $b);
180 }

  ViewVC Help
Powered by ViewVC 1.1.2