<?php
// $Id: profiletabs.module,v 1.2 2008/04/08 14:49:21 maartenvg Exp $

/**
 * Get profile categories
 */
function profiletabs_get_categories() {
  global $user;

  module_invoke_all('user', 'view', NULL, $user);

  $categories = array();
  foreach($user->content as $key => $data) {
    if (isset($data['#title'])) {
      $categories[$key] = $data['#title'];
    }
  }
  return $categories;
}

/**
 * Implementation of hook_menu().
 */
function profiletabs_menu() {
  global $user;
  $items = array();

  $categories = profiletabs_get_categories();

  // Profile tabs admin settings.
  $items['admin/settings/profiletabs'] = array(
    'title'            => 'Profile Tabs',
    'description'      => 'Administer the profile tabs. Which are shown and which is the default.',
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('profiletabs_admin_settings'),
    'access arguments' => array('administer site configuration')
  );

  // Default tab
  $default = variable_get('profiletabs_default', '');
  $default = $categories[$default];
  $items['user/%user/view/'. $default] = array(
    'title'            => $default,
    'type'             => MENU_DEFAULT_LOCAL_TASK,
    'page callback'    => 'user_view',
    'page arguments'   => array(1),
    'access arguments' => array('access user profiles'),
    'file'             => 'user.pages.inc',
    'file path'        => drupal_get_path('module', 'user'),
    'weight'           => 2,
  );

  // Other tabs
  foreach (profiletabs_get_categories() as $key => $name) {
    if ($default != $name && $name != NULL) {
      // Check that it is not hidden
      if (!variable_get('profiletabs_hidden_' . $key, 0)) {
        $items['user/%user/view/'. $name] = array(
          'title'            => $name,
          'type'             => MENU_LOCAL_TASK,
          'page callback'    => 'user_view',
          'page arguments'   => array(1),
          'access arguments' => array('access user profiles'),
          'file'             => 'user.pages.inc',
          'file path'        => drupal_get_path('module', 'user'),
          'weight'           => 3,
        );
      }
    }
  }
  return $items;
}

/**
 * Generate settings form.
 */
function profiletabs_admin_settings() {
  // Get categories
  $categories =  profiletabs_get_categories();

  $form = array();

  $form['profiletabs_default'] = array(
    '#type'          => 'select',
    '#title'         => t('Default category'),
    '#default_value' => variable_get('profiletabs_default', ''),
    '#description'   => t('Choose which profile category should be shown by default.'),
    '#options'       => $categories,
  );
  
  $form['hidden'] = array(
    '#type'          => 'fieldset',
    '#title'         => t('Hidden categories'),
    '#description'   => t('Choose which categories should not have a tab on its own, but should be shown on the default tab.'),
  );

  // Form for hidden tabs
  foreach($categories as $key => $name) {
    $form['hidden']['profiletabs_hidden_' . $key] = array(
      '#type'          => 'checkbox',
      '#title'         => $name,
      '#default_value' => variable_get('profiletabs_hidden_' . $key, 0),
    );
  }
  
  $form['#submit'][] = 'profiletabs_admin_settings_submit';
  return system_settings_form($form);
}

/**
* Rebuild profile tabs on submit
*/
function profiletabs_admin_settings_submit($form, $form_state) {
  menu_rebuild();
}

/**
 *  Implementation of hook_profile_alter().
 */
function profiletabs_profile_alter(&$account) {  
  // Which tab are we trying to see?
  $goal = arg(3);
  
  // Get default tab
  $categories = profiletabs_get_categories();
  $default = variable_get('profiletabs_default', '');
  $default = $categories[$default];

  // If the current default tab isn't visible (e.g. when you select a tab that
  // is only visible for admins) default to the History-tab.
  if (!isset($account->content[$default])) {
    $default = t('History');
  }

  // No goal? Go to the default tab. Else check if we're at the default tab.
  if (empty($goal)) {
    $goal = $default;
    $default_check = 1;
  }
  else {
    $default_check = $goal == $default;
  }
  
  // Remove all fields that are not to be shown on this tab, i.e. fields that are not
  // the current field and tabs that are hidden and it isn't the default tab.
  foreach ($account->content as $key => $field) {
    $title = $field['#title'];
    if ($title != $goal) {
      unset($account->content[$key]);
    }
  }
}
