You are here

function spaces_preset_form in Spaces 6.2

Same name and namespace in other branches
  1. 5.2 spaces_admin.inc \spaces_preset_form()
  2. 6.3 spaces.admin.inc \spaces_preset_form()
  3. 6 spaces_admin.inc \spaces_preset_form()
  4. 7.3 spaces.admin.inc \spaces_preset_form()
  5. 7 spaces.admin.inc \spaces_preset_form()

Form for adding or editing a spaces preset.

Parameters

$op: A string for the operation to perform. Either 'add' or 'edit'.

$type: A space type string.

$preset_id: The preset identifier for edit forms.

Return value

A FormAPI array.

1 string reference to 'spaces_preset_form'
spaces_menu in ./spaces.module
Implementation of hook_menu().

File

./spaces_admin.inc, line 117

Code

function spaces_preset_form(&$form_state, $op = 'add', $type, $preset_id = NULL) {
  $form = array();
  $space = spaces_load($type);
  switch ($op) {
    case 'add':
      drupal_set_title(t('Add spaces preset'));
      break;
    case 'edit':
      if ($type && $preset_id) {
        drupal_set_title(t('Edit preset: !presetname', array(
          '!presetname' => $preset_id,
        )));

        // Enforce preset settings on space object
        $space->preset = $preset_id;
        spaces_preset_enforce($space);

        // Load preset for form info
        $presets = spaces_presets($type);
        $preset = $presets[$preset_id];
      }
      break;
  }

  // Preset fields
  $form['preset'] = array(
    '#tree' => true,
  );
  $form['preset']['id'] = array(
    '#type' => 'textfield',
    '#title' => t('Preset id'),
    '#required' => true,
    '#maxlength' => 64,
    '#description' => t('Enter an id for your preset. It may only contain lowercase letters, numbers, and underscores or dashes. <strong>Example:</strong> private_space'),
  );
  $form['preset']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Preset name'),
    '#required' => true,
    '#maxlength' => 255,
    '#description' => t('Enter a name for your preset. It will be displayed to users in forms and messages.'),
  );
  $form['preset']['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Description'),
    '#description' => t('Enter a description for your preset. It should help users understand what the preset provides.'),
  );

  // Features/settings form
  $form['features_form'] = _spaces_features_form($space);
  $form['features_form']['#tree'] = FALSE;
  $form['features_form']['#theme'] = 'spaces_features_form';

  // Add locks to features/settings
  $form['features_form']['locked'] = array(
    '#tree' => TRUE,
    'features' => array(
      '#tree' => TRUE,
    ),
    'settings' => array(
      '#tree' => TRUE,
    ),
  );
  foreach (element_children($form['features_form']['features']) as $id) {
    $form['features_form']['locked']['features'][$id] = array(
      '#type' => 'checkbox',
    );
  }
  foreach (element_children($form['features_form']['settings']) as $id) {
    $form['features_form']['locked']['settings'][$id] = array(
      '#type' => 'checkbox',
    );
  }

  // Type-specific form
  $form[$type] = $space
    ->form('preset');

  // Set default values
  if ($op == 'edit') {
    $form['preset']['id']['#default_value'] = $form['preset']['id']['#value'] = $preset_id;
    $form['preset']['id']['#disabled'] = true;
    $form['preset']['name']['#default_value'] = $preset['name'];
    $form['preset']['description']['#default_value'] = $preset['description'];
    foreach (element_children($form['features_form']['features']) as $id) {
      $form['features_form']['locked']['features'][$id]['#default_value'] = isset($preset['preset']['locked']['features'][$id]) ? $preset['preset']['locked']['features'][$id] : 0;
    }
    foreach (element_children($form['features_form']['settings']) as $id) {
      $form['features_form']['locked']['settings'][$id]['#default_value'] = isset($preset['preset']['locked']['settings'][$id]) ? $preset['preset']['locked']['settings'][$id] : 0;
    }
  }
  $form['buttons'] = array(
    '#tree' => FALSE,
    '#theme' => 'features_form_buttons',
  );
  $form['buttons']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#weight' => 10,
  );
  return $form;
}