| 1 |
<?php
|
| 2 |
// $Id: profiletabs.module,v 1.2 2008/04/08 14:49:21 maartenvg Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_menu().
|
| 6 |
*/
|
| 7 |
function profiletabs_menu($may_cache) {
|
| 8 |
if ($may_cache) {
|
| 9 |
$items[] = array(
|
| 10 |
'path' => 'admin/settings/profiletabs',
|
| 11 |
'title' => t('Profile Tabs'),
|
| 12 |
'description' => t('Administer the profile tabs. Which are shown and which is the default.'),
|
| 13 |
'callback' => 'drupal_get_form',
|
| 14 |
'callback arguments' => array('profiletabs_admin_settings'),
|
| 15 |
'access' => user_access('administer site configuration'),
|
| 16 |
);
|
| 17 |
}
|
| 18 |
else {
|
| 19 |
// Are we at a profile? Continue
|
| 20 |
if (arg(0) == 'user' && is_numeric(arg(1)) && arg(1) > 0) {
|
| 21 |
// Load profile owner
|
| 22 |
$account = user_load(array('uid' => arg(1)));
|
| 23 |
|
| 24 |
$categories = module_invoke_all('user', 'view', '', $account);
|
| 25 |
$default = variable_get('profiletabs_default', t('History'));
|
| 26 |
$hidden = variable_get('profiletabs_hidden', array());
|
| 27 |
|
| 28 |
// If the current default tab isn't visible (e.g. when you select a tab that
|
| 29 |
// is only visible for admins) default to the History-tab.
|
| 30 |
if (!isset($categories[$default])) {
|
| 31 |
$default = t('History');
|
| 32 |
}
|
| 33 |
|
| 34 |
if ($account !== FALSE) {
|
| 35 |
// Make tabs for all non-hidden categories
|
| 36 |
foreach ($categories as $key => $category) {
|
| 37 |
if (empty($hidden[$key]) && !empty($category)) {
|
| 38 |
$items[] = array(
|
| 39 |
'path' => 'user/'. arg(1) .'/view/'. $key,
|
| 40 |
'title' => $key,
|
| 41 |
'type' => $key == $default ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
|
| 42 |
);
|
| 43 |
}
|
| 44 |
}
|
| 45 |
}
|
| 46 |
}
|
| 47 |
}
|
| 48 |
return $items;
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Generate settings form.
|
| 53 |
*/
|
| 54 |
function profiletabs_admin_settings() {
|
| 55 |
global $user;
|
| 56 |
$form = array();
|
| 57 |
|
| 58 |
// Get categories
|
| 59 |
$categories = module_invoke_all('user', 'view', '', $user);
|
| 60 |
foreach ($categories as $key => $category) {
|
| 61 |
if (!empty($category)) {
|
| 62 |
$options[$key] = $key;
|
| 63 |
}
|
| 64 |
}
|
| 65 |
|
| 66 |
$form['profiletabs_default'] = array(
|
| 67 |
'#type' => 'select',
|
| 68 |
'#title' => t('Default category'),
|
| 69 |
'#default_value' => variable_get('profiletabs_default', t('History')),
|
| 70 |
'#description' => t('Choose which profile category should be shown by default.'),
|
| 71 |
'#options' => $options,
|
| 72 |
);
|
| 73 |
|
| 74 |
$form['profiletabs_hidden'] = array(
|
| 75 |
'#type' => 'checkboxes',
|
| 76 |
'#title' => t('Hidden tabs'),
|
| 77 |
'#default_value' => variable_get('profiletabs_hidden', array()),
|
| 78 |
'#description' => t('Choose which categories should not have a tab on its own, but should be shown on the default tab.'),
|
| 79 |
'#options' => $options,
|
| 80 |
);
|
| 81 |
return system_settings_form($form);
|
| 82 |
}
|
| 83 |
|
| 84 |
|
| 85 |
/**
|
| 86 |
* Implementation of hook_profile_alter().
|
| 87 |
*/
|
| 88 |
function profiletabs_profile_alter(&$account, &$fields) {
|
| 89 |
// Which tab are we trying to see?
|
| 90 |
$goal = arg(3);
|
| 91 |
|
| 92 |
// Get default tab
|
| 93 |
$default = variable_get('profiletabs_default', t('History'));
|
| 94 |
// Get hidden tabs
|
| 95 |
$hidden = variable_get('profiletabs_hidden', array());
|
| 96 |
|
| 97 |
// If the current default tab isn't visible (e.g. when you select a tab that
|
| 98 |
// is only visible for admins) default to the History-tab.
|
| 99 |
if (!isset($fields[$default])) {
|
| 100 |
$default = t('History');
|
| 101 |
}
|
| 102 |
|
| 103 |
// No goal? Go to the default tab. Else check if we're at the default tab.
|
| 104 |
if (empty($goal)) {
|
| 105 |
$goal = $default;
|
| 106 |
$default_check = 1;
|
| 107 |
}
|
| 108 |
else {
|
| 109 |
$default_check = $goal == $default;
|
| 110 |
}
|
| 111 |
|
| 112 |
// Remove all fields that are not to be shown on this tab, i.e. fields that are not
|
| 113 |
// the current field and tabs that are hidden and it isn't the default tab.
|
| 114 |
foreach ($fields as $key => $field) {
|
| 115 |
if ($key!=$goal && !(!empty($hidden[$key]) && $default_check)) {
|
| 116 |
unset($fields[$key]);
|
| 117 |
}
|
| 118 |
}
|
| 119 |
}
|