You are here

function flag_get_counts in Flag 7.2

Same name and namespace in other branches
  1. 5 flag.module \flag_get_counts()
  2. 6.2 flag.module \flag_get_counts()
  3. 6 flag.module \flag_get_counts()
  4. 7.3 flag.module \flag_get_counts()

Get flag counts for all flags on a node.

Parameters

$content_type: The content type (usually 'node').

$content_id: The content ID (usually the node ID).

$reset: Reset the internal cache and execute the SQL query another time.

Return value

$flags An array of the structure [name] => [number of flags].

2 calls to flag_get_counts()
flag_flag::flag in ./flag.inc
Flags, or unflags, an item.
flag_flag::get_count in ./flag.inc
Returns the number of times an item is flagged.

File

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

Code

function flag_get_counts($content_type, $content_id, $reset = FALSE) {
  $counts =& drupal_static(__FUNCTION__);
  if ($reset) {
    $counts = array();
    if (!isset($content_type)) {
      return;
    }
  }
  if (!isset($counts[$content_type][$content_id])) {
    $counts[$content_type][$content_id] = array();
    $query = db_select('flags', 'f');
    $query
      ->leftJoin('flag_counts', 'fc', 'f.fid = fc.fid');
    $result = $query
      ->fields('f', array(
      'name',
    ))
      ->fields('fc', array(
      'count',
    ))
      ->condition('fc.content_type', $content_type)
      ->condition('fc.content_id', $content_id)
      ->execute();
    foreach ($result as $row) {
      $counts[$content_type][$content_id][$row->name] = $row->count;
    }
  }
  return $counts[$content_type][$content_id];
}