/[drupal]/drupal/modules/profile/profile.admin.inc
ViewVC logotype

Contents of /drupal/modules/profile/profile.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.34 - (show annotations) (download) (as text)
Tue Nov 3 05:27:18 2009 UTC (3 weeks, 3 days ago) by webchick
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10
Changes since 1.33: +4 -3 lines
File MIME type: text/x-php
#602522 by effulgentsia, sun, and moshe weitzman: Make links in renderable arrays and forms (e.g. 'Operations') alterable.
1 <?php
2 // $Id: profile.admin.inc,v 1.33 2009/10/09 01:00:02 dries 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($form) {
16 $result = db_query('SELECT title, name, type, category, fid, weight FROM {profile_field} ORDER BY category, weight');
17
18 $categories = array();
19 foreach ($result as $field) {
20 // Collect all category information
21 $categories[] = $field->category;
22
23 // Save all field information
24 $form[$field->fid]['name'] = array('#markup' => check_plain($field->name));
25 $form[$field->fid]['title'] = array('#markup' => check_plain($field->title));
26 $form[$field->fid]['type'] = array('#markup' => $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('#type' => 'link', '#title' => t('edit'), '#href' => "admin/config/people/profile/edit/$field->fid");
30 $form[$field->fid]['delete'] = array('#type' => 'link', '#title' => t('delete'), '#href' => "admin/config/people/profile/delete/$field->fid");
31 }
32
33 // Add the category 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 // @todo: Any reason this isn't done using an element with #theme = 'links'?
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/config/people/profile/add/$key") . '</li>';
59 }
60 $addnewfields .= '</ul>';
61 $form['addnewfields'] = array('#markup' => $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_update('profile_field')
78 ->fields(array(
79 'weight' => $weight,
80 'category' => $category,
81 ))
82 ->condition('fid', $fid)
83 ->execute();
84 }
85 }
86 }
87
88 drupal_set_message(t('Profile fields have been updated.'));
89 cache_clear_all();
90 menu_rebuild();
91 }
92
93 /**
94 * Theme the profile field overview into a drag and drop enabled table.
95 *
96 * @ingroup themeable
97 * @see profile_admin_overview()
98 */
99 function theme_profile_admin_overview($variables) {
100 $form = $variables['form'];
101
102 drupal_add_css(drupal_get_path('module', 'profile') . '/profile.css');
103 // Add javascript if there's more than one field.
104 if (isset($form['submit'])) {
105 drupal_add_js(drupal_get_path('module', 'profile') . '/profile.js');
106 }
107
108 $rows = array();
109 $categories = array();
110 $category_number = 0;
111 foreach (element_children($form) as $key) {
112 // Don't take form control structures.
113 if (array_key_exists('category', $form[$key])) {
114 $field = &$form[$key];
115 $category = $field['category']['#default_value'];
116
117 if (!isset($categories[$category])) {
118 // Category classes are given numeric IDs because there's no guarantee
119 // class names won't contain invalid characters.
120 $categories[$category] = $category_number;
121 $category_field['#attributes']['class'] = array('profile-category', 'profile-category-' . $category_number);
122 $rows[] = array(array('data' => $category, 'colspan' => 7, 'class' => array('category')));
123 $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' => array('category-' . $category_number . '-message', 'category-message', 'category-populated'));
124
125 // Make it draggable only if there is more than one field
126 if (isset($form['submit'])) {
127 drupal_add_tabledrag('profile-fields', 'order', 'sibling', 'profile-weight', 'profile-weight-' . $category_number);
128 drupal_add_tabledrag('profile-fields', 'match', 'sibling', 'profile-category', 'profile-category-' . $category_number);
129 }
130 $category_number++;
131 }
132
133 // Add special drag and drop classes that group fields together.
134 $field['weight']['#attributes']['class'] = array('profile-weight', 'profile-weight-' . $categories[$category]);
135 $field['category']['#attributes']['class'] = array('profile-category', 'profile-category-' . $categories[$category]);
136
137 // Add the row
138 $row = array();
139 $row[] = drupal_render($field['title']);
140 $row[] = drupal_render($field['name']);
141 $row[] = drupal_render($field['type']);
142 if (isset($form['submit'])) {
143 $row[] = drupal_render($field['category']);
144 $row[] = drupal_render($field['weight']);
145 }
146 $row[] = drupal_render($field['edit']);
147 $row[] = drupal_render($field['delete']);
148 $rows[] = array('data' => $row, 'class' => array('draggable'));
149 }
150 }
151 if (empty($rows)) {
152 $rows[] = array(array('data' => t('No fields available.'), 'colspan' => 7));
153 }
154
155 $header = array(t('Title'), t('Name'), t('Type'));
156 if (isset($form['submit'])) {
157 $header[] = t('Category');
158 $header[] = t('Weight');
159 }
160 $header[] = array('data' => t('Operations'), 'colspan' => 2);
161
162 $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'profile-fields')));
163 $output .= drupal_render_children($form);
164
165 return $output;
166 }
167
168 /**
169 * Menu callback: Generate a form to add/edit a user profile field.
170 *
171 * @ingroup forms
172 * @see profile_field_form_validate()
173 * @see profile_field_form_submit()
174 */
175 function profile_field_form($form, &$form_state, $arg = NULL) {
176 if (arg(4) == 'edit') {
177 if (is_numeric($arg)) {
178 $fid = $arg;
179
180 $edit = db_query('SELECT * FROM {profile_field} WHERE fid = :fid', array('fid' => $fid))->fetchAssoc();
181
182 if (!$edit) {
183 drupal_not_found();
184 return;
185 }
186 drupal_set_title(t('edit %title', array('%title' => $edit['title'])), PASS_THROUGH);
187 $form['fid'] = array('#type' => 'value',
188 '#value' => $fid,
189 );
190 $type = $edit['type'];
191 }
192 else {
193 drupal_not_found();
194 return;
195 }
196 }
197 else {
198 $types = _profile_field_types();
199 if (!isset($types[$arg])) {
200 drupal_not_found();
201 return;
202 }
203 $type = $arg;
204 drupal_set_title(t('add new %type', array('%type' => $types[$type])), PASS_THROUGH);
205 $edit = array('name' => 'profile_');
206 $form['type'] = array('#type' => 'value', '#value' => $type);
207 }
208 $edit += array(
209 'category' => '',
210 'title' => '',
211 'explanation' => '',
212 'weight' => 0,
213 'page' => '',
214 'autocomplete' => '',
215 'required' => '',
216 'register' => '',
217 );
218 $form['fields'] = array('#type' => 'fieldset',
219 '#title' => t('Field settings'),
220 );
221 $form['fields']['category'] = array('#type' => 'textfield',
222 '#title' => t('Category'),
223 '#default_value' => $edit['category'],
224 '#autocomplete_path' => 'admin/config/people/profile/autocomplete',
225 '#description' => t('The category the new field should be part of. Categories are used to group fields logically. An example category is "Personal information".'),
226 '#required' => TRUE,
227 );
228 $form['fields']['title'] = array('#type' => 'textfield',
229 '#title' => t('Title'),
230 '#default_value' => $edit['title'],
231 '#description' => t('The title of the new field. The title will be shown to the user. An example title is "Favorite color".'),
232 '#required' => TRUE,
233 );
234 $form['fields']['name'] = array('#type' => 'textfield',
235 '#title' => t('Form name'),
236 '#default_value' => $edit['name'],
237 '#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.
238 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".'),
239 '#required' => TRUE,
240 );
241 $form['fields']['explanation'] = array('#type' => 'textarea',
242 '#title' => t('Explanation'),
243 '#default_value' => $edit['explanation'],
244 '#description' => t('An optional explanation to go with the new field. The explanation will be shown to the user.'),
245 );
246 if ($type == 'selection') {
247 $form['fields']['options'] = array('#type' => 'textarea',
248 '#title' => t('Selection options'),
249 '#default_value' => isset($edit['options']) ? $edit['options'] : '',
250 '#description' => t('A list of all options. Put each option on a separate line. Example options are "red", "blue", "green", etc.'),
251 );
252 }
253 $form['fields']['visibility'] = array('#type' => 'radios',
254 '#title' => t('Visibility'),
255 '#default_value' => isset($edit['visibility']) ? $edit['visibility'] : PROFILE_PUBLIC,
256 '#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.')),
257 );
258 if ($type == 'selection' || $type == 'list' || $type == 'textfield') {
259 $form['fields']['page'] = array('#type' => 'textfield',
260 '#title' => t('Page title'),
261 '#default_value' => $edit['page'],
262 '#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.'),
263 );
264 }
265 elseif ($type == 'checkbox') {
266 $form['fields']['page'] = array('#type' => 'textfield',
267 '#title' => t('Page title'),
268 '#default_value' => $edit['page'],
269 '#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.'),
270 );
271 }
272 $form['fields']['weight'] = array('#type' => 'weight',
273 '#title' => t('Weight'),
274 '#default_value' => $edit['weight'],
275 '#description' => t('The weights define the order in which the form fields are shown. Lighter fields "float up" towards the top of the category.'),
276 );
277 $form['fields']['autocomplete'] = array('#type' => 'checkbox',
278 '#title' => t('Form will auto-complete while user is typing.'),
279 '#default_value' => $edit['autocomplete'],
280 '#description' => t('For security, auto-complete will be disabled if the user does not have access to user profiles.'),
281 );
282 $form['fields']['required'] = array('#type' => 'checkbox',
283 '#title' => t('The user must enter a value.'),
284 '#default_value' => $edit['required'],
285 );
286 $form['fields']['register'] = array('#type' => 'checkbox',
287 '#title' => t('Visible in user registration form.'),
288 '#default_value' => $edit['register'],
289 );
290 $form['submit'] = array('#type' => 'submit',
291 '#value' => t('Save field'),
292 );
293 return $form;
294 }
295
296 /**
297 * Validate profile_field_form submissions.
298 */
299 function profile_field_form_validate($form, &$form_state) {
300 // Validate the 'field name':
301 if (preg_match('/[^a-zA-Z0-9_-]/', $form_state['values']['name'])) {
302 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.'));
303 }
304
305 $users_table = drupal_get_schema('users');
306 if (!empty($users_table['fields'][$form_state['values']['name']])) {
307 form_set_error('name', t('The specified form name is reserved for use by Drupal.'));
308 }
309 // Validate the category:
310 if (!$form_state['values']['category']) {
311 form_set_error('category', t('You must enter a category.'));
312 }
313 if (strtolower($form_state['values']['category']) == 'account') {
314 form_set_error('category', t('The specified category name is reserved for use by Drupal.'));
315 }
316 $query = db_select('profile_field');
317 $query->fields('profile_field', array('fid'));
318
319 if (isset($form_state['values']['fid'])) {
320 $query->condition('fid', $form_state['values']['fid']);
321 }
322 $query_name = clone $query;
323
324 $title = $query
325 ->condition('title', $form_state['values']['title'])
326 ->condition('category', $form_state['values']['category'])
327 ->execute()
328 ->fetchField();
329 if ($title) {
330 form_set_error('title', t('The specified title is already in use.'));
331 }
332 $name = $query_name
333 ->condition('name', $form_state['values']['name'])
334 ->execute()
335 ->fetchField();
336 if ($name) {
337 form_set_error('name', t('The specified name is already in use.'));
338 }
339 if ($form_state['values']['visibility'] == PROFILE_HIDDEN) {
340 if ($form_state['values']['required']) {
341 form_set_error('required', t('A hidden field cannot be required.'));
342 }
343 if ($form_state['values']['register']) {
344 form_set_error('register', t('A hidden field cannot be set to visible on the user registration form.'));
345 }
346 }
347 }
348
349 /**
350 * Process profile_field_form submissions.
351 */
352 function profile_field_form_submit($form, &$form_state) {
353 if (!isset($form_state['values']['options'])) {
354 $form_state['values']['options'] = '';
355 }
356 if (!isset($form_state['values']['page'])) {
357 $form_state['values']['page'] = '';
358 }
359 if (!isset($form_state['values']['fid'])) {
360 // Remove all elements that are not profile_field columns.
361 $values = array_intersect_key($form_state['values'], array_flip(array('type', 'category', 'title', 'name', 'explanation', 'visibility', 'page', 'weight', 'autocomplete', 'required', 'register', 'options')));
362 db_insert('profile_field')
363 ->fields($values)
364 ->execute();
365 drupal_set_message(t('The field has been created.'));
366 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/config/people/profile'));
367 }
368 else {
369 db_update('profile_field')
370 ->fields($form_state['values'])
371 ->condition('fid', $form_state['values']['fid'])
372 ->exeucte();
373 drupal_set_message(t('The field has been updated.'));
374 }
375 cache_clear_all();
376 menu_rebuild();
377
378 $form_state['redirect'] = 'admin/config/people/profile';
379 return;
380 }
381
382 /**
383 * Menu callback; deletes a field from all user profiles.
384 */
385 function profile_field_delete($form, &$form_state, $fid) {
386 $field = db_query("SELECT title FROM {profile_field} WHERE fid = :fid", array(':fid' => $fid))->fetchObject();
387 if (!$field) {
388 drupal_not_found();
389 return;
390 }
391 $form['fid'] = array('#type' => 'value', '#value' => $fid);
392 $form['title'] = array('#type' => 'value', '#value' => $field->title);
393
394 return confirm_form($form,
395 t('Are you sure you want to delete the field %field?', array('%field' => $field->title)), 'admin/config/people/profile',
396 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/config/people/profile/edit/' . $fid))),
397 t('Delete'), t('Cancel'));
398 }
399
400 /**
401 * Process a field delete form submission.
402 */
403 function profile_field_delete_submit($form, &$form_state) {
404 db_delete('profile_field')
405 ->condition('fid', $form_state['values']['fid'])
406 ->execute();
407 db_delete('profile_value')
408 ->condition('fid', $form_state['values']['fid'])
409 ->execute();
410
411 cache_clear_all();
412
413 drupal_set_message(t('The field %field has been deleted.', array('%field' => $form_state['values']['title'])));
414 watchdog('profile', 'Profile field %field deleted.', array('%field' => $form_state['values']['title']), WATCHDOG_NOTICE, l(t('view'), 'admin/config/people/profile'));
415
416 $form_state['redirect'] = 'admin/config/people/profile';
417 return;
418 }
419
420 /**
421 * Retrieve a pipe delimited string of autocomplete suggestions for profile categories
422 */
423 function profile_admin_settings_autocomplete($string) {
424 $matches = array();
425 $result = db_query_range("SELECT category FROM {profile_field} WHERE LOWER(category) LIKE LOWER(:category)", 0, 10, array(':category' => $string . '%'));
426 foreach ($result as $data) {
427 $matches[$data->category] = check_plain($data->category);
428 }
429 drupal_json_output($matches);
430 }

  ViewVC Help
Powered by ViewVC 1.1.2