You are here

function legal_form_user_profile_form_alter in Legal 7.2

Same name and namespace in other branches
  1. 7 legal.module \legal_form_user_profile_form_alter()

Implements hook_form_FORM_ID_alter().

File

./legal.module, line 290
Module file for Legal.

Code

function legal_form_user_profile_form_alter(&$form, $form_state) {
  global $user;
  global $language;
  $accepted = FALSE;
  if ($form['#user_category'] != 'account') {
    return;
  }

  // Set reminder to change password if coming from one time login link.
  if (isset($_REQUEST['pass-reset-token'])) {
    $messages = drupal_get_messages('status', FALSE);
    $status_messages = isset($messages['status']) ? $messages['status'] : array();
    $reminder = t('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.');
    if (!in_array($reminder, $status_messages)) {
      drupal_set_message($reminder);
    }
  }
  $account = $form['#user'];

  // Get last accepted version for this account.
  $legal_account = legal_get_accept($account->uid);

  // If no version has been accepted yet, get version with current language revision.
  if (empty($legal_account['version'])) {
    $conditions = legal_get_conditions($language->language);

    // No conditions set yet.
    if (empty($conditions['conditions'])) {
      return;
    }
  }
  else {
    $conditions = legal_get_conditions($legal_account['language']);

    // No conditions set yet.
    if (empty($conditions['conditions'])) {
      return;
    }

    // Check latest version of T&C has been accepted.
    $accepted = legal_version_check($account->uid, $conditions['version'], $conditions['revision'], $legal_account);

    // Enable language switching if version accepted and revision up to date.
    if ($accepted && $legal_account['language'] != $language->language) {
      $conditions = legal_get_conditions($language->language);
    }
  }
  $form = array_merge($form, legal_display_fields($conditions));
  if ($accepted === TRUE) {
    $form['legal']['legal_accept']['#value'] = 1;
    if (!empty($conditions['extras'])) {
      foreach ($conditions['extras'] as $key => $label) {
        if (!empty($label)) {
          $form['legal'][$key]['#value'] = 1;
        }
      }
    }
  }

  // Disable checkbox if:
  //  - user is not account owner;
  //  - latest T&C has already been accepted.
  if ($account->uid != $user->uid || $accepted == TRUE) {
    $form['legal']['legal_accept']['#attributes'] = array(
      'disabled' => 'disabled',
    );
    if (!empty($conditions['extras'])) {
      reset($conditions['extras']);
      foreach ($conditions['extras'] as $key => $label) {
        if (!empty($label)) {
          $form['legal'][$key]['#attributes'] = array(
            'disabled' => 'disabled',
          );
        }
      }
    }
  }

  // Not required if user is not account owner.
  if ($account->uid != $user->uid) {
    $form['legal']['legal_accept']['#required'] = FALSE;
    if (!empty($conditions['extras'])) {
      reset($conditions['extras']);
      foreach ($conditions['extras'] as $key => $label) {
        if (!empty($label)) {
          $form['legal'][$key]['#required'] = FALSE;
        }
      }
    }
  }

  // Enable account owner to accept.
  if ($account->uid == $user->uid && $accepted != TRUE) {
    $form['legal']['legal_accept']['#default_value'] = isset($edit['legal_accept']) ? $edit['legal_accept'] : '';
    $form['legal']['legal_accept']['#required'] = TRUE;
    if (!empty($conditions['extras'])) {
      reset($conditions['extras']);
      foreach ($conditions['extras'] as $key => $label) {
        if (!empty($label)) {
          $form['legal'][$key]['#default_value'] = isset($edit[$key]) ? $edit[$key] : '';
          $form['legal'][$key]['#required'] = TRUE;
        }
      }
    }
  }
  return theme('legal_display', array(
    'form' => $form,
  ));
}