You are here

function farm_soil_disturbance_form_submit in farmOS 7

Soil disturbance quick form submit.

File

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

Code

function farm_soil_disturbance_form_submit($form, &$form_state) {

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

  // Get the disturbance timestamp.
  $timestamp = strtotime($form_values['timestamp']);

  // Parse the area name, create new one if it doesn't exist.
  $area_name = $form_values['area']['name'];
  $areas = farm_term_parse_names($area_name, 'farm_areas', TRUE);

  // If no areas were found/created, bail with an error.
  if (empty($areas)) {
    drupal_set_message(t('An error occurred while creating/loading areas.'), 'error');
    return;
  }

  // We assume only one area is being amended.
  $area = reset($areas);

  // The log type will be an activity.
  $log_type = 'farm_activity';

  // Initialize an empty measurements array.
  $measurements = array();

  // Add the total area size.
  $total_area = array(
    'measure' => 'area',
    'value' => $form_values['area']['size']['total'],
    'units' => $form_values['area']['size']['units'],
    'label' => t('total area size'),
  );
  $measurements[] = $total_area;

  // Add the percentage of total area amended.
  $percentage = $form_values['area']['percentage'];
  $measurements[] = array(
    'measure' => 'ratio',
    'value' => $percentage,
    'units' => '%',
    'label' => t('percentage of area disturbed'),
  );

  // Calculate and add the total area disturbed, rounded to 2 decimals (use
  // BCMath where available).
  $scale = 2;
  if (function_exists('bcmul') && function_exists('bcdiv')) {
    $total_area_disturbed = bcmul($total_area['value'], bcdiv($percentage, '100', $scale), $scale);
  }
  else {
    $total_area_disturbed = round($total_area['value'] * ($percentage / 100), $scale);
  }
  $measurements[] = array(
    'measure' => 'area',
    'value' => $total_area_disturbed,
    'units' => $total_area['units'],
    'label' => t('total area disturbed'),
  );

  // Set log name.
  $log_name_parts = array(
    entity_label('taxonomy_term', $area),
    check_plain($form_values['disturbance']['activity']),
  );
  $log_name = t('Soil disturbance') . ': ' . implode(' ', $log_name_parts);

  // Add the "Tillage" log category.
  $categories = array(
    'Tillage',
  );

  // Create a new farm quantity log.
  $log = farm_quantity_log_create($log_type, $log_name, $timestamp, TRUE, array(), $measurements, '', $categories);

  // Get the log entity wrapper.
  $log_wrapper = entity_metadata_wrapper('log', $log);

  // Add the area reference.
  $log_wrapper->field_farm_area[] = $area;

  // Add notes (activity, field condition, crops in field, other notes).
  $notes = array();
  if (!empty($form_values['disturbance']['activity'])) {
    $notes[] = t('Activity') . ': ' . check_plain($form_values['disturbance']['activity']);
  }
  if (!empty($form_values['notes']['condition'])) {
    $notes[] = t('Field condition') . ': ' . check_plain($form_values['notes']['condition']);
  }
  if (!empty($form_values['notes']['crops'])) {
    $notes[] = t('Crops in field') . ': ' . check_plain($form_values['notes']['crops']);
  }
  if (!empty($form_values['notes']['other']['value'])) {
    $notes[] = check_plain($form_values['notes']['other']['value']);
  }
  if (!empty($notes)) {
    $log_wrapper->field_farm_notes->value
      ->set(implode("\n\n", $notes));
    $log_wrapper->field_farm_notes->format
      ->set($form_values['notes']['other']['format']);
  }

  // Save the log (via its wrapper).
  $log_wrapper
    ->save();

  // Link the log to the quick form.
  if (function_exists('farm_quick_entity_link')) {
    farm_quick_entity_link('farm_soil_disturbance_form', 'log', $log);
  }

  // Add the log to $form_state['storage'] so that other submit functions
  // can work with it.
  $form_state['storage']['log'] = $log;
}