duplicatemail.module 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @file
  4. * This is a helper module to add blocks and menu items and function
  5. * to help deal with accounts that might have duplicate email addresses.
  6. * It is based on the examples module block_example: an example outlining
  7. * how a module can define blocks to be displayed on each page.
  8. */
  9. /**
  10. * Implements hook_views_api().
  11. */
  12. function duplicatemail_views_api() {
  13. return array(
  14. 'api' => 3,
  15. );
  16. }
  17. /**
  18. * Implements hook_block_info().
  19. *
  20. * This hook both declares what blocks are provided by the module, and
  21. * generates the contents of the blocks themselves.
  22. */
  23. function duplicatemail_block_info() {
  24. return array(
  25. 'duplicatemail-list' => array(
  26. 'info' => t('Duplicate Mail: system wide, lists accounts that have the same email as another account'),
  27. 'cache' => DRUPAL_CACHE_GLOBAL,
  28. )
  29. );
  30. }
  31. /**
  32. * Implements hook_block_view().
  33. */
  34. function duplicatemail_block_view($delta) {
  35. switch ($delta) {
  36. case 'duplicatemail-list':
  37. return array(
  38. 'subject' => t('Duplicate emails'),
  39. 'content' => duplicatemail_list(),
  40. );
  41. break;
  42. }
  43. }
  44. /**
  45. * A block content function.
  46. */
  47. function duplicatemail_list() {
  48. $mails = db_query('SELECT mail FROM {users} GROUP BY mail HAVING count(mail) > 1')->fetchCol();
  49. // Bail out early if there are no duplicates.
  50. if (!$mails) {
  51. return t('All accounts have unique email addresses.');
  52. }
  53. // Grab all the user data for accounts with addresses identified as
  54. // duplicates. This is a little convoluted, but it lets us grab all the user
  55. // data in one shot.
  56. $uids = db_select('users', 'u')
  57. ->fields('u', array('uid'))
  58. ->condition('mail', $mails, 'IN')
  59. ->orderBy('mail')
  60. ->execute()
  61. ->fetchCol();
  62. $duplicate_users = user_load_multiple($uids);
  63. $duplicate_mails = array();
  64. foreach ($duplicate_users as $duplicate_user) {
  65. $duplicate_mails[$duplicate_user->mail][] = $duplicate_user;
  66. }
  67. // Turn the data we've got into markup.
  68. $output = t('Accounts with duplicate email address:') . '<br />';
  69. foreach ($duplicate_mails as $mail => $users) {
  70. $output .= "$mail<br />";
  71. foreach ($users as $duplicate_user) {
  72. $output .= l($duplicate_user->name, "user/{$duplicate_user->uid}");
  73. $output .= '<br />';
  74. }
  75. }
  76. return $output;
  77. }