You are here

function encrypt_config_form in Encrypt 7.3

Same name and namespace in other branches
  1. 7.2 includes/encrypt.admin.inc \encrypt_config_form()

Form constructor for the configuration edit form.

Parameters

array $config: (optional) An array representing the configuration, when editing an existing configuration.

See also

encrypt_config_form_validate()

encrypt_config_form_submit()

1 string reference to 'encrypt_config_form'
encrypt_menu in ./encrypt.module
Implements hook_menu().

File

includes/encrypt.admin.inc, line 103
This file holds the functions for the Encrypt admin settings.

Code

function encrypt_config_form($form, &$form_state, $config = NULL) {
  $default_config = variable_get('encrypt_default_config', NULL);

  // Clear the plugin cache on the first page load (but not on AJAX refreshes).
  if (!isset($form_state['values'])) {
    _encrypt_clear_plugin_cache();
  }

  // Retrieve all methods and providers.
  $methods = encrypt_get_encryption_methods();
  $providers = encrypt_get_key_providers();

  // Make sure all plugin files are included (for form validation, etc.)
  // @todo: There has to be a better/faster/cleaner way to do this.
  foreach ($providers as $provider) {
    $form_state['build_info']['files']["encrypt:plugins:{$provider['name']}"] = $provider['path'] . '/' . $provider['file'];
  }

  // Create a list of method titles to be used for radio buttons.
  $method_options = array();
  foreach ($methods as $id => $method) {
    $method_options[$id] = $method['title'];
  }

  // Create a list of provider titles to be used for radio buttons.
  $provider_options = array();
  foreach ($providers as $id => $provider) {
    $provider_options[$id] = $provider['title'];
  }
  $form = array();
  $form['#attached'] = array(
    'css' => array(
      drupal_get_path('module', 'encrypt') . '/encrypt.css',
    ),
  );
  $form['label'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#default_value' => $config['label'],
    '#description' => t('The human-readable name of the configuration.'),
    '#required' => TRUE,
    '#size' => 30,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#default_value' => $config['name'],
    '#maxlength' => 32,
    '#disabled' => isset($config['name']),
    '#machine_name' => array(
      'exists' => 'encrypt_config_load',
      'source' => array(
        'label',
      ),
    ),
    '#description' => t('A unique machine-readable name for the configuration. It must only contain lowercase letters, numbers, and underscores.'),
  );
  $form['description'] = array(
    '#title' => t('Description'),
    '#type' => 'textarea',
    '#default_value' => $config['description'],
    '#description' => t('A short description of the configuration.'),
  );
  $form['settings'] = array(
    '#type' => 'vertical_tabs',
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'encrypt') . '/js/encrypt_admin.js',
      ),
    ),
  );
  $form['general_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('General settings'),
    '#collapsible' => TRUE,
    '#group' => 'settings',
  );
  $form['general_settings']['enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enabled'),
    '#default_value' => $config['enabled'],
    '#description' => t('If checked, this configuration will be available for encryption. The default configuration must be enabled.'),
  );

  // If this is the default configuration, disable the enabled checkbox.
  if ($config['name'] == $default_config) {
    $form['general_settings']['enabled']['#disabled'] = TRUE;
  }
  $form['method_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Encryption method settings'),
    '#collapsible' => TRUE,
    '#group' => 'settings',
  );
  $form['method_settings']['encrypt_encryption_method'] = array(
    '#type' => 'radios',
    '#title' => t('Encryption Method'),
    '#description' => t('Define the default encryption method for the site. Since encryption methods are stored with the data, this can be changed even after you have stored encrypted data.'),
    '#required' => TRUE,
    '#options' => $method_options,
    '#default_value' => $config['method'],
    '#ajax' => array(
      'method' => 'replace',
      'callback' => 'encrypt_encryption_methods_additional_settings_ajax',
      'wrapper' => 'encrypt-encryption-methods-additional-settings',
    ),
  );

  // Disable any method with dependency errors.
  _encrypt_admin_form_add_options($methods, $form['method_settings']['encrypt_encryption_method']);
  $form['method_settings']['method_settings_wrapper'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="encrypt-encryption-methods-additional-settings">',
    '#suffix' => '</div>',
  );
  if (isset($form_state['values']['encrypt_encryption_method'])) {
    $method = $form_state['values']['encrypt_encryption_method'];
  }
  elseif (isset($config['method'])) {
    $method = $config['method'];
  }
  else {
    $method = NULL;
  }
  if ($method) {
    if ($method_settings_form = ctools_plugin_get_function($methods[$method], 'settings form')) {
      $form['method_settings']['method_settings_wrapper']['method_settings'] = array(
        '#type' => 'fieldset',
        '#title' => t('Additional Encryption Method Settings'),
      );
      $form['method_settings']['method_settings_wrapper']['method_settings']['encrypt_encryption_methods_' . $method . '_settings'] = array(
        '#tree' => TRUE,
      ) + call_user_func($method_settings_form, $config['method_settings']);
    }
  }
  $form['provider_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Key provider settings'),
    '#collapsible' => TRUE,
    '#group' => 'settings',
  );
  $form['provider_settings']['encrypt_key_provider'] = array(
    '#type' => 'radios',
    '#title' => t('Key Provider'),
    '#description' => t('Select the method by which encrypt will retrieve an encryption key. NOTE: Once this is set, it is not a good idea to change it. All of your encrypted data may be lost if the encryption key changes.'),
    '#required' => TRUE,
    '#options' => $provider_options,
    '#default_value' => $config['provider'],
    '#ajax' => array(
      'method' => 'replace',
      'callback' => 'encrypt_key_providers_additional_settings_ajax',
      'wrapper' => 'encrypt-key-providers-additional-settings',
    ),
  );

  // Disable any method with dependency errors.
  _encrypt_admin_form_add_options($providers, $form['provider_settings']['encrypt_key_provider']);
  $form['provider_settings']['key_settings_wrapper'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="encrypt-key-providers-additional-settings">',
    '#suffix' => '</div>',
  );
  if (isset($form_state['values']['encrypt_key_provider'])) {
    $provider = $form_state['values']['encrypt_key_provider'];
  }
  elseif (isset($config['provider'])) {
    $provider = $config['provider'];
  }
  else {
    $provider = NULL;
  }
  if ($provider) {
    if ($provider_settings_form = ctools_plugin_get_function($providers[$provider], 'settings form')) {
      $form['provider_settings']['key_settings_wrapper']['key_settings'] = array(
        '#type' => 'fieldset',
        '#title' => t('Additional Key Provider Settings'),
      );
      $form['provider_settings']['key_settings_wrapper']['key_settings']['encrypt_key_providers_' . $provider . '_settings'] = array(
        '#tree' => TRUE,
      ) + call_user_func($provider_settings_form, $config['provider_settings']);
    }
  }
  $form['created'] = array(
    '#type' => 'value',
    '#value' => $config['created'],
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
    '#submit' => array(
      'encrypt_config_form_submit',
    ),
    '#weight' => 40,
  );
  if (isset($config['name'])) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete configuration'),
      '#submit' => array(
        'encrypt_config_form_delete_submit',
      ),
      '#limit_validation_errors' => array(),
      '#weight' => 45,
    );
  }
  return $form;
}