You are here

function farm_movement_asset_movement_query in farmOS 7

Build a query to find movement logs of a specific asset.

Parameters

int|string $asset_id: The asset id to search for. This can either be a specific id, or a field alias string from another query (ie: 'mytable.assetid'). For an example of field alias string usage, see the Views relationship handler code in farm_movement_handler_relationship_location::query().

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.

bool $single: Whether or not to limit the query to a single result. Defaults to TRUE.

string $field: If the log id is desired, use "log_id. If the movement field_collection id is desired, use "movement_id".

Return value

\SelectQuery Returns a SelectQuery object.

4 calls to farm_movement_asset_movement_query()
farm_movement_asset_latest_movement in modules/farm/farm_movement/farm_movement.location.inc
Load an asset's latest log that defines a movement.
farm_movement_asset_next_movement in modules/farm/farm_movement/farm_movement.location.inc
Load an asset's next log that defines a movement.
farm_movement_handler_relationship_location::build_query in modules/farm/farm_movement/views/handlers/farm_movement_handler_relationship_location.inc
farm_sensor_area_sensors in modules/farm/farm_sensor/farm_sensor.module
Load an array of sensors in an area.

File

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

Code

function farm_movement_asset_movement_query($asset_id, $time = REQUEST_TIME, $done = TRUE, $single = TRUE, $field = 'log_id') {

  /**
   * Please read the comments in farm_log_asset_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.
   */

  // Use the farm_log_asset_query() helper function to start a query object.
  $query = farm_log_asset_query($asset_id, $time, $done, NULL, $single);

  // Add a query tag to identify where this came from.
  $query
    ->addTag('farm_movement_asset_movement_query');

  // Join in the Movement field collection and filter to only include logs with
  // movements. Use an inner join to exclude logs that do not have any
  // movement field collections attached.
  $query
    ->innerJoin('field_data_field_farm_movement', 'ss_fdffm', "ss_fdffm.entity_type = 'log' AND ss_fdffm.entity_id = ss_log.id AND ss_fdffm.deleted = 0");

  // Join in the movement's "move to" field, and filter to only include logs
  // that have a movement with a "move to" value. Use an inner join to exclude
  // logs that do not have a "move to" area reference.
  $query
    ->innerJoin('field_data_field_farm_move_to', 'ss_fdffmt', "ss_fdffmt.entity_type = 'field_collection_item' AND ss_fdffmt.bundle = 'field_farm_movement' AND ss_fdffmt.entity_id = ss_fdffm.field_farm_movement_value AND ss_fdffmt.deleted = 0");

  // If $field is 'log_id', then add the log ID field.
  if ($field == 'log_id') {
    $query
      ->addField('ss_log', 'id');
  }
  elseif ($field == 'movement_id') {
    $query
      ->addField('ss_fdffm', 'field_farm_movement_value');
  }

  // Return the query object.
  return $query;
}