You are here

complete_profile.module in Complete profile 7

File

complete_profile.module
View source
<?php

/**
 * Implements hook_menu().
 */
function complete_profile_menu() {
  $items['user/%user/complete-profile'] = array(
    'title' => 'Complete your profile',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'complete_profile_form',
      1,
    ),
    'access callback' => 'complete_profile_form_access',
    'access arguments' => array(
      1,
    ),
    'type' => MENU_CALLBACK,
    'file' => 'complete_profile.pages.inc',
  );
  return $items;
}

/**
 * Implements hook_complete_profile_controller() on behalf of user module.
 */
function user_complete_profile_controller() {
  $info = array();
  $info['user'] = 'UserCompleteProfileController';
  return $info;
}

/**
 * Implements hook_complete_profile_controller() on behalf of profile module.
 */
function profile_complete_profile_controller() {
  $info = array();
  $info['profile'] = 'ProfileCompleteProfileController';
  return $info;
}

/**
 * Return an array of complete profile controller classes.
 */
function profile_complete_get_controllers() {
  $controllers =& drupal_static(__FUNCTION__);
  if (!isset($controllers)) {
    $controllers = module_invoke_all('complete_profile_controller');
    drupal_alter('complete_profile_controller', $controllers);
  }
  return array_filter($controllers, 'class_exists');
}

/**
 * Implements hook_page_build().
 */
function complete_profile_page_build() {

  // First check if we can redirect in the first place before inspecting the
  // user fields. Then check for empty fields, and then get the redirect.
  if (complete_profile_can_redirect() && complete_profile_account_check() && ($redirect = complete_profile_get_redirect())) {
    drupal_goto($redirect['path'], $redirect['options']);
  }
}

/**
 * Implements hook_user_update().
 */
function complete_profile_user_update(&$edit, $account, $category) {

  // Updating registration fields for an existing user.
  if ($category == 'register' && module_exists('profile')) {
    profile_save_profile($edit, $account, $category, TRUE);
  }

  // Clear the complete profile check.
  if ($account->uid == $GLOBALS['user']->uid) {
    complete_profile_set_account_checked(0);
  }
}

/**
 * Implements hook_field_create_instance().
 */
function complete_profile_field_create_instance($instance) {

  // A new required user field has been added. Reset the timestamp check.
  if ($instance['entity_type'] == 'user' && !empty($instance['required'])) {
    complete_profile_update_check_timestamp();
  }
}

/**
 * Implements hook_field_update_instance().
 */
function complete_profile_field_update_instance($instance, $prior_instance) {

  // A user field has been converted from unrequired to required. Reset the
  // timestamp check variable.
  if ($instance['entity_type'] == 'user' && !empty($instance['required']) && empty($prior_instance['required'])) {
    complete_profile_update_check_timestamp();
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function complete_profile_form_profile_field_form_alter(&$form, &$form_state) {
  $form['#submit'][] = 'complete_profile_update_check_timestamp';
}

/**
 * Reset the minimum timestamp that requires re-checking all accounts.
 */
function complete_profile_update_check_timestamp() {
  variable_set('complete_profile_check_timestamp', REQUEST_TIME);
}
function complete_profile_can_redirect() {
  if (empty($GLOBALS['user']->uid)) {
    return FALSE;
  }

  // Ensure that POST/PUT and CLI requests are not redirected.
  if ($_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != 'HEAD' || drupal_is_cli()) {
    return FALSE;
  }

  // Certain paths cannot be redirected to the complete profile form.
  $exclude_paths = variable_get('complete_profile_exclude_paths', implode("\n", array(
    'user/*/complete-profile',
    'user/logout',
    'admin/*',
    'contact',
  )));
  if (drupal_match_path(current_path(), $exclude_paths)) {
    return FALSE;
  }
  return TRUE;
}
function complete_profile_account_check() {
  if (complete_profile_get_account_checked() > variable_get('complete_profile_check_timestamp', 0)) {
    return FALSE;
  }
  $account = user_load($GLOBALS['user']->uid);
  if (complete_profile_entity_has_empty_required_fields('user', $account)) {
    return TRUE;
  }
  else {
    complete_profile_set_account_checked(REQUEST_TIME);
    return FALSE;
  }
}
function complete_profile_get_account_checked() {
  return !empty($_SESSION['complete_profile_checked']) ? $_SESSION['complete_profile_checked'] : 0;
}
function complete_profile_set_account_checked($timestamp) {
  $_SESSION['complete_profile_checked'] = $timestamp;
}
function complete_profile_entity_has_empty_required_fields($entity_type, $entity) {
  $controllers = profile_complete_get_controllers();
  foreach ($controllers as $controller) {
    if ($controller::hasEmptyRequiredFields($entity)) {
      return TRUE;
    }
  }
  return FALSE;
}
function complete_profile_get_redirect($account = NULL) {
  if (!isset($account)) {
    $account = user_load($GLOBALS['user']->uid);
  }
  $redirect = array();
  $redirect['path'] = 'user/' . $account->uid . '/complete-profile';
  $redirect['options'] = array(
    'query' => array(),
  );

  // Allow modules to alter the redirect destination.
  drupal_alter('complete_profile_redirect', $redirect, $account);
  $destination = drupal_get_destination();

  // Unset the global destination since we don't want drupal_goto() to read
  // it and since we're passing the destination into the query string again.
  unset($_GET['destination']);
  $redirect['options']['query'] += $destination;
  return $redirect;
}
function complete_profile_form_access($account) {
  if (empty($account->uid)) {
    return FALSE;
  }
  elseif (!complete_profile_entity_has_empty_required_fields('user', $account)) {
    return FALSE;
  }
  elseif (user_access('administer users')) {
    return TRUE;
  }
  else {
    return $GLOBALS['user']->uid == $account->uid;
  }
}