? webform_nodeapi.diff
Index: webform.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform/webform.module,v
retrieving revision 1.136
diff -u -p -r1.136 webform.module
--- webform.module	20 Jun 2009 23:04:58 -0000	1.136
+++ webform.module	4 Jul 2009 18:30:41 -0000
@@ -304,7 +304,7 @@ function webform_menu_load($nid) {
     return FALSE;
   }
   $node = node_load($nid);
-  if (!isset($node->type) || $node->type != 'webform') {
+  if (!isset($node->type) || !array_key_exists($node->type, webform_variable_get('webform_node_types'))) {
     return FALSE;
   }
   return $node;
@@ -565,126 +565,199 @@ function webform_file_download($file) {
 }
 
 /**
- * Implementation of hook_insert().
+ * Implementation of hook_nodeapi().
  */
-function webform_insert($node) {
-  module_load_include('inc', 'webform', 'includes/webform.components');
 
-  // Insert the Webform.
-  db_query("INSERT INTO {webform} (nid, confirmation, confirmation_format, teaser, submit_notice, submit_text, submit_limit, submit_interval, additional_validate, additional_submit) VALUES (%d, '%s', %d, %d, %d, '%s', %d, %d, '%s', '%s')", $node->nid, $node->webform['confirmation'], $node->webform['confirmation_format'], $node->webform['teaser'], $node->webform['submit_notice'], $node->webform['submit_text'], $node->webform['submit_limit'], $node->webform['submit_interval'], $node->webform['additional_validate'], $node->webform['additional_submit']);
+function webform_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
+  if (array_key_exists($node->type, webform_variable_get('webform_node_types'))) {
+    switch ($op) {
+      case 'insert':
+        module_load_include('inc', 'webform', 'includes/webform.components');
+
+        // Insert the Webform.
+        db_query("INSERT INTO {webform} (nid, confirmation, confirmation_format, teaser, submit_notice, submit_text, submit_limit, submit_interval, additional_validate, additional_submit) VALUES (%d, '%s', %d, %d, %d, '%s', %d, %d, '%s', '%s')", $node->nid, $node->webform['confirmation'], $node->webform['confirmation_format'], $node->webform['teaser'], $node->webform['submit_notice'], $node->webform['submit_text'], $node->webform['submit_limit'], $node->webform['submit_interval'], $node->webform['additional_validate'], $node->webform['additional_submit']);
+      
+        // Insert the components into the database. Used with clone.module.
+        if (isset($node->webform['components']) && !empty($node->webform['components'])) {
+          foreach ($node->webform['components'] as $cid => $component) {
+            $component['nid'] = $node->nid;
+            webform_component_insert($component);
+          }
+        }
+      
+        // Set the per-role submission access control.
+        foreach (array_filter($node->webform['roles']) as $rid) {
+          db_query('INSERT INTO {webform_roles} (nid, rid) VALUES (%d, %d)', $node->nid, $rid);
+        }
+        break;
+      case 'update':
+        // Update the webform by deleting existing data and replacing with the new.
+        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_roles} WHERE nid = %d', $node->nid);
+        webform_insert($node);
+        break;
+      case 'delete':
+        // 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->nid, $cid);
+        }
+      
+        // 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_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);
+        break;
+      case 'load':
+        module_load_include('inc', 'webform', 'includes/webform.components');
+        $additions = array();
+
+        if ($webform = db_fetch_array(db_query('SELECT * FROM {webform} WHERE nid = %d', $node->nid))) {
+          $additions['webform'] = $webform;
+
+          $additions['webform']['roles'] = array();
+          $result = db_query('SELECT rid FROM {webform_roles} WHERE nid = %d', $node->nid);
+          while ($role = db_fetch_object($result)) {
+            $additions['webform']['roles'][] = $role->rid;
+          }
 
-  // Insert the components into the database. Used with clone.module.
-  if (isset($node->webform['components']) && !empty($node->webform['components'])) {
-    foreach ($node->webform['components'] as $cid => $component) {
-      $component['nid'] = $node->nid;
-      webform_component_insert($component);
-    }
-  }
+          $additions['webform']['emails'] = array();
+          $result = db_query('SELECT * FROM {webform_emails} WHERE nid = %d', $node->nid);
+          while ($email = db_fetch_array($result)) {
+            $additions['webform']['emails'][$email['eid']] = $email;
+          }
+        }
+        else {
+          $additions['webform'] = array(
+            'confirmation' => '',
+            'confirmation_format' => FILTER_FORMAT_DEFAULT,
+            'teaser' => 0,
+            'submit_notice' => 0,
+            'submit_text' => '',
+            'submit_limit' => -1,
+            'submit_interval' => -1,
+            'additional_validate' => '',
+            'additional_submit' => '',
+            'roles' => array(1, 2),
+            'emails' => array(),
+          );
+        }
 
-  // Set the per-role submission access control.
-  foreach (array_filter($node->webform['roles']) as $rid) {
-    db_query('INSERT INTO {webform_roles} (nid, rid) VALUES (%d, %d)', $node->nid, $rid);
-  }
-}
+        $additions['webform']['components'] = array();
+        $additions['webform']['additional_emails'] = array();
+        $result = db_query('SELECT * FROM {webform_component} WHERE nid = %d ORDER BY weight, name', $node->nid);
+        while ($c = db_fetch_array($result)) {
+          $component =& $additions['webform']['components'][$c['cid']];
+          $component['nid'] = $node->nid;
+          $component['cid'] = $c['cid'];
+          $component['form_key'] = $c['form_key'] ? $c['form_key'] : $c['cid'];
+          $component['name'] = t($c['name']);
+          $component['type'] = $c['type'];
+          $component['value'] = $c['value'];
+          $component['extra'] = unserialize($c['extra']);
+          $component['mandatory'] = $c['mandatory'];
+          $component['email'] = $c['email'];
+          $component['pid'] = $c['pid'];
+          $component['weight'] = $c['weight'];
+          if (isset($component['extra']['email']) && $component['extra']['email']) {
+            $additions['webform']['additional_emails'][$c['cid']] = $c['cid'];
+          }
 
-/**
- * Implementation of hook_update().
- */
-function webform_update($node) {
-  // Update the webform by deleting existing data and replacing with the new.
-  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_roles} WHERE nid = %d', $node->nid);
-  webform_insert($node);
-}
+          webform_component_defaults($component);
+        }
 
