You are here

webform_template.module in Webform Template 7

Same filename and directory in other branches
  1. 6 webform_template.module
  2. 7.4 webform_template.module

File

webform_template.module
View source
<?php

/**
 * Implementation of hook_menu().
 */
function webform_template_menu() {
  $items = array();

  // Admin Settings.
  $items['admin/config/content/webform_template'] = array(
    'title' => 'Webform Template settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'webform_template_config_form',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
    'description' => 'Enable webform template for content types',
    'file' => 'includes/webform_template.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_field_extra_fields().
 */
function webform_template_field_extra_fields() {
  $extra = array();
  $types = array_filter(variable_get('webform_template_dest', array()));
  foreach ($types as $type) {
    $extra['node'][$type] = array(
      'form' => array(
        'webform_template' => array(
          'label' => t('Webform template'),
          'description' => t('Webform template form'),
          'weight' => 0,
        ),
      ),
    );
  }
  return $extra;
}

/**
 * Implements hook_node_form_alter().
 */
function webform_template_form_node_form_alter(&$form, &$form_state, $form_id) {
  $tpl_src = array_filter(variable_get('webform_template_src', array()));
  $tpl_dest = array_filter(variable_get('webform_template_dest', array()));

  // Show the template form if conditions are met.
  if (isset($tpl_dest[$form['type']['#value']]) && !empty($tpl_src)) {
    $show = TRUE;

    // in some cases, the form should be hidden.
    // All other modules to hide the template selection
    drupal_alter('webform_template_show_selection', $show, $form);
    if ($show) {
      $show_lang = variable_get('webform_template_lang');
      $q = db_select('node', 'n')
        ->fields('n', array(
        'nid',
        'title',
        'language',
      ))
        ->condition('status', 1)
        ->condition('type', $tpl_src, 'IN')
        ->orderBy('tnid');
      if (!variable_get('webform_template_defeat_nodeaccess', FALSE)) {
        $q
          ->addTag('node_access');
      }
      $result = $q
        ->execute()
        ->fetchAll();
      $templates = array();
      foreach ($result as $row) {
        $templates[$row->nid] = $row->title;
        if (!empty($show_lang)) {
          $templates[$row->nid] .= ' [' . $row->language . ']';
        }
      }
      $form['webform_template'] = array(
        '#type' => 'fieldset',
        '#title' => t('Webform Template'),
        '#weight' => 5,
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
      );
      $form['webform_template']['source'] = array(
        '#type' => 'select',
        '#title' => t('Available webforms'),
        '#options' => $templates,
        '#empty_option' => '- ' . t('none') . ' -',
        '#default_value' => '',
        '#description' => t('Make a selection to copy the fields of the selected webform to this node.'),
      );
      if (!empty($form['nid']['#value'])) {
        $form['webform_template']['source']['#description'] .= '<br /><strong>' . t('Setting this will overwrite any webform you previously defined!') . '</strong>';
      }

      // add a submit function before the usual submit.
      array_unshift($form['#submit'], 'webform_template_submit');
    }
    else {
      $form['webform_template'] = array(
        '#type' => 'fieldset',
        '#title' => t('Webform Template'),
        '#weight' => 5,
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
      );
      $form['webform_template']['notice'] = array(
        '#markup' => t('Template selection disabled.'),
      );
    }
  }
}

/**
 * Form submission handler for node forms.
 */
function webform_template_submit($form, &$form_state) {

  // this approach
  if (isset($form_state['complete form']['webform_template']['source']['#value'])) {
    $template = $form_state['complete form']['webform_template']['source']['#value'];
    if (!empty($template)) {

      // @todo SECURE THIS!!
      $_SESSION['webform_template'] = $template;
    }
  }
}

/**
 * Implements hook_node_insert().
 */
function webform_template_node_insert($node) {
  if (isset($_SESSION['webform_template'])) {
    $template = node_load($_SESSION['webform_template']);
    $webform = $template->webform;
    $node->webform = $webform;
    $node->webform['nid'] = $node->nid;

    // Integration with other modules.
    if (module_exists('webform_validation')) {
      _webform_template_webform_validation_helper($node, $template);
    }

    // Allow other modules to modify the webform.
    module_invoke_all('webform_template_insert', $node, $template);

    // Clean up session.
    unset($_SESSION['webform_template']);
  }
}

/**
 * Implements hook_node_update().
 */
function webform_template_node_update($node) {
  if (isset($_SESSION['webform_template'])) {

    // Allow components clean up extra data, such as uploaded files.
    module_load_include('inc', 'webform', 'includes/webform.components');
    foreach ($node->webform['components'] as $cid => $component) {
      webform_component_delete($node, $component);
    }

    // Remove any trace of webform data from the database.
    db_delete('webform')
      ->condition('nid', $node->nid)
      ->execute();
    db_delete('webform_component')
      ->condition('nid', $node->nid)
      ->execute();
    db_delete('webform_emails')
      ->condition('nid', $node->nid)
      ->execute();
    db_delete('webform_roles')
      ->condition('nid', $node->nid)
      ->execute();
    db_delete('webform_submissions')
      ->condition('nid', $node->nid)
      ->execute();
    db_delete('webform_submitted_data')
      ->condition('nid', $node->nid)
      ->execute();
    $template = node_load($_SESSION['webform_template']);
    $webform = $template->webform;
    $node->webform = $webform;
    $node->webform['nid'] = $node->nid;
    module_load_include('inc', 'webform', 'includes/webform.components');
    module_load_include('inc', 'webform', 'includes/webform.emails');
    $node->webform['record_exists'] = (bool) drupal_write_record('webform', $node->webform);

    // Insert the components into the database.
    if (isset($node->webform['components']) && !empty($node->webform['components'])) {
      foreach ($node->webform['components'] as $cid => $component) {
        $component['nid'] = $node->nid;
        webform_component_insert($component);
      }
    }

    // Insert emails.
    if (isset($node->webform['emails']) && !empty($node->webform['emails'])) {
      foreach ($node->webform['emails'] as $eid => $email) {
        $email['nid'] = $node->nid;
        webform_email_insert($email);
      }
    }

    // Integration with other modules.
    if (module_exists('webform_validation')) {
      _webform_template_webform_validation_helper($node, $template, 'update');
    }

    // Allow other modules to modify the webform.
    module_invoke_all('webform_template_update', $node, $template);

    // Clean up session.
    unset($_SESSION['webform_template']);
  }
}

/**
 * Helper function, integrates with webform_validation.module.
 */
function _webform_template_webform_validation_helper($node, $template, $op = 'insert') {

  // When updating, delete the previous validation rules.
  if ($op == 'update') {
    $rules = webform_validation_get_node_rules($node->nid);
    if ($rules) {
      foreach (array_keys($rules) as $ruleid) {
        webform_dynamic_delete_rule($ruleid);
      }
    }
  }
  $original_nid = $template->nid;

  // Get existing rules for original node
  $rules = webform_validation_get_node_rules($original_nid);
  if ($rules) {
    foreach ($rules as $orig_ruleid => $rule) {
      unset($rule['ruleid']);
      $rule['action'] = 'add';
      $rule['nid'] = $node->nid;

      // attach existing rules to new node
      $rule['rule_components'] = $rule['components'];
      webform_validation_rule_save($rule);
    }
  }
}

Functions