You are here

function flag_get_user_flags in Flag 7.2

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

Find what a user has flagged, either a single node or on the entire site.

Parameters

$content_type: The type of content that will be retrieved. Usually 'node'.

$content_id: Optional. The content ID to check for flagging. If none given, all content flagged by this user will be returned.

$uid: Optional. The user ID whose flags we're checking. If none given, the current user will be used.

$sid: Optional. The user SID (provided by Session API) whose flags we're checking. If none given, the current user will be used. The SID is 0 for logged in users.

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

Return value

$flags If returning a single item's flags (that is, when $content_id isn't NULL), an array of the structure [flag_name] => (fcid => [fcid], uid => [uid], content_id => [content_id], timestamp => [timestamp], ...)

If returning all items' flags, an array of arrays for each flag: [flag_name] => [content_id] => Object from above.

2 calls to flag_get_user_flags()
flag_flag::flag in ./flag.inc
Flags, or unflags, an item.
flag_flag::get_flagging_record in ./flag.inc
Returns the flagging record.

File

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

Code

function flag_get_user_flags($content_type, $content_id = NULL, $uid = NULL, $sid = NULL, $reset = FALSE) {
  $flagged_content =& drupal_static(__FUNCTION__);
  if ($reset) {
    $flagged_content = array();
    if (!isset($content_type)) {
      return;
    }
  }
  $uid = !isset($uid) ? $GLOBALS['user']->uid : $uid;
  $sid = !isset($sid) ? flag_get_sid($uid) : $sid;
  if (isset($content_id)) {
    if (!isset($flagged_content[$uid][$sid][$content_type][$content_id])) {
      $flag_names = _flag_get_flag_names();
      $flagged_content[$uid][$sid][$content_type][$content_id] = array();
      $result = db_select('flag_content', 'fc')
        ->fields('fc')
        ->condition('content_type', $content_type)
        ->condition('content_id', $content_id)
        ->condition(db_or()
        ->condition('uid', $uid)
        ->condition('uid', 0))
        ->condition('sid', $sid)
        ->execute();
      foreach ($result as $flag_content) {
        $flagged_content[$uid][$sid][$content_type][$content_id][$flag_names[$flag_content->fid]] = $flag_content;
      }
    }
    return $flagged_content[$uid][$sid][$content_type][$content_id];
  }
  else {
    if (!isset($flagged_content[$uid][$sid][$content_type]['all'])) {
      $flag_names = _flag_get_flag_names();
      $flagged_content[$uid][$sid][$content_type]['all'] = array();
      $result = db_select('flag_content', 'fc')
        ->fields('fc')
        ->condition('content_type', $content_type)
        ->condition(db_or()
        ->condition('uid', $uid)
        ->condition('uid', 0))
        ->condition('sid', $sid)
        ->execute();
      foreach ($result as $flag_content) {
        $flagged_content[$uid][$sid][$content_type]['all'][$flag_names[$flag_content->fid]][$flag_content->content_id] = $flag_content;
      }
    }
    return $flagged_content[$uid][$sid][$content_type]['all'];
  }
}