You are here

function farm_movement_asset_latest_movement in farmOS 7

Load an asset's latest log that defines a movement.

Parameters

FarmAsset $asset: The farm_asset object to look for.

int $time: Unix timestamp limiter. Only logs before this time will be included. Defaults to the current time. Set to 0 to load the absolute last.

$done: Whether or not to only show logs that are marked as "done". TRUE will limit to logs that are done, and FALSE will limit to logs that are not done. If any other value is used, no filtering will be applied. Defaults to TRUE.

Return value

Log|bool Returns a log entity. FALSE if something goes wrong.

4 calls to farm_movement_asset_latest_movement()
farm_livestock_birth_form_submit in modules/farm/farm_livestock/farm_livestock.farm_quick.birth.inc
Submit callback for birth quick form.
farm_movement_asset_geometry in modules/farm/farm_movement/farm_movement.location.inc
Find the geometry of an asset, based on movement logs.
farm_movement_asset_location in modules/farm/farm_movement/farm_movement.location.inc
Find the location of an asset, based on movement logs.
farm_movement_asset_location_markup in modules/farm/farm_movement/farm_movement.location.inc
Generate markup that describes an asset's current location.

File

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

Code

function farm_movement_asset_latest_movement(FarmAsset $asset, $time = REQUEST_TIME, $done = TRUE) {

  /**
   * Please read the comments in farm_movement_asset_movement_query() to
   * understand how this works, and to be aware of the limitations and
   * responsibilities we have in this function with regard to sanitizing query
   * inputs.
   */

  // If the asset doesn't have an ID (for instance if it is new and hasn't been
  // saved yet), bail.
  if (empty($asset->id)) {
    return FALSE;
  }

  // Make a query for loading the latest movement log.
  $query = farm_movement_asset_movement_query($asset->id, $time, $done);

  // Execute the query and gather the log id.
  $result = $query
    ->execute();
  $log_id = $result
    ->fetchField();

  // If a log id exists, load and return it.
  if (!empty($log_id)) {
    return log_load($log_id);
  }
  return FALSE;
}