Index: mollom.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/mollom/mollom.module,v retrieving revision 1.2.2.89 diff -u -r1.2.2.89 mollom.module --- mollom.module 13 Aug 2009 16:21:08 -0000 1.2.2.89 +++ mollom.module 21 Sep 2009 16:01:35 -0000 @@ -119,6 +119,26 @@ 'file' => 'mollom.pages.inc', 'type' => MENU_CALLBACK, ); + + // Settings form for configuring Webform forms + if (module_exists('webform')) { + + // Provide a Default Menu + $items['admin/settings/mollom/settings'] = array( + 'title' => t('Settings'), + 'type' => MENU_DEFAULT_LOCAL_TASK + ); + + // Provide a Settings for Webform Menu + $items['admin/settings/mollom/webform'] = array( + 'title' => t('Webform'), + 'description' => t('Add webforms to Mollom'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('mollom_webform_settings'), + 'access arguments' => array('administer site configuration'), + 'type' => MENU_LOCAL_TASK, + ); + } return $items; } @@ -192,7 +212,7 @@ function mollom_mail_alter(&$message) { // Only attach the Mollom report link to mails sent by actual users and not // any mails sent by Drupal since they should never be reported as spam. - $valid_ids = array('page_mail', 'page_copy', 'user_mail', 'user_copy'); + $valid_ids = array('page_mail', 'page_copy', 'user_mail', 'user_copy', 'webform_submission'); if (isset($GLOBALS['mollom_response']['session_id']) && in_array($message['id'], $valid_ids)) { $report_link = t('Report as inappropriate: @link', array('@link' => url('mollom/contact/'. $GLOBALS['mollom_response']['session_id'], array('absolute' => TRUE)))); // The _mail_alter hook seems to accept both arrays as strings so we @@ -458,6 +478,23 @@ 'mode' => MOLLOM_MODE_ANALYSIS, ); } + + // Add webforms that are set as protected + if (module_exists('webform')) { + $sql = "SELECT nid, title FROM {node} WHERE type = '%s' 'webform'"; + $result = db_query($sql); + + while($row = db_fetch_object($result)) { + $settings = variable_get("mollom_webform_settings_$row->nid", NULL); + if($settings['enabled']) { + + $forms["webform_client_form_$row->nid"] = array( + 'name' => $row->title . ' (webform)', + 'mode' => MOLLOM_MODE_ANALYSIS, + ); + } + } + } } return $forms; @@ -631,6 +668,9 @@ 'mollom' => array( 'arguments' => array('element' => NULL), ), + 'mollom_webform_settings' => array( + 'arguments' => array('form' => NULL), + ), ); } @@ -659,6 +699,13 @@ $data = mollom_data_node_form($form_state['values']); } else { + // Check if form is created by webform.module + $pos = strpos($form_id, 'webform_client_form_'); + // The webform.module forms use dynamic form IDs so must use a special + // case for these. + if ($pos !==FALSE) { + $data = mollom_data_webform_form($form_state['values']); + } $function = 'mollom_data_'. $form_id; if (function_exists($function)) { $data = $function($form_state['values']); @@ -885,7 +932,208 @@ } /** - * This helper function is used by developers to debug the form API workflow in this module. + * Webform settings form + */ + +function mollom_webform_settings($form_state) { + + $sql = "SELECT nid FROM {node} WHERE type = '%s' 'webform'"; + $result = db_query($sql); + + $webforms = array(); + while($row = db_fetch_object($result)) { + $node = node_load($row->nid); + + $components = array(); + + if (is_array($node->webformcomponents)) { // if we are using webform 1 + foreach($node->webformcomponents as $component) { + $components[$component['form_key']] = $component['name']; + } + } elseif (is_array($node->webform['components'])) { // if we are using webform 2 + foreach($node->webform['components'] as $component) { + $components[$component['form_key']] = $component['name']; + } + } + + $webforms[$node->nid] = array( + 'title' => check_plain($node->title), + 'components' => $components, + ); + + } + + $form = array(); + + $form['#tree'] = TRUE; + $form['#prefix'] = '
To use Mollom with Webform you need to map the fields in your webform to the fields sent to Mollom for SPAM evaluation. You must specify a value for the submission "body", but should provide mappings for as many fields as possible. Just because a webform is shown as protected on this page, it does not mean it is set to protected on the Mollom settings page.
'; + + foreach($webforms as $nid => $webform) { + + $webform['components'] = array_merge(array(t('Empty')), $webform['components']); + + $default = variable_get("mollom_webform_settings_$nid", array()); + $form[$nid]['webform'] = array( + '#type' => 'markup', + '#title' => t('Webform'), + '#value' => $webform['title'], + ); + $form[$nid]['title'] = array( + '#type' => 'select', + '#title' => t('Title'), + '#default_value' => isset($default['title']) ? $default['title'] : 0, + '#multiple' => FALSE, + '#options' => $webform['components'], + ); + $form[$nid]['body'] = array( + '#type' => 'select', + '#title' => t('Body *'), + '#default_value' => isset($default['body']) ? $default['body'] : 0, + '#multiple' => FALSE, + '#options' => $webform['components'], + ); + $form[$nid]['name'] = array( + '#type' => 'select', + '#title' => t('Name'), + '#default_value' => isset($default['name']) ? $default['name'] : 0, + '#multiple' => FALSE, + '#options' => $webform['components'], + ); + $form[$nid]['mail'] = array( + '#type' => 'select', + '#title' => t('Mail'), + '#default_value' => isset($default['mail']) ? $default['mail'] : 0, + '#multiple' => FALSE, + '#options' => $webform['components'], + ); + $form[$nid]['url'] = array( + '#type' => 'select', + '#title' => t('URL'), + '#default_value' => isset($default['url']) ? $default['url'] : 0, + '#multiple' => FALSE, + '#options' => $webform['components'], + ); + $form[$nid]['enabled'] = array( + '#type' => 'checkbox', + '#title' => t('Protected'), + '#default_value' => $default['enabled'], + ); + + } + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Save configuration'), + ); + + return $form; +} + +function theme_mollom_webform_settings($form) { + //Headers for webform settings table + $header = array( + array('data' => t('Protected'), 'class' => 'checkbox'), + t('Webform'), + t('Title'), + t('Body *'), + t('Name'), + t('Mail'), + t('URL') + ); + + //Individual table row for each webform without element titles + $rows = array(); + foreach (element_children($form) as $nid) { + if(is_array($form[$nid]['webform'])) { + + unset($form[$nid]['webform']['#title']); + unset($form[$nid]['title']['#title']); + unset($form[$nid]['body']['#title']); + unset($form[$nid]['name']['#title']); + unset($form[$nid]['mail']['#title']); + unset($form[$nid]['url']['#title']); + unset($form[$nid]['enabled']['#title']); + + //Check webform published status + $node = node_load($nid); + if ($node->status == 0) { + $status = ' (unpublished)'; + } + else { + $status = ''; + } + + $row = array( + array('data' => drupal_render($form[$nid]['enabled']), 'class' => 'checkbox'), + '' . drupal_render($form[$nid]['webform']) . '' . $status, + array('data' => drupal_render($form[$nid]['title']), 'class' => 'select'), + array('data' => drupal_render($form[$nid]['body']), 'class' => 'select'), + array('data' => drupal_render($form[$nid]['name']), 'class' => 'select'), + array('data' => drupal_render($form[$nid]['mail']), 'class' => 'select'), + array('data' => drupal_render($form[$nid]['url']), 'class' => 'select'), + ); + $rows[] = $row; + } + } + $output = theme('table', $header, $rows); + $output .= drupal_render($form); + return $output; +} + +function mollom_webform_settings_validate($form, &$form_state) { + foreach($form_state['values'] as $nid => $webform) { + if(is_array($form[$nid]['webform'])) { // it's a webform + if(empty($form_state['values'][$nid]['body']) && !empty($form_state['values'][$nid]['enabled'])) { + form_set_error("$nid][body", t('You must specify a webform field mapping for the submission body for the webform ' . drupal_render($form[$nid]['webform']))); + } + } + } +} + +function mollom_webform_settings_submit($form, &$form_state) { + foreach($form_state['values'] as $nid => $webform) { + if(is_array($form[$nid]['webform'])) { // it's a webform + $settings = array( + 'title' => $webform['title'], + 'name' => $webform['name'], + 'body' => $webform['body'], + 'mail' => $webform['mail'], + 'url' => $webform['url'], + 'enabled' => $webform['enabled'], + ); + variable_set("mollom_webform_settings_$nid", $settings); + + if(!$settings['enabled']) { + variable_set("mollom_webform_client_form_$nid", 0); + } + } + } + drupal_set_message(t('Your settings have been saved. Remember to select the preferred Mollom protection method on the Mollom settings page.')); +} + + /** + * This function will be called by mollom_validate to prepare the + * XML-RPC data from the webform submission form's $form_values ... + */ +function mollom_data_webform_form(&$form_state) { + global $user; + + $settings = variable_get("mollom_webform_settings_".$form_state['details']['nid'], NULL); + + $submitted = $form_state['submitted']; + $data = array( + 'post_title' => isset($submitted[$settings['title']]) && !empty($submitted[$settings['title']]) ? $submitted[$settings['title']] : '', + 'post_body' => isset($submitted[$settings['body']]) && !empty($submitted[$settings['body']]) ? $submitted[$settings['body']] : '', + 'author_name' => isset($submitted[$settings['name']]) && !empty($submitted[$settings['name']]) ? $submitted[$settings['name']] : '', + 'author_mail' => isset($submitted[$settings['mail']]) && !empty($submitted[$settings['mail']]) ? $submitted[$settings['mail']] : '', + 'author_url' => isset($submitted[$settings['url']]) && !empty($submitted[$settings['url']]) ? $submitted[$settings['url']] : '', + 'author_id' => $user->uid > 0 ? $user->uid : NULL, + 'author_ip' => $form_state['values']['nid'] ? '' : ip_address(), + ); + return $data; +} + +/** * This helper function is used by developers to debug the form API workflow in this module. * Uncomment the function body to activate. */ function _mollom_debug($message) {