You are here

function flag_flag::flag in Flag 5

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

Flags, on 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 368
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) {
  if (!isset($account)) {
    $account = $GLOBALS['user'];
  }
  if (!$account) {
    return FALSE;
  }
  if (!$account->uid) {

    // Anonymous users can't flag with this system. For now.
    //
    // @todo This is legacy code. $flag->user_access() should handle this.
    // This will also make it posible to have flags that do support anonymous
    // users.
    return FALSE;
  }
  if (!$skip_permission_check && !$this
    ->user_access($account)) {

    // User has no permission to use this flag.
    return FALSE;
  }
  if (!$this
    ->applies_to_content_id($content_id)) {

    // Flag does not apply to this content.
    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, TRUE);

  // Perform the flagging or unflagging of this flag.
  $uid = $this->global ? 0 : $account->uid;
  $flagged = $this
    ->_is_flagged($content_id, $uid);
  if ($action == 'unflag' && $flagged) {
    $this
      ->_unflag($content_id, $uid);

    // Let other modules perform actions.
    module_invoke_all('flag', 'unflag', $this, $content_id, $account);
  }
  elseif ($action == 'flag' && !$flagged) {
    $this
      ->_flag($content_id, $uid);

    // Let other modules perform actions.
    module_invoke_all('flag', 'flag', $this, $content_id, $account);
  }
  return TRUE;
}