You are here

function flag_flag::flag in Flag 7.2

Same name and namespace in other branches
  1. 5 flag.inc \flag_flag::flag()
  2. 6.2 flag.inc \flag_flag::flag()
  3. 6 flag.inc \flag_flag::flag()
  4. 7.3 includes/flag/flag_flag.inc \flag_flag::flag()

Flags, or unflags, an item.

Parameters

$action: Either 'flag' or 'unflag'.

$content_id: The ID of the item to flag or unflag.

$account: The user on whose behalf to flag. Leave empty for the current user.

$skip_permission_check: Flag the item even if the $account user don't have permission to do so.

Return value

FALSE if some error occured (e.g., user has no permission, flag isn't applicable to the item, etc.), TRUE otherwise.

File

./flag.inc, line 655
Implements various flags. Uses object oriented style inspired by that of Views 2.

Class

flag_flag
This abstract class represents a flag, or, in Views 2 terminology, "a handler".

Code

function flag($action, $content_id, $account = NULL, $skip_permission_check = FALSE) {

  // Get the user.
  if (!isset($account)) {
    $account = $GLOBALS['user'];
  }
  if (!$account) {
    return FALSE;
  }

  // Check access and applicability.
  if (!$skip_permission_check) {
    if (!$this
      ->access($content_id, $action, $account)) {

      // User has no permission to flag/unflag this object.
      return FALSE;
    }
  }
  else {

    // We are skipping permission checks. However, at a minimum we must make
    // sure the flag applies to this content type:
    if (!$this
      ->applies_to_content_id($content_id)) {
      return FALSE;
    }
  }

  // Clear various caches; We don't want code running after us to report
  // wrong counts or false flaggings.
  flag_get_counts(NULL, NULL, TRUE);
  flag_get_user_flags(NULL, NULL, NULL, NULL, TRUE);
  drupal_static_reset('flag_get_content_flags');

  // Find out which user id to use.
  $uid = $this->global ? 0 : $account->uid;

  // Find out which session id to use.
  if ($this->global) {
    $sid = 0;
  }
  else {
    $sid = flag_get_sid($uid, TRUE);

    // Anonymous users must always have a session id.
    if ($sid == 0 && $account->uid == 0) {
      return FALSE;
    }
  }

  // Perform the flagging or unflagging of this flag.
  $flagged = $this
    ->_is_flagged($content_id, $uid, $sid);
  if ($action == 'unflag') {
    if ($this
      ->uses_anonymous_cookies()) {
      $this
        ->_unflag_anonymous($content_id);
    }
    if ($flagged) {
      $fcid = $this
        ->_unflag($content_id, $uid, $sid);
      module_invoke_all('flag', 'unflag', $this, $content_id, $account, $fcid);
    }
  }
  elseif ($action == 'flag') {
    if ($this
      ->uses_anonymous_cookies()) {
      $this
        ->_flag_anonymous($content_id);
    }
    if (!$flagged) {
      $fcid = $this
        ->_flag($content_id, $uid, $sid);
      module_invoke_all('flag', 'flag', $this, $content_id, $account, $fcid);
    }
  }
  return TRUE;
}