You are here

function flag_page in Flag 6

Same name and namespace in other branches
  1. 5 flag.module \flag_page()
  2. 6.2 flag.module \flag_page()
  3. 7.3 includes/flag.pages.inc \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.

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

File

./flag.module, line 445

Code

function flag_page($action = NULL, $flag_name = NULL, $content_id = NULL) {
  $js = isset($_REQUEST['js']);
  $token = $_REQUEST['token'];

  // 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.');
  }
  else {
    $result = flag($action, $flag_name, $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_set_header('Content-Type: text/javascript; charset=utf-8');
      print drupal_to_js(array(
        'status' => FALSE,
        'errorMessage' => $error,
      ));
      exit;
    }
    else {
      drupal_set_message($error);
      drupal_access_denied();
      return;
    }
  }

  // If successful, return data according to the request type.
  if ($js) {
    drupal_set_header('Content-Type: text/javascript; charset=utf-8');
    $flag = flag_get_flag($flag_name);
    $flag->link_type = 'toggle';
    print drupal_to_js(array(
      'status' => TRUE,
      'newLink' => $flag
        ->theme($flag
        ->is_flagged($content_id) ? 'unflag' : 'flag', $content_id, TRUE),
      // Further information for the benefit of custom JavaScript event handlers:
      'contentId' => $content_id,
      'contentType' => $flag->content_type,
      'flagName' => $flag->name,
      'flagStatus' => $flag
        ->is_flagged($content_id) ? 'flagged' : 'unflagged',
    ));
    exit;
  }
  else {
    $flag = flag_get_flag($flag_name);
    drupal_set_message($flag
      ->get_label($action . '_message', $content_id));
    drupal_goto();
  }
}