You are here

function farm_movement_create in farmOS 7

Create a log for moving assets to areas.

Parameters

array|FarmAsset $assets: Array of assets to include in the move.

array $areas: An array of areas to move to.

int $timestamp: The timestamp of the move. Defaults to the current time.

string $log_type: The type of log to create. Defaults to "farm_observation".

bool $done: Boolean indicating whether or not the log should be marked "done". Defaults to TRUE.

string $geom: Optionally provide a movement geometry in WKT format. If this is empty, the geometry will be copied from the referenced area(s).

Return value

\Log Returns the log that was created.

4 calls to farm_movement_create()
farm_crop_planting_quick_form_submit in modules/farm/farm_crop/farm_crop.farm_quick.planting.inc
Planting quick form submit.
farm_livestock_move_form_submit in modules/farm/farm_livestock/farm_livestock.farm_quick.move.inc
Submit function for movement quick form.
farm_movement_asset_location_submit in modules/farm/farm_movement/farm_movement.location.inc
Submit handler for processing the asset location field.
farm_movement_asset_move_action in modules/farm/farm_movement/farm_movement.location.inc
Action function for farm_movement_asset_move.

File

modules/farm/farm_movement/farm_movement.location.inc, line 918
Code for managing the location of assets with movement logs.

Code

function farm_movement_create($assets, $areas = array(), $timestamp = REQUEST_TIME, $log_type = 'farm_observation', $done = TRUE, $geom = '') {

  // If no areas are defined, bail.
  if (empty($areas)) {
    return;
  }

  // If $assets isn't an array, wrap it.
  if (!is_array($assets)) {
    $assets = array(
      $assets,
    );
  }

  // If the log is an observation, set the name to:
  // "[assets] located in [areas]".
  // If the log is an activity, set the name to:
  // "Move [assets] to [areas]".
  $log_name = '';
  $assets_summary = farm_log_entity_label_summary('farm_asset', $assets);
  $areas_summary = farm_log_entity_label_summary('taxonomy_term', $areas);
  $arguments = array(
    '!assets' => $assets_summary,
    '!areas' => $areas_summary,
  );
  if ($log_type == 'farm_observation') {
    $log_name = t('!assets located in !areas', $arguments);
  }
  elseif ($log_type == 'farm_activity') {
    $log_name = t('Move !assets to !areas', $arguments);
  }

  // Create a new farm log entity.
  $log = farm_log_create($log_type, $log_name, $timestamp, $done, $assets);

  // Create a new movement field_collection entity attached to the log.
  $movement = entity_create('field_collection_item', array(
    'field_name' => 'field_farm_movement',
  ));
  $movement
    ->setHostEntity('log', $log);

  // Create an entity wrapper for the adjustment.
  $movement_wrapper = entity_metadata_wrapper('field_collection_item', $movement);

  // Iterate through the areas and add each to the "Move to" field.
  foreach ($areas as $area) {
    $movement_wrapper->field_farm_move_to[] = $area;
  }

  // Add geometry, if provided.
  if (!empty($geom)) {
    $movement_wrapper->field_farm_geofield
      ->set(array(
      array(
        'geom' => $geom,
      ),
    ));
  }

  // Save the movement.
  $movement_wrapper
    ->save();

  // Return the log.
  return $log;
}