You are here

function rabbit_hole_nodeapi in Rabbit Hole 6

Implements hook_nodeapi().

File

./rabbit_hole.module, line 212
Main module file for Rabbit Hole.

Code

function rabbit_hole_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':
      if (arg(1) != $node->nid || !preg_match('/node\\/' . $node->nid . '(\\/view|)$/', $_GET['q']) || user_access('bypass rabbit hole')) {

        // The node is not being viewed at it's own page, or the user is able to
        // bypass Rabbit Hole, exit early.
        return;
      }

      // Get the action. Use the one specified for this node, or fallback to the
      // default value for the content type.
      $action = isset($node->rabbit_hole_action) ? $node->rabbit_hole_action != RABBIT_HOLE_USE_DEFAULT ? $node->rabbit_hole_action : variable_get('rabbit_hole_action_' . $node->type, RABBIT_HOLE_DISPLAY_CONTENT) : variable_get('rabbit_hole_action_' . $node->type, RABBIT_HOLE_DISPLAY_CONTENT);

      // If we should perform a redirect, we will also get the path and response.
      if ($action == RABBIT_HOLE_PAGE_REDIRECT) {
        if (isset($node->rabbit_hole_action) && $node->rabbit_hole_action != RABBIT_HOLE_USE_DEFAULT) {

          // Get the redirect path and response from the node.
          $redirect_path = $node->rabbit_hole_redirect;
          $redirect_response = $node->rabbit_hole_redirect_response;
        }
        else {

          // Get the redirect path and response from the content type.
          $redirect_path = variable_get('rabbit_hole_redirect_' . $node->type, '<front>');
          $redirect_response = variable_get('rabbit_hole_redirect_response_' . $node->type, RABBIT_HOLE_PAGE_REDIRECT_RESPONSE_DEFAULT);
        }
      }

      // Replace the tokens in the provided redirect path if the tokens module
      // exists.
      if (module_exists('token')) {
        $redirect_path = token_replace($redirect_path, 'node', $node);
      }

      // Now, let's see what we should do.
      switch ($action) {
        case RABBIT_HOLE_ACCESS_DENIED:

          // TODO: Is this the proper way to deliver an access denied page?
          drupal_access_denied();
          exit;
        case RABBIT_HOLE_PAGE_NOT_FOUND:

          // TODO: Is this the proper way to deliver a not found page?
          drupal_not_found();
          exit;
        case RABBIT_HOLE_PAGE_REDIRECT:

          // Redirect the user to the specified path.
          drupal_goto($redirect_path, NULL, NULL, $redirect_response);
      }
      break;
  }
}