You are here

function farm_soil_disturbance_form in farmOS 7

Soil disturbance quick form.

2 string references to 'farm_soil_disturbance_form'
farm_soil_disturbance_form_submit in modules/farm/farm_soil/farm_soil.farm_quick.disturbance.inc
Soil disturbance quick form submit.
farm_soil_farm_quick_forms in modules/farm/farm_soil/farm_soil.farm_quick.inc
Implements hook_farm_quick_forms().

File

modules/farm/farm_soil/farm_soil.farm_quick.disturbance.inc, line 11
Farm soil disturbance quick form.

Code

function farm_soil_disturbance_form($form, &$form_state) {

  // Wrapper fieldset.
  $form['disturbance'] = array(
    '#type' => 'fieldset',
    '#title' => t('Record a soil disturbance'),
    '#description' => t('Use this form to record disturbances to your soil. This can include tillage, compaction, or any other action that causes soil breakdown. A new activity log will be created.'),
    '#tree' => TRUE,
  );

  // Date select (default to now).
  $form['disturbance']['timestamp'] = array(
    '#type' => 'date_select',
    '#title' => t('Date'),
    '#date_format' => 'M j Y H:i',
    '#date_type' => DATE_FORMAT_UNIX,
    '#date_year_range' => '-10:+3',
    '#default_value' => REQUEST_TIME,
    '#required' => TRUE,
  );

  // Area information fieldset.
  $form['disturbance']['area'] = array(
    '#type' => 'fieldset',
    '#title' => t('Area information'),
  );

  // Area reference.
  $form['disturbance']['area']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Area name'),
    '#description' => t('Enter the name of the area that was disturbed. A list of existing area options will appear as you type. If the area does not exist, a new one will be created.'),
    '#autocomplete_path' => 'taxonomy/autocomplete/field_farm_area',
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => 'farm_soil_disturbance_form_area_size_ajax',
      'wrapper' => 'area-size',
    ),
  );

  // Alias $form_state['values']['disturbance'] for easier use.
  $form_values = array();
  if (!empty($form_state['values']['disturbance'])) {
    $form_values =& $form_state['values']['disturbance'];
  }

  // If the area name has been entered, attempt to load it.
  // If multiple areas are entered, only use the first one.
  $area = FALSE;
  if (!empty($form_values['area']['name'])) {
    $area_name = $form_values['area']['name'];
    $areas = farm_term_parse_names($area_name, 'farm_areas');
    $area = reset($areas);
  }

  // Measurement type.
  $form['disturbance']['area']['measurement'] = array(
    '#type' => 'radios',
    '#title' => t('Measurement type'),
    '#description' => t('Select how you would like to measure this area.'),
    '#options' => array(
      'total' => t('Total area'),
      'dimensions' => t('Length and width'),
    ),
    '#default_value' => 'total',
    '#ajax' => array(
      'callback' => 'farm_soil_disturbance_form_area_size_ajax',
      'wrapper' => 'area-size',
    ),
  );

  // Area size wrapper.
  $form['disturbance']['area']['size'] = array(
    '#prefix' => '<div id="area-size">',
    '#suffix' => '</div>',
  );

  // Display fields depending on the measurement type.
  // If the measurement type is "dimensions", show length and width fields.
  if (!empty($form_values['area']['measurement']) && $form_values['area']['measurement'] == 'dimensions') {

    // Load the default area and length units.
    $size = 'small';
    $area_units = farm_area_default_units('area', $size);
    $length_units = farm_area_default_units('length', $size);

    // Area length.
    $form['disturbance']['area']['size']['length'] = array(
      '#type' => 'textfield',
      '#title' => t('Area length'),
      '#description' => t('How long is the area in @units?', array(
        '@units' => $length_units,
      )),
      '#input_group' => TRUE,
      '#field_suffix' => $length_units,
      '#ajax' => array(
        'callback' => 'farm_soil_disturbance_form_area_size_ajax',
        'wrapper' => 'area-size',
      ),
    );

    // Area width.
    $form['disturbance']['area']['size']['width'] = array(
      '#type' => 'textfield',
      '#title' => t('Area width'),
      '#description' => t('How wide is the area in @units?', array(
        '@units' => $length_units,
      )),
      '#input_group' => TRUE,
      '#field_suffix' => $length_units,
      '#ajax' => array(
        'callback' => 'farm_soil_disturbance_form_area_size_ajax',
        'wrapper' => 'area-size',
      ),
    );

    // Auto-calculate total surface area and store it in a hidden field.
    $total_area = '';
    if (!empty($form_values['area']['size']['length']) && !empty($form_values['area']['size']['width'])) {
      $total_area = $form_values['area']['size']['length'] * $form_values['area']['size']['width'];
      unset($form_state['input']['disturbance']['area']['size']['total']);
      unset($form_state['input']['disturbance']['area']['size']['units']);
    }
    $form['disturbance']['area']['size']['total'] = array(
      '#type' => 'hidden',
      '#value' => $total_area,
    );
    $form['disturbance']['area']['size']['units'] = array(
      '#type' => 'hidden',
      '#value' => $area_units,
    );
  }
  else {

    // Attempt to auto-calculate the total surface area from the area polygon.
    $total_area = '';
    if (!empty($area)) {
      $total_area = farm_area_calculate_area($area->tid);
    }

    // If a total area was calculated, prepare the form values.
    if (!empty($total_area)) {

      // Get the relative area size.
      $size = farm_area_relative_size($total_area);

      // Determine the default units for the relative area size.
      $units = farm_area_default_units('area', $size);

      // Convert and format the value.
      $total_area = farm_area_format_calculated_area($total_area, FALSE);

      // Reset the $form_state input values so they can be overridden.
      unset($form_state['input']['disturbance']['area']['size']['total']);
      unset($form_state['input']['disturbance']['area']['size']['units']);
    }

    // Total surface area.
    $form['disturbance']['area']['size']['total'] = array(
      '#type' => 'textfield',
      '#title' => t('Area size'),
      '#default_value' => $total_area,
      '#required' => TRUE,
    );

    // Units.
    $form['disturbance']['area']['size']['units'] = array(
      '#type' => 'select',
      '#title' => t('Area size units'),
      '#options' => drupal_map_assoc(array(
        farm_area_default_units('area', 'big'),
        farm_area_default_units('area', 'small'),
      )),
      '#default_value' => !empty($units) ? $units : NULL,
      '#required' => TRUE,
    );
  }

  // Percentage of area disturbed.
  $form['disturbance']['area']['percentage'] = array(
    '#type' => 'textfield',
    '#title' => t('Percentage of area disturbed'),
    '#description' => t('What percentage of the area was disturbed by this activity?'),
    '#default_value' => '100',
    '#required' => TRUE,
    '#input_group' => TRUE,
    '#field_suffix' => '%',
  );

  // Disturbance information fieldset.
  $form['disturbance']['disturbance'] = array(
    '#type' => 'fieldset',
    '#title' => t('Disturbance information'),
  );

  // Activity.
  $form['disturbance']['disturbance']['activity'] = array(
    '#type' => 'textfield',
    '#title' => t('Activity'),
    '#description' => t('What activity was performed?'),
    '#required' => TRUE,
  );

  // Notes fieldset.
  $form['disturbance']['notes'] = array(
    '#type' => 'fieldset',
    '#title' => t('Notes'),
  );

  // Field condition.
  $form['disturbance']['notes']['condition'] = array(
    '#type' => 'textfield',
    '#title' => t('Field condition'),
    '#description' => t('Briefly describe the field conditions of this area when the action was taken.'),
  );

  // Crops in field.
  $form['disturbance']['notes']['crops'] = array(
    '#type' => 'textfield',
    '#title' => t('Crops in field'),
    '#description' => t('List the crops that are in this area, or will be in this area during/after this action.'),
  );

  // Other notes.
  $form['disturbance']['notes']['other'] = array(
    '#type' => 'text_format',
    '#title' => t('Other notes'),
    '#description' => t('Include any other notes that are relevant to this soil disturbance for future reference.'),
    '#format' => 'farm_format',
  );

  // Submit button.
  $form['disturbance']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Create log'),
  );

  // Return the form.
  return $form;
}