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

Line #Times calledCode
1
<?php
2
// $Id: profile.admin.inc,v 1.11 2008/07/16 21:59:27 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
 */
1559
function profile_admin_overview() {
1623
  $result = db_query('SELECT title, name, type, category, fid, weight FROM
{profile_fields} ORDER BY category, weight');
17
1823
  $form = array();
1923
  $categories = array();
2023
  while ($field = db_fetch_object($result)) {
21
    // Collect all category information
229
    $categories[] = $field->category;
23
24
    // Save all field information
259
    $form[$field->fid]['name'] = array('#markup' =>
check_plain($field->name));
269
    $form[$field->fid]['title'] = array('#markup' =>
check_plain($field->title));
279
    $form[$field->fid]['type'] = array('#markup' => $field->type);
289
    $form[$field->fid]['category'] = array('#type' => 'select',
'#default_value' => $field->category, '#options' => array());
299
    $form[$field->fid]['weight'] = array('#type' => 'weight',
'#default_value' => $field->weight);
309
    $form[$field->fid]['edit'] = array('#markup' => l(t('edit'),
"admin/user/profile/edit/$field->fid"));
319
    $form[$field->fid]['delete'] = array('#markup' => l(t('delete'),
"admin/user/profile/delete/$field->fid"));
329
  }
33
34
  // Add the cateogory combo boxes
3523
  $categories = array_unique($categories);
3623
  foreach ($form as $fid => $field) {
379
    foreach ($categories as $cat => $category) {
389
      $form[$fid]['category']['#options'][$category] = $category;
399
    }
409
  }
41
42
  // Display the submit button only when there's more than one field
4323
  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
4822
    foreach ($form as $fid => $field) {
498
      unset($form[$fid]['weight']);
508
      $form[$fid]['category']['#type'] = 'value';
518
    }
52
  }
5323
  $form['#tree'] = TRUE;
54
5523
  $addnewfields = '<h2>' . t('Add new field') . '</h2>';
5623
  $addnewfields .= '<ul>';
5723
  foreach (_profile_field_types() as $key => $value) {
5823
    $addnewfields .= '<li>' . l($value, "admin/user/profile/add/$key") .
'</li>';
5923
  }
6023
  $addnewfields .= '</ul>';
6123
  $form['addnewfields'] = array('#markup' => $addnewfields);
62
6323
  return $form;
640
}
65
66
/**
67
 * Submit handler to update changed profile field weights and categories.
68
 *
69
 * @see profile_admin_overview()
70
 */
7159
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
 */
9359
function theme_profile_admin_overview($form) {
9423
  drupal_add_css(drupal_get_path('module', 'profile') . '/profile.css');
95
  // Add javascript if there's more than one field.
9623
  if (isset($form['submit'])) {
971
    drupal_add_js(drupal_get_path('module', 'profile') . '/profile.js');
981
  }
99
10023
  $rows = array();
10123
  $categories = array();
10223
  $category_number = 0;
10323
  foreach (element_children($form) as $key) {
104
    // Don't take form control structures.
10523
    if (array_key_exists('category', $form[$key])) {
1069
      $field = &$form[$key];
1079
      $category = $field['category']['#default_value'];
108
1099
      if (!isset($categories[$category])) {
110
        // Category classes are given numeric IDs because there's no
guarantee
111
        // class names won't contain invalid characters.
1129
        $categories[$category] = $category_number;
1139
        $category_field['#attributes']['class'] = 'profile-category
profile-category-' . $category_number;
1149
        $rows[] = array(array('data' => $category, 'colspan' => 7, 'class'
=> 'category'));
1159
        $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
1189
        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
        }
1229
        $category_number++;
1239
      }
124
125
      // Add special drag and drop classes that group fields together.
1269
      $field['weight']['#attributes']['class'] = 'profile-weight
profile-weight-' . $categories[$category];
1279
      $field['category']['#attributes']['class'] = 'profile-category
profile-category-' . $categories[$category];
128
129
      // Add the row
1309
      $row = array();
1319
      $row[] = drupal_render($field['title']);
1329
      $row[] = drupal_render($field['name']);
1339
      $row[] = drupal_render($field['type']);
1349
      if (isset($form['submit'])) {
1351
        $row[] = drupal_render($field['category']);
1361
        $row[] = drupal_render($field['weight']);
1371
      }
1389
      $row[] = drupal_render($field['edit']);
1399
      $row[] = drupal_render($field['delete']);
1409
      $rows[] = array('data' => $row, 'class' => 'draggable');
1419
    }
14223
  }
14323
  if (empty($rows)) {
14414
    $rows[] = array(array('data' => t('No fields available.'), 'colspan' =>
7));
14514
  }
