You are here

function signup_sign_up_user in Signup 5

Same name and namespace in other branches
  1. 5.2 signup.module \signup_sign_up_user()
  2. 6.2 signup.module \signup_sign_up_user()
  3. 6 signup.module \signup_sign_up_user()
  4. 7 signup.module \signup_sign_up_user()

Signs up a user to a node.

NOTE: other modules can call this function. To do so, $signup_form must be as follows:

$signup_form['nid'] : nid of the node to which the user will be signed up $signup_form['uid'] : uid of the user to sign up $signup_form['signup_anon_mail'] : Optional. An email address of an anonymous user to sign up. Only include this if the user is not already registered with the site. $signup_form['uid'] will automatically be set to 0 if this element is passed in. NOTE: It's highly recommended to call the signup_validate_anon_email function in the external module's validation cycle or perform that function's validation logic prior to passing in this element! $signup_form['signup_form_data'] : an array of key/value pairs -- key is the data category, value is the user input

1 call to signup_sign_up_user()
signup_form_submit in ./signup.module
Executes the user signup form

File

./signup.module, line 1003

Code

function signup_sign_up_user($signup_form) {
  $node = node_load($signup_form['nid']);

  // Since this is an API call, we need to validate that there are no
  // duplicate signups being generated, even though through the usual
  // UI, there's no way to reach this function if it's a duplicate.
  // How to find duplicates is different for anonymous and
  // authenticated signups.
  if (!empty($signup_form['signup_anon_mail'])) {

    // Ensure the uid is 0 for anonymous signups, even if it's not duplicate.
    $signup_form['uid'] = 0;

    // Now, see if this email is already signed-up.
    if (db_num_rows(db_query("SELECT anon_mail FROM {signup_log} WHERE anon_mail = '%s' AND nid = %d", $signup_form['signup_anon_mail'], $node->nid))) {
      drupal_set_message(t('Anonymous user %email is already signed up for %title', array(
        '%email' => $signup_form['signup_anon_mail'],
        '%title' => $node->title,
      ), 'error'));
      return FALSE;
    }
  }
  else {

    // This query does the JOIN on {users} so we can avoid a full
    // user_load() just so theme('username') can have the data it
    // needs for the error message we might print out.
    $query = db_query("SELECT sl.uid, u.name FROM {signup_log} sl INNER JOIN {users} u ON sl.uid = u.uid WHERE sl.uid = %d AND sl.nid = %d", $signup_form['uid'], $signup_form['nid']);
    if (db_num_rows($query)) {
      $user = db_fetch_object($query);
      drupal_set_message(t('User !user is already signed up for %title', array(
        '!user' => theme('username', $user),
        '%title' => $node->title,
      )), 'error');
      return FALSE;
    }
  }

  // If we made it this far, we're going to need the full $user object.
  $user = user_load(array(
    'uid' => $signup_form['uid'],
  ));
  if (user_access('sign up for content') && !$node->signup_completed) {

    // Grab the current time once, since we need it in a few places.
    $curtime = time();

    // Allow other modules to inject data into the user's signup data.
    $extra = module_invoke_all('signup_sign_up', $node, $user);
    $signup_info = array();
    if (!empty($signup_form['signup_form_data'])) {
      $signup_info = $signup_form['signup_form_data'];
    }
    if (!empty($extra)) {
      $signup_info = array_merge($signup_info, $extra);
    }
    $signup_form_data = serialize($signup_info);

    // Figure out if confirmation or reminder emails will be sent and
    // inform the user.
    $confirmation_email = $node->signup_send_confirmation ? '  ' . t('You will receive a confirmation email shortly which contains further event information.') : '';
    $reminder_email = $node->signup_send_reminder ? '  ' . t('You will receive a reminder email !number !days before the event.', array(
      '!number' => $node->signup_reminder_days_before,
      '!days' => format_plural($node->signup_reminder_days_before, 'day', 'days'),
    )) : '';

    // Insert the user into the signup_log.
    db_query("INSERT INTO {signup_log} (uid, nid, anon_mail, signup_time, form_data) VALUES (%d, %d, '%s', %d, '%s')", $signup_form['uid'], $signup_form['nid'], $signup_form['signup_anon_mail'], $curtime, $signup_form_data);

    // Must include this here as event module doesn't include timezone
    // support on all page requests.
    if (module_exists('event')) {
      require_once drupal_get_path('module', 'event') . '/event_timezones.inc';
    }

    // Format the start time, and compose the user's signup data for
    // later use in the emails.
    $offset = $node->event_start ? event_get_offset($node->timezone, $node->event_start) : '';
    $starttime = $node->event_start ? _event_date(variable_get('signup_date_string', 'D, M jS, g:i A'), $node->event_start, $offset) : t('[Untimed]');
    $signup_data = '';
    if (isset($signup_form['signup_form_data'])) {
      $signup_data = theme('signup_email_token_custom_data', $signup_form['signup_form_data']);
    }

    // Determine if this is an anon signup or not, and get the
    // appropriate email address to use.
    $user_mail = $signup_form['signup_anon_mail'] ? $signup_form['signup_anon_mail'] : $user->mail;

    // This is not used for web display, only email, so the output is
    // not being sanitized and linked as we would for web output.
    $trans = array(
      "%event" => $node->title,
      "%time" => $starttime,
      "%username" => $user->name,
      "%useremail" => $user_mail,
      "%info" => $signup_data,
    );
    $from = variable_get('site_mail', ini_get('sendmail_from'));

    // If a confirmation is to be sent, compose the mail message,
    // translate the string substitutions, and send it.
    if ($node->signup_send_confirmation && $user_mail) {
      $subject = t('Signup confirmation for event: !event', array(
        '!event' => $node->title,
      ));
      $message = strtr($node->signup_confirmation_email, $trans);
      drupal_mail('signup_confirmation_mail', $user_mail, $subject, $message, $from);
    }

    // If a forwarding email is to be sent, compose the mail message,
    // translate the string substitutions, and send it.
    if ($node->signup_forwarding_email) {
      $header = array(
        'From' => t('New Event Signup') . "<{$from}>",
      );
      $subject = t('Signup confirmation for event: !title', array(
        '!title' => $node->title,
      ));
      $message = t('The following information was submitted as a signup for !title', array(
        '!title' => $node->title,
      )) . "\n\r" . t('Date/Time: !time', array(
        '!time' => $starttime,
      )) . ":\n\r\n\r" . "\n\r" . t('Username:') . $user->name;
      if (!empty($user->uid)) {

        // For authenticated users, just include a link to their profile page.
        $message .= "\n\r" . t('Profile page:') . url('user/' . $user->uid, NULL, NULL, TRUE);
      }
      else {

        // For anonymous users, their email is all we've got, so disclose it.
        $message .= "\n\r" . t('E-mail:') . $user_mail;
      }
      $message .= "\n\r\n\r" . $signup_data;
      drupal_mail('signup_forwarding_mail', $node->signup_forwarding_email, $subject, $message, $from, $header);
    }
    drupal_set_message(t('Signup to !title confirmed.', array(
      '!title' => l($node->title, "node/{$node->nid}"),
    )) . $confirmation_email . $reminder_email);
  }
  else {
    drupal_access_denied();
  }
}