You are here

function webform_node_load in Webform 7.4

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

Implements hook_node_load().

File

./webform.module, line 1786
This module provides a simple way to create forms and questionnaires.

Code

function webform_node_load($nodes, $types) {

  // Quick check to see if we need to do anything at all for these nodes.
  $webform_types = webform_node_types();
  if (count(array_intersect($types, $webform_types)) == 0) {
    return;
  }
  module_load_include('inc', 'webform', 'includes/webform.components');

  // Select all webforms that match these node IDs.
  $result = db_select('webform')
    ->fields('webform')
    ->condition('nid', array_keys($nodes), 'IN')
    ->execute()
    ->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
  foreach ($result as $nid => $webform) {

    // Load the basic information for each node.
    $nodes[$nid]->webform = $webform;
    $nodes[$nid]->webform['record_exists'] = TRUE;

    // Expand the list of excluded preview components.
    $nodes[$nid]->webform['preview_excluded_components'] = array_filter(explode(',', $webform['preview_excluded_components']));
  }

  // Load the components, emails, and defaults for all webform-enabled nodes.
  // @todo Increase efficiency here by pulling in all information all at once
  // instead of individual queries.
  foreach ($nodes as $nid => $node) {
    if (!in_array($node->type, $webform_types)) {
      continue;
    }

    // If a webform record doesn't exist, just return the defaults.
    if (!isset($nodes[$nid]->webform)) {
      $nodes[$nid]->webform = webform_node_defaults();
      continue;
    }
    $nodes[$nid]->webform['roles'] = db_select('webform_roles')
      ->fields('webform_roles', array(
      'rid',
    ))
      ->condition('nid', $nid)
      ->execute()
      ->fetchCol();
    $nodes[$nid]->webform['emails'] = db_select('webform_emails')
      ->fields('webform_emails')
      ->condition('nid', $nid)
      ->execute()
      ->fetchAllAssoc('eid', PDO::FETCH_ASSOC);

    // Unserialize the mappings and excluded component list for e-mails.
    foreach ($nodes[$nid]->webform['emails'] as $eid => $email) {
      $nodes[$nid]->webform['emails'][$eid]['excluded_components'] = array_filter(explode(',', $email['excluded_components']));
      $nodes[$nid]->webform['emails'][$eid]['extra'] = unserialize($email['extra']);
      if (webform_variable_get('webform_format_override')) {
        $nodes[$nid]->webform['emails'][$eid]['html'] = webform_variable_get('webform_default_format');
      }
    }

    // Load components for each node.
    $nodes[$nid]->webform['components'] = db_select('webform_component')
      ->fields('webform_component')
      ->condition('nid', $nid)
      ->orderBy('weight')
      ->orderBy('name')
      ->execute()
      ->fetchAllAssoc('cid', PDO::FETCH_ASSOC);

    // Do a little cleanup on each component.
    foreach ($nodes[$nid]->webform['components'] as $cid => $component) {
      $nodes[$nid]->webform['components'][$cid]['nid'] = $nid;
      $nodes[$nid]->webform['components'][$cid]['extra'] = unserialize($component['extra']);
      webform_component_defaults($nodes[$nid]->webform['components'][$cid]);
    }

    // Organize the components into a fieldset-based order.
    if (!empty($nodes[$nid]->webform['components'])) {
      $component_tree = array();
      $page_count = 1;
      _webform_components_tree_build($nodes[$nid]->webform['components'], $component_tree, 0, $page_count);
      $nodes[$nid]->webform['components'] = _webform_components_tree_flatten($component_tree['children']);
    }

    // Load all the conditional information, if any.
    $nodes[$nid]->webform['conditionals'] = db_select('webform_conditional')
      ->fields('webform_conditional')
      ->condition('nid', $nid)
      ->orderBy('weight')
      ->execute()
      ->fetchAllAssoc('rgid', PDO::FETCH_ASSOC);
    if ($nodes[$nid]->webform['conditionals']) {
      $rules = db_select('webform_conditional_rules')
        ->fields('webform_conditional_rules')
        ->condition('nid', $nid)
        ->orderBy('rgid')
        ->orderBy('rid')
        ->execute();
      foreach ($rules as $rule) {
        $nodes[$nid]->webform['conditionals'][$rule->rgid]['rules'][$rule->rid] = (array) $rule;
      }
      $actions = db_select('webform_conditional_actions')
        ->fields('webform_conditional_actions')
        ->condition('nid', $nid)
        ->orderBy('rgid')
        ->orderBy('aid')
        ->execute();
      foreach ($actions as $action) {
        $nodes[$nid]->webform['conditionals'][$action->rgid]['actions'][$action->aid] = (array) $action;
      }
    }
  }
}