-/**
- * Implementation of hook_delete().
- */
-function webform_delete(&$node) {
-  // 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->nid, $cid);
-  }
+        // Organize the components into a fieldset-based order.
+        if (!empty($additions['webform']['components'])) {
+          $component_tree = array();
+          $page_count = 1;
+          _webform_components_tree_build($additions['webform']['components'], $component_tree, 0, $page_count);
+          $additions['webform']['components'] = _webform_components_tree_flatten($component_tree['children']);
+        }
+        return $additions;
+        break;
+      case 'view':
+        global $user;
+
+        $teaser = $a3;
+        $page = $a4;
+        // If a teaser, do not display the form.
+        if ($teaser && !$node->webform['teaser']) {
+          $node->content['teaser'] = array('#value' => check_markup($node->teaser, $node->format, FALSE));
+          return $node;
+        }
 
-  // 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_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);
-}
+        $submission = array();
+        $submission_count = 0;
+        $enabled = TRUE;
+        $logging_in = FALSE;
+        $limit_exceeded = FALSE;
+
+        if ($node->build_mode == NODE_BUILD_PREVIEW) {
+          $additions = webform_load($node);
+          $node->webform['components'] = $additions->webform['components'];
+        }
 
-/**
- * Implementation of hook_load().
- */
-function webform_load($node) {
-  module_load_include('inc', 'webform', 'includes/webform.components');
-  $additions = new stdClass();
+        // When logging in using a form on the same page as a webform node, surpress
+        // output messages so that they don't show up after the user has logged in.
+        // See http://drupal.org/node/239343.
+        if (isset($_POST['op']) && isset($_POST['name']) && isset($_POST['pass'])) {
+          $logging_in = TRUE;
+        }
 
-  if ($webform = db_fetch_array(db_query('SELECT * FROM {webform} WHERE nid = %d', $node->nid))) {
-    $additions->webform = $webform;
+        // Check if the user's role can submit this webform.
+        if (variable_get('webform_submission_access_control', 1)) {
+          $allowed_roles = array();
+          foreach ($node->webform['roles'] as $rid) {
+            $allowed_roles[$rid] = isset($user->roles[$rid]) ? TRUE : FALSE;
+          }
+          if (array_search(TRUE, $allowed_roles) === FALSE && $user->uid != 1) {
+            $enabled = FALSE;
+          }
+        }
+        else {
+          // If not using Webform submission access control, allow for all roles.
+          $allowed_roles = array_keys(user_roles());
+        }
 
-    $additions->webform['roles'] = array();
-    $result = db_query('SELECT rid FROM {webform_roles} WHERE nid = %d', $node->nid);
-    while ($role = db_fetch_object($result)) {
-      $additions->webform['roles'][] = $role->rid;
-    }
+        // Check if the user can add another submission.
+        if ($node->webform['submit_limit'] != -1) { // -1: Submissions are never throttled.
+          module_load_include('inc', 'webform', 'includes/webform.submissions');
 
-    $additions->webform['emails'] = array();
-    $result = db_query('SELECT * FROM {webform_emails} WHERE nid = %d', $node->nid);
-    while ($email = db_fetch_array($result)) {
-      $additions->webform['emails'][$email['eid']] = $email;
-    }
-  }
-  else {
-    $additions->webform = array(
-      'confirmation' => '',
-      'confirmation_format' => FILTER_FORMAT_DEFAULT,
-      'teaser' => 0,
-      'submit_notice' => 0,
-      'submit_text' => '',
-      'submit_limit' => -1,
-      'submit_interval' => -1,
-      'additional_validate' => '',
-      'additional_submit' => '',
-      'roles' => array(1, 2),
-      'emails' => array(),
-    );
-  }
+          if ($limit_exceeded = _webform_submission_limit_check($node)) {
+            $enabled = FALSE;
+          }
+        }
 
-  $additions->webform['components'] = array();
-  $additions->webform['additional_emails'] = array();
-  $result = db_query('SELECT * FROM {webform_component} WHERE nid = %d ORDER BY weight, name', $node->nid);
-  while ($c = db_fetch_array($result)) {
-    $component =& $additions->webform['components'][$c['cid']];
-    $component['nid'] = $node->nid;
-    $component['cid'] = $c['cid'];
-    $component['form_key'] = $c['form_key'] ? $c['form_key'] : $c['cid'];
-    $component['name'] = t($c['name']);
-    $component['type'] = $c['type'];
-    $component['value'] = $c['value'];
-    $component['extra'] = unserialize($c['extra']);
-    $component['mandatory'] = $c['mandatory'];
-    $component['email'] = $c['email'];
-    $component['pid'] = $c['pid'];
-    $component['weight'] = $c['weight'];
-    if (isset($component['extra']['email']) && $component['extra']['email']) {
-      $additions->webform['additional_emails'][$c['cid']] = $c['cid'];
-    }
+        // Get a count of previous submissions by this user.
+        if ($user->uid && (user_access('access own webform submissions') || user_access('access webform results') || user_access('access webform submissions'))) {
+          $submission_count = db_result(db_query('SELECT count(*) FROM {webform_submissions} WHERE nid = %d AND uid = %d', $node->nid, $user->uid));
+        }
 
-    webform_component_defaults($component);
-  }
+        // Render the form and generate the output.
+        $form = drupal_get_form('webform_client_form_'. $node->nid, $node, $submission, $enabled);
+        $output = theme('webform_view', $node, $teaser, $page, $form, $enabled);
+
+        // Remove the surrounding <form> tag if this is a preview.
+        if ($node->build_mode == NODE_BUILD_PREVIEW) {
+          $output = preg_replace('/<\/?form[^>]*>/', '', $output);
+        }
 
-  // Organize the components into a fieldset-based order.
-  if (!empty($additions->webform['components'])) {
-    $component_tree = array();
-    $page_count = 1;
-    _webform_components_tree_build($additions->webform['components'], $component_tree, 0, $page_count);
-    $additions->webform['components'] = _webform_components_tree_flatten($component_tree['children']);
+        // Print out messages for the webform.
+        if (!$node->build_mode == NODE_BUILD_PREVIEW && !$logging_in) {
+          theme('webform_view_messages', $node, $teaser, $page, $submission_count, $limit_exceeded, $allowed_roles);
+        }
+
+        // Add the output to the node.
+        $node = node_prepare($node, $teaser);
+        if (isset($output)) {
+          $node->content['webform'] = array('#value' => $output, '#weight' => 1);
+        }
+        break;
+    }
   }
