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

Contents of /contributions/modules/ignore_user/ignore_user.module

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


Revision 1.1 - (show annotations) (download) (as text)
Fri Apr 18 07:19:12 2008 UTC (19 months, 1 week ago) by jaydub
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
File MIME type: text/x-php
Initial commit of ignore_user module.
1 <?php
2 // $Id$
3
4 /**
5 * Implementation of hook_help().
6 */
7 function ignore_user_help($section) {
8 switch ($section) {
9 case 'admin/help#ignore_user':
10 return t("<p>The ignore_user module allows users to choose to ignore node and comment content created by other users. A link is provided in the content to show or hide the content from the ignored user in the node or comment view.</p>");
11 }
12 }
13
14 /**
15 * Implementation of hook_perm().
16 */
17 function ignore_user_perm() {
18 return array('ignore user');
19 }
20
21 /**
22 * Implementation of hook_menu().
23 */
24 function ignore_user_menu($may_cache) {
25 $items = array();
26
27 if ($may_cache) {
28 $items[] = array(
29 'path' => 'ignore_user/list',
30 'title' => t('My ignore list'),
31 'description' => t('Add or remove users that you wish to ignore.'),
32 'callback' => 'drupal_get_form',
33 'callback arguments' => 'ignore_user_ignore_list_form',
34 'access' => user_access('ignore user'),
35 );
36 $items[] = array(
37 'path' => 'ignore_user/add',
38 'title' => t('Add to ignore list'),
39 'description' => t('Add user that you wish to ignore.'),
40 'callback' => 'ignore_user_add_user',
41 'access' => user_access('ignore user'),
42 'type' => MENU_CALLBACK,
43 );
44 $items[] = array(
45 'path' => 'ignore_user/remove',
46 'title' => t('Remove from ignore list'),
47 'description' => t('Remove user from your ignore list.'),
48 'callback' => 'ignore_user_remove_user',
49 'access' => user_access('ignore user'),
50 'type' => MENU_CALLBACK,
51 );
52 }
53 else {
54 drupal_add_js(drupal_get_path('module', 'ignore_user') .'/ignore_user.js', 'module');
55 }
56
57 return $items;
58 }
59
60 /**
61 * This form allows the user to add/remove other users to/from
62 * their ignored users list.
63 */
64 function ignore_user_ignore_list_form() {
65 $form = array();
66
67 $form['add_user'] = array(
68 '#type' => 'fieldset',
69 '#title' => t('Add user to Ignore List'),
70 );
71
72 $form['add_user']['username'] = array(
73 '#type' => 'textfield',
74 '#title' => t('Username'),
75 '#description' => t('To lookup a username start typing in the box above. A list of usernames will appear as you type.'),
76 '#size' => 40,
77 '#maxlength' => 60,
78 '#autocomplete_path' => 'user/autocomplete',
79 );
80
81 $form['add_user']['submit'] = array(
82 '#type' => 'submit',
83 '#value' => t('Ignore user'),
84 );
85
86 $form['current_list'] = array(
87 '#type' => 'fieldset',
88 '#title' => t('Current users on your Ignore List'),
89 );
90 $form['current_list']['ignored_users'] = array(
91 '#type' => 'markup',
92 '#value' => '',
93 );
94
95 $ignored_users = _ignore_user_ignored_users();
96 foreach ($ignored_users as $ignored_user) {
97 $form['username'][$ignored_user['iuid']] = array(
98 '#value' => $ignored_user['username'],
99 );
100 $form['delete'][$ignored_user['iuid']] = array(
101 '#value' => l(t('delete'), 'ignore_user/remove/'. $ignored_user['iuid'], array()),
102 );
103 }
104 $form['pager'] = array('#value' => theme('pager', NULL, 10, 0));
105
106 return $form;
107 }
108
109 /**
110 * Theme the form where user's can manage their ignore list
111 */
112 function theme_ignore_user_ignore_list_form($form) {
113 $output = drupal_render($form['add_user']);
114
115 $header = array(t('Username'), t('Operations'));
116
117 if (isset($form['username']) && is_array($form['username'])) {
118 foreach (element_children($form['username']) as $key) {
119 $row = array();
120 $row[] = drupal_render($form['username'][$key]);
121 $row[] = drupal_render($form['delete'][$key]);
122 $rows[] = $row;
123 }
124 }
125 else {
126 $rows[] = array(array('data' => t('You have not added any users to your Ignore List'), 'colspan' => '2'));
127 }
128
129 $form['current_list']['ignored_users']['#value'] = theme('table', $header, $rows);
130 $output .= drupal_render($form['current_list']);
131
132 if ($form['pager']['#value']) {
133 $output .= drupal_render($form['pager']);
134 }
135 $output .= drupal_render($form);
136
137 return $output;
138 }
139
140 /**
141 * ignore user form submit handler
142 */
143 function ignore_user_ignore_list_form_submit($form_id, $form_values) {
144 if ($form_values['op'] == t('Ignore user')) {
145 ignore_user_add_user_username($form_values['username']);
146 drupal_set_message(t('%username has been added to your ignore list', array('%username' => $form_values['username'])));
147 }
148 }
149
150 /**
151 * Implementation of hook_user().
152 */
153 function ignore_user_user($op, &$edit, &$account) {
154 switch ($op) {
155 case 'delete':
156 db_query('DELETE FROM {ignore_user} WHERE uid = %d OR iuid = %d', $account->uid);
157 break;
158 case 'view':
159 break;
160 }
161 }
162
163 /**
164 * Implementation of hook_link().
165 */
166 function ignore_user_link($type, $object = NULL, $teaser = FALSE) {
167 global $user;
168
169 if (!$user || !$user->uid) {
170 return;
171 }
172
173 static $ignored;
174 $links = array();
175
176 if ($type == 'node' && $user->uid != $object->uid && $object->uid != 0 && user_access('ignore users')) {
177 if (!isset($ignored[$object->uid])) {
178 $ignored[$object->uid] = db_result(db_query('SELECT COUNT(*) FROM {ignore_user} WHERE uid = %d AND iuid = %d', $user->uid, $object->uid));
179 }
180 if ($ignored[$object->uid] == 0) {
181 $links['ignore_user'] = array(
182 'title' => t('Ignore user'),
183 'href' => 'ignore_user/add/'. $object->uid,
184 'query' => 'destination='. $_GET['q'],
185 'attributes' => array(
186 'class' => 'ignore-user',
187 'title' => t('Add user to your ignore list'),
188 ),
189 );
190 }
191 }
192 else if ($type == 'comment' && $user->uid != $object->uid && $object->uid != 0 && user_access('ignore users')) {
193 if (!isset($ignored[$object->uid])) {
194 $ignored[$object->uid] = db_result(db_query('SELECT COUNT(*) FROM {ignore_user} WHERE uid = %d AND iuid = %d', $user->uid, $object->uid));
195 }
196 if ($ignored[$object->uid] == 0) {
197 $links['ignore_user'] = array(
198 'title' => t('Ignore user'),
199 'href' => 'ignore_user/add'. $object->uid,
200 'query' => 'destination='. $_GET['q'],
201 'attributes' => array(
202 'class' => 'ignore-user',
203 'title' => t('Add user to your ignore list'),
204 ),
205 );
206 }
207 }
208
209 return $links;
210 }
211
212 /**
213 * Implementation of hook_nodeapi().
214 */
215 function ignore_user_nodeapi(&$node, $op, $teaser = NULL, $page = FALSE) {
216 global $user;
217 static $authors;
218
219 if (!$user || !$user->uid || !$node->uid) {
220 return;
221 }
222
223 switch ($op) {
224 case 'alter':
225 if (_ignore_user_ignored_user($node->uid)) {
226 if (!$authors[$node->uid]) {
227 $authors[$node->uid] = user_load(array('uid' => $node->uid));
228 }
229 $output = t('<div class="ignore-user-container">!username is on your <a href="!ignore_list">ignore list</a>. Click <a href="!node" class="ignore-user-content-link">here</a> to view this post.', array('!username' => theme('username', $authors[$node->uid]), '!ignore_list' => '/ignore_user/list', '!node' => '/node/'. $node->nid));
230 $output .= '<div class="ignore-user-content">'. ($teaser ? $node->teaser : $node->body) .'</div></div>';
231 $teaser ? $node->teaser = $output : $node->body = $output;
232 }
233 break;
234 }
235 }
236
237 /**
238 * Implementation of hook_comment().
239 */
240 function ignore_user_comment($comment, $op) {
241 global $user;
242 static $authors;
243
244 if (!$user || !$user->uid || !$comment->uid) {
245 return;
246 }
247
248 switch ($op) {
249 case 'view':
250 if (_ignore_user_ignored_user($comment->uid)) {
251 if (!$authors[$comment->uid]) {
252 $authors[$comment->uid] = user_load(array('uid' => $comment->uid));
253 }
254 $output = t('<div class="ignore-user-container">!username is on your <a href="!ignore_list">ignore list</a>. Click <a href="!comment" class="ignore-user-content-link">here</a> to view this post.', array('!username' => theme('username', $authors[$comment->uid]), '!ignore_list' => 'ignore_user/list', '!node' => '/node/'. $comment->nid .'#comment-'. $comment->cid));
255 $output .= '<div class="ignore-user-content">'. $comment->comment .'</div></div>';
256 $comment->comment = $output;
257 }
258 break;
259 }
260 }
261
262 /**
263 * Add other user to user's ignore list by username
264 */
265 function ignore_user_add_user_username($name = NULL) {
266 global $user;
267
268 if (isset($name)) {
269 $iuid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $name));
270
271 if ($user->uid == $iuid) {
272 drupal_set_message(t("You can't add yourself to your own ignore list"));
273 drupal_goto('ignore_user/list');
274 }
275
276 if (!empty($iuid)) {
277 db_query('DELETE FROM {ignore_user} WHERE uid = %d AND iuid = %d', $user->uid, $iuid);
278 db_query('INSERT INTO {ignore_user} (uid, iuid) VALUES (%d, %d)', $user->uid, $iuid);
279 }
280 }
281 }
282
283 /**
284 * Add other user to user's ignore list by user ID
285 */
286 function ignore_user_add_user($iuid = NULL) {
287 global $user;
288
289 if ($user->uid == $iuid) {
290 drupal_set_message(t("You can't add yourself to your own ignore list"));
291 drupal_goto('ignore_user/list');
292 }
293
294 if (is_numeric($iuid) && $iuid > 0) {
295 db_query('DELETE FROM {ignore_user} WHERE uid = %d AND iuid = %d', $user->uid, $iuid);
296 db_query('INSERT INTO {ignore_user} (uid, iuid) VALUES (%d, %d)', $user->uid, $iuid);
297 drupal_goto();
298 }
299 else {
300 drupal_not_found();
301 }
302 }
303
304 /**
305 * Remove other user from user's ignore list
306 */
307 function ignore_user_remove_user($iuid = NULL) {
308 global $user;
309
310 db_query('DELETE FROM {ignore_user} WHERE uid = %d AND iuid = %d', $user->uid, $iuid);
311 drupal_goto('ignore_user/list');
312 }
313
314 /**
315 * Check if specified user is on user's ignore list
316 */
317 function _ignore_user_ignored_user($iuid) {
318 global $user;
319 static $users;
320
321 if (db_result(db_query('SELECT COUNT(*) FROM {ignore_user} WHERE uid = %d AND iuid = %d', $user->uid, $iuid))) {
322 return TRUE;
323 }
324 return FALSE;
325 }
326
327 /**
328 * Return list of ignored users
329 */
330 function _ignore_user_ignored_users() {
331 global $user;
332 $ignored_users = array();
333
334 $result = db_query('SELECT iuid FROM {ignore_user} WHERE uid = %d', $user->uid);
335 while ($row = db_fetch_object($result)) {
336 $ignored_users[] = array(
337 'iuid' => $row->iuid,
338 'username' => theme('username', user_load(array('uid' => $row->iuid))),
339 );
340 }
341
342 return $ignored_users;
343 }

  ViewVC Help
Powered by ViewVC 1.1.2