You are here

function flag_page in Flag 7.2

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.3 includes/flag.pages.inc \flag_page()

Menu callback for (un)flagging a node.

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

Parameters

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

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

File

./flag.module, line 936
The Flag module.

Code

function flag_page($action, $flag, $content_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, then perform the flagging.
  if (!flag_check_token($token, $content_id)) {
    $error = t('Bad token. You seem to have followed an invalid link.');
  }
  elseif ($user->uid == 0 && !$has_js) {
    $error = t('You must have JavaScript and cookies enabled in your browser to flag content.');
  }
  else {
    $result = $flag
      ->flag($action, $content_id);
    if (!$result) {
      $error = t('You are not allowed to flag, or unflag, this content.');
    }
  }

  // If an error was received, set a message and exit.
  if (isset($error)) {
    if ($js) {
      drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8');
      print drupal_json_encode(array(
        'status' => FALSE,
        'errorMessage' => $error,
      ));
      drupal_exit();
    }
    else {
      drupal_set_message($error);
      drupal_access_denied();
      return;
    }
  }

  // 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';
    print drupal_json_encode(flag_build_javascript_info($flag, $content_id));
    drupal_exit();
  }
  else {
    drupal_set_message($flag
      ->get_label($action . '_message', $content_id));
    drupal_goto();
  }
}