You are here

function webform_view in Webform 6.2

Same name and namespace in other branches
  1. 5.2 webform.module \webform_view()
  2. 5 webform.module \webform_view()

Implementation of hook_view().

File

./webform.module, line 1075

Code

function webform_view(&$node, $teaser = 0, $page = 0) {
  global $user;

  // If a teaser, do not display the form.
  if ($teaser && !$node->webform['teaser']) {
    $node->content['teaser'] = array(
      '#value' => check_markup($node->teaser, $node->format, FALSE),
    );
    return $node;
  }
  $submission = array();
  $submission_count = 0;
  $enabled = TRUE;
  $preview = FALSE;
  $logging_in = FALSE;
  $limit_exceeded = FALSE;
  if ($node->build_mode == NODE_BUILD_PREVIEW) {
    $preview = TRUE;
    $additions = webform_load($node);
    $node->webform['components'] = $additions->webform['components'];
  }

  // When logging in using a form on the same page as a webform node, surpress
  // 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;
  }

  // Check if the user's role can submit this webform.
  if (variable_get('webform_submission_access_control', 1)) {
    $allowed_roles = array();
    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());
  }

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

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

    // Disable the form if the limit is exceeded and page cache is not active.
    if (($limit_exceeded = _webform_submission_limit_check($node)) && ($user->uid != 0 || variable_get('cache', CACHE_DISABLED) == CACHE_DISABLED)) {
      $enabled = FALSE;
    }
  }

  // Get a count of previous submissions by this user.
  if ($user->uid && (user_access('access own webform submissions') || user_access('access webform results') || user_access('access webform submissions'))) {
    $submission_count = db_result(db_query('SELECT count(*) FROM {webform_submissions} WHERE nid = %d AND uid = %d', $node->nid, $user->uid));
  }

  // Render the form and generate the output.
  $form = drupal_get_form('webform_client_form_' . $node->nid, $node, $submission, $enabled, $preview);
  $output = theme('webform_view', $node, $teaser, $page, $form, $enabled);

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

  // Print out messages for the webform.
  if ($node->build_mode != NODE_BUILD_PREVIEW && !$logging_in && ($user->uid != 0 || !variable_get('cache', CACHE_DISABLED))) {
    theme('webform_view_messages', $node, $teaser, $page, $submission_count, $limit_exceeded, $allowed_roles);
  }

  // Add the output to the node.
  $node = node_prepare($node, $teaser);
  if (isset($output)) {
    $node->content['webform'] = array(
      '#value' => $output,
      '#weight' => 1,
    );
  }
  return $node;
}