You are here

function farm_livestock_move_form_submit in farmOS 7

Submit function for movement quick form.

File

modules/farm/farm_livestock/farm_livestock.farm_quick.move.inc, line 555
Farm livestock move quick form.

Code

function farm_livestock_move_form_submit($form, &$form_state) {

  // Get the movement status.
  $done = $form_state['values']['move']['done'];

  // Get the movement timestamp.
  $timestamp = strtotime($form_state['values']['move']['date']);

  // Load the asset.
  $assets = $form_state['storage']['assets'];
  if (empty($assets)) {
    return;
  }

  // Create post grazing observation log if values were submitted.
  if (isset($form_state['values']['move']['observations']['post'])) {

    // Save the pasture height units.
    $pasture_height_units = $form_state['values']['move']['observations']['pasture_height_units'];

    // Build post grazing measurements.
    $post_grazing_measurements = array();

    // Add post grazing pasture height measurement.
    if (!empty($form_state['values']['move']['observations']['post']['pasture_height'])) {
      $post_grazing_measurements[] = array(
        'measure' => 'length',
        'value' => $form_state['values']['move']['observations']['post']['pasture_height'],
        'units' => $pasture_height_units,
        'label' => 'Pasture Height',
      );
    }

    // Add post grazing forage quality measurement.
    if (!empty($form_state['values']['move']['observations']['post']['forage_quality'])) {
      $post_grazing_measurements[] = array(
        'measure' => 'rating',
        'value' => $form_state['values']['move']['observations']['post']['forage_quality'],
        'label' => 'Forage Quality',
      );
    }

    // Save the photo file id.
    $post_grazing_file_id = $form_state['values']['move']['observations']['post']['photos'];

    // Create post grazing observation log if measurements or photos are provided.
    // Do this before creating a movement log for the asset.
    if (!empty($post_grazing_measurements) || !empty($post_grazing_file_id)) {
      $post_grazing_log = farm_quantity_log_create('farm_observation', 'Post grazing observation', $timestamp, TRUE, $assets, $post_grazing_measurements);

      // Link the post grazing log to the quick form.
      if (function_exists('farm_quick_entity_link')) {
        farm_quick_entity_link('farm_livestock_move_form', 'log', $post_grazing_log);
      }

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

      // Check if a file was uploaded.
      if (!empty($post_grazing_file_id)) {

        // Load file.
        $file = file_load($post_grazing_file_id);

        // Make the storage of the file permanent.
        $file->status = FILE_STATUS_PERMANENT;

        // Save the file information.
        file_save($file);

        // Save the image with the log.
        $log_wrapper->field_farm_images[] = array(
          'fid' => $post_grazing_file_id,
        );
      }

      // Load current locations of all assets.
      $previous_areas = array();
      foreach ($assets as $asset) {
        $previous_areas = array_merge($previous_areas, farm_movement_asset_location($asset, $timestamp));
      }

      // Remove any duplicate areas.
      $previous_areas = array_unique($previous_areas, SORT_REGULAR);

      // Link post grazing logs to the area(s) animals are moving from.
      if (!empty($previous_areas)) {

        // Add areas to log.
        foreach ($previous_areas as $area) {
          $log_wrapper->field_farm_area[] = $area;
        }
      }

      // Load current location geometry of all assets.
      $geoms = array();
      foreach ($assets as $asset) {
        $geoms[] = farm_movement_asset_geometry($asset, $timestamp);
      }

      // Combine geometries.
      $combined_geometry = farm_map_combine_geoms($geoms);

      // Add to log.
      if (!empty($combined_geometry)) {
        $log_wrapper->field_farm_geofield
          ->set(array(
          array(
            'geom' => $combined_geometry
              ->out('wkt'),
          ),
        ));
      }

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

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

  // Get the geometry.
  $geom = '';
  if (!empty($form_state['values']['move']['area']['geometry']['data'])) {
    $geom = $form_state['values']['move']['area']['geometry']['data'];
  }

  // Edit planned movement log.
  if (!empty($form_state['values']['move']['log'])) {

    // Get the movement log.
    $movement_log = $form_state['values']['move']['log'];

    // Update status.
    $movement_log->done = $done;

    // Update timestamp.
    $movement_log->timestamp = $timestamp;

    // Update asset references.
    $movement_log->field_farm_asset[LANGUAGE_NONE] = array();
    foreach ($assets as $asset) {
      $movement_log->field_farm_asset[LANGUAGE_NONE][] = array(
        'target_id' => $asset->id,
      );
    }

    // Update the movement info.
    $log_wrapper = entity_metadata_wrapper('log', $movement_log);
    if (!empty($log_wrapper->field_farm_movement)) {

      // Get the movement wrapper.
      $movement_wrapper = $log_wrapper->field_farm_movement;

      // Update values.
      $movement_wrapper->field_farm_move_to
        ->set($areas);
      $movement_wrapper->field_farm_geofield
        ->set(array(
        array(
          'geom' => $geom,
        ),
      ));
      $movement_wrapper
        ->save();
    }

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

    // Update log name.
    $movement_log->name = '';
    farm_movement_populate_name($movement_log);

    // Display "Updated log" status message.
    $log_name = entity_label('log', $movement_log);
    $log_uri = entity_uri('log', $movement_log);
    $log_url = url($log_uri['path']);
    drupal_set_message(t('Log updated: <a href="@log_url">@log_name</a>', array(
      '@log_url' => $log_url,
      '@log_name' => $log_name,
    )));
  }
  else {

    // Create activity log with asset movement to areas and specific geometry.
    $movement_log = farm_movement_create($assets, $areas, $timestamp, 'farm_activity', $done, $geom);
  }

  // Save the movement log.
  log_save($movement_log);

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

  // Create pre grazing observation log if values were submitted.
  if (isset($form_state['values']['move']['observations']['pre'])) {

    // Save the pasture height units.
    $pasture_height_units = $form_state['values']['move']['observations']['pasture_height_units'];

    // Build pre grazing measurements.
    $pre_grazing_measurements = array();

    // Add pre grazing pasture height measurement.
    if (!empty($form_state['values']['move']['observations']['pre']['pasture_height'])) {
      $pre_grazing_measurements[] = array(
        'measure' => 'length',
        'value' => $form_state['values']['move']['observations']['pre']['pasture_height'],
        'units' => $pasture_height_units,
        'label' => 'Pasture Height',
      );
    }

    // Add pre grazing forage quality measurement.
    if (!empty($form_state['values']['move']['observations']['pre']['forage_quality'])) {
      $pre_grazing_measurements[] = array(
        'measure' => 'rating',
        'value' => $form_state['values']['move']['observations']['pre']['forage_quality'],
        'label' => 'Forage Quality',
      );
    }

    // Save the photo file id.
    $pre_grazing_file_id = $form_state['values']['move']['observations']['pre']['photos'];

    // Create pre grazing observation log if measurements or photos are provided.
    if (!empty($pre_grazing_measurements) || !empty($pre_grazing_file_id)) {
      $pre_grazing_log = farm_quantity_log_create('farm_observation', 'Pre grazing observation', $timestamp, TRUE, $assets, $pre_grazing_measurements);

      // Link the pre grazing log to the quick form.
      if (function_exists('farm_quick_entity_link')) {
        farm_quick_entity_link('farm_livestock_move_form', 'log', $pre_grazing_log);
      }

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

      // Check if a file was uploaded.
      if (!empty($pre_grazing_file_id)) {

        // Load file.
        $file = file_load($pre_grazing_file_id);

        // Make the storage of the file permanent.
        $file->status = FILE_STATUS_PERMANENT;

        // Save the file information.
        file_save($file);

        // Save the image with the log.
        $log_wrapper->field_farm_images[] = array(
          'fid' => $pre_grazing_file_id,
        );
      }

      // Link pre grazing logs to the area animals are moving to.
      if (!empty($areas)) {

        // Add areas to log.
        foreach ($areas as $area) {
          $log_wrapper->field_farm_area[] = $area;
        }
      }

      // Save custom geometry to pre-grazing logs.
      if (!empty($geom)) {
        $log_wrapper->field_farm_geofield
          ->set(array(
          array(
            'geom' => $geom,
          ),
        ));
      }

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