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

Contents of /contributions/modules/flag_content/flag_content.module

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


Revision 1.16 - (show annotations) (download) (as text)
Tue Feb 26 23:16:43 2008 UTC (20 months, 4 weeks ago) by kbahey
Branch: MAIN
CVS Tags: HEAD
Changes since 1.15: +55 -59 lines
File MIME type: text/x-php
#226904 by setvik: flag_content_user returns invalid array to hook_user if user
  flagging is disabled
1 <?php
2 // $Id: flag_content.module,v 1.6.2.12 2008/02/26 23:15:47 kbahey Exp $
3
4 // Copyright 2006-2007 Khalid Baheyeldin http://2bits.com
5
6 define('FLAG_CONTENT_NODE_TYPE', 'flag_content_node_type_');
7 define('FLAG_CONTENT_PERM_ADD', 'flag content');
8 define('FLAG_CONTENT_PERM_ADMIN', 'administer flag content');
9 define('FLAG_CONTENT_PERM_MANAGE', 'manage flagged list');
10 define('FLAG_CONTENT_EMAIL', 'flag_content_email');
11 define('FLAG_CONTENT_USER', 'flag_content_user');
12 define('FLAG_CONTENT_COMMENT', 'flag_content_comment');
13 define('FLAG_CONTENT_TYPE_NODE', 'node');
14 define('FLAG_CONTENT_TYPE_USER', 'user');
15 define('FLAG_CONTENT_TYPE_COMMENT', 'comment');
16
17 function flag_content_help($section) {
18 switch ($section) {
19 case 'admin/help#flag_content':
20 return t('Allow users to flag nodes, users or comments for the administrator to review.');
21 }
22 }
23
24 function flag_content_perm() {
25 return array(FLAG_CONTENT_PERM_ADD, FLAG_CONTENT_PERM_MANAGE, FLAG_CONTENT_PERM_ADMIN);
26 }
27
28 function flag_content_menu($may_cache) {
29 $items = array();
30 if ($may_cache) {
31 $items[] = array(
32 'path' => 'admin/settings/flag_content',
33 'title' => t('Flag content'),
34 'description' => t('Settings for the flag content module.'),
35 'callback' => 'drupal_get_form',
36 'callback arguments' => array('flag_content_admin_settings'),
37 'access' => user_access(FLAG_CONTENT_PERM_ADMIN),
38 );
39
40 $items[] = array(
41 'path' => 'flag_content/add',
42 'callback' => 'drupal_get_form',
43 'callback arguments' => array('flag_content_add'),
44 'type' => MENU_CALLBACK,
45 'access' => user_access(FLAG_CONTENT_PERM_ADD),
46 );
47
48 $items[] = array(
49 'path' => 'flag_content/unflag',
50 'callback' => 'drupal_get_form',
51 'callback arguments' => array('flag_content_unflag'),
52 'type' => MENU_CALLBACK,
53 'access' => user_access(FLAG_CONTENT_PERM_MANAGE),
54 );
55
56 $items[] = array(
57 'path' => 'admin/content/flag_content',
58 'callback' => 'flag_content_view',
59 'title' => t('Flagged items'),
60 'description' => t('View, edit, delete flagged nodes, comments, or users.'),
61 'access' => user_access(FLAG_CONTENT_PERM_MANAGE),
62 );
63 }
64 return $items;
65 }
66
67 function flag_content_admin_settings() {
68 $set = 'types';
69 $form[$set] = array(
70 '#type' => 'fieldset',
71 '#title' => t('Enable flagging for these content types'),
72 );
73
74 foreach(node_get_types() as $node_type) {
75 $type = $node_type->type;
76 $name = $node_type->name;
77 $form[$set][FLAG_CONTENT_NODE_TYPE . $type] = array(
78 '#type' => 'checkbox',
79 '#title' => $name,
80 '#return_value' => 1,
81 '#default_value' => variable_get(FLAG_CONTENT_NODE_TYPE . $type, 0),
82 );
83 }
84 $set = 'other';
85 $form[$set] = array(
86 '#type' => 'fieldset',
87 '#title' => t('Enable flagging for users and comments'),
88 );
89 $form[$set][FLAG_CONTENT_USER] = array(
90 '#type' => 'checkbox',
91 '#title' => t('Flag users'),
92 '#return_value' => 1,
93 '#default_value' => variable_get(FLAG_CONTENT_USER, 0),
94 );
95
96 $form[$set][FLAG_CONTENT_COMMENT] = array(
97 '#type' => 'checkbox',
98 '#title' => t('Flag comments'),
99 '#return_value' => 1,
100 '#default_value' => variable_get(FLAG_CONTENT_COMMENT, 0),
101 );
102
103 $form['email'][FLAG_CONTENT_EMAIL] = array(
104 '#type' => 'textfield',
105 '#title' => t('Email address'),
106 '#size' => 60,
107 '#maxlength' => 128,
108 '#required' => true,
109 '#default_value' => variable_get(FLAG_CONTENT_EMAIL, variable_get('site_mail', ini_get('sendmail_from'))),
110 '#description' => t('Email address to send alerts on flagged items to.'),
111 );
112
113 return system_settings_form($form);
114 }
115
116 /**
117 * Implementation of hook_user().
118 */
119 function flag_content_user($op, &$edit, &$account, $category = NULL) {
120 global $user;
121
122 switch($op) {
123 case 'view':
124 if ($user->uid == $account->uid) {
125 // User is viewing their own user page, don't show them anything
126 return array();
127 }
128
129 $items = array();
130 $links = flag_content_link(FLAG_CONTENT_TYPE_USER, $account);
131 foreach($links as $key => $value) {
132 $items[] = array(
133 'class' => $key,
134 'value' => l($value['title'], $value['href'], $value['attributes']),
135 );
136 }
137
138 if (is_array($items)) {
139 return array(t('Flag user') => $items);
140 }
141 break;
142
143 case 'delete':
144 db_query("DELETE FROM {flag_content} WHERE eid = %d AND type = '%s'", $account->uid, FLAG_CONTENT_TYPE_USER);
145 break;
146 }
147 }
148
149 /**
150 * Implementation of hook_comment().
151 */
152 function flag_content_comment($a1, $op) {
153 switch ($op) {
154 case 'delete':
155 db_query("DELETE FROM {flag_content} WHERE eid = %d AND type = '%s'", $a1->cid, FLAG_CONTENT_TYPE_COMMENT);
156 break;
157 }
158 }
159
160 /**
161 * Implementation of hook_link().
162 * $entry is actually $node but since this is supporting both users and nodes, we call it $entry (eventually support comments?)
163 */
164
165 function flag_content_link($type, $entry = null, $teaser = false) {
166 global $user;
167 $links = array();
168
169 // Do not allow users to flag themselves.
170 if ($type == FLAG_CONTENT_TYPE_USER && $entry->uid == $user->uid) {
171 return $links;
172 }
173
174 if (user_access(FLAG_CONTENT_PERM_ADD)) {
175 switch ($type) {
176 case FLAG_CONTENT_TYPE_NODE:
177 if (variable_get(FLAG_CONTENT_NODE_TYPE . $entry->type, 0)) {
178 $eid = $entry->nid;
179 }
180 break;
181 case FLAG_CONTENT_TYPE_USER:
182 if (variable_get(FLAG_CONTENT_USER, 0)) {
183 $eid = $entry->uid;
184 }
185 break;
186 case FLAG_CONTENT_TYPE_COMMENT:
187 if (variable_get(FLAG_CONTENT_COMMENT, 0)) {
188 $eid = $entry->cid;
189 }
190 break;
191 }
192
193 if (is_numeric($eid)) {
194 $options = array('class' => 'flag_content');
195
196 if (!_flag_content_check($eid, $type)) {
197 // Not already flagged, flag it for admin
198 $links['flag_content_add'] = array(
199 'title' => t('flag this'),
200 'href' => "flag_content/add/$eid/$type",
201 'attributes' => array(
202 'class' => 'flag_content',
203 'title' => t('Notify the administrators this posting is problematic')
204 )
205 );
206 }
207 else {
208 // If has admin privileges, show an unflag link
209 if (user_access(FLAG_CONTENT_PERM_MANAGE)) {
210 $links['flag_content_unflag'] = array(
211 'title' => t('unflag'),
212 'href' => "flag_content/unflag/$eid/$type",
213 'attributes' => array(
214 'class' => 'flag_content',
215 'title' => t('Remove flagged marking')
216 )
217 );
218 }
219 else {
220 // Otherwise just show it as flagged
221 $links['flag_content_remove_flag'] = array(
222 'title' => t('flagged'),
223 );
224 }
225 }
226 }
227 }
228
229 return $links;
230 }
231
232 function flag_content_add($eid = 0, $type = FLAG_CONTENT_TYPE_NODE) {
233 switch ($type) {
234 case FLAG_CONTENT_TYPE_NODE:
235 $node = node_load($eid);
236 $title = $node->title;
237 $type_label = $node->type;
238 $path = $type . '/' . $eid;
239 break;
240 case FLAG_CONTENT_TYPE_USER:
241 $account = user_load(array('uid' => $eid));
242 $title = $account->name;
243 $type_label = $type;
244 $path = $type . '/' . $eid;
245 break;
246 case FLAG_CONTENT_TYPE_COMMENT:
247 $comment = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $eid));
248 $title = $comment->subject;
249 $type_label = $type;
250 $path = "node/$comment->nid";
251 break;
252 }
253
254 $form['eid'] = array(
255 '#type' => 'hidden',
256 '#value' => $eid,
257 );
258
259 $form['type'] = array(
260 '#type' => 'hidden',
261 '#value' => $type,
262 );
263
264 return confirm_form(
265 $form,
266 t('Are you sure you want to flag the @type @title', array('@type' => $type_label, '@title' => $title)),
267 $_GET['destination'] ? $_GET['destination'] : $path,
268 t(''),
269 t('Flag'),
270 t('Cancel'));
271 }
272
273 function flag_content_add_submit($form_id, $form_values) {
274 global $user;
275 $eid = $form_values['eid'];
276 $type = $form_values['type'];
277 if ($eid && $type) {
278 // Insert the data into the table
279 db_query("INSERT INTO {flag_content} (fid, eid, uid, type, timestamp) VALUES (0, %d, %d, '%s', %d)",
280 $eid, $user->uid, $type, time());
281 // Prepare the data
282 switch ($type) {
283 case FLAG_CONTENT_TYPE_NODE:
284 $entry = node_load(array('nid' => $eid));
285 $path = $type . '/' . $eid;
286 $title = $entry->title;
287 break;
288 case FLAG_CONTENT_TYPE_USER:
289 $entry = user_load(array('uid' => $eid));
290 $path = $type . '/' . $eid;
291 $title = $entry->name;
292 break;
293 case FLAG_CONTENT_TYPE_COMMENT:
294 $entry = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $eid));
295 $path = "node/$entry->nid";
296 $title = $entry->subject;
297 break;
298 }
299
300 // Email the admin
301 $email = theme('flag_content_mail', $entry, $type);
302 drupal_mail('flag_content_admin', $email['to'], $email['subject'], $email['body'], $email['from']);
303
304 // Log the operation:
305 watchdog('flag content', t("@name flagged item @entry for review.",
306 array ('@name' => theme('placeholder', $user->name ." <" . $email['from'] . ">"), '@entry' => l($title, $path))));
307
308 // Give feedback to the user
309 drupal_set_message(t('The @type was flagged for the administrator', array('@type' => $type == FLAG_CONTENT_TYPE_NODE ? t('item') : $type)));
310 }
311
312 return $_GET['destination'] ? $_GET['destination'] : $path;
313 }
314
315 function flag_content_unflag($eid = 0, $type = FLAG_CONTENT_TYPE_NODE) {
316 switch ($type) {
317 case FLAG_CONTENT_TYPE_NODE:
318 $node = node_load($eid);
319 $title = $node->title;
320 $type_label = $node->type;
321 break;
322 case FLAG_CONTENT_TYPE_USER:
323 $account = user_load(array('uid' => $eid));
324 $title = $account->name;
325 $type_label = $type;
326 break;
327 case FLAG_CONTENT_TYPE_COMMENT:
328 $comment = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $eid));
329 $title = $comment->subject;
330 $type_label = $type;
331 break;
332 }
333
334 $form['eid'] = array(
335 '#type' => 'hidden',
336 '#value' => $eid,
337 );
338 $form['type'] = array(
339 '#type' => 'hidden',
340 '#value' => $type,
341 );
342 return confirm_form(
343 $form,
344 t('Are you sure you want to unflag the @type @title', array('@type' => $type_label, '@title' => $title)),
345 $_GET['destination'] ? $_GET['destination'] : $type. '/'. $eid,
346 t(''),
347 t('Unflag'),
348 t('Cancel'));
349 }
350
351 function flag_content_unflag_submit($form_id, $form_values) {
352 $eid = $form_values['eid'];
353 $type = $form_values['type'];
354 if ($eid && $type) {
355 _flag_content_unflag($eid, $type);
356 drupal_set_message(t('The @type was unflagged.', array('@type' => $type == FLAG_CONTENT_TYPE_NODE ? t('item') : $type)));
357 }
358 return 'admin/content/flag_content';
359 }
360
361 function flag_content_view() {
362 print theme('page', theme('flag_content_view', _flag_content_get_list()));
363 }
364
365 function theme_flag_content_view($list = array()) {
366 $rows = array();
367 $header = array(t('Item'), t('Type'), t('Flagged by'), t('Date'), t('Operations'));
368 if (!empty($list)) {
369 foreach($list as $entry) {
370 switch ($entry->type) {
371 case FLAG_CONTENT_TYPE_NODE:
372 $node = node_load(array('nid' => $entry->eid));
373 $title = l($node->title, 'node/'. $node->nid, array('title' => $node->title));
374 $author = theme('username', user_load(array('uid' => $node->uid)));
375 $item = t('!title - (author: !author)', array('!title' => $title, '!author' => $author));
376 $edit = "node/$entry->eid/edit";
377 $delete = "node/$entry->eid/delete";
378 $type = $node->type;
379 break;
380 case FLAG_CONTENT_TYPE_USER:
381 $item = theme('username', user_load(array('uid' => $entry->eid)));
382 $edit = "user/$entry->eid/edit";
383 $delete = "user/$entry->eid/delete";
384 $type = $entry->type;
385 break;
386 case FLAG_CONTENT_TYPE_COMMENT:
387 $comment = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $entry->eid));
388 $subject = $comment->subject ? $comment->subject : 'comment';
389 $item = l($subject, url('node/' . $comment->nid, NULL, NULL, 1) . "#comment-" . $comment->cid);
390 $edit = "comment/edit/$comment->cid";
391 $delete = "comment/delete/$comment->cid";
392 $type = $entry->type;
393 break;
394 }
395
396 $timestamp = format_date($entry->timestamp, 'custom', 'Y-m-d H:i');
397 $by = theme('username', user_load(array('uid' => $entry->uid)));
398 $ops = l(t('edit'), $edit);
399 $ops .= ' ' . l(t('unflag'), "flag_content/unflag/$entry->eid/$entry->type");
400 $ops .= ' ' . l(t('delete'), $delete);
401 $rows[] = array($item, $type, $by, $timestamp, $ops);
402 }
403 }
404 else {
405 $rows[] = array('data' => array(t('No flagged items.')));
406 }
407 return theme('table', $header, $rows);
408 }
409
410 function flag_content_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
411 switch ($op) {
412 case 'delete':
413 // Node is being deleted, delete it from the flag_content table
414 _flag_content_unflag($node->nid);
415 break;
416 }
417 }
418
419 function _flag_content_unflag($eid, $type = FLAG_CONTENT_TYPE_NODE) {
420 db_query("DELETE FROM {flag_content} WHERE eid = %d AND type = '%s'", $eid, $type);
421 }
422
423 function _flag_content_get_list() {
424 $rows = array();
425 $result = db_query("SELECT * FROM {flag_content} ORDER BY timestamp ASC");
426 while ($row = db_fetch_object($result)) {
427 $rows[$row->fid] = $row;
428 }
429 return $rows;
430 }
431
432 function _flag_content_check($eid, $type = FLAG_CONTENT_TYPE_NODE) {
433 global $user;
434 return db_result(db_query("SELECT COUNT(*) FROM {flag_content} WHERE eid = %d AND type = '%s'", $eid, $type));
435 }
436
437 function theme_flag_content_mail($entry = NULL, $type = FLAG_CONTENT_TYPE_NODE) {
438 global $user, $base_url;
439 switch ($type) {
440 case FLAG_CONTENT_TYPE_NODE:
441 $title = $entry->title;
442 $eid = $entry->nid;
443 $path = "$type/$eid";
444 break;
445 case FLAG_CONTENT_TYPE_USER:
446 $title = $entry->name;
447 $eid = $entry->uid;
448 $path = "$type/$eid";
449 break;
450 case FLAG_CONTENT_TYPE_COMMENT:
451 $title = $entry->subject;
452 $eid = $entry->cid;
453 $path = url('node/' . $entry->nid, NULL, NULL, 0) . "#comment-" . $entry->cid;
454 break;
455 }
456
457 if ($user->uid) {
458 $name = $user->name;
459 $from = $user->mail;
460 $user_url = $base_url . base_path() . "user/$user->uid";
461 }
462 else {
463 $name = variable_get('anonymous', 'Anonymous');
464 $from = variable_get('site_mail', '');
465 $user_url = t('n/a');
466 }
467 $site = variable_get('site_name', 'drupal site');
468 $to = variable_get(FLAG_CONTENT_EMAIL, variable_get('site_mail', ini_get('sendmail_from')));
469 $subject = "[Flag]: [$title] from [$site]";
470 $body = t("The following user flagged an item for your review.\n\nUser: @name (@user_url)", array(
471 '@name' => $name,
472 '@user_url' => $user_url,
473 ));
474 $body .= t("\nItem: @title (@entry_url)", array(
475 '@title' => $title,
476 '@entry_url' => $base_url . base_path() . $path,
477 ));
478
479 $body .= t("\n\nManage the flagged items list at @manage", array(
480 '@manage' => $base_url . base_path() . 'admin/content/flag_content',
481 ));
482
483 $to = check_plain($to);
484 $from = check_plain($from);
485 $name = check_plain($name);
486
487 return array(
488 'to' => $to,
489 'from' => $from,
490 'subject' => $subject,
491 'body' => $body,
492 );
493 }

  ViewVC Help
Powered by ViewVC 1.1.2