View source
<?php
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;
}
function user_complete_profile_controller() {
$info = array();
$info['user'] = 'UserCompleteProfileController';
return $info;
}
function profile_complete_profile_controller() {
$info = array();
$info['profile'] = 'ProfileCompleteProfileController';
return $info;
}
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');
}
function complete_profile_page_build() {
if (complete_profile_can_redirect() && complete_profile_account_check() && ($redirect = complete_profile_get_redirect())) {
drupal_goto($redirect['path'], $redirect['options']);
}
}
function complete_profile_user_update(&$edit, $account, $category) {
if ($category == 'register' && module_exists('profile')) {
profile_save_profile($edit, $account, $category, TRUE);
}
if ($account->uid == $GLOBALS['user']->uid) {
complete_profile_set_account_checked(0);
}
}
function complete_profile_field_create_instance($instance) {
if ($instance['entity_type'] == 'user' && !empty($instance['required'])) {
complete_profile_update_check_timestamp();
}
}
function complete_profile_field_update_instance($instance, $prior_instance) {
if ($instance['entity_type'] == 'user' && !empty($instance['required']) && empty($prior_instance['required'])) {
complete_profile_update_check_timestamp();
}
}
function complete_profile_form_profile_field_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'complete_profile_update_check_timestamp';
}
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;
}
if ($_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != 'HEAD' || drupal_is_cli()) {
return FALSE;
}
$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(),
);
drupal_alter('complete_profile_redirect', $redirect, $account);
$destination = drupal_get_destination();
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;
}
}