You are here

function fasttoggle_node_option in Fasttoggle 5

Same name and namespace in other branches
  1. 6 fasttoggle.toggle.inc \fasttoggle_node_option()

Menu callback. Toggle options for a node if the action is confirmed via POST. Otherwise, display a confirmation form.

1 string reference to 'fasttoggle_node_option'
fasttoggle_menu in ./fasttoggle.module
Implementation of hook_menu().

File

./fasttoggle.module, line 395
Enables fast toggling of binary or not so binary settings

Code

function fasttoggle_node_option($node, $option) {
  $options = fasttoggle_get_options('node', $node);

  // Check if the action is valid. This is essential to ensure the user has
  // access to the action.
  if (isset($options[$option]) && isset($_GET['token']) && drupal_valid_token($_GET['token'], $option . '_' . $node->nid, true)) {

    // The action is confirmed: either via form submit or via AJAX/POST
    if (isset($_POST['confirm']) && $_POST['confirm']) {

      // Get the next ID.
      while (key($options[$option]) != $node->{$option}) {
        next($options[$option]);
      }
      if (next($options[$option]) === FALSE) {
        reset($options[$option]);
      }

      // Save the node.
      $node->{$option} = key($options[$option]);
      node_save($node);

      // Output the new status for the updated link text on AJAX changes
      if (isset($_POST['javascript']) && $_POST['javascript']) {
        drupal_set_header('Content-Type: text/javascript; charset=utf-8');
        echo drupal_to_js(array(
          'text' => $options[$option][intval($node->{$option})],
          'callback' => 'node',
          'option' => $option,
          'status' => $node->{$option},
        ));
        exit;
      }
      else {
        drupal_goto();
      }
    }
    else {

      // The action is not confirmed. The user came here through a regular link;
      // no AJAX was involved. That means, we need a confirmation form so that
      // we get a POST form.
      return drupal_get_form('fasttoggle_node_option_confirm', $node, $options[$option][intval(!$node->{$option})]);
    }
  }
  else {
    return MENU_NOT_FOUND;
  }
}