You are here

function farm_soil_amendment_form_submit in farmOS 7

Soil amendment quick form submit.

File

modules/farm/farm_soil/farm_soil.farm_quick.amendment.inc, line 513
Farm soil amendment quick form.

Code

function farm_soil_amendment_form_submit($form, &$form_state) {

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

  // Get the amendment 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 input.
  $log_type = 'farm_input';

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

  // Add the total quantity of amendment applied.
  $total_amendment = array(
    'measure' => $form_values['application']['quantity']['measure'],
    'value' => $form_values['application']['quantity']['value'],
    'units' => $form_values['application']['quantity']['units'],
    'label' => t('total amendment applied'),
  );
  $measurements[] = $total_amendment;

  // 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['application']['percentage'];
  $measurements[] = array(
    'measure' => 'ratio',
    'value' => $form_values['application']['percentage'],
    'units' => '%',
    'label' => t('percentage of area amended'),
  );

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

  // Add the nutrient analysis for N, P, and K.
  $nutrients = array(
    'n' => 'N',
    'p' => 'P',
    'k' => 'K',
  );
  foreach ($nutrients as $nutrient => $label) {
    if (!empty($form_values['amendment']['nutrients'][$nutrient])) {
      $measurements[] = array(
        'measure' => 'ratio',
        'value' => $form_values['amendment']['nutrients'][$nutrient],
        'units' => '%',
        'label' => $label,
      );
    }
  }

  // Get the amendment name for inclusion in the log name.
  $material = $form_values['amendment']['material'];

  // Set log name.
  $log_name = t('Soil amendment: !qty !units !material into !area', array(
    '!qty' => $total_amendment['value'],
    '!units' => $total_amendment['units'],
    '!material' => $material,
    '!area' => entity_label('taxonomy_term', $area),
  ));

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

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

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

  // Create/load amendment material and add it to the log.
  $material_term = farm_term($material, 'farm_materials');
  $log_wrapper->field_farm_material[] = $material_term;

  // Set the purpose to "Soil amendment".
  $log_wrapper->field_farm_input_purpose
    ->set(t('Soil amendment'));

  // Add method of application.
  $log_wrapper->field_farm_input_method
    ->set(check_plain($form_values['application']['method']));

  // Add source/manufacturer, if it exists.
  if (!empty($form_values['amendment']['source'])) {
    $log_wrapper->field_farm_input_source
      ->set(check_plain($form_values['amendment']['source']));
  }

  // Add lot number, if it exists.
  if (!empty($form_values['amendment']['source_extra']['lot'])) {
    $log_wrapper->field_farm_lot_number
      ->set(check_plain($form_values['amendment']['source_extra']['lot']));
  }

  // Add date of purchase, if it exists.
  if (!empty($form_values['amendment']['source_extra']['purchase_date'])) {
    $purchase_date = strtotime($form_values['amendment']['source_extra']['purchase_date']);
    $log_wrapper->field_farm_date_purchase
      ->set($purchase_date);
  }

  // Add notes (field condition, crops in field, other notes).
  $notes = array();
  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_amendment_form', 'log', $log);
  }
}