You are here

function farm_crop_planting_quick_form_submit in farmOS 7

Planting quick form submit.

File

modules/farm/farm_crop/farm_crop.farm_quick.planting.inc, line 326
Farm planting quick form.

Code

function farm_crop_planting_quick_form_submit($form, &$form_state) {

  // Remember the season for future reference.
  $season = $form_state['values']['planting']['season'];
  variable_set('farm_crop_planting_season', $season);

  // Load/create the season term.
  $seasons = farm_term_parse_names($season, 'farm_season', TRUE);

  // Get the crop(s) and load/create terms for each.
  $crops = array();
  $crop_tags = $form_state['values']['planting']['crops'];
  foreach ($crop_tags as $tag) {
    $term = farm_term($tag, 'farm_crops');
    if (!empty($term)) {
      $crops[] = $term;
    }
  }

  // Create a new planting asset.
  $values = array(
    'type' => 'planting',
    'name' => $form_state['values']['planting']['name'],
  );
  $planting_asset = entity_create('farm_asset', $values);
  $planting_wrapper = entity_metadata_wrapper('farm_asset', $planting_asset);

  // Add each season.
  foreach ($seasons as $season) {
    $planting_wrapper->field_farm_season[] = $season;
  }

  // Add the crop(s).
  foreach ($crops as $crop) {
    $planting_wrapper->field_farm_crop[] = $crop;
  }

  // Save the planting.
  $planting_wrapper
    ->save();

  // Link the asset to this quick form.
  if (function_exists('farm_quick_entity_link')) {
    farm_quick_entity_link('farm_crop_planting_form', 'farm_asset', $planting_asset);
  }

  // Set a message.
  $label = entity_label('farm_asset', $planting_asset);
  $uri = entity_uri('farm_asset', $planting_asset);
  drupal_set_message(t('Planting created') . ': ' . l($label, $uri['path']));

  // Iterate through the logs.
  foreach ($form_state['values']['planting']['logs'] as $name => $log_values) {

    // If the log is not enabled, skip it.
    if (empty($log_values['enabled'])) {
      continue;
    }

    // Convert the date to a timestamp.
    $timestamp = strtotime($log_values['date']);

    // If the location is available, load areas.
    $areas = array();
    if (!empty($log_values['location'])) {
      $areas = farm_term_parse_names($log_values['location'], 'farm_areas', TRUE);
    }

    // Build quantity measurements.
    $measurements = array();
    if (!empty($log_values['quantity']['value'])) {
      $measurements[] = $log_values['quantity'];
    }

    // Mark the log as done or not done.
    $done = FALSE;
    if (!empty($log_values['done'])) {
      $done = TRUE;
    }

    // If there is a location, create a movement log.
    if (!empty($log_values['location'])) {
      $log = farm_movement_create($planting_asset, $areas, $timestamp, $log_values['type'], $done);
    }
    else {
      $log = farm_log_create($log_values['type'], '', $timestamp, $done, array(
        $planting_asset,
      ));
    }

    // If there are quantity measurements, add them to the log.
    if (!empty($measurements)) {
      farm_quantity_log_add_measurements($log, $measurements);
    }

    // Create an entity metadata wrapper for the log.
    $log_wrapper = entity_metadata_wrapper('log', $log);

    // Set the log notes, if available.
    if (!empty($log_values['notes']['notes']['value'])) {
      $log_wrapper->field_farm_notes = $log_values['notes']['notes'];
    }

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

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