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