You are here

function flag_lists_do_flag in Flag Lists 6

Same name and namespace in other branches
  1. 7.3 flag_lists.module \flag_lists_do_flag()
  2. 7 flag_lists.module \flag_lists_do_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 doesn'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.

2 calls to flag_lists_do_flag()
flag_lists_generate_lists_form_submit in ./flag_lists.admin.inc
Submit handler for flag_lists_generate_lists_form.
flag_lists_page in ./flag_lists.module
Menu callback for (un)flagging a node.

File

./flag_lists.module, line 979
The Flag Lists module.

Code

function flag_lists_do_flag($flag, $action, $content_id, $account = NULL, $skip_permission_check = FALSE) {
  if (!isset($account)) {
    $account = $GLOBALS['user'];
  }
  if (!$account) {
    return FALSE;
  }
  if (!$skip_permission_check) {
    if (!$flag
      ->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 (!$flag
      ->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);
  // Find out which user id to use.
  $uid = $flag->global ? 0 : $account->uid;
  $sid = flag_get_sid($uid);

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

  // Perform the flagging or unflagging of this flag. We invoke hook_flag here
  // because we do our own flagging.
  $flagged = _flag_lists_is_flagged($flag, $content_id, $uid, $sid);
  if ($action == 'unflag') {
    if ($flagged) {
      $fcid = _flag_lists_unflag($flag, $content_id, $uid, $sid);
      module_invoke_all('flag', 'unflag', $flag, $content_id, $account, $fcid);
    }
  }
  elseif ($action == 'flag') {
    if (!$flagged) {
      $fcid = _flag_lists_flag($flag, $content_id, $uid, $sid);
      module_invoke_all('flag', 'flag', $flag, $content_id, $account, $fcid);
    }
  }
  return TRUE;
}