You are here

function farm_plan_consideration_form in farmOS 7

Build the farm plan considerations form.

1 string reference to 'farm_plan_consideration_form'
farm_plan_consideration_menu in modules/farm/farm_plan/farm_plan_consideration/farm_plan_consideration.module
Implements hook_menu().

File

modules/farm/farm_plan/farm_plan_consideration/farm_plan_consideration.module, line 348
Farm plan consideration module.

Code

function farm_plan_consideration_form($form, &$form_state, $plan, $consideration_id = NULL) {

  // Set the page title.
  if (empty($consideration_id)) {
    $title = t('Add a consideration');
  }
  else {
    $title = t('Edit consideration');
  }
  drupal_set_title($title);

  // Store the plan in the form for future use.
  $form['plan'] = array(
    '#type' => 'value',
    '#value' => $plan,
  );

  // Load the consideration record, if one is specified.
  if (!empty($consideration_id)) {
    $consideration = farm_plan_consideration_load($consideration_id);
  }
  else {
    $consideration = farm_plan_consideration_create();
  }

  // Store the consideration in the form for future use.
  $form['consideration'] = array(
    '#type' => 'value',
    '#value' => $consideration,
  );

  // Build an array of consideration type options.
  $consideration_types = farm_plan_consideration_types();
  $consideration_type_options = array();
  foreach ($consideration_types as $type => $info) {
    if (!empty($info['label'])) {
      $consideration_type_options[$type] = $info['label'];
    }
  }

  // Consideration type.
  $form['type'] = array(
    '#type' => 'select',
    '#title' => t('Consideration type'),
    '#description' => t('Specify what type of consideration this is.'),
    '#options' => $consideration_type_options,
    '#default_value' => !empty($consideration->type) ? $consideration->type : 'concern',
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => 'farm_plan_consideration_form_ajax',
      'wrapper' => 'consideration-extra',
    ),
  );

  // Name.
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#description' => t('Provide a brief name for this consideration to describe it.'),
    '#default_value' => !empty($consideration->name) ? $consideration->name : '',
    '#required' => TRUE,
  );

  // Define date format.
  $date_format = 'Y-m-d';

  // Start date.
  $default_start_time = !empty($consideration->start_time) ? date($date_format, $consideration->start_time) : '';
  $form['start_time'] = array(
    '#type' => 'date_select',
    '#title' => t('Start date'),
    '#date_label_position' => 'within',
    '#date_format' => $date_format,
    '#date_year_range' => '-3:+10',
    '#default_value' => $default_start_time,
    '#required' => TRUE,
  );

  // End date.
  $default_end_time = !empty($consideration->end_time) ? date($date_format, $consideration->end_time) : '';
  $form['end_time'] = array(
    '#type' => 'date_select',
    '#title' => t('End date'),
    '#date_label_position' => 'within',
    '#date_format' => $date_format,
    '#date_year_range' => '-3:+10',
    '#default_value' => $default_end_time,
    '#required' => TRUE,
  );

  // Is this consideration specific to a plan?
  $plan_label = entity_label('farm_plan', $plan);
  if (!empty($consideration_id)) {
    if (!empty($consideration->plan_id)) {
      $form['plan_id'] = array(
        '#type' => 'markup',
        '#markup' => '<p><strong>' . t('This consideration is specific to plan %plan_label.', array(
          '%plan_label' => $plan_label,
        )) . '</strong></p>',
      );
    }
    else {
      $form['plan_id'] = array(
        '#type' => 'markup',
        '#markup' => '<p><strong>' . t('This consideration applies to all plans.') . '</p></strong>',
      );
    }
  }
  else {
    $form['plan_id'] = array(
      '#type' => 'checkbox',
      '#title' => t('Is this consideration specific to plan %plan_label?', array(
        '%plan_label' => $plan_label,
      )),
      '#description' => t('If this is checked, the consideration will only be visible in the context of this plan. Otherwise, it will be visible to all plans.'),
      '#default_value' => FALSE,
    );
  }

  // Add an area for extra fields. Modules can use this to add type-specific
  // configuration for their consideration types. For example: linking them
  // to entities.
  $form['extra'] = array(
    '#prefix' => '<div id="consideration-extra">',
    '#suffix' => '</div>',
  );

  // Submit button.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save consideration'),
  );
  return $form;
}