| 1 |
<?php
|
| 2 |
// $Id: user.tokens.inc,v 1.1 2009/08/19 20:19:37 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Builds placeholder replacement tokens for user-related data.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implement hook_token_info().
|
| 11 |
*/
|
| 12 |
function user_token_info() {
|
| 13 |
$types['user'] = array(
|
| 14 |
'name' => t('Users'),
|
| 15 |
'description' => t('Tokens related to individual user accounts.'),
|
| 16 |
'needs-data' => 'user',
|
| 17 |
);
|
| 18 |
$types['current-user'] = array(
|
| 19 |
'name' => t('Current user'),
|
| 20 |
'description' => t('Tokens related to the currently logged in user.'),
|
| 21 |
'type' => 'user',
|
| 22 |
);
|
| 23 |
|
| 24 |
$user['uid'] = array(
|
| 25 |
'name' => t('User ID'),
|
| 26 |
'description' => t("The unique ID of the user account."),
|
| 27 |
);
|
| 28 |
$user['name'] = array(
|
| 29 |
'name' => t("Name"),
|
| 30 |
'description' => t("The login name of the user account."),
|
| 31 |
);
|
| 32 |
$user['mail'] = array(
|
| 33 |
'name' => t("Email"),
|
| 34 |
'description' => t("The email address of the user account."),
|
| 35 |
);
|
| 36 |
$user['url'] = array(
|
| 37 |
'name' => t("URL"),
|
| 38 |
'description' => t("The URL of the account profile page."),
|
| 39 |
);
|
| 40 |
$user['edit-url'] = array(
|
| 41 |
'name' => t("Edit URL"),
|
| 42 |
'description' => t("The url of the account edit page."),
|
| 43 |
);
|
| 44 |
|
| 45 |
$user['last-login'] = array(
|
| 46 |
'name' => t("Last login"),
|
| 47 |
'description' => t("The date the user last logged in to the site."),
|
| 48 |
'type' => 'date',
|
| 49 |
);
|
| 50 |
$user['created'] = array(
|
| 51 |
'name' => t("Created"),
|
| 52 |
'description' => t("The date the user account was created."),
|
| 53 |
'type' => 'date',
|
| 54 |
);
|
| 55 |
|
| 56 |
return array(
|
| 57 |
'types' => array('user' => $types),
|
| 58 |
'tokens' => array('user' => $user),
|
| 59 |
);
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
| 63 |
* Implement hook_tokens().
|
| 64 |
*/
|
| 65 |
function user_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
| 66 |
global $user;
|
| 67 |
$url_options = array('absolute' => TRUE);
|
| 68 |
if (isset($options['language'])) {
|
| 69 |
$url_options['language'] = $options['language'];
|
| 70 |
$language_code = $options['language']->language;
|
| 71 |
}
|
| 72 |
else {
|
| 73 |
$language_code = NULL;
|
| 74 |
}
|
| 75 |
$sanitize = !empty($options['sanitize']);
|
| 76 |
|
| 77 |
$replacements = array();
|
| 78 |
|
| 79 |
if ($type == 'user' && !empty($data['user'])) {
|
| 80 |
$account = $data['user'];
|
| 81 |
foreach ($tokens as $name => $original) {
|
| 82 |
switch ($name) {
|
| 83 |
// Basic user account information.
|
| 84 |
case 'uid':
|
| 85 |
$replacements[$original] = $account->uid;
|
| 86 |
break;
|
| 87 |
|
| 88 |
case 'name':
|
| 89 |
$name = ($account->uid == 0) ? variable_get('anonymous', t('Anonymous')) : $account->name;
|
| 90 |
$replacements[$original] = $sanitize ? filter_xss($name) : $name;
|
| 91 |
break;
|
| 92 |
|
| 93 |
case 'mail':
|
| 94 |
$replacements[$original] = $sanitize ? check_plain($account->mail) : $account->mail;
|
| 95 |
break;
|
| 96 |
|
| 97 |
case 'url':
|
| 98 |
$replacements[$original] = url("user/$account->uid", $url_options);
|
| 99 |
break;
|
| 100 |
|
| 101 |
case 'edit-url':
|
| 102 |
$replacements[$original] = url("user/$account->uid/edit", $url_options);
|
| 103 |
break;
|
| 104 |
|
| 105 |
// These tokens are default variations on the chained tokens handled below.
|
| 106 |
case 'last-login':
|
| 107 |
$replacements[$original] = format_date($account->login, 'medium', '', NULL, $language_code);
|
| 108 |
break;
|
| 109 |
|
| 110 |
case 'created':
|
| 111 |
$replacements[$original] = format_date($account->created, 'medium', '', NULL, $language_code);
|
| 112 |
break;
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
if ($login_tokens = token_find_with_prefix($tokens, 'last-login')) {
|
| 117 |
$replacements += token_generate('date', $login_tokens, array('date' => $account->login), $options);
|
| 118 |
}
|
| 119 |
|
| 120 |
if ($registered_tokens = token_find_with_prefix($tokens, 'created')) {
|
| 121 |
$replacements += token_generate('date', $registered_tokens, array('date' => $account->created), $options);
|
| 122 |
}
|
| 123 |
}
|
| 124 |
if ($type == 'current-user') {
|
| 125 |
global $user;
|
| 126 |
$replacements += token_generate('user', $tokens, array('user' => $user), $options);
|
| 127 |
}
|
| 128 |
|
| 129 |
return $replacements;
|
| 130 |
}
|