You are here

function flag_lists_do_flag in Flag Lists 7.3

Same name and namespace in other branches
  1. 6 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'.

$entity_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.

3 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_ops_form_submit in ./flag_lists.module
flag_lists_page in ./flag_lists.module
Menu callback for (un)flagging a node.

File

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

Code

function flag_lists_do_flag($flag, $action, $entity_id, $account = NULL, $skip_permission_check = FALSE) {
  if (!isset($account)) {
    $account = $GLOBALS['user'];
  }
  if (!$account) {
    return FALSE;
  }
  if (!$skip_permission_check) {
    if (!$flag
      ->access($entity_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_entity_id($entity_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, $entity_id, $uid, $sid);
  if ($action == 'unflag') {
    if ($flagged) {
      $fcid = _flag_lists_unflag($flag, $entity_id, $uid, $sid);
      module_invoke_all('flag_unflag', $flag, $entity_id, $account, $fcid);
      return TRUE;
    }
  }
  elseif ($action == 'flag') {
    if (!$flagged) {
      $fcid = _flag_lists_flag($flag, $entity_id, $uid, $sid);
      module_invoke_all('flag_flag', $flag, $entity_id, $account, $fcid);
      return TRUE;
    }
  }
  return FALSE;
}