You are here

sharedemail.module in Shared Email 6

Same filename and directory in other branches
  1. 8.2 sharedemail.module
  2. 5 sharedemail.module
  3. 7 sharedemail.module

Allows users to share an email address

File

sharedemail.module
View source
<?php

/**
 * @file
 * Allows users to share an email address
 */

/**
 * Implementation of hook_help().
 */
function sharedemail_help($path, $arg) {
  switch ($path) {
    case 'admin/help#sharedemail':
      $output = '<p>' . t('Allows users to use the same email address for multiple accounts') . '</p>';
      return $output;
  }
}

/**
 * Implementation of hook_perm().
 */
function sharedemail_perm() {
  return array(
    'administer shared email',
    'show warning text',
  );
}

/**
 * Implementation of hook_menu().
 */
function sharedemail_menu() {
  $items['admin/settings/sharedemail'] = array(
    'title' => 'Shared Email',
    'description' => 'Configure the message that Shared Email displays.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'sharedemail_admin_settings',
    ),
    'access arguments' => array(
      'administer sharedemail',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Configure Sharedemail settings
 *
 * @ingroup forms
 * @see system_settings_form()
 */
function sharedemail_admin_settings() {
  $form = array();
  $msg = t('WARNING: The e-mail address you are using, has already been registered on this site by another user. ' . 'You should be aware that personal information such as password resets will be sent to this address. ' . 'We strongly recommend changing your registered address to a different e-mail address. ' . 'You can do this at any time from your account page when you login.');
  $form['sharedemail_msg'] = array(
    '#type' => 'textarea',
    '#title' => t('Shared Email Message'),
    '#default_value' => variable_get('sharedemail_msg', $msg),
    '#rows' => 15,
    '#description' => t('Warning message that is only displayed to users with appropriate permission, when they choose to save an email address already used by another user.'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_user().
 */
function sharedemail_user($type, &$edit, &$user, $category = NULL) {
  $mail = isset($edit['mail']) ? $edit['mail'] : '';
  switch ($type) {
    case 'validate':

      // If no problems with validation, do nothing
      if (user_validate_mail($mail)) {
        return;
      }

      // Show warning message if more than 1 user with the same email
      $uid = db_result(db_query("SELECT uid FROM {users} WHERE uid <> %d AND LOWER(mail) = LOWER('%s')", $user->uid, $edit['mail']));
      if (!empty($uid)) {
        $edit['mail'] = 'sharedemail_' . $mail;
        if (module_exists('logintoboggan')) {
          $edit['conf_mail'] = 'sharedemail_' . $mail;
        }

        // Show warning text to those with the proper permission
        if (user_access('show warning text')) {
          drupal_set_message(variable_get('sharedemail_msg', ''));
        }
      }
      break;
    case 'submit':
    case 'update':
      if (strpos($mail, 'sharedemail_') == 0 && isset($edit['mail'])) {
        $edit['mail'] = str_replace('sharedemail_', '', $mail);
        if (module_exists('logintoboggan')) {
          $edit['conf_mail'] = str_replace('sharedemail_', '', $mail);
        }
      }
      break;

    // Creating a new user, this hook is called after db insert
    case 'insert':
      $mail = $user->mail;
      if (strpos($mail, 'sharedemail_') == 0) {
        $realmail = str_replace('sharedemail_', '', $mail);
        db_query("UPDATE {users} SET mail = '%s' WHERE uid = '%d'", $realmail, $user->uid);
        $user->mail = $realmail;
      }
      break;
  }
}

/**
 *  Implementation of hook_simpletest().
 */
function sharedemail_simpletest() {
  $module_name = 'Shared E-mail';
  $dir = drupal_get_path('module', 'sharedemail') . '/tests';
  $tests = file_scan_directory($dir, '\\.test$');
  return array_keys($tests);
}

Functions

Namesort descending Description
sharedemail_admin_settings Configure Sharedemail settings
sharedemail_help Implementation of hook_help().
sharedemail_menu Implementation of hook_menu().
sharedemail_perm Implementation of hook_perm().
sharedemail_simpletest Implementation of hook_simpletest().
sharedemail_user Implementation of hook_user().