You are here

function farm_group_asset_membership_query in farmOS 7

Build a query to find group membership 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_group_handler_relationship_membership::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.

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.

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 membership field_collection id is desired, use "membership_id".

Return value

\SelectQuery Returns a SelectQuery object.

2 calls to farm_group_asset_membership_query()
farm_group_asset_latest_membership in modules/farm/farm_group/farm_group.module
Load an asset's latest log that defines a group membership.
farm_group_handler_relationship_membership::build_query in modules/farm/farm_group/views/handlers/farm_group_handler_relationship_membership.inc

File

modules/farm/farm_group/farm_group.module, line 622

Code

function farm_group_asset_membership_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_group_asset_membership_query');

  // Join in the Membership field collection. Use an inner join to exclude logs
  // that do not a membership field collection attached.
  $query
    ->innerJoin('field_data_field_farm_membership', 'ss_fdffm', "ss_fdffm.entity_type = 'log' AND ss_fdffm.entity_id = ss_log.id AND ss_fdffm.deleted = 0");

  // Join in the membership's "group" field. Use an inner join to exclude logs
  // that do not have a group reference.
  $query
    ->innerJoin('field_data_field_farm_group', 'ss_fdffg', "ss_fdffg.entity_type = 'field_collection_item' AND ss_fdffg.bundle = 'field_farm_membership' AND ss_fdffg.entity_id = ss_fdffm.field_farm_membership_value AND ss_fdffg.deleted = 0");

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

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