You are here

function webform_node_view in Webform 6.3

Same name and namespace in other branches
  1. 7.4 webform.module \webform_node_view()
  2. 7.3 webform.module \webform_node_view()

Implements hook_node_view().

3 calls to webform_node_view()
webform_block_view in ./webform.module
Implements hook_block_view().
webform_handler_field_form_body::render in views/webform_handler_field_form_body.inc
webform_nodeapi in ./webform.module
Implements hook_nodeapi().

File

./webform.module, line 1310

Code

function webform_node_view(&$node, $teaser, $page) {
  global $user;

  // If empty, a teaser, or a new node (during preview) do not display.
  if (empty($node->webform['components']) || $teaser && !$node->webform['teaser'] || empty($node->nid)) {
    return;
  }

  // Do not include the form in the search index if indexing is disabled.
  if (module_exists('search') && isset($node->build_mode) && $node->build_mode == NODE_BUILD_SEARCH_INDEX && !variable_get('webform_search_index', 1)) {
    return;
  }
  $info = array();
  $submission = array();
  $submission_count = 0;
  $enabled = TRUE;
  $logging_in = FALSE;
  $total_limit_exceeded = FALSE;
  $user_limit_exceeded = FALSE;
  $closed = FALSE;
  $allowed_roles = array();

  // Let webform form know if the node is displayed as teaser or not.
  $node->webform['is_teaser'] = $teaser;

  // If a teaser, tell the form to load subsequent pages on the node page.
  if ($teaser && !isset($node->webform['action'])) {
    $query = array_diff_key($_GET, array(
      'q' => '',
    ));
    $node->webform['action'] = url('node/' . $node->nid, array(
      'query' => $query,
    ));
  }

  // When logging in using a form on the same page as a webform node, suppress
  // output messages so that they don't show up after the user has logged in.
  // See http://drupal.org/node/239343.
  if (isset($_POST['op']) && isset($_POST['name']) && isset($_POST['pass'])) {
    $logging_in = TRUE;
  }
  if ($node->webform['status'] == 0) {
    $closed = TRUE;
    $enabled = FALSE;
  }
  else {

    // Check if the user's role can submit this webform.
    if (variable_get('webform_submission_access_control', 1)) {
      foreach ($node->webform['roles'] as $rid) {
        $allowed_roles[$rid] = isset($user->roles[$rid]) ? TRUE : FALSE;
      }
      if (array_search(TRUE, $allowed_roles) === FALSE && $user->uid != 1) {
        $enabled = FALSE;
      }
    }
    else {

      // If not using Webform submission access control, allow for all roles.
      $allowed_roles = array_keys(user_roles());
    }
  }

  // Get a count of previous submissions by this user. Note that the
  // webform_submission_access() function may disable the page cache for
  // anonymous users if they are allowed to edit their own submissions!
  if ($page && webform_submission_access($node, NULL, 'list')) {
    module_load_include('inc', 'webform', 'includes/webform.submissions');
    $submission_count = webform_get_submission_count($node->nid, $user->uid);
  }

  // Check if this page is cached or not.
  $cached = $user->uid == 0 && (variable_get('cache', 0) || function_exists('drupal_page_is_cacheable') && drupal_page_is_cacheable() === FALSE);

  // Check if the user can add another submission.
  if ($node->webform['submit_limit'] != -1) {

    // -1: Submissions are never throttled.
    module_load_include('inc', 'webform', 'includes/webform.submissions');

    // Disable the form if the limit is exceeded and page cache is not active.
    if (($user_limit_exceeded = _webform_submission_user_limit_check($node)) && !$cached) {
      $enabled = FALSE;
    }
  }

  // Check if the user can add another submission if there is a limit on total
  // submissions.
  if ($node->webform['total_submit_limit'] != -1) {

    // -1: Submissions are never throttled.
    module_load_include('inc', 'webform', 'includes/webform.submissions');

    // Disable the form if the limit is exceeded and page cache is not active.
    if (($total_limit_exceeded = _webform_submission_total_limit_check($node)) && !$cached) {
      $enabled = FALSE;
    }
  }

  // Check if this user has a draft for this webform.
  $is_draft = FALSE;
  if (($node->webform['allow_draft'] || $node->webform['auto_save']) && $user->uid != 0) {

    // Draft found - display form with draft data for further editing.
    if ($draft_sid = _webform_fetch_draft_sid($node->nid, $user->uid)) {
      module_load_include('inc', 'webform', 'includes/webform.submissions');
      $submission = webform_get_submission($node->nid, $draft_sid);
      $enabled = TRUE;
      $is_draft = TRUE;
    }
  }

  // Render the form and generate the output.
  $form = !empty($node->webform['components']) ? drupal_get_form('webform_client_form_' . $node->nid, $node, $submission, $is_draft) : '';
  $output = theme('webform_view', $node, $teaser, $page, $form, $enabled);

  // Remove the surrounding <form> tag if this is a preview.
  if (isset($node->build_mode) && $node->build_mode == NODE_BUILD_PREVIEW) {
    $output = preg_replace('/<\\/?form[^>]*>/', '', $output);
  }

  // Print out messages for the webform.
  if ((!isset($node->build_mode) || $node->build_mode != NODE_BUILD_PREVIEW) && !isset($node->webform_block) && !$logging_in) {
    theme('webform_view_messages', $node, $teaser, $page, $submission_count, $user_limit_exceeded, $total_limit_exceeded, $allowed_roles, $closed, $cached);
  }
  if (isset($output)) {
    if (module_exists('content')) {
      $weight = content_extra_field_weight($node->type, 'webform');
    }
    $node->content['webform'] = array(
      '#value' => $output,
      '#weight' => isset($weight) ? $weight : 10,
    );
  }
}