You are here

function farm_movement_area_movement_query in farmOS 7

Build a query to find movement logs to a specific area.

Parameters

int $area_id: The area id to search 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

\SelectQuery Returns a SelectQuery object.

1 call to farm_movement_area_movement_query()
farm_movement_area_history in modules/farm/farm_movement/farm_movement.location.inc
Retrieve an area's movement history. This will provide an array of arrival and departure logs for each asset that has been moved to the area. Only movement logs that have been marked "done" will be included in the history.

File

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

Code

function farm_movement_area_movement_query($area_id, $time = REQUEST_TIME, $done = TRUE) {

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

  // Ensure $area_id is valid, because it will be used directly in the query
  // string. This is defensive code. See note about farm_log_query() above.
  if (!is_numeric($area_id) || $area_id < 0) {
    $area_id = db_escape_field($area_id);
  }

  // Use the farm_log_query() helper function to start a query object. Do not
  // limit the results to a single row because by the very nature of this we
  // want to find all assets in the area, which may come from multiple logs.
  $query = farm_log_query($time, $done, NULL, FALSE);

  // Add a query tag to identify where this came from.
  $query
    ->addTag('farm_movement_area_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 a movement
  // field collection 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" the specified area. 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");
  $query
    ->where('ss_fdffmt.field_farm_move_to_tid = ' . $area_id);

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