-  return $additions;
 }
 
 /**
@@ -711,21 +784,8 @@ function webform_link($type, $node = NUL
  * Creates the standard form for editing or creating a webform.
  */
 function webform_form(&$node, &$form_state) {
-  // Set node defaults if empty.
-  if (!isset($node->nid) && !isset($node->webform)) {
-    $node->nid = 0;
-    $additions = webform_load($node);
-    $node->webform = $additions->webform;
-    $node->nid = NULL;
-  }
-
   $form = node_content_form($node, $form_state);
 
-  $form['webform'] = array(
-    '#type' => 'value',
-    '#value' => $node->webform,
-  );
-
   return $form;
 }
 
@@ -733,8 +793,32 @@ function webform_form(&$node, &$form_sta
  * Implementation of hook_form_alter().
  */
 function webform_form_alter(&$form, $form_state, $form_id) {
-  if ($form_id == 'webform_node_form' && empty($form['nid']['#value'])) {
-    $form['buttons']['submit']['#submit'][] = 'webform_form_submit';
+  $webforms = webform_variable_get('webform_node_types');
+  $types = node_get_types();
+  foreach ($types as $type) {
+    if (array_key_exists($type->type, $webforms)) {
+      if ($form_id == $type->type .'_node_form') {
+        if (empty($form['nid']['#value'])) {
+          $form['buttons']['submit']['#submit'][] = 'webform_form_submit';
+          if (!isset($form['#node']->webform)) {
+            $node = $form['#node'];
+            $node->nid = 0;
+            $additions = module_invoke('webform', 'nodeapi', $node, 'load');
+            $form['webform'] = array(
+              '#type' => 'value',
+              '#value' => $additions['webform'],
+            );
+          }
+        }
+        else {
+          $node = $form['#node'];
+          $form['webform'] = array(
+            '#type' => 'value',
+            '#value' => $node->webform,
+          );
+        }
+      }
+    }
   }
 }
 
@@ -750,88 +834,6 @@ function webform_form_submit($form, &$fo
 }
 
 /**
- * Implementation of hook_view().
- */
-function webform_view(&$node, $teaser = 0, $page = 0) {
-  global $user;
-
-  // If a teaser, do not display the form.
-  if ($teaser && !$node->webform['teaser']) {
-    $node->content['teaser'] = array('#value' => check_markup($node->teaser, $node->format, FALSE));
-    return $node;
-  }
-
-  $submission = array();
-  $submission_count = 0;
-  $enabled = TRUE;
-  $logging_in = FALSE;
-  $limit_exceeded = FALSE;
-
-  if ($node->build_mode == NODE_BUILD_PREVIEW) {
-    $additions = webform_load($node);
-    $node->webform['components'] = $additions->webform['components'];
-  }
-
-  // When logging in using a form on the same page as a webform node, surpress
-  // output messages so that they don't show up after the user has logged in.
-  // See http://drupal.org/node/239343.
-  if (isset($_POST['op']) && isset($_POST['name']) && isset($_POST['pass'])) {
-    $logging_in = TRUE;
-  }
-
-  // Check if the user's role can submit this webform.
-  if (variable_get('webform_submission_access_control', 1)) {
-    $allowed_roles = array();
-    foreach ($node->webform['roles'] as $rid) {
-      $allowed_roles[$rid] = isset($user->roles[$rid]) ? TRUE : FALSE;
-    }
-    if (array_search(TRUE, $allowed_roles) === FALSE && $user->uid != 1) {
-      $enabled = FALSE;
-    }
-  }
-  else {
-    // If not using Webform submission access control, allow for all roles.
-    $allowed_roles = array_keys(user_roles());
-  }
-
-  // Check if the user can add another submission.
-  if ($node->webform['submit_limit'] != -1) { // -1: Submissions are never throttled.
-    module_load_include('inc', 'webform', 'includes/webform.submissions');
-
-    if ($limit_exceeded = _webform_submission_limit_check($node)) {
-      $enabled = FALSE;
-    }
-  }
-
-  // Get a count of previous submissions by this user.
-  if ($user->uid && (user_access('access own webform submissions') || user_access('access webform results') || user_access('access webform submissions'))) {
-    $submission_count = db_result(db_query('SELECT count(*) FROM {webform_submissions} WHERE nid = %d AND uid = %d', $node->nid, $user->uid));
-  }
-
-  // Render the form and generate the output.
-  $form = drupal_get_form('webform_client_form_'. $node->nid, $node, $submission, $enabled);
-  $output = theme('webform_view', $node, $teaser, $page, $form, $enabled);
-
-  // Remove the surrounding <form> tag if this is a preview.
-  if ($node->build_mode == NODE_BUILD_PREVIEW) {
-    $output = preg_replace('/<\/?form[^>]*>/', '', $output);
-  }
-
-  // Print out messages for the webform.
-  if (!$node->build_mode == NODE_BUILD_PREVIEW && !$logging_in) {
-    theme('webform_view_messages', $node, $teaser, $page, $submission_count, $limit_exceeded, $allowed_roles);
-  }
-
-  // Add the output to the node.
-  $node = node_prepare($node, $teaser);
-  if (isset($output)) {
-    $node->content['webform'] = array('#value' => $output, '#weight' => 1);
-  }
-
-  return $node;
-}
-
-/**
  * Output the Webform into the node content.
  *
  * @param $node
@@ -932,6 +934,24 @@ function webform_mail($key, &$message, $
  * Menu callback for admin/webform/settings.
  */
 function webform_admin_settings() {
+  $types = node_get_types();
+  foreach ($types as $type) {
+    $type_options[$type->type] = $type->name;
+  }
+  $default_types = webform_variable_get('webform_node_types');
+  foreach ($default_types as $key => $type) {
+    if ($type) {
+      $default_options[] = $key;
+    }
+  }
+  $form['node_types'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Webform Node Types'),
+    '#description' => t('Webform allows you to enable the webform components for any content type.  Choose the types on which you would like to associate webform components.'),
+    '#options' => $type_options,
+    '#default_value' => $default_options,
+  );
+
   module_load_include('inc', 'webform', 'includes/webform.export');
 
   $form['components'] = array(
@@ -1052,10 +1072,15 @@ function webform_admin_settings() {
 
   $form = system_settings_form($form);
   $form['#theme'] = 'webform_admin_settings';
+  $form['#submit'][] = 'webform_admin_settings_submit';
 
   return $form;
 }
 
+function webform_admin_settings_submit($form, &$form_state) {
+  variable_set('webform_node_types', $form_state['values']['node_types']);
+}
+
 function theme_webform_admin_settings($form) {
   // Format the components into a table.
   foreach (element_children($form['components']) as $key) {
@@ -1933,6 +1958,9 @@ function webform_variable_get($variable)
     case 'webform_default_subject':
       $result = variable_get('webform_default_subject', t('Form submission from: %title'));
       break;
+    case 'webform_node_types':
+      $result = variable_get('webform_node_types', array('webform' => 'Webform'));
+      break;
   }
   return $result;
 }
