1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
c304a780
|
Assos Assos
|
* Plugin to provide a user context.
|
6 |
85ad3d82
|
Assos Assos
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Plugins are described by creating a $plugin array which will be used
|
10 |
|
|
* by the system that includes this file.
|
11 |
|
|
*/
|
12 |
|
|
$plugin = array(
|
13 |
|
|
'title' => t("User"),
|
14 |
|
|
'description' => t('A single user object.'),
|
15 |
|
|
'context' => 'ctools_context_create_user',
|
16 |
|
|
'edit form' => 'ctools_context_user_settings_form',
|
17 |
|
|
'defaults' => array('type' => 'select', 'uid' => ''),
|
18 |
|
|
'keyword' => 'user',
|
19 |
|
|
'context name' => 'user',
|
20 |
|
|
'convert list' => 'ctools_context_user_convert_list',
|
21 |
|
|
'convert' => 'ctools_context_user_convert',
|
22 |
|
|
'convert default' => 'name',
|
23 |
|
|
|
24 |
|
|
// This context is deprecated and should not be usable in the UI.
|
25 |
|
|
'no ui' => TRUE,
|
26 |
|
|
'no required context ui' => TRUE,
|
27 |
|
|
);
|
28 |
|
|
|
29 |
|
|
/**
|
30 |
|
|
* It's important to remember that $conf is optional here, because contexts
|
31 |
|
|
* are not always created from the UI.
|
32 |
|
|
*/
|
33 |
|
|
function ctools_context_create_user($empty, $data = NULL, $conf = FALSE) {
|
34 |
|
|
$context = new ctools_context(array('entity:user', 'entity', 'user'));
|
35 |
|
|
$context->plugin = 'user';
|
36 |
|
|
|
37 |
|
|
if ($empty) {
|
38 |
|
|
return $context;
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
if ($conf) {
|
42 |
|
|
if ($data['type'] == 'current') {
|
43 |
|
|
global $user;
|
44 |
560c3060
|
Julien Enselme
|
if (user_is_logged_in()) {
|
45 |
6e3ce7c2
|
Assos Assos
|
$data = user_load($user->uid);
|
46 |
560c3060
|
Julien Enselme
|
$data->logged_in_user = TRUE;
|
47 |
|
|
}
|
48 |
6e3ce7c2
|
Assos Assos
|
else {
|
49 |
|
|
$data = drupal_anonymous_user();
|
50 |
|
|
}
|
51 |
85ad3d82
|
Assos Assos
|
}
|
52 |
|
|
else {
|
53 |
|
|
$data = user_load($data['uid']);
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
|
|
// Load entity if the data provided is a numeric value. This kind of data is
|
57 |
|
|
// passed by some relationships.
|
58 |
|
|
if (is_numeric($data)) {
|
59 |
|
|
$data = user_load($data);
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
if (!empty($data)) {
|
63 |
|
|
$context->data = $data;
|
64 |
|
|
$context->title = isset($data->name) ? $data->name : t('Anonymous');
|
65 |
|
|
$context->argument = $data->uid;
|
66 |
|
|
return $context;
|
67 |
|
|
}
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
function ctools_context_user_settings_form($form, &$form_state) {
|
71 |
|
|
$conf = $form_state['conf'];
|
72 |
|
|
|
73 |
|
|
ctools_include('dependent');
|
74 |
|
|
$form['type'] = array(
|
75 |
|
|
'#title' => t('Enter the context type'),
|
76 |
|
|
'#type' => 'radios',
|
77 |
|
|
'#options' => array(
|
78 |
|
|
'select' => t('Select a user'),
|
79 |
|
|
'current' => t('Logged in user'),
|
80 |
|
|
),
|
81 |
|
|
'#default_value' => $conf['type'],
|
82 |
|
|
);
|
83 |
|
|
|
84 |
|
|
$form['user'] = array(
|
85 |
|
|
'#title' => t('Enter a user name'),
|
86 |
|
|
'#type' => 'textfield',
|
87 |
|
|
'#maxlength' => 512,
|
88 |
|
|
'#autocomplete_path' => 'user/autocomplete',
|
89 |
|
|
'#dependency' => array('radio:type' => array('select')),
|
90 |
|
|
);
|
91 |
|
|
|
92 |
|
|
if (!empty($conf['uid'])) {
|
93 |
|
|
$info = user_load($conf['uid']);
|
94 |
|
|
if ($info) {
|
95 |
e4c061ad
|
Assos Assos
|
$form['user']['#description'] = t('Currently set to !link', array('!link' => theme('username', array('account' => $info))));
|
96 |
85ad3d82
|
Assos Assos
|
}
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
$form['uid'] = array(
|
100 |
|
|
'#type' => 'value',
|
101 |
|
|
'#value' => $conf['uid'],
|
102 |
|
|
);
|
103 |
|
|
|
104 |
|
|
$form['set_identifier'] = array(
|
105 |
|
|
'#type' => 'checkbox',
|
106 |
|
|
'#default_value' => FALSE,
|
107 |
|
|
'#title' => t('Reset identifier to username'),
|
108 |
|
|
'#description' => t('If checked, the identifier will be reset to the user name of the selected user.'),
|
109 |
|
|
'#dependency' => array('radio:context[context_settings][type]' => array('select')),
|
110 |
|
|
);
|
111 |
|
|
|
112 |
|
|
return $form;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
/**
|
116 |
|
|
* Validate a user.
|
117 |
|
|
*/
|
118 |
|
|
function ctools_context_user_settings_form_validate($form, &$form_state) {
|
119 |
|
|
if ($form_state['values']['type'] != 'select') {
|
120 |
|
|
return;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
c304a780
|
Assos Assos
|
// Validate the autocomplete.
|
124 |
85ad3d82
|
Assos Assos
|
if (empty($form_state['values']['uid']) && empty($form_state['values']['user'])) {
|
125 |
|
|
form_error($form['user'], t('You must select a user.'));
|
126 |
|
|
return;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
if (empty($form_state['values']['user'])) {
|
130 |
|
|
return;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
$account = user_load_by_name($form_state['values']['user']);
|
134 |
|
|
|
135 |
|
|
if (!$account) {
|
136 |
|
|
form_error($form['user'], t('Invalid user selected.'));
|
137 |
|
|
}
|
138 |
|
|
else {
|
139 |
|
|
form_set_value($form['uid'], $account->uid, $form_state);
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
function ctools_context_user_settings_form_submit($form, &$form_state) {
|
144 |
|
|
if ($form_state['values']['set_identifier']) {
|
145 |
|
|
$account = user_load($form_state['values']['uid']);
|
146 |
|
|
$form_state['values']['identifier'] = $account->name;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
$form_state['conf']['type'] = $form_state['values']['type'];
|
150 |
|
|
$form_state['conf']['uid'] = $form_state['values']['uid'];
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
/**
|
154 |
|
|
* Provide a list of replacements.
|
155 |
|
|
*/
|
156 |
|
|
function ctools_context_user_convert_list() {
|
157 |
|
|
$tokens = token_info();
|
158 |
c2ac6d1d
|
Assos Assos
|
$list = array();
|
159 |
85ad3d82
|
Assos Assos
|
foreach ($tokens['tokens']['user'] as $id => $info) {
|
160 |
|
|
if (!isset($list[$id])) {
|
161 |
|
|
$list[$id] = $info['name'];
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
return $list;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
/**
|
169 |
|
|
* Convert a context into a string.
|
170 |
|
|
*/
|
171 |
|
|
function ctools_context_user_convert($context, $type) {
|
172 |
|
|
$tokens = token_info();
|
173 |
|
|
if (isset($tokens['tokens']['user'][$type])) {
|
174 |
|
|
$values = token_generate('user', array($type => $type), array('user' => $context->data));
|
175 |
|
|
if (isset($values[$type])) {
|
176 |
|
|
return $values[$type];
|
177 |
|
|
}
|
178 |
|
|
}
|
179 |
|
|
} |