View source
<?php
function webform_template_menu() {
$items = array();
$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;
}
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;
}
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()));
if (isset($tpl_dest[$form['type']['#value']]) && !empty($tpl_src)) {
$show = TRUE;
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>';
}
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.'),
);
}
}
}
function webform_template_submit($form, &$form_state) {
if (isset($form_state['complete form']['webform_template']['source']['#value'])) {
$template = $form_state['complete form']['webform_template']['source']['#value'];
if (!empty($template)) {
$_SESSION['webform_template'] = $template;
}
}
}
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;
if (module_exists('webform_validation')) {
_webform_template_webform_validation_helper($node, $template);
}
module_invoke_all('webform_template_insert', $node, $template);
unset($_SESSION['webform_template']);
}
}
function webform_template_node_update($node) {
if (isset($_SESSION['webform_template'])) {
module_load_include('inc', 'webform', 'includes/webform.components');
foreach ($node->webform['components'] as $cid => $component) {
webform_component_delete($node, $component);
}
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);
if (isset($node->webform['components']) && !empty($node->webform['components'])) {
foreach ($node->webform['components'] as $cid => $component) {
$component['nid'] = $node->nid;
webform_component_insert($component);
}
}
if (isset($node->webform['emails']) && !empty($node->webform['emails'])) {
foreach ($node->webform['emails'] as $eid => $email) {
$email['nid'] = $node->nid;
webform_email_insert($email);
}
}
if (module_exists('webform_validation')) {
_webform_template_webform_validation_helper($node, $template, 'update');
}
module_invoke_all('webform_template_update', $node, $template);
unset($_SESSION['webform_template']);
}
}
function _webform_template_webform_validation_helper($node, $template, $op = 'insert') {
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;
$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;
$rule['rule_components'] = $rule['components'];
webform_validation_rule_save($rule);
}
}
}