Code coverage for /20081101/modules/profile/profile.admin.inc

Line #Times calledCode
1
<?php
2
// $Id: profile.admin.inc,v 1.17 2008/10/13 00:33:04 webchick 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
 */
1563
function profile_admin_overview() {
1624
  $result = db_query('SELECT title, name, type, category, fid, weight FROM
{profile_fields} ORDER BY category, weight');
17
1824
  $form = array();
1924
  $categories = array();
2024
  while ($field = db_fetch_object($result)) {
21
    // Collect all category information
2210
    $categories[] = $field->category;
23
24
    // Save all field information
2510
    $form[$field->fid]['name'] = array('#markup' =>
check_plain($field->name));
2610
    $form[$field->fid]['title'] = array('#markup' =>
check_plain($field->title));
2710
    $form[$field->fid]['type'] = array('#markup' => $field->type);
2810
    $form[$field->fid]['category'] = array('#type' => 'select',
'#default_value' => $field->category, '#options' => array());
2910
    $form[$field->fid]['weight'] = array('#type' => 'weight',
'#default_value' => $field->weight);
3010
    $form[$field->fid]['edit'] = array('#markup' => l(t('edit'),
"admin/user/profile/edit/$field->fid"));
3110
    $form[$field->fid]['delete'] = array('#markup' => l(t('delete'),
"admin/user/profile/delete/$field->fid"));
3210
  }
33
34
  // Add the cateogory combo boxes
3524
  $categories = array_unique($categories);
3624
  foreach ($form as $fid => $field) {
3710
    foreach ($categories as $cat => $category) {
3810
      $form[$fid]['category']['#options'][$category] = $category;
3910
    }
4010
  }
41
42
  // Display the submit button only when there's more than one field
4324
  if (count($form) > 1) {
441
    $form['submit'] = array('#type' => 'submit', '#value' => t('Save
configuration'));
451
  }
46
  else {
47
    // Disable combo boxes when there isn't a submit button
4823
    foreach ($form as $fid => $field) {
499
      unset($form[$fid]['weight']);
509
      $form[$fid]['category']['#type'] = 'value';
519
    }
52
  }
5324
  $form['#tree'] = TRUE;
54
5524
  $addnewfields = '<h2>' . t('Add new field') . '</h2>';
5624
  $addnewfields .= '<ul>';
5724
  foreach (_profile_field_types() as $key => $value) {
5824
    $addnewfields .= '<li>' . l($value, "admin/user/profile/add/$key") .
'</li>';
5924
  }
6024
  $addnewfields .= '</ul>';
6124
  $form['addnewfields'] = array('#markup' => $addnewfields);
62
6324
  return $form;
640
}
65
66
/**
67
 * Submit handler to update changed profile field weights and categories.
68
 *
69
 * @see profile_admin_overview()
70
 */
7163
function profile_admin_overview_submit($form, &$form_state) {
720
  foreach (element_children($form_state['values']) as $fid) {
730
    if (is_numeric($fid)) {
740
      $weight = $form_state['values'][$fid]['weight'];
750
      $category = $form_state['values'][$fid]['category'];
760
      if ($weight != $form[$fid]['weight']['#default_value'] || $category
!= $form[$fid]['category']['#default_value']) {
770
        db_query("UPDATE {profile_fields} SET weight = %d, category = '%s'
WHERE fid = %d", $weight, $category, $fid);
780
      }
790
    }
800
  }
81
820
  drupal_set_message(t('Profile fields have been updated.'));
830
  cache_clear_all();
840
  menu_rebuild();
850
}
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
 */
9363
function theme_profile_admin_overview($form) {
9424
  drupal_add_css(drupal_get_path('module', 'profile') . '/profile.css');
95
  // Add javascript if there's more than one field.
9624
  if (isset($form['submit'])) {
971
    drupal_add_js(drupal_get_path('module', 'profile') . '/profile.js');
981
  }
99
10024
  $rows = array();
10124
  $categories = array();
10224
  $category_number = 0;
10324
  foreach (element_children($form) as $key) {
104
    // Don't take form control structures.
10524
    if (array_key_exists('category', $form[$key])) {
10610
      $field = &$form[$key];
10710
      $category = $field['category']['#default_value'];
108
10910
      if (!isset($categories[$category])) {
110
        // Category classes are given numeric IDs because there's no
guarantee
111
        // class names won't contain invalid characters.
11210
        $categories[$category] = $category_number;
11310
        $category_field['#attributes']['class'] = 'profile-category
profile-category-' . $category_number;
11410
        $rows[] = array(array('data' => $category, 'colspan' => 7, 'class'
=> 'category'));
11510
        $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
11810
        if (isset($form['submit'])) {
1191
          drupal_add_tabledrag('profile-fields', 'order', 'sibling',
'profile-weight', 'profile-weight-' . $category_number);
1201
          drupal_add_tabledrag('profile-fields', 'match', 'sibling',
'profile-category', 'profile-category-' . $category_number);
1211
        }
12210
        $category_number++;
12310
      }
124
125
      // Add special drag and drop classes that group fields together.
12610
      $field['weight']['#attributes']['class'] = 'profile-weight
profile-weight-' . $categories[$category];
12710
      $field['category']['#attributes']['class'] = 'profile-category
profile-category-' . $categories[$category];
128
129
      // Add the row
13010
      $row = array();
13110
      $row[] = drupal_render($field['title']);
13210
      $row[] = drupal_render($field['name']);
13310
      $row[] = drupal_render($field['type']);
13410
      if (isset($form['submit'])) {
1351
        $row[] = drupal_render($field['category']);
1361
        $row[] = drupal_render($field['weight']);
1371
      }
13810
      $row[] = drupal_render($field['edit']);
13910
      $row[] = drupal_render($field['delete']);
14010
      $rows[] = array('data' => $row, 'class' => 'draggable');
14110
    }
14224
  }
14324
  if (empty($rows)) {
14414
    $rows[] = array(array('data' => t('No fields available.'), 'colspan' =>
7));
14514
  }
146
14724
  $header = array(t('Title'), t('Name'), t('Type'));
14824
  if (isset($form['submit'])) {
1491
    $header[] = t('Category');
1501
    $header[] = t('Weight');
1511
  }
15224
  $header[] = array('data' => t('Operations'), 'colspan' => 2);
153
15424
  $output = theme('table', $header, $rows, array('id' =>
'profile-fields'));
15524
  $output .= drupal_render($form);
156
15724
  return $output;
1580
}
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
 */
16763
function profile_field_form(&$form_state, $arg = NULL) {
16820
  if (arg(3) == 'edit') {
1690
    if (is_numeric($arg)) {
1700
      $fid = $arg;
171
1720
      $edit = db_fetch_array(db_query('SELECT * FROM {profile_fields} WHERE
fid = %d', $fid));
173
1740
      if (!$edit) {
1750
        drupal_not_found();
1760
        return;
1770
      }
1780
      drupal_set_title(t('edit %title', array('%title' => $edit['title'])),
PASS_THROUGH);
1790
      $form['fid'] = array('#type' => 'value',
1800
        '#value' => $fid,
181
      );
1820
      $type = $edit['type'];
1830
    }
184
    else {
1850
      drupal_not_found();
1860
      return;
187
    }
1880
  }
189
  else {
19020
    $types = _profile_field_types();
19120
    if (!isset($types[$arg])) {
1920
      drupal_not_found();
1930
      return;
1940
    }
19520
    $type = $arg;
19620
    drupal_set_title(t('add new %type', array('%type' => $types[$type])),
PASS_THROUGH);
19720
    $edit = array('name' => 'profile_');
19820
    $form['type'] = array('#type' => 'value', '#value' => $type);
199
  }
200
  $edit += array(
20120
    'category' => '',
20220
    'title' => '',
20320
    'explanation' => '',
20420
    'weight' => 0,
20520
    'page' => '',
20620
    'autocomplete' => '',
20720
    'required' => '',
20820
    'register' => '',
2090
  );
21020
  $form['fields'] = array('#type' => 'fieldset',
21120
    '#title' => t('Field settings'),
212
  );
21320
  $form['fields']['category'] = array('#type' => 'textfield',
21420
    '#title' => t('Category'),
21520
    '#default_value' => $edit['category'],
21620
    '#autocomplete_path' => 'admin/user/profile/autocomplete',
21720
    '#description' => t('The category the new field should be part of.
Categories are used to group fields logically. An example category is
"Personal information".'),
21820
    '#required' => TRUE,
219
  );
22020
  $form['fields']['title'] = array('#type' => 'textfield',
22120
    '#title' => t('Title'),
22220
    '#default_value' => $edit['title'],
22320
    '#description' => t('The title of the new field. The title will be
shown to the user. An example title is "Favorite color".'),
22420
    '#required' => TRUE,
225
  );
22620
  $form['fields']['name'] = array('#type' => 'textfield',
22720
    '#title' => t('Form name'),
22820
    '#default_value' => $edit['name'],
22920
    '#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.
23020
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".'),
23120
    '#required' => TRUE,
232
  );
23320
  $form['fields']['explanation'] = array('#type' => 'textarea',
23420
    '#title' => t('Explanation'),
23520
    '#default_value' => $edit['explanation'],
23620
    '#description' => t('An optional explanation to go with the new field.
The explanation will be shown to the user.'),
237
  );
23820
  if ($type == 'selection') {
2392
    $form['fields']['options'] = array('#type' => 'textarea',
2402
      '#title' => t('Selection options'),
2412
      '#default_value' => isset($edit['options']) ? $edit['options'] : '',
2422
      '#description' => t('A list of all options. Put each option on a
separate line. Example options are "red", "blue", "green", etc.'),
243
    );
2442
  }
24520
  $form['fields']['visibility'] = array('#type' => 'radios',
24620
    '#title' => t('Visibility'),
24720
    '#default_value' => isset($edit['visibility']) ? $edit['visibility'] :
PROFILE_PUBLIC,
24820
    '#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
  );
25020
  if ($type == 'selection' || $type == 'list' || $type == 'textfield') {
25112
    $form['fields']['page'] = array('#type' => 'textfield',
25212
      '#title' => t('Page title'),
25312
      '#default_value' => $edit['page'],
25412
      '#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
    );
25612
  }
2578
  elseif ($type == 'checkbox') {
2582
    $form['fields']['page'] = array('#type' => 'textfield',
2592
      '#title' => t('Page title'),
2602
      '#default_value' => $edit['page'],
2612
      '#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
    );
2632
  }
26420
  $form['fields']['weight'] = array('#type' => 'weight',
26520
    '#title' => t('Weight'),
26620
    '#default_value' => $edit['weight'],
26720
    '#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
  );
26920
  $form['fields']['autocomplete'] = array('#type' => 'checkbox',
27020
    '#title' => t('Form will auto-complete while user is typing.'),
27120
    '#default_value' => $edit['autocomplete'],
27220
    '#description' => t('For security, auto-complete will be disabled if
the user does not have access to user profiles.'),
273
  );
27420
  $form['fields']['required'] = array('#type' => 'checkbox',
27520
    '#title' => t('The user must enter a value.'),
27620
    '#default_value' => $edit['required'],
277
  );
27820
  $form['fields']['register'] = array('#type' => 'checkbox',
27920
    '#title' => t('Visible in user registration form.'),
28020
    '#default_value' => $edit['register'],
281
  );
28220
  $form['submit'] = array('#type' => 'submit',
28320
    '#value' => t('Save field'),
284
  );
28520
  return $form;
2860
}
287
288
/**
289
 * Validate profile_field_form submissions.
290
 */
29163
function profile_field_form_validate($form, &$form_state) {
292
  // Validate the 'field name':
29310
  if (preg_match('/[^a-zA-Z0-9_-]/', $form_state['values']['name'])) {
2940
    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.'));
2950
  }
296
29710
  $users_table = drupal_get_schema('users');
29810
  if (!empty($users_table['fields'][$form_state['values']['name']])) {
2990
    form_set_error('name', t('The specified form name is reserved for use
by Drupal.'));
3000
  }
301
  // Validate the category:
30210
  if (!$form_state['values']['category']) {
3030
    form_set_error('category', t('You must enter a category.'));
3040
  }
30510
  if (strtolower($form_state['values']['category']) == 'account') {
3060
    form_set_error('category', t('The specified category name is reserved
for use by Drupal.'));
3070
  }
30810
  $args1 = array($form_state['values']['title'],
$form_state['values']['category']);
30910
  $args2 = array($form_state['values']['name']);
31010
  $query_suffix = '';
311
31210
  if (isset($form_state['values']['fid'])) {
3130
    $args1[] = $args2[] = $form_state['values']['fid'];
3140
    $query_suffix = ' AND fid != %d';
3150
  }
316
31710
  if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE title =
'%s' AND category = '%s'" . $query_suffix, $args1))) {
3180
    form_set_error('title', t('The specified title is already in use.'));
3190
  }
32010
  if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE name =
'%s'" . $query_suffix, $args2))) {
3210
    form_set_error('name', t('The specified name is already in use.'));
3220
  }
32310
  if ($form_state['values']['visibility'] == PROFILE_HIDDEN) {
3240
    if ($form_state['values']['required']) {
3250
      form_set_error('required', t('A hidden field cannot be required.'));
3260
    }
3270
    if ($form_state['values']['register']) {
3280
      form_set_error('register', t('A hidden field cannot be set to visible
on the user registration form.'));
3290
    }
3300
  }
33110
}
332
333
/**
334
 * Process profile_field_form submissions.
335
 */
33663
function profile_field_form_submit($form, &$form_state) {
33710
  if (!isset($form_state['values']['options'])) {
3389
    $form_state['values']['options'] = '';
3399
  }
34010
  if (!isset($form_state['values']['page'])) {
3413
    $form_state['values']['page'] = '';
3423
  }
34310
  if (!isset($form_state['values']['fid'])) {
34410
    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']);
345
34610
    drupal_set_message(t('The field has been created.'));
34710
    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'));
34810
  }
349
  else {
3500
    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']);
351
3520
    drupal_set_message(t('The field has been updated.'));
353
  }
35410
  cache_clear_all();
35510
  menu_rebuild();
356
35710
  $form_state['redirect'] = 'admin/user/profile';
35810
  return;
3590
}
360
361
/**
362
 * Menu callback; deletes a field from all user profiles.
363
 */
36463
function profile_field_delete(&$form_state, $fid) {
36514
  $field = db_fetch_object(db_query("SELECT title FROM {profile_fields}
WHERE fid = %d", $fid));
36614
  if (!$field) {
3670
    drupal_not_found();
3680
    return;
3690
  }
37014
  $form['fid'] = array('#type' => 'value', '#value' => $fid);
37114
  $form['title'] = array('#type' => 'value', '#value' => $field->title);
372
37314
  return confirm_form($form,
37414
    t('Are you sure you want to delete the field %field?', array('%field'
=> $field->title)), 'admin/user/profile',
37514
    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))),
37614
    t('Delete'), t('Cancel'));
3770
}
378
379
/**
380
 * Process a field delete form submission.
381
 */
38263
function profile_field_delete_submit($form, &$form_state) {
3837
  db_query('DELETE FROM {profile_fields} WHERE fid = %d',
$form_state['values']['fid']);
3847
  db_query('DELETE FROM {profile_values} WHERE fid = %d',
$form_state['values']['fid']);
385
3867
  cache_clear_all();
387
3887
  drupal_set_message(t('The field %field has been deleted.', array('%field'
=> $form_state['values']['title'])));
3897
  watchdog('profile', 'Profile field %field deleted.', array('%field' =>
$form_state['values']['title']), WATCHDOG_NOTICE, l(t('view'),
'admin/user/profile'));
390
3917
  $form_state['redirect'] = 'admin/user/profile';
3927
  return;
3930
}
394
395
/**
396
 * Retrieve a pipe delimited string of autocomplete suggestions for profile
categories
397
 */
39863
function profile_admin_settings_autocomplete($string) {
3990
  $matches = array();
4000
  $result = db_query_range("SELECT category FROM {profile_fields} WHERE
LOWER(category) LIKE LOWER(:category)", array(':category' => $string .'%'),
0, 10);
4010
  while ($data = db_fetch_object($result)) {
4020
    $matches[$data->category] = check_plain($data->category);
4030
  }
4040
  print drupal_to_js($matches);
4050
  exit();
4060
}
40763