Mercurial > defr > drupal > core
comparison modules/profile/profile.admin.inc @ 1:c1f4ac30525a 6.0
Drupal 6.0
author | Franck Deroche <webmaster@defr.org> |
---|---|
date | Tue, 23 Dec 2008 14:28:28 +0100 |
parents | |
children | 8b6c45761e01 |
comparison
equal
deleted
inserted
replaced
0:5a113a1c4740 | 1:c1f4ac30525a |
---|---|
1 <?php | |
2 // $Id: profile.admin.inc,v 1.8 2008/01/21 15:20:43 goba Exp $ | |
3 | |
4 /** | |
5 * @file | |
6 * Administrative page callbacks for the profile module. | |
7 */ | |
8 | |
9 /** | |
10 * Form builder to display a listing of all editable profile fields. | |
11 * | |
12 * @ingroup forms | |
13 * @see profile_admin_overview_submit() | |
14 */ | |
15 function profile_admin_overview() { | |
16 $result = db_query('SELECT title, name, type, category, fid, weight FROM {profile_fields} ORDER BY category, weight'); | |
17 | |
18 $form = array(); | |
19 $categories = array(); | |
20 while ($field = db_fetch_object($result)) { | |
21 // Collect all category information | |
22 $categories[] = $field->category; | |
23 | |
24 // Save all field information | |
25 $form[$field->fid]['name'] = array('#value' => check_plain($field->name)); | |
26 $form[$field->fid]['title'] = array('#value' => check_plain($field->title)); | |
27 $form[$field->fid]['type'] = array('#value' => $field->type); | |
28 $form[$field->fid]['category'] = array('#type' => 'select', '#default_value' => $field->category, '#options' => array()); | |
29 $form[$field->fid]['weight'] = array('#type' => 'weight', '#default_value' => $field->weight); | |
30 $form[$field->fid]['edit'] = array('#value' => l(t('edit'), "admin/user/profile/edit/$field->fid")); | |
31 $form[$field->fid]['delete'] = array('#value' => l(t('delete'), "admin/user/profile/delete/$field->fid")); | |
32 } | |
33 | |
34 // Add the cateogory combo boxes | |
35 $categories = array_unique($categories); | |
36 foreach ($form as $fid => $field) { | |
37 foreach ($categories as $cat => $category) { | |
38 $form[$fid]['category']['#options'][$category] = $category; | |
39 } | |
40 } | |
41 | |
42 // Display the submit button only when there's more than one field | |
43 if (count($form) > 1) { | |
44 $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration')); | |
45 } | |
46 else { | |
47 // Disable combo boxes when there isn't a submit button | |
48 foreach ($form as $fid => $field) { | |
49 unset($form[$fid]['weight']); | |
50 $form[$fid]['category']['#type'] = 'value'; | |
51 } | |
52 } | |
53 $form['#tree'] = TRUE; | |
54 | |
55 $addnewfields = '<h2>'. t('Add new field') .'</h2>'; | |
56 $addnewfields .= '<ul>'; | |
57 foreach (_profile_field_types() as $key => $value) { | |
58 $addnewfields .= '<li>'. l($value, "admin/user/profile/add/$key") .'</li>'; | |
59 } | |
60 $addnewfields .= '</ul>'; | |
61 $form['addnewfields'] = array('#value' => $addnewfields); | |
62 | |
63 return $form; | |
64 } | |
65 | |
66 /** | |
67 * Submit handler to update changed profile field weights and categories. | |
68 * | |
69 * @see profile_admin_overview() | |
70 */ | |
71 function profile_admin_overview_submit($form, &$form_state) { | |
72 foreach (element_children($form_state['values']) as $fid) { | |
73 if (is_numeric($fid)) { | |
74 $weight = $form_state['values'][$fid]['weight']; | |
75 $category = $form_state['values'][$fid]['category']; | |
76 if ($weight != $form[$fid]['weight']['#default_value'] || $category != $form[$fid]['category']['#default_value']) { | |
77 db_query("UPDATE {profile_fields} SET weight = %d, category = '%s' WHERE fid = %d", $weight, $category, $fid); | |
78 } | |
79 } | |
80 } | |
81 | |
82 drupal_set_message(t('Profile fields have been updated.')); | |
83 cache_clear_all(); | |
84 menu_rebuild(); | |
85 } | |
86 | |
87 /** | |
88 * Theme the profile field overview into a drag and drop enabled table. | |
89 * | |
90 * @ingroup themeable | |
91 * @see profile_admin_overview() | |
92 */ | |
93 function theme_profile_admin_overview($form) { | |
94 drupal_add_css(drupal_get_path('module', 'profile') .'/profile.css'); | |
95 // Add javascript if there's more than one field. | |
96 if (isset($form['submit'])) { | |
97 drupal_add_js(drupal_get_path('module', 'profile') .'/profile.js'); | |
98 } | |
99 | |
100 $rows = array(); | |
101 $categories = array(); | |
102 $category_number = 0; | |
103 foreach (element_children($form) as $key) { | |
104 // Don't take form control structures. | |
105 if (array_key_exists('category', $form[$key])) { | |
106 $field = &$form[$key]; | |
107 $category = $field['category']['#default_value']; | |
108 | |
109 if (!isset($categories[$category])) { | |
110 // Category classes are given numeric IDs because there's no guarantee | |
111 // class names won't contain invalid characters. | |
112 $categories[$category] = $category_number; | |
113 $category_field['#attributes']['class'] = 'profile-category profile-category-'. $category_number; | |
114 $rows[] = array(array('data' => $category, 'colspan' => 7, 'class' => 'category')); | |
115 $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'); | |
116 | |
117 // Make it dragable only if there is more than one field | |
118 if (isset($form['submit'])) { | |
119 drupal_add_tabledrag('profile-fields', 'order', 'sibling', 'profile-weight', 'profile-weight-'. $category_number); | |
120 drupal_add_tabledrag('profile-fields', 'match', 'sibling', 'profile-category', 'profile-category-'. $category_number); | |
121 } | |
122 $category_number++; | |
123 } | |
124 | |
125 // Add special drag and drop classes that group fields together. | |
126 $field['weight']['#attributes']['class'] = 'profile-weight profile-weight-'. $categories[$category]; | |
127 $field['category']['#attributes']['class'] = 'profile-category profile-category-'. $categories[$category]; | |
128 | |
129 // Add the row | |
130 $row = array(); | |
131 $row[] = drupal_render($field['title']); | |
132 $row[] = drupal_render($field['name']); | |
133 $row[] = drupal_render($field['type']); | |
134 if (isset($form['submit'])) { | |
135 $row[] = drupal_render($field['category']); | |
136 $row[] = drupal_render($field['weight']); | |
137 } | |
138 $row[] = drupal_render($field['edit']); | |
139 $row[] = drupal_render($field['delete']); | |
140 $rows[] = array('data' => $row, 'class' => 'draggable'); | |
141 } | |
142 } | |
143 if (empty($rows)) { | |
144 $rows[] = array(array('data' => t('No fields available.'), 'colspan' => 7)); | |
145 } | |
146 | |
147 $header = array(t('Title'), t('Name'), t('Type')); | |
148 if (isset($form['submit'])) { | |
149 $header[] = t('Category'); | |
150 $header[] = t('Weight'); | |
151 } | |
152 $header[] = array('data' => t('Operations'), 'colspan' => 2); | |
153 | |
154 $output = theme('table', $header, $rows, array('id' => 'profile-fields')); | |
155 $output .= drupal_render($form); | |
156 | |
157 return $output; | |
158 } | |
159 | |
160 /** | |
161 * Menu callback: Generate a form to add/edit a user profile field. | |
162 * | |
163 * @ingroup forms | |
164 * @see profile_field_form_validate() | |
165 * @see profile_field_form_submit() | |
166 */ | |
167 function profile_field_form(&$form_state, $arg = NULL) { | |
168 if (arg(3) == 'edit') { | |
169 if (is_numeric($arg)) { | |
170 $fid = $arg; | |
171 | |
172 $edit = db_fetch_array(db_query('SELECT * FROM {profile_fields} WHERE fid = %d', $fid)); | |
173 | |
174 if (!$edit) { | |
175 drupal_not_found(); | |
176 return; | |
177 } | |
178 drupal_set_title(t('edit %title', array('%title' => $edit['title']))); | |
179 $form['fid'] = array('#type' => 'value', | |
180 '#value' => $fid, | |
181 ); | |
182 $type = $edit['type']; | |
183 } | |
184 else { | |
185 drupal_not_found(); | |
186 return; | |
187 } | |
188 } | |
189 else { | |
190 $types = _profile_field_types(); | |
191 if (!isset($types[$arg])) { | |
192 drupal_not_found(); | |
193 return; | |
194 } | |
195 $type = $arg; | |
196 drupal_set_title(t('add new %type', array('%type' => $types[$type]))); | |
197 $edit = array('name' => 'profile_'); | |
198 $form['type'] = array('#type' => 'value', '#value' => $type); | |
199 } | |
200 $edit += array( | |
201 'category' => '', | |
202 'title' => '', | |
203 'explanation' => '', | |
204 'weight' => 0, | |
205 'page' => '', | |
206 'autocomplete' => '', | |
207 'required' => '', | |
208 'register' => '', | |
209 ); | |
210 $form['fields'] = array('#type' => 'fieldset', | |
211 '#title' => t('Field settings'), | |
212 ); | |
213 $form['fields']['category'] = array('#type' => 'textfield', | |
214 '#title' => t('Category'), | |
215 '#default_value' => $edit['category'], | |
216 '#autocomplete_path' => 'admin/user/profile/autocomplete', | |
217 '#description' => t('The category the new field should be part of. Categories are used to group fields logically. An example category is "Personal information".'), | |
218 '#required' => TRUE, | |
219 ); | |
220 $form['fields']['title'] = array('#type' => 'textfield', | |
221 '#title' => t('Title'), | |
222 '#default_value' => $edit['title'], | |
223 '#description' => t('The title of the new field. The title will be shown to the user. An example title is "Favorite color".'), | |
224 '#required' => TRUE, | |
225 ); | |
226 $form['fields']['name'] = array('#type' => 'textfield', | |
227 '#title' => t('Form name'), | |
228 '#default_value' => $edit['name'], | |
229 '#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. | |
230 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".'), | |
231 '#required' => TRUE, | |
232 ); | |
233 $form['fields']['explanation'] = array('#type' => 'textarea', | |
234 '#title' => t('Explanation'), | |
235 '#default_value' => $edit['explanation'], | |
236 '#description' => t('An optional explanation to go with the new field. The explanation will be shown to the user.'), | |
237 ); | |
238 if ($type == 'selection') { | |
239 $form['fields']['options'] = array('#type' => 'textarea', | |
240 '#title' => t('Selection options'), | |
241 '#default_value' => isset($edit['options']) ? $edit['options'] : '', | |
242 '#description' => t('A list of all options. Put each option on a separate line. Example options are "red", "blue", "green", etc.'), | |
243 ); | |
244 } | |
245 $form['fields']['visibility'] = array('#type' => 'radios', | |
246 '#title' => t('Visibility'), | |
247 '#default_value' => isset($edit['visibility']) ? $edit['visibility'] : PROFILE_PUBLIC, | |
248 '#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.')), | |
249 ); | |
250 if ($type == 'selection' || $type == 'list' || $type == 'textfield') { | |
251 $form['fields']['page'] = array('#type' => 'textfield', | |
252 '#title' => t('Page title'), | |
253 '#default_value' => $edit['page'], | |
254 '#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.'), | |
255 ); | |
256 } | |
257 else if ($type == 'checkbox') { | |
258 $form['fields']['page'] = array('#type' => 'textfield', | |
259 '#title' => t('Page title'), | |
260 '#default_value' => $edit['page'], | |
261 '#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.'), | |
262 ); | |
263 } | |
264 $form['fields']['weight'] = array('#type' => 'weight', | |
265 '#title' => t('Weight'), | |
266 '#default_value' => $edit['weight'], | |
267 '#description' => t('The weights define the order in which the form fields are shown. Lighter fields "float up" towards the top of the category.'), | |
268 ); | |
269 $form['fields']['autocomplete'] = array('#type' => 'checkbox', | |
270 '#title' => t('Form will auto-complete while user is typing.'), | |
271 '#default_value' => $edit['autocomplete'], | |
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 print drupal_to_js($matches); | |
403 exit(); | |
404 } |