'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_form_alter(). */ function webform_template_form_alter(&$form, &$form_state, $form_id) { if (isset($form['#node'])) { $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_lang = variable_get('webform_template_lang',FALSE); $query = "SELECT nid, title, language FROM {node} WHERE type IN (".db_placeholders($tpl_src,'varchar').") AND status = 1 ORDER BY tnid"; $result = db_query($query, $tpl_src); $templates[] = '- ' . t('none') . ' -'; while ($row = db_fetch_object($result)) { $templates[$row->nid] = $row->title; if (!empty($show_lang) && !empty($row->language)) { $templates[$row->nid] .= ' [' . $row->language . ']'; } } $form['webform_template'] = array( '#type' => 'fieldset', '#title' => t('Webform'), '#weight' => 5, '#collapsible' => FALSE, '#collapsed' => FALSE, ); $form['webform_template']['source'] = array( '#type' => 'select', '#title' => t('Templates'), '#options' => $templates, '#default_value' => 'none', '#description' => t('Pick a template if you want to attach a webform with predefined fields.'), ); if (!empty($form['nid']['#value'])) { $form['webform_template']['source']['#description'] .= '
' . t('Setting this will overwrite any webform you previously defined!') . ''; } // add a submit function before the usual submit array_unshift($form['#submit'], 'webform_template_submit'); } } } /** * Form submission handler for node forms. */ function webform_template_submit($form, &$form_state) { if (isset($form_state['values']['source']) && is_numeric($form_state['values']['source'])) { $template = $form_state['values']['source']; if (!empty($template)) { $_SESSION['webform_template'] = $template; } } } /** * Implements hook_nodeapi(). */ function webform_template_nodeapi($node, $op) { switch($op) { case 'insert': _webform_template_attach($node); break; case 'update': _webform_template_attach($node, 'delete'); break; } } /** * Internal function * Adds webform template to a node if $_SESSION indicates a template. */ function _webform_template_attach($node, $op = NULL) { if (isset($_SESSION['webform_template'])) { if ($op == 'delete') { if (!in_array($node->type, webform_variable_get('webform_node_types'))) { return; } // 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_query('DELETE FROM {webform} WHERE nid = %d', $node->nid); db_query('DELETE FROM {webform_component} WHERE nid = %d', $node->nid); db_query('DELETE FROM {webform_emails} WHERE nid = %d', $node->nid); db_query('DELETE FROM {webform_roles} WHERE nid = %d', $node->nid); db_query('DELETE FROM {webform_submissions} WHERE nid = %d', $node->nid); db_query('DELETE FROM {webform_submitted_data} WHERE nid = %d', $node->nid); db_query('DELETE FROM {webform_last_download} WHERE nid = %d', $node->nid); } $template = node_load($_SESSION['webform_template']); $webform = $template->webform; module_load_include('inc', 'webform', 'includes/webform.components'); module_load_include('inc', 'webform', 'includes/webform.emails'); $node->webform = $webform; $node->webform['nid'] = $node->nid; $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); } } unset($_SESSION['webform_template']); } }