You are here

function user_readonly_admin_settings in User Read-Only 6

Same name and namespace in other branches
  1. 5 user_readonly.module \user_readonly_admin_settings()
  2. 7 user_readonly.module \user_readonly_admin_settings()

Configuration form for setting default restrictions.

Return value

array

1 string reference to 'user_readonly_admin_settings'
user_readonly_menu in ./user_readonly.module
Implementation of hook_menu().

File

./user_readonly.module, line 66
This module provides restrictions on user account/profile fields.

Code

function user_readonly_admin_settings() {

  // important: tell Drupal to keep the tree structure of the form
  $form = array(
    '#tree' => TRUE,
  );
  $settings = _user_readonly_get();

  // build the list of fields that we can configure
  $fields = array();

  // drupal_retrieve_form() call changes the page title... so we workaround this
  $title = drupal_get_title();
  $form_state = array();
  $user_edit = drupal_retrieve_form('user_edit_form', $form_state, '', array(
    'name' => '',
    'mail' => '',
  ));
  drupal_set_title($title);
  foreach ($user_edit as $k => $v) {
    if ($k[0] != '_' && !element_property($k) && is_array($v)) {
      foreach ($v as $key => $value) {
        if (!element_property($key) && is_array($v[$key])) {
          $fields[$key] = check_plain($v['#title']) . (empty($v['#title']) ? '' : ' » ') . check_plain($key == 'pass' ? t('Password') : $value['#title']);
        }
      }
    }
  }
  if (module_exists('profile')) {
    $result = db_query('SELECT name, title FROM {profile_fields} ORDER BY category, weight, title');
    while ($obj = db_fetch_object($result)) {
      $fields[$obj->name] = $obj->title;
    }
  }
  $roles = user_roles(TRUE);
  $modes = array(
    0 => t('Use the default role settings.'),
    'deny' => t('Only <strong>DENY</strong> changes to users with these roles.'),
    'allow' => t('Only <strong>ALLOW</strong> changes to users with these roles.'),
  );

  // create the part of the form all the fields use for every field options
  $partialf = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $partialf['mode'] = array(
    '#type' => 'radios',
    '#title' => t('Restrictions mode'),
    '#default_value' => $settings[0]['mode'],
    '#options' => $modes,
    '#description' => t('Define the restriction rules. To deny all changes, choose ALLOW, but do not select any fields below.'),
  );
  $partialf['roles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Roles'),
    '#default_value' => $settings[0]['roles'],
    '#options' => $roles,
  );
  $partialf['action'] = array(
    '#type' => 'radios',
    '#title' => t('When restrictions apply'),
    '#default_value' => $settings[0]['action'],
    '#options' => array(
      0 => t('Use the default settings.'),
      'disable' => t('Disable the fields.'),
      'hide' => t('Hide the fields.'),
    ),
    '#description' => t('Disabling fields greys them out but users can still see the fields. Hiding fields removes them from the form. This option is independent from the above restriction rule setting.'),
  );

  // create the default settings form
  $defaultf = $partialf;
  $defaultf['#collapsed'] = FALSE;
  $defaultf['#title'] = t('Default settings for all fields');
  unset($defaultf['mode']['#options'][0]);
  unset($defaultf['action']['#options'][0]);
  $form[0] = $defaultf;

  // create the form for all fields
  foreach ($fields as $fid => $fname) {
    $partialf['#title'] = $fname;
    if (isset($settings[$fid])) {
      $defaults = $settings[$fid];
    }
    if (empty($defaults)) {
      $defaults = array(
        'mode' => 0,
        'roles' => array(),
        'action' => 0,
      );
    }
    $partialf['mode']['#default_value'] = $defaults['mode'];
    $partialf['roles']['#default_value'] = $defaults['roles'];
    $partialf['action']['#default_value'] = $defaults['action'];
    $form[$fid] = $partialf;
    if (!empty($defaults['mode']) || !empty($defaults['action'])) {
      $form[$fid]['#collapsed'] = FALSE;
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );

  // doesn't use standard system_settings_form() because the config is stored as an array of role settings
  return $form;
}