You are here

function flag_rules_action_fetch_entity_by_user in Flag 7.3

Same name and namespace in other branches
  1. 7.2 flag.rules.inc \flag_rules_action_fetch_entity_by_user()

Base action implementation: Fetch entities flagged by a user.

1 string reference to 'flag_rules_action_fetch_entity_by_user'
flag_rules_action_info in ./flag.rules.inc
Implements hook_rules_action_info().

File

./flag.rules.inc, line 430
Rules integration for the Flag module.

Code

function flag_rules_action_fetch_entity_by_user($flag, $entity) {
  $query = db_select('flagging', 'fc')
    ->fields('fc', array(
    'entity_id',
  ))
    ->condition('entity_type', $flag->entity_type)
    ->condition('fid', $flag->fid);

  // For global flags the user parameter is ignored, so we add the
  // extra 'uid' condition when the flag is NOT global.
  if (!$flag->global) {
    $user = entity_metadata_wrapper('user', $entity);
    $sid = $user->flag_sid
      ->value();
    $query = $query
      ->condition('uid', $user->uid
      ->value());

    // Filter out any bad session ids and any users that aren't anonymous.
    if (!empty($sid) && $sid != -1) {
      $query
        ->condition('sid', $sid);
    }
  }
  $result = $query
    ->execute();
  $flagged = $result
    ->fetchCol();
  return array(
    'content_flagged_by_user' => $flagged,
  );
}