You are here

function flag_lists_page in Flag Lists 7.3

Same name and namespace in other branches
  1. 6 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 1407
The Flag Lists module.

Code

function flag_lists_page($action = NULL, $flag_name = NULL, $entity_id = NULL) {
  global $user;

  // Shorten up the variables that affect the behavior of this page.
  $js = isset($_REQUEST['js']);
  $token = isset($_REQUEST['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, $entity_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 (empty($flag_name) || !($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, $entity_id);
  }

  // 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_to_js(array(
        'status' => FALSE,
        'errorMessage' => $error,
      ));
      exit;
    }
    else {
      drupal_set_message($error);
      drupal_access_denied();
      exit;
    }
  }

  // If successful, return data according to the request type.
  if ($js) {
    drupal_add_http_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, $entity_id, $user->uid, $sid) ? 'unflag' : 'flag';
    $new_link = $flag
      ->theme($new_action, $entity_id, array(
      "after_flagging" => TRUE,
    ));
    flag_lists_fix_link($new_link, $new_action);
    drupal_json_output(array(
      'status' => TRUE,
      'newLink' => $new_link,
      // Further information for the benefit of custom JavaScript event handlers:
      'contentId' => $entity_id,
      'contentType' => $flag->entity_type,
      'flagName' => $flag->name,
      'flagStatus' => $action,
    ));
    exit;
  }
  else {
    $flag = flag_lists_get_flag($flag->fid);
    drupal_set_message($flag
      ->get_label($action . '_message', $entity_id));
    drupal_goto();
  }
}