You are here

function farm_log_asset_query in farmOS 7

Helper function for building a select query of logs related to assets.

This builds on top of the more general farm_log_query() function.

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 field handler code in farm_movement_handler_relationship_location::query(). If this is omitted, the asset reference table will still be joined in, but no further filtering will be done.

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.

bool|null $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 this is set to NULL, no filtering will be applied. Defaults to TRUE.

string|null $type: The log type to filter by. If this is NULL, no filtering will be applied.

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

Return value

\SelectQuery Returns a SelectQuery object.

See also

farm_log_query()

7 calls to farm_log_asset_query()
farm_group_asset_membership_query in modules/farm/farm_group/farm_group.module
Build a query to find group membership logs of a specific asset.
farm_group_members_query in modules/farm/farm_group/farm_group.module
Build a query to find members of a specific group.
farm_log_handler_relationship_asset::build_query in modules/farm/farm_log/views/handlers/farm_log_handler_relationship_asset.inc
farm_movement_area_assets_query in modules/farm/farm_movement/farm_movement.location.inc
Build a query to find assets in a given area.
farm_movement_asset_movement_query in modules/farm/farm_movement/farm_movement.location.inc
Build a query to find movement logs of a specific asset.

... See full list

File

modules/farm/farm_log/farm_log.module, line 635
Code for the Farm Log feature.

Code

function farm_log_asset_query($asset_id = NULL, $time = REQUEST_TIME, $done = TRUE, $type = NULL, $single = 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.
   */

  // Start with a query from the farm_log_query() helper function.
  $query = farm_log_query($time, $done, $type, $single);

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

  // Ensure $asset_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($asset_id) || $asset_id < 0) {
    $asset_id = db_escape_field($asset_id);
  }

  // Join in asset reference field. Use an inner join to exclude logs that do
  // not have any asset references.
  $query
    ->innerJoin('field_data_field_farm_asset', 'ss_fdffa', "ss_fdffa.entity_type = 'log' AND ss_fdffa.entity_id = ss_log.id AND ss_fdffa.deleted = 0");

  // If an asset ID is specified, only include logs that reference it.
  if (!empty($asset_id)) {
    $query
      ->where('ss_fdffa.field_farm_asset_target_id = ' . $asset_id);
  }

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