You are here

function flag_lists_page in Flag Lists 6

Same name and namespace in other branches
  1. 7.3 flag_lists.module \flag_lists_page()
  2. 7 flag_lists.module \flag_lists_page()

Menu callback for (un)flagging a node.

Used both for the regular callback as well as the JS version. We use this instead of the flag module's because our flags are not in the flags table.

1 string reference to 'flag_lists_page'
flag_lists_menu in ./flag_lists.module
Implementation of hook_menu().

File

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

Code

function flag_lists_page($action = NULL, $flag_name = NULL, $content_id = NULL) {
  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 {
    if (!($flag = flag_lists_get_flag($flag_name))) {

      // Flag does not exist.
      $error = t('You are not allowed to flag, or unflag, this content.');
    }

    // Identify it as ours.
    $flag->module = 'flag_lists';
    flag_lists_do_flag($flag, $action, $content_id);
  }

  // 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_lists_get_flag($flag_name);
    //   $flag->link_type = 'toggle';
    $sid = flag_get_sid($user->uid);
    $new_action = _flag_lists_is_flagged($flag, $content_id, $user->uid, $sid) ? 'unflag' : 'flag';
    $new_link = $flag
      ->theme($new_action, $content_id, TRUE);
    flag_lists_fix_link($new_link, $new_action);
    print drupal_to_js(array(
      'status' => TRUE,
      'newLink' => $new_link,
      // Further information for the benefit of custom JavaScript event handlers:
      'contentId' => $content_id,
      'contentType' => $flag->content_type,
      'flagName' => $flag->name,
      'flagStatus' => $action,
    ));
    exit;
  }
  else {
    $flag = flag_lists_get_flag($flag->fid);
    drupal_set_message($flag
      ->get_label($action . '_message', $content_id));
    drupal_goto();
  }
}