146
14723
  $header = array(t('Title'), t('Name'), t('Type'));
14823
  if (isset($form['submit'])) {
1491
    $header[] = t('Category');
1501
    $header[] = t('Weight');
1511
  }
15223
  $header[] = array('data' => t('Operations'), 'colspan' => 2);
153
15423
  $output = theme('table', $header, $rows, array('id' =>
'profile-fields'));
15523
  $output .= drupal_render($form);
156
15723
  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
 */
16759
function profile_field_form(&$form_state, $arg = NULL) {
16818
  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'])));
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 {
19018
    $types = _profile_field_types();
19118
    if (!isset($types[$arg])) {
1920
      drupal_not_found();
1930
      return;
1940
    }
19518
    $type = $arg;
19618
    drupal_set_title(t('add new %type', array('%type' => $types[$type])));
19718
    $edit = array('name' => 'profile_');
19818
    $form['type'] = array('#type' => 'value', '#value' => $type);
199
  }
200
  $edit += array(
20118
    'category' => '',
20218
    'title' => '',
20318
    'explanation' => '',
20418
    'weight' => 0,
20518
    'page' => '',
20618
    'autocomplete' => '',
20718
    'required' => '',
20818
    'register' => '',
2090
  );
21018
  $form['fields'] = array('#type' => 'fieldset',
21118
    '#title' => t('Field settings'),
212
  );
21318
  $form['fields']['category'] = array('#type' => 'textfield',
21418
    '#title' => t('Category'),
21518
    '#default_value' => $edit['category'],
21618
    '#autocomplete_path' => 'admin/user/profile/autocomplete',
21718
    '#description' => t('The category the new field should be part of.
Categories are used to group fields logically. An example category is
"Personal information".'),
21818
    '#required' => TRUE,
219
  );
22018
  $form['fields']['title'] = array('#type' => 'textfield',
22118
    '#title' => t('Title'),
22218
    '#default_value' => $edit['title'],
22318
    '#description' => t('The title of the new field. The title will be
shown to the user. An example title is "Favorite color".'),
22418
    '#required' => TRUE,
225
  );
22618
  $form['fields']['name'] = array('#type' => 'textfield',
22718
    '#title' => t('Form name'),
22818
    '#default_value' => $edit['name'],
22918
    '#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.
23018
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".'),
23118
    '#required' => TRUE,
232
  );
23318
  $form['fields']['explanation'] = array('#type' => 'textarea',
23418
    '#title' => t('Explanation'),
23518
    '#default_value' => $edit['explanation'],
23618
    '#description' => t('An optional explanation to go with the new field.
The explanation will be shown to the user.'),
237
  );
23818
  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
  }
24518
  $form['fields']['visibility'] = array('#type' => 'radios',
24618
    '#title' => t('Visibility'),
24718
    '#default_value' => isset($edit['visibility']) ? $edit['visibility'] :
PROFILE_PUBLIC,
24818
    '#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
  );
25018
  if ($type == 'selection' || $type == 'list' || $type == 'textfield') {
25110
    $form['fields']['page'] = array('#type' => 'textfield',
25210
      '#title' => t('Page title'),
25310
      '#default_value' => $edit['page'],
25410
      '#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
    );
25610
  }
2578
  else if ($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
  }
26418
  $form['fields']['weight'] = array('#type' => 'weight',
26518
    '#title' => t('Weight'),
26618
    '#default_value' => $edit['weight'],
26718
    '#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
  );
26918
  $form['fields']['autocomplete'] = array('#type' => 'checkbox',
27018
    '#title' => t('Form will auto-complete while user is typing.'),
27118
    '#default_value' => $edit['autocomplete'],
272
  );
27318
  $form['fields']['required'] = array('#type' => 'checkbox',
27418
    '#title' => t('The user must enter a value.'),
27518
    '#default_value' => $edit['required'],
276
  );
27718
  $form['fields']['register'] = array('#type' => 'checkbox',
27818
    '#title' => t('Visible in user registration form.'),
27918
    '#default_value' => $edit['register'],
280
  );
28118
  $form['submit'] = array('#type' => 'submit',
28218
    '#value' => t('Save field'),
283
  );
28418
  return $form;
2850
}
286
287
/**
288
 * Validate profile_field_form submissions.
289
 */
29059
function profile_field_form_validate($form, &$form_state) {
291
  // Validate the 'field name':
2929
  if (preg_match('/[^a-zA-Z0-9_-]/', $form_state['values']['name'])) {
2930
    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.'));
2940
  }
295
2969
  $users_table = drupal_get_schema('users');
2979
  if (!empty($users_table['fields'][$form_state['values']['name']])) {
2980
    form_set_error('name', t('The specified form name is reserved for use
by Drupal.'));
2990
  }
300
  // Validate the category:
3019
  if (!$form_state['values']['category']) {
3020
    form_set_error('category', t('You must enter a category.'));
3030
  }
3049
  if (strtolower($form_state['values']['category']) == 'account') {
3050
    form_set_error('category', t('The specified category name is reserved
for use by Drupal.'));
3060
  }
3079
  $args1 = array($form_state['values']['title'],
$form_state['values']['category']);
3089
  $args2 = array($form_state['values']['name']);
3099
  $query_suffix = '';
310
3119
  if (isset($form_state['values']['fid'])) {
3120
    $args1[] = $args2[] = $form_state['values']['fid'];
3130
    $query_suffix = ' AND fid != %d';
3140
  }
315
3169
  if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE title =
'%s' AND category = '%s'" . $query_suffix, $args1))) {
3170
    form_set_error('title', t('The specified title is already in use.'));
3180
  }
3199
  if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE name =
'%s'" . $query_suffix, $args2))) {
3200
    form_set_error('name', t('The specified name is already in use.'));
3210
  }
3229
  if ($form_state['values']['visibility'] == PROFILE_HIDDEN) {
3230
    if ($form_state['values']['required']) {
3240
      form_set_error('required', t('A hidden field cannot be required.'));
3250
    }
3260
    if ($form_state['values']['register']) {
3270
      form_set_error('register', t('A hidden field cannot be set to visible
on the user registration form.'));
3280
    }
3290
  }
3309
}
331
332
/**
333
 * Process profile_field_form submissions.
334
 */
33559
function profile_field_form_submit($form, &$form_state) {
3369
  if (!isset($form_state['values']['options'])) {
3378
    $form_state['values']['options'] = '';
3388
  }
3399
  if (!isset($form_state['values']['page'])) {
3403
    $form_state['values']['page'] = '';
3413
  }
3429
  if (!isset($form_state['values']['fid'])) {
3439
    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']);
344
3459
    drupal_set_message(t('The field has been created.'));
3469
    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'));
3479
  }
348
  else {
3490
    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']);
350
3510
    drupal_set_message(t('The field has been updated.'));
352
  }
3539
  cache_clear_all();
3549
  menu_rebuild();
355
3569
  $form_state['redirect'] = 'admin/user/profile';
3579
  return;
3580
}
359
360
/**
361
 * Menu callback; deletes a field from all user profiles.
362
 */
36359
function profile_field_delete(&$form_state, $fid) {
36414
  $field = db_fetch_object(db_query("SELECT title FROM {profile_fields}
WHERE fid = %d", $fid));
36514
  if (!$field) {
3660
    drupal_not_found();
3670
    return;
3680
  }
36914
  $form['fid'] = array('#type' => 'value', '#value' => $fid);
37014
  $form['title'] = array('#type' => 'value', '#value' => $field->title);
371
37214
  return confirm_form($form,
37314
    t('Are you sure you want to delete the field %field?', array('%field'
=> $field->title)), 'admin/user/profile',
37414
    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))),
37514
    t('Delete'), t('Cancel'));
3760
}
377
378
/**
379
 * Process a field delete form submission.
380
 */
38159
function profile_field_delete_submit($form, &$form_state) {
3827
  db_query('DELETE FROM {profile_fields} WHERE fid = %d',
$form_state['values']['fid']);
3837
  db_query('DELETE FROM {profile_values} WHERE fid = %d',
$form_state['values']['fid']);
384
3857
  cache_clear_all();
386
3877
  drupal_set_message(t('The field %field has been deleted.', array('%field'
=> $form_state['values']['title'])));
3887
  watchdog('profile', 'Profile field %field deleted.', array('%field' =>
$form_state['values']['title']), WATCHDOG_NOTICE, l(t('view'),
'admin/user/profile'));
389
3907
  $form_state['redirect'] = 'admin/user/profile';
3917
  return;
3920
}
393
394
/**
395
 * Retrieve a pipe delimited string of autocomplete suggestions for profile
categories
396
 */
39759
function profile_admin_settings_autocomplete($string) {
3980
  $matches = array();
3990
  $result = db_query_range("SELECT category FROM {profile_fields} WHERE
LOWER(category) LIKE LOWER('%s%%')", $string, 0, 10);
4000
  while ($data = db_fetch_object($result)) {
4010
    $matches[$data->category] = check_plain($data->category);
4020
  }
4030
  print drupal_to_js($matches);
4040
  exit();
4050
}
40659