You are here

function flag_trim_flag in Flag 7.3

Same name and namespace in other branches
  1. 6.2 flag.module \flag_trim_flag()
  2. 7.2 flag.module \flag_trim_flag()

Trim a flag to a certain size.

Parameters

$fid: The flag object.

$account: The user object on behalf the trimming will occur.

$cutoff_size: The number of flaggings allowed. Any flaggings beyond that will be trimmed.

$trim_newest: An optional boolean indicating whether to trim the newest flags.

$permissions_check: (optional) A boolean indicating whether to skip permissions. This will trim the flag if $permissions_check is TRUE even if the user doesn't have the permission to flag/unflag.

1 call to flag_trim_flag()
flag_rules_action_trim in ./flag.rules.inc
Base action implementation: Trim flag.

File

./flag.module, line 2297
The Flag module.

Code

function flag_trim_flag($flag, $account, $cutoff_size, $trim_newest, $permissions_check = FALSE) {
  $query = db_select('flagging', 'fc')
    ->fields('fc')
    ->condition('fid', $flag->fid)
    ->condition(db_or()
    ->condition('uid', $account->uid)
    ->condition('uid', 0))
    ->condition('sid', flag_get_sid($account->uid));

  // If $trim_newest is TRUE, then, we should order by 'ASC' as we should trim
  // the newest flags.
  if ($trim_newest) {
    $query
      ->orderBy('timestamp', 'ASC');
  }
  else {
    $query
      ->orderBy('timestamp', 'DESC');
  }

  // Execute the query.
  $result = $query
    ->execute();
  $i = 1;
  foreach ($result as $row) {
    if ($i++ > $cutoff_size) {
      flag('unflag', $flag->name, $row->entity_id, $account, $permissions_check);
    }
  }
}