You are here

function sharedemail_user in Shared Email 6

Same name and namespace in other branches
  1. 5 sharedemail.module \sharedemail_user()

Implementation of hook_user().

File

./sharedemail.module, line 71
Allows users to share an email address

Code

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;
  }
}