You are here

function flag_page in Flag 7.3

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

Menu callback for (un)flagging a node.

Used both for the regular callback as well as the JS version.

Parameters

$action: The action about to be performed. One of 'flag' or 'unflag'.

$flag: The flag object.

$entity_id: The ID of the entity to be acted upon. The type is implicit in the flag.

1 string reference to 'flag_page'
flag_menu in ./flag.module
Implements hook_menu().

File

includes/flag.pages.inc, line 20
Menu callbacks for the Flag module.

Code

function flag_page($action, $flag, $entity_id) {
  global $user;

  // Shorten up the variables that affect the behavior of this page.
  $js = isset($_REQUEST['js']);
  $token = $_REQUEST['token'];

  // Specifically $_GET to avoid getting the $_COOKIE variable by the same key.
  $has_js = isset($_GET['has_js']);

  // Check the flag token, and then javascript status.
  if (!flag_check_token($token, $entity_id)) {
    $flag->errors['token'] = t('Bad token. You seem to have followed an invalid link.');
  }
  elseif ($user->uid == 0 && !$has_js) {
    $flag->errors['javascript'] = t('You must have JavaScript and cookies enabled in your browser to flag content.');
  }

  // If no errors have been detected thus far, perform the flagging.
  // Further errors may still be detected during validation and prevent
  // the operation from succeeding.
  if (!$flag->errors) {
    $flag
      ->flag($action, $entity_id);
  }

  // If successful, return data according to the request type.
  if ($js) {
    drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8');
    $flag->link_type = 'toggle';

    // Any errors that have been set will be output below
    // the flag link with javascript.
    print drupal_json_encode(flag_build_javascript_info($flag, $entity_id));
    drupal_exit();
  }
  else {
    $errors = $flag
      ->get_errors();
    if ($errors) {

      // If an error was received, set a message and exit.
      foreach ($errors as $error) {
        drupal_set_message($error, 'error');
      }
      if (isset($errors['access-denied'])) {
        return MENU_ACCESS_DENIED;
      }
      else {
        drupal_goto();
      }
    }
    else {
      drupal_set_message($flag
        ->get_label($action . '_message', $entity_id));
      drupal_goto();
    }
  }
}