comparison sites/all/modules/ctools/plugins/contexts/user_edit_form.inc @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
1 <?php
2 /**
3 * @file
4 *
5 * Plugin to provide a user_edit_form context
6 */
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 edit form'),
14 'description' => t('A user edit form.'),
15 'context' => 'ctools_context_create_user_edit_form',
16 'edit form' => 'ctools_context_user_edit_form_settings_form',
17 'defaults' => array('uid' => ''),
18 'keyword' => 'user_edit',
19 'context name' => 'user_edit_form',
20 'convert list' => 'ctools_context_user_edit_convert_list',
21 'convert' => 'ctools_context_user_edit_convert',
22 'placeholder form' => array(
23 '#type' => 'textfield',
24 '#description' => t('Enter the user ID of a user for this argument:'),
25 ),
26 );
27
28 /**
29 * It's important to remember that $conf is optional here, because contexts
30 * are not always created from the UI.
31 */
32 function ctools_context_create_user_edit_form($empty, $user = NULL, $conf = FALSE) {
33 // Determine the user category.
34 $category = !empty($conf['category']) ? $conf['category'] : FALSE;
35 unset($conf['category']);
36
37 // Return previously created contexts, per category.
38 static $created = array();
39 if (!empty($created[$category])) {
40 return $created[$category];
41 }
42 // If no category was specified, use the default 'account'.
43 if (!$category) {
44 $category = 'account';
45 }
46
47 $context = new ctools_context(array('form', 'user_edit', 'user_form', 'user_edit_form', 'user', 'entity:user'));
48 // Store this context for later.
49 $created[$category] = $context;
50 $context->plugin = 'user_edit_form';
51 if ($empty) {
52 return $context;
53 }
54
55 if (!empty($conf)) {
56 // In this case, $user is actually our $conf array.
57 $uid = is_array($user) && isset($user['uid']) ? $user['uid'] : (is_object($user) ? $user->uid : 0);
58
59 if (module_exists('translation')) {
60 if ($translation = module_invoke('translation', 'user_uid', $uid, $GLOBALS['language']->language)) {
61 $uid = $translation;
62 $reload = TRUE;
63 }
64 }
65
66 if (is_array($user) || !empty($reload)) {
67 $user = user_load($uid);
68 }
69 }
70
71 if (!empty($user)) {
72 $form_id = 'user_profile_form';
73
74 $form_state = array('want form' => TRUE, 'build_info' => array('args' => array($user, $category)));
75
76 $file = drupal_get_path('module', 'user') . '/user.pages.inc';
77 require_once DRUPAL_ROOT . '/' . $file;
78 // This piece of information can let other modules know that more files
79 // need to be included if this form is loaded from cache:
80 $form_state['build_info']['files'] = array($file);
81
82 $form = drupal_build_form($form_id, $form_state);
83
84 // Fill in the 'node' portion of the context
85 $context->data = $user;
86 $context->title = isset($user->name) ? $user->name : '';
87 $context->argument = $user->uid;
88
89 $context->form = $form;
90 $context->form_state = &$form_state;
91 $context->form_id = $form_id;
92 $context->form_title = isset($user->name) ? $user->name : '';
93 $context->restrictions['form'] = array('form');
94 return $context;
95 }
96 }
97
98 function ctools_context_user_edit_form_settings_form($form, &$form_state) {
99 $conf = &$form_state['conf'];
100
101 $form['user'] = array(
102 '#title' => t('Enter the name or UID of a node'),
103 '#type' => 'textfield',
104 '#maxlength' => 512,
105 '#autocomplete_path' => 'ctools/autocomplete/user',
106 '#weight' => -10,
107 );
108
109 if (!empty($conf['uid'])) {
110 $info = db_query('SELECT * FROM {user} WHERE uid = :uid', array(':uid' => $conf['uid']))->fetchObject();
111 if ($info) {
112 $link = l(t("'%name' [user id %uid]", array('%name' => $info->name, '%uid' => $info->uid)), "user/$info->uid", array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
113 $form['user']['#description'] = t('Currently set to !link', array('!link' => $link));
114 }
115 }
116
117 $form['uid'] = array(
118 '#type' => 'value',
119 '#value' => $conf['uid'],
120 );
121
122 $form['set_identifier'] = array(
123 '#type' => 'checkbox',
124 '#default_value' => FALSE,
125 '#title' => t('Reset identifier to user name'),
126 '#description' => t('If checked, the identifier will be reset to the user name of the selected user.'),
127 );
128
129 return $form;
130 }
131
132 /**
133 * Validate a node.
134 */
135 function ctools_context_user_edit_form_settings_form_validate($form, &$form_state) {
136 // Validate the autocomplete
137 if (empty($form_state['values']['uid']) && empty($form_state['values']['user'])) {
138 form_error($form['user'], t('You must select a user.'));
139 return;
140 }
141
142 if (empty($form_state['values']['user'])) {
143 return;
144 }
145
146 $uid = $form_state['values']['user'];
147 $preg_matches = array();
148 $match = preg_match('/\[id: (\d+)\]/', $uid, $preg_matches);
149 if (!$match) {
150 $match = preg_match('/^id: (\d+)/', $uid, $preg_matches);
151 }
152
153 if ($match) {
154 $uid = $preg_matches[1];
155 }
156 if (is_numeric($uid)) {
157 $user = db_query('SELECT uid FROM {user} WHEREuid = :uid', array(':uid' => $uid))->fetchObject();
158 }
159 else {
160 $user = db_query('SELECT uid FROM {user} WHERE LOWER(name) = LOWER(:name)', array(':name' => $uid))->fetchObject();
161 }
162
163 form_set_value($form['uid'], $user->uid, $form_state);
164 }
165 function ctools_context_user_edit_form_settings_form_submit($form, &$form_state) {
166 if ($form_state['values']['set_identifier']) {
167 $user = user_load($form_state['values']['uid']);
168 $form_state['values']['identifier'] = $user->name;
169 }
170
171 // This will either be the value set previously or a value set by the
172 // validator.
173 $form_state['conf']['uid'] = $form_state['values']['uid'];
174 }
175
176 /**
177 * Provide a list of ways that this context can be converted to a string.
178 */
179 function ctools_context_user_edit_convert_list() {
180 // Pass through to the "node" context convert list.
181 $plugin = ctools_get_context('user');
182 return ctools_context_user_convert_list();
183 }
184
185 /**
186 * Convert a context into a string.
187 */
188 function ctools_context_user_edit_convert($context, $type) {
189 // Pass through to the "node" context convert list.
190 $plugin = ctools_get_context('user');
191 return ctools_context_user_convert($context, $type);
192 }