You are here

function legal_form_user_profile_form_alter in Legal 7

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

Implements hook_form_FORM_ID_alter().

File

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

Code

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

  // If this profile belongs to user 1 or if it has an exempt user role we don't
  // display Terms & Conditions. Pass the user of the profile being viewed, not
  // the current user.
  if (legal_user_is_exempt($account)) {
    return;
  }
  if ($form['#user_category'] != 'account') {
    return;
  }

  // Set reminder to change password if coming from one time login link and
  // user hasn't changed his/her password yet.
  if (isset($_REQUEST['pass-reset-token']) && isset($_COOKIE['Drupal_visitor_legal_pass_reset'])) {
    user_cookie_delete('legal_pass_reset');
    $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);
    }
  }

  // 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;
        }
      }
    }
  }
  $form = theme('legal_display', array(
    'form' => $form,
  ));
}