You are here

function fasttoggle_node_option in Fasttoggle 6

Same name and namespace in other branches
  1. 5 fasttoggle.module \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.toggle.inc, line 12
Handles toggling of options.

Code

function fasttoggle_node_option($node, $group, $option) {
  global $user;
  $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[$group][$option]) && isset($_GET['token']) && drupal_valid_token($_GET['token'], $group . '_' . $option . '_' . $node->nid, TRUE)) {

    // The action is confirmed: either via form submit or via AJAX/POST
    if (isset($_POST['confirm']) && $_POST['confirm']) {
      $selectorClass = 'fasttoggle-status-node-' . $node->nid . '-' . $group . '-' . $option;

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

      // Save the node.
      $node->{$option} = key($options[$group][$option]['label']);
      $newClass = 'fasttoggle-status-node-' . $group . '-' . $option . '-' . $node->{$option};
      node_save($node);
      $labels = fasttoggle_fasttoggle_labels(FASTTOGGLE_LABEL_STATUS);
      watchdog('fasttoggle', 'Node @type "%title" @option toggled to @value by @username', array(
        '@type' => $node->type,
        '%title' => $node->title,
        '@option' => $option,
        '@value' => $labels['node_' . $option][$node->{$option}],
        '@username' => $user->name,
      ));

      // Let other modules respond.
      module_invoke_all('fasttoggle_toggle', 'node', $node, $option);

      // 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[$group][$option]['label'][intval($node->{$option})],
          'callback' => 'node',
          'option' => $option,
          'status' => $node->{$option},
          'className' => 'fasttoggle-status-node-' . $group . '-' . $option . '-' . intval($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.
      $labels = _fasttoggle_get_label('node_' . $group . '_' . $option, FASTTOGGLE_LABEL_STATUS);
      return drupal_get_form('fasttoggle_node_option_confirm', $node, $labels[intval(!$node->{$option})]);
    }
  }
  else {
    return MENU_NOT_FOUND;
  }
}