'Webform Associations', 'description' => t('Webform Associate allows you to auto create a webform when an associated content type is created. Associated webforms are auto appended to the bottom of the associated node.'), 'page callback' => 'drupal_get_form', 'page arguments' => array('webform_associate_admin'), 'access arguments' => array('administer webform associations'), 'type' => MENU_NORMAL_ITEM, ); return $items; } function webform_associate_menu_alter(&$items) { $webform_items = webform_menu(); foreach ($webform_items as $key => $item) { if (strpos($key, 'node/%webform_menu') !== FALSE) { $new_key = str_replace('node/%webform_menu', 'node/%webform_associate_webform', $key); // Use the existing router item which has, e.g., the 'module' set $items[$new_key] = $items[$key]; $callback = $items[$new_key]['access callback']; $items[$new_key]['access callback'] = 'webform_associate_menu_access'; $arguments = $items[$new_key]['access arguments']; switch ($callback) { case 'node_access': $items[$new_key]['access arguments'] = array($callback, $arguments, 1); break; case 'user_access': $items[$new_key]['access arguments'] = array($callback, $arguments, 1); break; case 'webform_submission_access': if ($new_key == 'node/%webform_associate_webform/submissions' || $new_key == 'node/%webform_associate_webform/submissions') { $items[$new_key]['access arguments'] = array($callback, $arguments, 1); } else { $items[$new_key]['access arguments'] = array($callback, $arguments, 1, 3); } break; case 'webform_results_access': $items[$new_key]['access arguments'] = array($callback, $arguments, 1); break; } unset($items[$key]); } } } function webform_associate_menu_access($func, $arguments = array(), $node, $a3 = NULL) { $vars = variable_get('webform_associate_nodes', array()); if (!in_array('webform', $vars)) { $vars[] = 'webform'; } foreach ($vars as $var) { if ($node->type == $var) { switch ($func) { case 'node_access': return $func($arguments[0], $node->nid); break; case 'user_access': return $func($arguments[0]); break; case 'webform_submission_access': if (!$a3) { return $func($node->nid, '', $arguments[2]); } else { return $func($node->nid, $a3, $arguments[2]); } break; case 'webform_results_access': return $func($node, $arguments[1]); break; } } else { return FALSE; } } } /** * Menu loader function. Given a nid, load the associated webform. */ function webform_associate_webform_load($nid) { $node = node_load($nid); if ($node->type != 'webform') { $ids = webform_associate_lookup($nid); if (isset($ids->wid)) { $node = node_load($ids->wid); } } return $node; } function webform_associate_menu_load($nid) { $wid = webform_associate_lookup($nid); $node = node_load($wid); return $node; } function webform_associate_perm() { return array('administer webform associations'); } function webform_associate_content_types() { $types = content_types(); foreach ($types as $type) { if ($type['type'] != 'webform') { $node_types[$type['type']] = $type['name']; } } return $node_types; } function webform_associate_admin() { $form = array(); $form['types'] = array( '#type' => 'checkboxes', '#title' => t('Content Types'), '#description' => t('Please select the types of content with which you would like to associate a webform creation utility.'), '#options' => webform_associate_content_types(), '#default_value' => variable_get('webform_associate_nodes', array()), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save Settings'), ); $form['reset'] = array( '#type' => 'submit', '#value' => t('Reset Settings'), '#submit' => array('webform_associate_admin_reset'), ); return $form; } function webform_associate_admin_submit($form, &$state) { variable_set('webform_associate_nodes', $state['values']['types']); } function webform_associate_admin_reset() { variable_set('webform_associate_nodes', array()); } function webform_associate_redirect($form, &$form_state) { $ids = webform_associate_lookup(arg(1), 'wid'); if ($ids) { $url = 'node/'. $ids->nid .'/edit/components'; } $form_state['redirect'] = $url; } function webform_associate_form_alter(&$form, $form_state, $form_id) { switch ($form_id) { case 'webform_client_form_'. arg(1): if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'submission' && is_numeric(arg(3))) { $ids = webform_associate_lookup($form['submission']['#value']->nid, 'wid'); if ($ids) { $node = node_load($ids->nid); $form['submission_info']['form']['#value'] = '
Form: '. l($node->title, 'node/'. $node->nid) .'
'; } //$form['#action'] = '/node/'. $form['submission']['#value']->nid .'/submission/'. $form['submission']['#value']->sid .'/edit'; } break; case 'webform_component_delete_form': case 'webform_component_edit_form': $ids = webform_associate_lookup($form['nid']['#value'], 'wid'); if ($ids) { $form['#submit'][] = 'webform_associate_redirect'; } break; case 'webform_components_form': $ids = webform_associate_lookup($form['nid']['#value'], 'wid'); if ($ids) { $form['#action'] = base_path(). 'node/'. $ids->nid .'/edit/components'; } break; } $vars = variable_get('webform_associate_nodes', array()); foreach ($vars as $var) { switch ($form_id) { case $var .'_node_form': $form['mail_settings'] = array( '#type' => 'fieldset', '#title' => t('Mail Settings'), '#weight' => -20, ); $form['mail_settings']['email'] = array( '#type' => 'textfield', '#title' => t('E-mail to address'), '#description' => t('Form submissions will be e-mailed to this address. Leave blank for none. Multiple e-mail addresses may be separated by commas.'), ); if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'edit') { $ids = webform_associate_lookup(arg(1)); $wnode = node_load($ids->wid); $form['mail_settings']['email']['#default_value'] = $wnode->webform['email']; } //$form['buttons']['submit']['#submit'][] = 'webform_associate_form_submit'; } if ($var) { if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) { //$form['#action'] = base_path() . drupal_get_path_alias('node/'. arg(1)); //print_r($form); } } } } function webform_associate_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { if ($node->type == 'webform' && $op == 'view') { $ids = webform_associate_lookup($node->nid, 'wid'); if ($ids) { drupal_goto('node/'. $ids->nid); } } $types = variable_get('webform_associate_nodes', array()); if (in_array($node->type, $types, TRUE)) { switch ($op) { case 'view': $ids = webform_associate_lookup($node->nid); $wnode = node_load($ids->wid); $webform = webform_load($wnode); $node->webform = $webform->webform; $node->nid = $ids->wid; webform_view($node); $node->nid = $ids->nid; break; case 'insert': $wnode = new stdClass(); $wnode->type = 'webform'; $wnode->title = $node->title .' Webform'; $wnode->body = $node->body; $wnode->uid = $node->uid; $wnode->webform = array(//defaults 'teaser' => 0, 'submit_text' => '', 'submit_limit' => -1, 'submit_interval' => -1, 'email' => $node->email, 'email_from_name' => 'default', 'email_from_address' => 'default', 'email_subject' => 'default', 'additional_validate' => '', 'additional_submit' => '', 'roles' => array(1, 2), ); node_save($wnode); webform_associate_save($node->nid, $wnode->nid); webform_associate_components($node->nid); break; case 'update': $ids = webform_associate_lookup($node->nid); $wnode = node_load($ids->wid); $wnode->title = $node->title .' Webform'; $wnode->body = $node->body; $wnode->uid = $node->uid; $wnode->webform['email'] = $node->email; node_save($wnode); break; case 'delete': $ids = webform_associate_lookup($node->nid); //drupal_set_message('This will delete all the data associated with this '. $node->type); node_delete($ids->wid); break; } } } function webform_associate_save($nid, $wid) { db_query("INSERT INTO {webform_associated_nodes} (nid, wid) VALUES (%d, '%d')", $nid, $wid); } function webform_associate_lookup($id, $type = 'nid') { $results = db_fetch_object(db_query('SELECT nid, wid FROM {webform_associated_nodes} WHERE %s = %d', $type, $id)); return $results; } function webform_associate_node_access($access, $webform_node) { $nid = webform_associate_lookup($webform_node->nid, 'wid'); $node = node_load($nid); return node_access($access, $node); } function webform_associate_components($nid) { // Remove the the submitted message added by node module. unset($_SESSION['messages']['status']); drupal_set_message(t('The new webform %title has been created. Add new fields to your webform with the form below.', array('%title' => $form_state['values']['title']))); drupal_goto('node/'. $nid .'/edit/components'); }