1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Administrative page callbacks for the profile module. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Form builder to display a listing of all editable profile fields. |
---|
10 | * |
---|
11 | * @ingroup forms |
---|
12 | * @see profile_admin_overview_submit() |
---|
13 | */ |
---|
14 | function profile_admin_overview() { |
---|
15 | $result = db_query('SELECT title, name, type, category, fid, weight FROM {profile_fields} ORDER BY category, weight'); |
---|
16 | |
---|
17 | $form = array(); |
---|
18 | $categories = array(); |
---|
19 | while ($field = db_fetch_object($result)) { |
---|
20 | // Collect all category information |
---|
21 | $categories[] = $field->category; |
---|
22 | |
---|
23 | // Save all field information |
---|
24 | $form[$field->fid]['name'] = array('#value' => check_plain($field->name)); |
---|
25 | $form[$field->fid]['title'] = array('#value' => check_plain($field->title)); |
---|
26 | $form[$field->fid]['type'] = array('#value' => $field->type); |
---|
27 | $form[$field->fid]['category'] = array('#type' => 'select', '#default_value' => $field->category, '#options' => array()); |
---|
28 | $form[$field->fid]['weight'] = array('#type' => 'weight', '#default_value' => $field->weight); |
---|
29 | $form[$field->fid]['edit'] = array('#value' => l(t('edit'), "admin/user/profile/edit/$field->fid")); |
---|
30 | $form[$field->fid]['delete'] = array('#value' => l(t('delete'), "admin/user/profile/delete/$field->fid")); |
---|
31 | } |
---|
32 | |
---|
33 | // Add the cateogory combo boxes |
---|
34 | $categories = array_unique($categories); |
---|
35 | foreach ($form as $fid => $field) { |
---|
36 | foreach ($categories as $cat => $category) { |
---|
37 | $form[$fid]['category']['#options'][$category] = $category; |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | // Display the submit button only when there's more than one field |
---|
42 | if (count($form) > 1) { |
---|
43 | $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration')); |
---|
44 | } |
---|
45 | else { |
---|
46 | // Disable combo boxes when there isn't a submit button |
---|
47 | foreach ($form as $fid => $field) { |
---|
48 | unset($form[$fid]['weight']); |
---|
49 | $form[$fid]['category']['#type'] = 'value'; |
---|
50 | } |
---|
51 | } |
---|
52 | $form['#tree'] = TRUE; |
---|
53 | |
---|
54 | $addnewfields = '<h2>'. t('Add new field') .'</h2>'; |
---|
55 | $addnewfields .= '<ul>'; |
---|
56 | foreach (_profile_field_types() as $key => $value) { |
---|
57 | $addnewfields .= '<li>'. l($value, "admin/user/profile/add/$key") .'</li>'; |
---|
58 | } |
---|
59 | $addnewfields .= '</ul>'; |
---|
60 | $form['addnewfields'] = array('#value' => $addnewfields); |
---|
61 | |
---|
62 | return $form; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * Submit handler to update changed profile field weights and categories. |
---|
67 | * |
---|
68 | * @see profile_admin_overview() |
---|
69 | */ |
---|
70 | function profile_admin_overview_submit($form, &$form_state) { |
---|
71 | foreach (element_children($form_state['values']) as $fid) { |
---|
72 | if (is_numeric($fid)) { |
---|
73 | $weight = $form_state['values'][$fid]['weight']; |
---|
74 | $category = $form_state['values'][$fid]['category']; |
---|
75 | if ($weight != $form[$fid]['weight']['#default_value'] || $category != $form[$fid]['category']['#default_value']) { |
---|
76 | db_query("UPDATE {profile_fields} SET weight = %d, category = '%s' WHERE fid = %d", $weight, $category, $fid); |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | drupal_set_message(t('Profile fields have been updated.')); |
---|
82 | cache_clear_all(); |
---|
83 | menu_rebuild(); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Theme the profile field overview into a drag and drop enabled table. |
---|
88 | * |
---|
89 | * @ingroup themeable |
---|
90 | * @see profile_admin_overview() |
---|
91 | */ |
---|
92 | function theme_profile_admin_overview($form) { |
---|
93 | drupal_add_css(drupal_get_path('module', 'profile') .'/profile.css'); |
---|
94 | // Add javascript if there's more than one field. |
---|
95 | if (isset($form['submit'])) { |
---|
96 | drupal_add_js(drupal_get_path('module', 'profile') .'/profile.js'); |
---|
97 | } |
---|
98 | |
---|
99 | $rows = array(); |
---|
100 | $categories = array(); |
---|
101 | $category_number = 0; |
---|
102 | foreach (element_children($form) as $key) { |
---|
103 | // Don't take form control structures. |
---|
104 | if (array_key_exists('category', $form[$key])) { |
---|
105 | $field = &$form[$key]; |
---|
106 | $category = $field['category']['#default_value']; |
---|
107 | |
---|
108 | if (!isset($categories[$category])) { |
---|
109 | // Category classes are given numeric IDs because there's no guarantee |
---|
110 | // class names won't contain invalid characters. |
---|
111 | $categories[$category] = $category_number; |
---|
112 | $category_field['#attributes']['class'] = 'profile-category profile-category-'. $category_number; |
---|
113 | $rows[] = array(array('data' => $category, 'colspan' => 7, 'class' => 'category')); |
---|
114 | $rows[] = array('data' => array(array('data' => '<em>'. t('No fields in this category. If this category remains empty when saved, it will be removed.') .'</em>', 'colspan' => 7)), 'class' => 'category-'. $category_number .'-message category-message category-populated'); |
---|
115 | |
---|
116 | // Make it dragable only if there is more than one field |
---|
117 | if (isset($form['submit'])) { |
---|
118 | drupal_add_tabledrag('profile-fields', 'order', 'sibling', 'profile-weight', 'profile-weight-'. $category_number); |
---|
119 | drupal_add_tabledrag('profile-fields', 'match', 'sibling', 'profile-category', 'profile-category-'. $category_number); |
---|
120 | } |
---|
121 | $category_number++; |
---|
122 | } |
---|
123 | |
---|
124 | // Add special drag and drop classes that group fields together. |
---|
125 | $field['weight']['#attributes']['class'] = 'profile-weight profile-weight-'. $categories[$category]; |
---|
126 | $field['category']['#attributes']['class'] = 'profile-category profile-category-'. $categories[$category]; |
---|
127 | |
---|
128 | // Add the row |
---|
129 | $row = array(); |
---|
130 | $row[] = drupal_render($field['title']); |
---|
131 | $row[] = drupal_render($field['name']); |
---|
132 | $row[] = drupal_render($field['type']); |
---|
133 | if (isset($form['submit'])) { |
---|
134 | $row[] = drupal_render($field['category']); |
---|
135 | $row[] = drupal_render($field['weight']); |
---|
136 | } |
---|
137 | $row[] = drupal_render($field['edit']); |
---|
138 | $row[] = drupal_render($field['delete']); |
---|
139 | $rows[] = array('data' => $row, 'class' => 'draggable'); |
---|
140 | } |
---|
141 | } |
---|
142 | if (empty($rows)) { |
---|
143 | $rows[] = array(array('data' => t('No fields available.'), 'colspan' => 7)); |
---|
144 | } |
---|
145 | |
---|
146 | $header = array(t('Title'), t('Name'), t('Type')); |
---|
147 | if (isset($form['submit'])) { |
---|
148 | $header[] = t('Category'); |
---|
149 | $header[] = t('Weight'); |
---|
150 | } |
---|
151 | $header[] = array('data' => t('Operations'), 'colspan' => 2); |
---|
152 | |
---|
153 | $output = theme('table', $header, $rows, array('id' => 'profile-fields')); |
---|
154 | $output .= drupal_render($form); |
---|
155 | |
---|
156 | return $output; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * Menu callback: Generate a form to add/edit a user profile field. |
---|
161 | * |
---|
162 | * @ingroup forms |
---|
163 | * @see profile_field_form_validate() |
---|
164 | * @see profile_field_form_submit() |
---|
165 | */ |
---|
166 | function profile_field_form(&$form_state, $arg = NULL) { |
---|
167 | if (arg(3) == 'edit') { |
---|
168 | if (is_numeric($arg)) { |
---|
169 | $fid = $arg; |
---|
170 | |
---|
171 | $edit = db_fetch_array(db_query('SELECT * FROM {profile_fields} WHERE fid = %d', $fid)); |
---|
172 | |
---|
173 | if (!$edit) { |
---|
174 | drupal_not_found(); |
---|
175 | return; |
---|
176 | } |
---|
177 | drupal_set_title(t('edit %title', array('%title' => $edit['title']))); |
---|
178 | $form['fid'] = array('#type' => 'value', |
---|
179 | '#value' => $fid, |
---|
180 | ); |
---|
181 | $type = $edit['type']; |
---|
182 | } |
---|
183 | else { |
---|
184 | drupal_not_found(); |
---|
185 | return; |
---|
186 | } |
---|
187 | } |
---|
188 | else { |
---|
189 | $types = _profile_field_types(); |
---|
190 | if (!isset($types[$arg])) { |
---|
191 | drupal_not_found(); |
---|
192 | return; |
---|
193 | } |
---|
194 | $type = $arg; |
---|
195 | drupal_set_title(t('add new %type', array('%type' => $types[$type]))); |
---|
196 | $edit = array('name' => 'profile_'); |
---|
197 | $form['type'] = array('#type' => 'value', '#value' => $type); |
---|
198 | } |
---|
199 | $edit += array( |
---|
200 | 'category' => '', |
---|
201 | 'title' => '', |
---|
202 | 'explanation' => '', |
---|
203 | 'weight' => 0, |
---|
204 | 'page' => '', |
---|
205 | 'autocomplete' => '', |
---|
206 | 'required' => '', |
---|
207 | 'register' => '', |
---|
208 | ); |
---|
209 | $form['fields'] = array('#type' => 'fieldset', |
---|
210 | '#title' => t('Field settings'), |
---|
211 | ); |
---|
212 | $form['fields']['category'] = array('#type' => 'textfield', |
---|
213 | '#title' => t('Category'), |
---|
214 | '#default_value' => $edit['category'], |
---|
215 | '#autocomplete_path' => 'admin/user/profile/autocomplete', |
---|
216 | '#description' => t('The category the new field should be part of. Categories are used to group fields logically. An example category is "Personal information".'), |
---|
217 | '#required' => TRUE, |
---|
218 | ); |
---|
219 | $form['fields']['title'] = array('#type' => 'textfield', |
---|
220 | '#title' => t('Title'), |
---|
221 | '#default_value' => $edit['title'], |
---|
222 | '#description' => t('The title of the new field. The title will be shown to the user. An example title is "Favorite color".'), |
---|
223 | '#required' => TRUE, |
---|
224 | ); |
---|
225 | $form['fields']['name'] = array('#type' => 'textfield', |
---|
226 | '#title' => t('Form name'), |
---|
227 | '#default_value' => $edit['name'], |
---|
228 | '#description' => t('The name of the field. The form name is not shown to the user but used internally in the HTML code and URLs. |
---|
229 | Unless you know what you are doing, it is highly recommended that you prefix the form name with <code>profile_</code> to avoid name clashes with other fields. Spaces or any other special characters except dash (-) and underscore (_) are not allowed. An example name is "profile_favorite_color" or perhaps just "profile_color".'), |
---|
230 | '#required' => TRUE, |
---|
231 | ); |
---|
232 | $form['fields']['explanation'] = array('#type' => 'textarea', |
---|
233 | '#title' => t('Explanation'), |
---|
234 | '#default_value' => $edit['explanation'], |
---|
235 | '#description' => t('An optional explanation to go with the new field. The explanation will be shown to the user.'), |
---|
236 | ); |
---|
237 | if ($type == 'selection') { |
---|
238 | $form['fields']['options'] = array('#type' => 'textarea', |
---|
239 | '#title' => t('Selection options'), |
---|
240 | '#default_value' => isset($edit['options']) ? $edit['options'] : '', |
---|
241 | '#description' => t('A list of all options. Put each option on a separate line. Example options are "red", "blue", "green", etc.'), |
---|
242 | ); |
---|
243 | } |
---|
244 | $form['fields']['visibility'] = array('#type' => 'radios', |
---|
245 | '#title' => t('Visibility'), |
---|
246 | '#default_value' => isset($edit['visibility']) ? $edit['visibility'] : PROFILE_PUBLIC, |
---|
247 | '#options' => array(PROFILE_HIDDEN => t('Hidden profile field, only accessible by administrators, modules and themes.'), PROFILE_PRIVATE => t('Private field, content only available to privileged users.'), PROFILE_PUBLIC => t('Public field, content shown on profile page but not used on member list pages.'), PROFILE_PUBLIC_LISTINGS => t('Public field, content shown on profile page and on member list pages.')), |
---|
248 | ); |
---|
249 | if ($type == 'selection' || $type == 'list' || $type == 'textfield') { |
---|
250 | $form['fields']['page'] = array('#type' => 'textfield', |
---|
251 | '#title' => t('Page title'), |
---|
252 | '#default_value' => $edit['page'], |
---|
253 | '#description' => t('To enable browsing this field by value, enter a title for the resulting page. The word <code>%value</code> will be substituted with the corresponding value. An example page title is "People whose favorite color is %value". This is only applicable for a public field.'), |
---|
254 | ); |
---|
255 | } |
---|
256 | else if ($type == 'checkbox') { |
---|
257 | $form['fields']['page'] = array('#type' => 'textfield', |
---|
258 | '#title' => t('Page title'), |
---|
259 | '#default_value' => $edit['page'], |
---|
260 | '#description' => t('To enable browsing this field by value, enter a title for the resulting page. An example page title is "People who are employed". This is only applicable for a public field.'), |
---|
261 | ); |
---|
262 | } |
---|
263 | $form['fields']['weight'] = array('#type' => 'weight', |
---|
264 | '#title' => t('Weight'), |
---|
265 | '#default_value' => $edit['weight'], |
---|
266 | '#description' => t('The weights define the order in which the form fields are shown. Lighter fields "float up" towards the top of the category.'), |
---|
267 | ); |
---|
268 | $form['fields']['autocomplete'] = array('#type' => 'checkbox', |
---|
269 | '#title' => t('Form will auto-complete while user is typing.'), |
---|
270 | '#default_value' => $edit['autocomplete'], |
---|
271 | '#description' => t('For security, auto-complete will be disabled if the user does not have access to user profiles.'), |
---|
272 | ); |
---|
273 | $form['fields']['required'] = array('#type' => 'checkbox', |
---|
274 | '#title' => t('The user must enter a value.'), |
---|
275 | '#default_value' => $edit['required'], |
---|
276 | ); |
---|
277 | $form['fields']['register'] = array('#type' => 'checkbox', |
---|
278 | '#title' => t('Visible in user registration form.'), |
---|
279 | '#default_value' => $edit['register'], |
---|
280 | ); |
---|
281 | $form['submit'] = array('#type' => 'submit', |
---|
282 | '#value' => t('Save field'), |
---|
283 | ); |
---|
284 | return $form; |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * Validate profile_field_form submissions. |
---|
289 | */ |
---|
290 | function profile_field_form_validate($form, &$form_state) { |
---|
291 | // Validate the 'field name': |
---|
292 | if (preg_match('/[^a-zA-Z0-9_-]/', $form_state['values']['name'])) { |
---|
293 | form_set_error('name', t('The specified form name contains one or more illegal characters. Spaces or any other special characters except dash (-) and underscore (_) are not allowed.')); |
---|
294 | } |
---|
295 | |
---|
296 | if (in_array($form_state['values']['name'], user_fields())) { |
---|
297 | form_set_error('name', t('The specified form name is reserved for use by Drupal.')); |
---|
298 | } |
---|
299 | // Validate the category: |
---|
300 | if (!$form_state['values']['category']) { |
---|
301 | form_set_error('category', t('You must enter a category.')); |
---|
302 | } |
---|
303 | if (strtolower($form_state['values']['category']) == 'account') { |
---|
304 | form_set_error('category', t('The specified category name is reserved for use by Drupal.')); |
---|
305 | } |
---|
306 | $args1 = array($form_state['values']['title'], $form_state['values']['category']); |
---|
307 | $args2 = array($form_state['values']['name']); |
---|
308 | $query_suffix = ''; |
---|
309 | |
---|
310 | if (isset($form_state['values']['fid'])) { |
---|
311 | $args1[] = $args2[] = $form_state['values']['fid']; |
---|
312 | $query_suffix = ' AND fid != %d'; |
---|
313 | } |
---|
314 | |
---|
315 | if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE title = '%s' AND category = '%s'". $query_suffix, $args1))) { |
---|
316 | form_set_error('title', t('The specified title is already in use.')); |
---|
317 | } |
---|
318 | if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'". $query_suffix, $args2))) { |
---|
319 | form_set_error('name', t('The specified name is already in use.')); |
---|
320 | } |
---|
321 | if ($form_state['values']['visibility'] == PROFILE_HIDDEN) { |
---|
322 | if ($form_state['values']['required']) { |
---|
323 | form_set_error('required', t('A hidden field cannot be required.')); |
---|
324 | } |
---|
325 | if ($form_state['values']['register']) { |
---|
326 | form_set_error('register', t('A hidden field cannot be set to visible on the user registration form.')); |
---|
327 | } |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | /** |
---|
332 | * Process profile_field_form submissions. |
---|
333 | */ |
---|
334 | function profile_field_form_submit($form, &$form_state) { |
---|
335 | if (!isset($form_state['values']['options'])) { |
---|
336 | $form_state['values']['options'] = ''; |
---|
337 | } |
---|
338 | if (!isset($form_state['values']['page'])) { |
---|
339 | $form_state['values']['page'] = ''; |
---|
340 | } |
---|
341 | if (!isset($form_state['values']['fid'])) { |
---|
342 | db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, required, register, visibility, autocomplete, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, %d, '%s', '%s')", $form_state['values']['title'], $form_state['values']['name'], $form_state['values']['explanation'], $form_state['values']['category'], $form_state['values']['type'], $form_state['values']['weight'], $form_state['values']['required'], $form_state['values']['register'], $form_state['values']['visibility'], $form_state['values']['autocomplete'], $form_state['values']['options'], $form_state['values']['page']); |
---|
343 | |
---|
344 | drupal_set_message(t('The field has been created.')); |
---|
345 | watchdog('profile', 'Profile field %field added under category %category.', array('%field' => $form_state['values']['title'], '%category' => $form_state['values']['category']), WATCHDOG_NOTICE, l(t('view'), 'admin/user/profile')); |
---|
346 | } |
---|
347 | else { |
---|
348 | db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, required = %d, register = %d, visibility = %d, autocomplete = %d, options = '%s', page = '%s' WHERE fid = %d", $form_state['values']['title'], $form_state['values']['name'], $form_state['values']['explanation'], $form_state['values']['category'], $form_state['values']['weight'], $form_state['values']['required'], $form_state['values']['register'], $form_state['values']['visibility'], $form_state['values']['autocomplete'], $form_state['values']['options'], $form_state['values']['page'], $form_state['values']['fid']); |
---|
349 | |
---|
350 | drupal_set_message(t('The field has been updated.')); |
---|
351 | } |
---|
352 | cache_clear_all(); |
---|
353 | menu_rebuild(); |
---|
354 | |
---|
355 | $form_state['redirect'] = 'admin/user/profile'; |
---|
356 | return; |
---|
357 | } |
---|
358 | |
---|
359 | /** |
---|
360 | * Menu callback; deletes a field from all user profiles. |
---|
361 | */ |
---|
362 | function profile_field_delete(&$form_state, $fid) { |
---|
363 | $field = db_fetch_object(db_query("SELECT title FROM {profile_fields} WHERE fid = %d", $fid)); |
---|
364 | if (!$field) { |
---|
365 | drupal_not_found(); |
---|
366 | return; |
---|
367 | } |
---|
368 | $form['fid'] = array('#type' => 'value', '#value' => $fid); |
---|
369 | $form['title'] = array('#type' => 'value', '#value' => $field->title); |
---|
370 | |
---|
371 | return confirm_form($form, |
---|
372 | t('Are you sure you want to delete the field %field?', array('%field' => $field->title)), 'admin/user/profile', |
---|
373 | t('This action cannot be undone. If users have entered values into this field in their profile, these entries will also be deleted. If you want to keep the user-entered data, instead of deleting the field you may wish to <a href="@edit-field">edit this field</a> and change it to a hidden profile field so that it may only be accessed by administrators.', array('@edit-field' => url('admin/user/profile/edit/'. $fid))), |
---|
374 | t('Delete'), t('Cancel')); |
---|
375 | } |
---|
376 | |
---|
377 | /** |
---|
378 | * Process a field delete form submission. |
---|
379 | */ |
---|
380 | function profile_field_delete_submit($form, &$form_state) { |
---|
381 | db_query('DELETE FROM {profile_fields} WHERE fid = %d', $form_state['values']['fid']); |
---|
382 | db_query('DELETE FROM {profile_values} WHERE fid = %d', $form_state['values']['fid']); |
---|
383 | |
---|
384 | cache_clear_all(); |
---|
385 | |
---|
386 | drupal_set_message(t('The field %field has been deleted.', array('%field' => $form_state['values']['title']))); |
---|
387 | watchdog('profile', 'Profile field %field deleted.', array('%field' => $form_state['values']['title']), WATCHDOG_NOTICE, l(t('view'), 'admin/user/profile')); |
---|
388 | |
---|
389 | $form_state['redirect'] = 'admin/user/profile'; |
---|
390 | return; |
---|
391 | } |
---|
392 | |
---|
393 | /** |
---|
394 | * Retrieve a pipe delimited string of autocomplete suggestions for profile categories |
---|
395 | */ |
---|
396 | function profile_admin_settings_autocomplete($string) { |
---|
397 | $matches = array(); |
---|
398 | $result = db_query_range("SELECT category FROM {profile_fields} WHERE LOWER(category) LIKE LOWER('%s%%')", $string, 0, 10); |
---|
399 | while ($data = db_fetch_object($result)) { |
---|
400 | $matches[$data->category] = check_plain($data->category); |
---|
401 | } |
---|
402 | drupal_json($matches); |
---|
403 | } |
---|