You are here

function party_hat_form in Party 8.2

Same name and namespace in other branches
  1. 7 modules/party_hat/party_hat.admin.inc \party_hat_form()

Entity form for party hats.

Parameters

$hat: A hat object.

File

modules/party_hat/party_hat.admin.inc, line 13
Contains admin page callbacks for the party hats module.

Code

function party_hat_form($form, &$form_state, $hat, $op = 'edit') {
  $form['#hat'] = $hat;
  if (isset($hat->hid)) {
    $form['hid'] = array(
      '#type' => 'hidden',
      '#value' => $hat->hid,
    );
    drupal_set_title(t('Edit hat: @label', array(
      '@label' => $hat->label,
    )));
  }
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Hat name'),
    '#required' => TRUE,
    '#default_value' => isset($hat->label) ? $hat->label : '',
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#title' => t('Hat machine name'),
    '#default_value' => isset($hat->name) ? $hat->name : '',
    '#description' => t('A unique, machine readable name for the hat'),
    '#machine_name' => array(
      'exists' => 'party_hat_hat_machine_name_exists',
      'source' => array(
        'label',
      ),
    ),
    '#disabled' => isset($hat->name),
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => isset($hat->description) ? $hat->description : '',
  );
  $parents = party_hat_get_tree();
  $descendants = isset($hat->name) ? party_hat_get_tree($hat->name) : array();
  $options = array(
    '' => t('--No Parent--'),
  );
  foreach ($parents as $parent) {

    // Don't include this hat in the options.
    if (isset($hat->name) && $parent->name == $hat->name) {
      continue;
    }

    // Don't include hats that are descendents of this hat in the options.
    if (isset($descendants[$parent->name])) {
      continue;
    }
    $options[$parent->name] = str_repeat('-', $parent->depth) . $parent->label;
  }
  $form['parent'] = array(
    '#type' => 'select',
    '#title' => t('Parent'),
    '#options' => $options,
    '#default_value' => isset($hat->parent) ? $hat->parent : NULL,
    '#description' => t('The parent hat for this hat.'),
  );
  field_attach_form('party_hat', $hat, $form, $form_state);
  $form['required'] = array(
    '#type' => 'checkbox',
    '#title' => t('Required'),
    '#default_value' => isset($hat->required) ? $hat->required : 0,
    '#description' => 'If enabled, all parties require this hat.',
    '#weight' => 1,
  );
  $form['data_set_rules'] = array(
    '#type' => 'fieldset',
    '#title' => t('Data rules'),
    '#description' => 'Set rules for which data set these hats give.',
    '#weight' => 3,
    '#tree' => TRUE,
    '#theme' => 'crm_hat_data_set_rules_form',
  );
  foreach (party_get_data_set_info() as $data_set_name => $data_set) {

    // Get the rule.
    if (isset($hat->name)) {
      $default = party_hat_get_data_set_rule($hat, $data_set_name);
      foreach ($default as $key => $var) {
        if ($var == 1) {
          $default[$key] = $key;
        }
      }
    }
    else {
      $default = array(
        'has' => 0,
        'multiple' => 0,
      );
    }
    $form['data_set_rules'][$data_set_name] = array(
      '#type' => 'checkboxes',
      '#title' => check_plain($data_set['label']),
      '#default_value' => $default,
      '#options' => array(
        // We have to use different keys to what the access hook expects for
        // FormAPI radio values to work.
        'has' => t('Allowed'),
        'multiple' => t('Multiple'),
      ),
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 99,
  );
  return $form;
}