You are here

account_profile.module in Account Profile 6

Same filename and directory in other branches
  1. 7.2 account_profile.module

Account Profile module

@author: Rafal W <kenorb@>

File

account_profile.module
View source
<?php

/**
 * @file
 *   Account Profile module
 *
 *
 * @author: Rafal W <kenorb@>
 */

/**
 * Implementation of hook_menu().
 */
function account_profile_menu() {
  $items['admin/settings/account_profile'] = array(
    'title' => 'Account Profile',
    'description' => t('Settings for Account Profile'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'account_profile_get_admin_form',
    ),
    'access arguments' => array(
      'administer account_profile',
    ),
    'file' => 'account_profile.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 *
 * @return array An array of valid permissions for the account_profile module
 */
function account_profile_perm() {
  return array(
    'administer account_profile',
  );
}

/**
 * Implementation of hook_form_alter().
 */
function account_profile_form_alter(&$form, $form_state, $form_id) {
  $profile = variable_get('account_profile_main_profile', key(content_profile_get_types('names')));

  // get main profile name
  switch ($form_id) {
    case 'user_profile_form':
      if (variable_get('account_profile_redirect', TRUE)) {
        if (isset($_REQUEST['destination'])) {
          $destination = $_REQUEST['destination'];
          unset($_REQUEST['destination']);
          drupal_goto($_GET['q'] . "/{$profile}", array(
            'destination' => $destination,
          ));
        }
        else {
          drupal_goto($_GET['q'] . "/{$profile}");
        }

        // header("Location: " . base_path() . $_GET['q'] . "/$profile");
        exit;
      }
      break;
    case $profile . '_node_form':

      /* integration with account form */
      if (is_numeric(arg(1))) {

        // activate only on edit page (not on registration page)
        // check if we are on a user account or node edit page
        if (arg(0) == 'user') {

          // on account edit page
          $user = user_load(arg(1));
        }
        elseif (arg(0) == 'node') {

          // on content profile node edit page
          $node = node_load(arg(1));
          $user = user_load($node->uid);
        }
        module_load_include('pages.inc', 'user');
        $form['account_profile_uid'] = array(
          '#type' => 'value',
          '#value' => $user->uid,
        );
        $account_form = user_profile_form(array(), $user);
        unset($account_form['submit'], $account_form['delete']);

        // remove duplicated Save button and Delete
        $form = array_merge($account_form, $form);
        $form['account']['#tree'] = TRUE;
        $form['#validate'][] = 'account_profile_form_validate';
        $form['#validate'][] = 'user_validate_picture';
        $form['#submit'][] = 'account_profile_form_submit';
        $form['#submit'][] = 'account_profile_user_profile_submit';
      }

      /* Integration with Notifications autosubscribe module  */
      if (module_exists('notifications_autosubscribe')) {

        // Call hook_form_alter from notifications_autosubscribe
        // Simulating we are on the user edit form so the
        // autosubscribe check box is included on the form
        notifications_autosubscribe_form_alter($form, $form_state, 'user_profile_form');
      }
      break;
    default:
  }
}

/**
 * Implementation of hook_menu_alter().
 *
 * Remove Account tab
 *
 * @param $items @type{array}
 * @return null
 */
function account_profile_menu_alter(&$items) {
  $profile = variable_get('account_profile_main_profile', key(content_profile_get_types('names')));

  // get main profile name
  $disable_edit_tab = variable_get('account_profile_edit_tab', TRUE);
  $items['user/%user_category/edit/' . $profile]['type'] = MENU_CALLBACK;

  // remove content profile tab
  if ($disable_edit_tab) {
    $items['user/%user_category/edit']['type'] = MENU_CALLBACK;
  }
}

/**
 * Validation function for the user account and profile editing form.
 */
function account_profile_form_validate($form, &$form_state) {
  user_module_invoke('validate', $form_state['values']['account'], $form_state['values']['_account'], $form_state['values']['_category']);

  // Validate the username when: user is editing own account and can change username; or an admin user
  if ($GLOBALS['user']->uid == $form['#uid'] && user_access('change own username') || user_access('administer users')) {
    if ($error = user_validate_name($form_state['values']['account']['name'])) {
      form_set_error('name', $error);
    }
  }
}

/**
 * Submit function for the user account and profile editing form.
 */
function account_profile_form_submit($form, &$form_state) {

  // Prepare account data and values for user_save
  $account = $form_state['values']['_account'];
  $category = $form_state['values']['_category'];

  // prevent to store node values in user->data field.
  $values = $form_state['values']['account'];

  // If username has changed, update the node's name for node_save()
  // otherwise it doesn't find the old username
  $form_state['values']['name'] = $form_state['values']['account']['name'];

  // Save user data
  user_module_invoke('submit', $values, $account, $category);
  user_save($account, $values, $category);

  // Clear the page cache because pages can contain usernames and/or profile information:
  cache_clear_all();
  drupal_set_message(t('User Account ' . $form_state['values']['account']['name'] . ' has been updated.'));
  return;
}

Functions

Namesort descending Description
account_profile_form_alter Implementation of hook_form_alter().
account_profile_form_submit Submit function for the user account and profile editing form.
account_profile_form_validate Validation function for the user account and profile editing form.
account_profile_menu Implementation of hook_menu().
account_profile_menu_alter Implementation of hook_menu_alter().
account_profile_perm Implementation of hook_perm().