Index: webform.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/webform/webform.module,v retrieving revision 1.197 diff -u -r1.197 webform.module --- webform.module 3 Mar 2010 02:51:37 -0000 1.197 +++ webform.module 8 Mar 2010 00:55:53 -0000 @@ -18,7 +18,7 @@ function webform_help($section = 'admin/help#webform', $arg = NULL) { $output = ''; switch ($section) { - case 'admin/settings/webform': + case 'admin/config/content/webform': $type_list = webform_admin_type_list(); $output = t('Webform enables nodes to have attached forms and questionnaires.'); if ($type_list) { @@ -37,10 +37,10 @@ $types = webform_admin_type_list(); if (empty($types)) { $types = t('Webform-enabled piece of content'); - $types_message = t('Webform is currently not enabled on any content types.') . ' ' . t('Visit the Webform settings page and enable Webform on at least one content type.', array('!url' => url('admin/settings/webform'))); + $types_message = t('Webform is currently not enabled on any content types.') . ' ' . t('Visit the Webform settings page and enable Webform on at least one content type.', array('!url' => url('admin/config/content/webform'))); } else { - $types_message = t('Optional: Enable Webform on multiple types by visiting the Webform settings page.', array('!url' => array('admin/settings/webform'))); + $types_message = t('Optional: Enable Webform on multiple types by visiting the Webform settings page.', array('!url' => array('admin/config/content/webform'))); } $output = t("
This module lets you create forms or questionnaires and define their content. Submissions from these forms are stored in the database and optionally also sent by e-mail to a predefined address.
Here is how to create one:
@@ -89,7 +89,7 @@ ); // Admin Settings. - $items['admin/settings/webform'] = array( + $items['admin/config/content/webform'] = array( 'title' => 'Webform settings', 'page callback' => 'drupal_get_form', 'page arguments' => array('webform_admin_settings'), @@ -471,15 +471,31 @@ /** * Implementation of hook_perm(). */ -function webform_perm() { +function webform_permission() { return array( - 'access all webform results', - 'access own webform results', - 'edit all webform submissions', - 'delete all webform submissions', - 'access own webform submissions', - 'edit own webform submissions', - 'delete own webform submissions', + 'access all webform results' => array( + 'title' => t('Access all webform results'), + 'description' => t('Grants access to the "Results" tab on all webform nodes. Generally an administrative permission.'), + ), + 'access own webform results' => array( + 'title' => t('Access own webform results'), + 'description' => t('Grants access to the "Results" tab on webform nodes to the author.'), + ), + 'edit all webform submissions' => array( + 'title' => t('Edit all webform submissions'), + ), + 'delete all webform submissions' => array( + 'title' => t('Delete all webform submissions'), + ), + 'access own webform submissions' => array( + 'title' => t('Access own webform submissions'), + ), + 'edit own webform submissions' => array( + 'title' => t('Edit own webform submissions'), + ), + 'delete own webform submissions' => array( + 'title' => t('Delete own webform submissions'), + ), ); } @@ -490,124 +506,124 @@ $theme = array( // webform.module. 'webform_view' => array( - 'arguments' => array('node' => NULL, 'teaser' => NULL, 'page' => NULL, 'form' => NULL, 'enabled' => NULL), + 'render element' => 'webform', ), 'webform_view_messages' => array( - 'arguments' => array('node' => NULL, 'teaser' => NULL, 'page' => NULL, 'submission_count' => NULL, 'limit_exceeded' => NULL, 'allowed_roles' => NULL), + 'variables' => array('node' => NULL, 'teaser' => NULL, 'page' => NULL, 'submission_count' => NULL, 'limit_exceeded' => NULL, 'allowed_roles' => NULL), ), 'webform_form' => array( - 'arguments' => array('form' => NULL), + 'render element' => 'form', 'template' => 'templates/webform-form', 'pattern' => 'webform_form_[0-9]+', ), 'webform_advanced_submit_limit_form' => array( - 'arguments' => array('form' => NULL), + 'render element' => 'form', 'file' => 'includes/webform.pages.inc', ), 'webform_confirmation' => array( - 'arguments' => array('node' => NULL, 'sid' => NULL), + 'variables' => array('node' => NULL, 'sid' => NULL), 'template' => 'templates/webform-confirmation', 'pattern' => 'webform_confirmation_[0-9]+', ), 'webform_element' => array( - 'arguments' => array('element' => NULL, 'value' => NULL), + 'render element' => 'element', ), 'webform_element_text' => array( - 'arguments' => array('element' => NULL, 'value' => NULL), + 'render element' => 'element', ), 'webform_mail_message' => array( - 'arguments' => array('node' => NULL, 'submission' => NULL, 'cid' => NULL), + 'variables' => array('node' => NULL, 'submission' => NULL, 'cid' => NULL), 'template' => 'templates/webform-mail', 'pattern' => 'webform_mail(_[0-9]+)?', ), 'webform_mail_headers' => array( - 'arguments' => array('node' => NULL, 'submission' => NULL, 'cid' => NULL), + 'variables' => array('node' => NULL, 'submission' => NULL, 'cid' => NULL), 'pattern' => 'webform_mail_headers_[0-9]+', ), 'webform_token_help' => array( - 'arguments' => array(), + 'variables' => array(), ), // webform.admin.inc. 'webform_admin_settings' => array( - 'arguments' => array('form' => NULL), + 'render element' => 'form', 'file' => 'includes/webform.admin.inc', ), 'webform_admin_content' => array( - 'arguments' => array('nodes' => NULL), + 'variables' => array('nodes' => NULL), 'file' => 'includes/webform.admin.inc', ), // webform.emails.inc. 'webform_emails_form' => array( - 'arguments' => array('form' => NULL), + 'render element' => 'form', 'file' => 'includes/webform.emails.inc', ), 'webform_email_add_form' => array( - 'arguments' => array('form' => NULL), + 'render element' => 'form', 'file' => 'includes/webform.emails.inc', ), 'webform_email_edit_form' => array( - 'arguments' => array('form' => NULL), + 'render element' => 'form', 'file' => 'includes/webform.emails.inc', ), // webform.components.inc. 'webform_components_page' => array( - 'arguments' => array('node' => NULL, 'mode' => NULL, 'form' => NULL), + 'variables' => array('node' => NULL, 'mode' => NULL, 'form' => NULL), 'file' => 'includes/webform.components.inc', ), 'webform_components_form' => array( - 'arguments' => array('form' => NULL), + 'render element' => 'form', 'file' => 'includes/webform.components.inc', ), 'webform_component_select' => array( - 'arguments' => array('element' => NULL), + 'render element' => 'element', 'file' => 'includes/webform.components.inc', ), // webform.report.inc. 'webform_results_per_page' => array( - 'arguments' => array('total_count' => NULL, 'pager_count' => NULL), + 'variables' => array('total_count' => NULL, 'pager_count' => NULL), 'file' => 'includes/webform.report.inc', ), 'webform_results_submissions_header' => array( - 'arguments' => array('node' => NULL), + 'variables' => array('node' => NULL), 'file' => 'includes/webform.report.inc', ), 'webform_results_submissions' => array( - 'arguments' => array('node' => NULL, 'submissions' => NULL, 'total_count' => NULL, 'pager_count' => NULL), + 'variables' => array('node' => NULL, 'submissions' => NULL, 'total_count' => 0, 'pager_count' => 0), 'file' => 'includes/webform.report.inc', ), 'webform_results_table_header' => array( - 'arguments' => array('node' => NULL), + 'variables' => array('node' => NULL), 'file' => 'includes/webform.report.inc', ), 'webform_results_table' => array( - 'arguments' => array('node' => NULL, 'components' => NULL, 'submissions' => NULL, 'node' => NULL, 'total_count' => NULL, 'pager_count' => NULL), + 'variables' => array('node' => NULL, 'components' => NULL, 'submissions' => NULL, 'node' => NULL, 'total_count' => NULL, 'pager_count' => NULL), 'file' => 'includes/webform.report.inc', ), 'webform_results_download_form' => array( - 'arguments' => array('form' => NULL), + 'render element' => 'form', 'file' => 'includes/webform.report.inc', ), 'webform_results_download_select_format' => array( - 'arguments' => array('element' => NULL), + 'render element' => 'element', 'file' => 'includes/webform.report.inc', ), 'webform_results_analysis' => array( - 'arguments' => array('node' => NULL, 'data' => NULL, 'sids' => array(), 'component' => NULL), + 'variables' => array('node' => NULL, 'data' => NULL, 'sids' => array(), 'component' => NULL), 'file' => 'includes/webform.report.inc', ), // webform.submissions.inc 'webform_submission_page' => array( - 'arguments' => array('node' => NULL, 'submission' => NULL, 'submission_content' => NULL, 'submission_navigation' => NULL, 'submission_information' => NULL), + 'variables' => array('node' => NULL, 'submission' => NULL, 'submission_content' => NULL, 'submission_navigation' => NULL, 'submission_information' => NULL), 'template' => 'templates/webform-submission-page', 'file' => 'includes/webform.submissions.inc', ), 'webform_submission_information' => array( - 'arguments' => array('node' => NULL, 'submission' => NULL), + 'variables' => array('node' => NULL, 'submission' => NULL), 'template' => 'templates/webform-submission-information', 'file' => 'includes/webform.submissions.inc', ), 'webform_submission_navigation' => array( - 'arguments' => array('node' => NULL, 'submission' => NULL, 'mode' => NULL), + 'variables' => array('node' => NULL, 'submission' => NULL, 'mode' => NULL), 'template' => 'templates/webform-submission-navigation', 'file' => 'includes/webform.submissions.inc', ), @@ -777,6 +793,8 @@ /** * Implementation of hook_node_type(). + * + * Not a real hook in Drupal 7. Re-used for consistency with the D6 version. */ function webform_node_type($op, $info) { $webform_types = webform_variable_get('webform_node_types'); @@ -794,31 +812,17 @@ } /** - * Implementation of hook_nodeapi(). + * Implementation of hook_node_type_update(). */ -function webform_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { - if (!in_array($node->type, webform_variable_get('webform_node_types'))) { - return; - } +function webform_node_type_update($info) { + webform_node_type('update', $info); +} - switch ($op) { - case 'insert': - webform_node_insert($node); - break; - case 'update': - webform_node_update($node); - break; - case 'delete': - webform_node_delete($node); - break; - case 'prepare': - webform_node_prepare($node); - break; - case 'load': - return webform_node_load($node); - case 'view': - return webform_node_view($node, $teaser, $page); - } +/** + * Implementation of hook_node_type_delete(). + */ +function webform_node_type_delete($info) { + webform_node_type('delete', $info); } /** @@ -832,7 +836,22 @@ module_load_include('inc', 'webform', 'includes/webform.components'); // Insert the webform. - db_query("INSERT INTO {webform} (nid, confirmation, confirmation_format, redirect_url, teaser, allow_draft, submit_notice, submit_text, submit_limit, submit_interval, additional_validate, additional_submit) VALUES (%d, '%s', %d, '%s', %d, %d, %d, '%s', %d, %d, '%s', '%s')", $node->nid, $node->webform['confirmation'], $node->webform['confirmation_format'], $node->webform['redirect_url'], $node->webform['teaser'], $node->webform['allow_draft'], $node->webform['submit_notice'], $node->webform['submit_text'], $node->webform['submit_limit'], $node->webform['submit_interval'], $node->webform['additional_validate'], $node->webform['additional_submit']); + db_insert('webform') + ->fields(array( + 'nid' => $node->nid, + 'confirmation' => $node->webform['confirmation'], + 'confirmation_format' => $node->webform['confirmation_format'], + 'redirect_url' => $node->webform['redirect_url'], + 'teaser' => $node->webform['teaser'], + 'allow_draft' => $node->webform['allow_draft'], + 'submit_notice' => $node->webform['submit_notice'], + 'submit_text' => $node->webform['submit_text'], + 'submit_limit' => $node->webform['submit_limit'], + 'submit_interval' => $node->webform['submit_interval'], + 'additional_validate' => $node->webform['additional_validate'], + 'additional_submit' => $node->webform['additional_submit'], + )) + ->execute(); // Insert the components into the database. Used with clone.module. if (isset($node->webform['components']) && !empty($node->webform['components'])) { @@ -844,7 +863,12 @@ // 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); + db_insert('webform_roles') + ->fields(array( + 'nid' => $node->nid, + 'rid' => $rid + )) + ->execute(); } } @@ -857,9 +881,9 @@ } // 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); + db_delete('webform')->condition('nid', $node->nid)->execute(); + db_delete('webform_component')->condition('nid', $node->nid)->execute(); + db_delete('webform_roles')->condition('nid', $node->nid)->execute(); webform_node_insert($node); } @@ -878,96 +902,108 @@ } // 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); + db_delete('webform')->condition('nid', $node->nid)->execute(); + db_delete('webform_component')->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(); } /** * Implementation of hook_node_prepare(). */ -function webform_node_prepare(&$node) { - if (!isset($node->webform)) { - $additions = webform_node_load($node); - $node->webform = $additions['webform']; +function webform_node_prepare($node) { + $webform_types = webform_variable_get('webform_node_types'); + if (in_array($node->type, $webform_types) && !isset($node->webform)) { + $nodes = array($node->nid => $node); + $types = array($node->type); + webform_node_load($nodes, $types); } } /** * Implementation of hook_node_load(). */ -function webform_node_load($node) { +function webform_node_load(&$nodes, $types) { module_load_include('inc', 'webform', 'includes/webform.components'); - $additions = array(); - if (isset($node->nid) && ($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; - } - - $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; - $additions['webform']['emails'][$email['eid']]['excluded_components'] = array_filter(explode(',', $email['excluded_components'])); - } - } - else { - $additions['webform'] = array( - 'confirmation' => '', - 'confirmation_format' => FILTER_FORMAT_DEFAULT, - 'redirect_url' => '', - 'teaser' => 0, - 'allow_draft' => 0, - 'submit_notice' => 0, - 'submit_text' => '', - 'submit_limit' => -1, - 'submit_interval' => -1, - 'additional_validate' => '', - 'additional_submit' => '', - 'roles' => array(1, 2), - 'emails' => array(), - ); + // Quick check to see if we need to do anything at all for these nodes. + $webform_types = webform_variable_get('webform_node_types'); + if (count(array_intersect($types, $webform_types)) == 0) { + return; } - $additions['webform']['components'] = array(); - - // If we don't have a NID yet, no point in doing additional queries. - if (!isset($node->nid)) { - return $additions; - } - - $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['pid'] = $c['pid']; - $component['weight'] = $c['weight']; - - webform_component_defaults($component); + // Load defaults for all nodes that are webforms. + foreach ($nodes as $nid => $node) { + if (in_array($node->type, $webform_types)) { + $nodes[$nid]->webform = array( + 'confirmation' => '', + 'confirmation_format' => filter_default_format(), + 'redirect_url' => '', + 'teaser' => 0, + 'allow_draft' => 0, + 'submit_notice' => 0, + 'submit_text' => '', + 'submit_limit' => -1, + 'submit_interval' => -1, + 'additional_validate' => '', + 'additional_submit' => '', + 'roles' => array(1, 2), + 'emails' => array(), + ); + } } - // 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']); + // Select all webforms that match these node IDs. + $result = db_select('webform') + ->fields('webform') + ->condition('nid', array_keys($nodes), 'IN') + ->execute() + ->fetchAllAssoc('nid', PDO::FETCH_ASSOC); + + foreach ($result as $nid => $webform) { + // Load the basic information for each node. + $nodes[$nid]->webform = $webform; + $nodes[$nid]->webform['roles'] = db_select('webform_roles') + ->fields('webform_roles', array('rid')) + ->condition('nid', $nid) + ->execute() + ->fetchCol(); + $nodes[$nid]->webform['emails'] = db_select('webform_emails') + ->fields('webform_emails') + ->condition('nid', $nid) + ->execute() + ->fetchAllAssoc('eid', PDO::FETCH_ASSOC); + + // Unserialize the exclude component list for e-mails. + foreach ($nodes[$nid]->webform['emails'] as $eid => $email) { + $nodes[$nid]->webform['emails'][$eid]['excluded_components'] = array_filter(explode(',', $email['excluded_components'])); + } + + // Load components for each node. + $nodes[$nid]->webform['components'] = db_select('webform_component') + ->fields('webform_component') + ->condition('nid', $nid) + ->orderBy('weight') + ->orderBy('name') + ->execute() + ->fetchAllAssoc('cid', PDO::FETCH_ASSOC); + + // Do a little cleanup on each component. + foreach ($nodes[$nid]->webform['components'] as $cid => $component) { + $nodes[$nid]->webform['components'][$cid]['nid'] = $nid; + $nodes[$nid]->webform['components'][$cid]['extra'] = unserialize($component['extra']); + webform_component_defaults($nodes[$nid]->webform['components'][$cid]); + } + + // Organize the components into a fieldset-based order. + if (!empty($nodes[$nid]->webform['components'])) { + $component_tree = array(); + $page_count = 1; + _webform_components_tree_build($nodes[$nid]->webform['components'], $component_tree, 0, $page_count); + $nodes[$nid]->webform['components'] = _webform_components_tree_flatten($component_tree['children']); + } } - return $additions; } /** @@ -1022,8 +1058,13 @@ /** * Implementation of hook_node_view(). */ -function webform_node_view(&$node, $teaser, $page) { +function webform_node_view(&$node, $view_mode) { global $user; + + // Set teaser and page variables a la Drupal 6. + $teaser = $view_mode == 'teaser'; + $page = arg(0) == 'node' && arg(1) == $node->nid; + // If a teaser or a new node (during preview), do not display the form. if (($teaser && !$node->webform['teaser']) || empty($node->nid)) { return; @@ -1087,23 +1128,27 @@ // Render the form and generate the output. $form = drupal_get_form('webform_client_form_' . $node->nid, $node, $submission, $is_draft); - $output = theme('webform_view', $node, $teaser, $page, $form, $enabled); // Remove the surrounding