| 1 |
<?php
|
| 2 |
// $Id: accountmenu.module,v 1.7 2009/09/29 18:11:29 mattyoung Exp $
|
| 3 |
/**
|
| 4 |
* @file accountmenu.module
|
| 5 |
* Provide a dynamic Log in/My account/Log out account menu
|
| 6 |
*
|
| 7 |
* This module provides a dynamic menu for login, logout and access to my account page.
|
| 8 |
* The menu is dynamic such that when he user is not logged in, only the "Log in" link
|
| 9 |
* is shown. After the user has logged in, the menu change to "My account" "Log out".
|
| 10 |
* The "Log in" link returns to the original page from where the user originally click.
|
| 11 |
* This menu defaults to the menu name "Account menu". However, it can be move to
|
| 12 |
* any menu at admin/settings/accountmenu.
|
| 13 |
*
|
| 14 |
*/
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
function accountmenu_help($path, $arg) {
|
| 19 |
$output = '';
|
| 20 |
switch ($path) {
|
| 21 |
case 'admin/help#accountmenu':
|
| 22 |
$output .= '<p>'. t('Provides a dynamic "Log in|My account|Log out" account menu') .'</p>';
|
| 23 |
$output .= '<p>'. t('By default, the links are under the menu "Account menu", they can be <a href="@settings">moved</a> to be part of any menu.', array('@settings' => url('admin/settings/accountmenu'))) .'</p>';
|
| 24 |
$output .= '<p>'. t('The Account menu links can be <a href="@customize">customized</a>, the "My account" link title can include the tokens <em>!name</em> or <em>!realname</em>, <em>!name</em> is replaced with the log in name and <em>!realname</em> is replaced with the real name if the <a href="@realname-module">RealName module</a> is installed, if not, <em>!realname</em> is replace with the log in name.',
|
| 25 |
array('@customize' => url('admin/build/menu-customize/'. variable_get('accountmenu_menu_name', 'accountmenu')),
|
| 26 |
'@realname-module' => url('http://drupal.org/project/realname', array('external' => TRUE)),
|
| 27 |
)) .'</p>';
|
| 28 |
$output .= '<p>'. t('The Account menu links are displayed with a <a href="@block">block</a>.',
|
| 29 |
array('@block' => url('admin/build/block'),
|
| 30 |
)) .'</p>';
|
| 31 |
break;
|
| 32 |
}
|
| 33 |
return $output;
|
| 34 |
}
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
function accountmenu_menu() {
|
| 39 |
$items['admin/settings/accountmenu'] = array(
|
| 40 |
'title' => 'Account menu',
|
| 41 |
'description' => 'Configure account menu',
|
| 42 |
'page callback' => 'drupal_get_form',
|
| 43 |
'page arguments' => array('accountmenu_settings_form'),
|
| 44 |
'access arguments' => array('administer site configuration'),
|
| 45 |
'type' => MENU_NORMAL_ITEM,
|
| 46 |
'file' => 'accountmenu.admin.inc',
|
| 47 |
);
|
| 48 |
|
| 49 |
return $items;
|
| 50 |
}
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
function _accountmenu_reset_menu() {
|
| 55 |
// delete our links before making them again. we are really doing a move
|
| 56 |
// but there is no move api call so we delete first and re-create.
|
| 57 |
// !!! any menu cusgtomization made through the menu module UI are lost.
|
| 58 |
_accountmenu_delete_menu();
|
| 59 |
_accountmenu_setup_menu();
|
| 60 |
$t = get_t();
|
| 61 |
drupal_set_message($t('Account menu has been reset, any menu customization is lost and must be <a href="@customize">customized</a> again.', array('@customize' => url('admin/build/menu-customize/'. variable_get('accountmenu_menu_name', 'accountmenu')))));
|
| 62 |
}
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
function _accountmenu_delete_menu() {
|
| 67 |
$result = db_query("SELECT * FROM {menu_links} WHERE module = 'accountmenu'");
|
| 68 |
while ($link = db_fetch_array($result)) {
|
| 69 |
_menu_delete_item($link);
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
function _accountmenu_setup_menu() {
|
| 76 |
$item = array(
|
| 77 |
'link_title' => 'Log in/Create account',
|
| 78 |
'link_path' => 'user/login',
|
| 79 |
'options' => array('alter' => TRUE,
|
| 80 |
'attributes' => array('title' => t('Log in, create new account or request new password.'),
|
| 81 |
'class' => 'popups-form-reload',
|
| 82 |
),
|
| 83 |
),
|
| 84 |
'weight' => 0,
|
| 85 |
'menu_name' => variable_get('accountmenu_menu_name', 'accountmenu'),
|
| 86 |
'module' => 'accountmenu',
|
| 87 |
);
|
| 88 |
menu_link_save($item);
|
| 89 |
|
| 90 |
$item = array(
|
| 91 |
'link_title' => 'Register',
|
| 92 |
'link_path' => 'user/register',
|
| 93 |
'hidden' => 1,
|
| 94 |
'options' => array('alter' => TRUE,
|
| 95 |
'attributes' => array('title' => t('Register a new account'),
|
| 96 |
'class' => 'popups-form-reload',
|
| 97 |
),
|
| 98 |
),
|
| 99 |
'weight' => 0,
|
| 100 |
'menu_name' => variable_get('accountmenu_menu_name', 'accountmenu'),
|
| 101 |
'module' => 'accountmenu',
|
| 102 |
);
|
| 103 |
menu_link_save($item);
|
| 104 |
|
| 105 |
$item = array(
|
| 106 |
'link_title' => 'My account',
|
| 107 |
'link_path' => 'user/%',
|
| 108 |
'title_callback' => NULL,
|
| 109 |
'title_arguments' => NULL,
|
| 110 |
'options' => array('alter' => TRUE,
|
| 111 |
'attributes' => array('title' => t('View user profile.'))),
|
| 112 |
'weight' => 0,
|
| 113 |
'menu_name' => variable_get('accountmenu_menu_name', 'accountmenu'),
|
| 114 |
'module' => 'accountmenu',
|
| 115 |
);
|
| 116 |
menu_link_save($item);
|
| 117 |
|
| 118 |
$item = array(
|
| 119 |
'link_title' => 'Log out',
|
| 120 |
'link_path' => 'logout',
|
| 121 |
'weight' => 1,
|
| 122 |
'menu_name' => variable_get('accountmenu_menu_name', 'accountmenu'),
|
| 123 |
'module' => 'accountmenu',
|
| 124 |
);
|
| 125 |
menu_link_save($item);
|
| 126 |
}
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
function _accountmenu_menu_need_destination($path) {
|
| 131 |
return in_array($path, array(
|
| 132 |
'user/login',
|
| 133 |
'user/register',
|
| 134 |
));
|
| 135 |
}
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
function accountmenu_translated_menu_link_alter(&$item) {
|
| 140 |
if ($item['module'] != 'accountmenu') {
|
| 141 |
return;
|
| 142 |
}
|
| 143 |
if (_accountmenu_menu_need_destination($item['link_path'])) {
|
| 144 |
if (function_exists('popups_add_popups')) {
|
| 145 |
popups_add_popups();
|
| 146 |
}
|
| 147 |
if (_accountmenu_is_404() || arg(0) === 'user') {
|
| 148 |
$item['localized_options']['query'] = 'destination=<front>'; // on a 404 page or any 'user' paths, send user to the front
|
| 149 |
}
|
| 150 |
else {
|
| 151 |
$item['localized_options']['query'] = drupal_get_destination();
|
| 152 |
}
|
| 153 |
// potx will complain here, but line 496 of menu.inc is doing the same so
|
| 154 |
$item['title'] = t($item['link_title']);
|
| 155 |
}
|
| 156 |
elseif ($item['link_path'] == 'user/%') {
|
| 157 |
// I don't do check_plain() here because menu display will do it
|
| 158 |
global $user;
|
| 159 |
$name = $realname = _accountmenu_ucfirst($user->name);
|
| 160 |
if (function_exists('realname_get_user')) {
|
| 161 |
$account = realname_get_user($user->uid); // initialize realname
|
| 162 |
$realname = $account->name;
|
| 163 |
}
|
| 164 |
// potx will complain here, but line 496 of menu.inc is doing the same so
|
| 165 |
$item['title'] = t($item['link_title'], array('!name' => $name,
|
| 166 |
'!realname' => $realname));
|
| 167 |
}
|
| 168 |
}
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
function _accountmenu_is_404() {
|
| 173 |
return strpos(drupal_get_headers(), 'HTTP/1.1 404 Not Found') != FALSE;
|
| 174 |
}
|
| 175 |
|
| 176 |
|
| 177 |
function _accountmenu_ucfirst($string) {
|
| 178 |
return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1);
|
| 179 |
}
|