? 690894_webform_start_end_date.patch
Index: webform.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform/webform.install,v
retrieving revision 1.40.2.22
diff -u -p -r1.40.2.22 webform.install
--- webform.install	18 Oct 2010 08:08:59 -0000	1.40.2.22
+++ webform.install	21 Dec 2010 13:45:58 -0000
@@ -85,6 +85,16 @@ function webform_schema() {
         'not null' => TRUE,
         'default' => -1,
       ),
+      'start_date' => array(
+        'description' => 'Start date for form submissions.',
+        'type' => 'int',
+        'default' => NULL,
+      ),
+      'end_date' => array(
+        'description' => 'End date for form submissions.',
+        'type' => 'int',
+        'default' => NULL,
+      ),
     ),
     'primary key' => array('nid'),
   );
@@ -1259,6 +1269,29 @@ function webform_update_6322() {
 }
 
 /**
+ * Add fields for scheduling open/close of form submissions.
+ */
+function webform_update_6323() {
+  $ret = array();
+
+  // Safety check to prevent re-adding existing column.
+  if (db_column_exists('webform', 'start_date')) {
+    return $ret;
+  }
+  // Safety check to prevent re-adding existing column.
+  if (db_column_exists('webform', 'end_date')) {
+    return $ret;
+  }
+
+  // Add the new start & end date columns.
+  db_add_field($ret, 'webform', 'start_date', array('type' => 'int', 'length' => 11));
+  db_add_field($ret, 'webform', 'end_date', array('type' => 'int', 'length' => 11));
+
+  return $ret;
+}
+
+
+/**
  * Recursively delete all files and folders in the specified filepath, then
  * delete the containing folder.
  *
Index: webform.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform/webform.module,v
retrieving revision 1.196.2.69
diff -u -p -r1.196.2.69 webform.module
--- webform.module	5 Nov 2010 01:05:50 -0000	1.196.2.69
+++ webform.module	21 Dec 2010 13:45:58 -0000
@@ -495,7 +495,7 @@ function webform_theme() {
       'arguments' => array('node' => NULL, 'teaser' => NULL, 'page' => NULL, 'form' => NULL, 'enabled' => NULL),
     ),
     'webform_view_messages' => array(
-      'arguments' => array('node' => NULL, 'teaser' => NULL, 'page' => NULL, 'submission_count' => NULL, 'limit_exceeded' => NULL, 'allowed_roles' => NULL),
+      'arguments' => array('node' => NULL, 'teaser' => NULL, 'page' => NULL, 'submission_count' => NULL, 'limit_exceeded' => NULL, 'allowed_roles' => NULL, 'date_disabled' => NULL),
     ),
     'webform_form' => array(
       'arguments' => array('form' => NULL),
@@ -1173,6 +1173,8 @@ function webform_node_view(&$node, $teas
   $enabled = TRUE;
   $logging_in = FALSE;
   $limit_exceeded = FALSE;
+  $date_disabled = FALSE;
+  $allowed_roles = array();
 
   // 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.
@@ -1181,19 +1183,29 @@ function webform_node_view(&$node, $teas
     $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;
-    }
+  // Make sure that the form isn't expired (having an end date previous to today)
+  // or not yet active (begin date after today)
+  $start = $node->webform['start_date']; // 0 if unset.
+  $end = $node->webform['end_date'];
+  $now = time();
+  if (($start && $start > $now) || ($end && $end < $now)) {
+    $date_disabled = TRUE;
+    $enabled = FALSE;
   }
   else {
-    // If not using Webform submission access control, allow for all roles.
-    $allowed_roles = array_keys(user_roles());
+    // Check if the user's role can submit this webform.
+    if (variable_get('webform_submission_access_control', 1)) {
+      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.
@@ -1235,7 +1247,7 @@ function webform_node_view(&$node, $teas
 
   // Print out messages for the webform.
   if ($node->build_mode != NODE_BUILD_PREVIEW && !isset($node->webform_block) && !$logging_in) {
-    theme('webform_view_messages', $node, $teaser, $page, $submission_count, $limit_exceeded, $allowed_roles);
+    theme('webform_view_messages', $node, $teaser, $page, $submission_count, $limit_exceeded, $allowed_roles, $date_disabled);
   }
 
   if (isset($output)) {
@@ -1283,8 +1295,10 @@ function theme_webform_view($node, $teas
  *   Boolean value if the submission limit for this user has been exceeded.
  * @param $allowed_roles
  *   A list of user roles that are allowed to submit this webform.
+ * @param $date_disabled
+ *   Boolean value if submissions are closed due to date settings.
  */
-function theme_webform_view_messages($node, $teaser, $page, $submission_count, $limit_exceeded, $allowed_roles) {
+function theme_webform_view_messages($node, $teaser, $page, $submission_count, $limit_exceeded, $allowed_roles, $date_disabled) {
   global $user;
 
   $type = 'notice';
@@ -1312,6 +1326,10 @@ function theme_webform_view_messages($no
       $message = t('You do not have permission to view this form.');
     }
   }
+  // Form submission disabled by date setting.
+  elseif ($date_disabled) {
+    $message = t('Submissions for this form are currently closed.');
+  }
 
   // If the user has exceeded the limit of submissions, explain the limit.
   elseif ($limit_exceeded && !$cached) {
@@ -1827,7 +1845,7 @@ function webform_client_form_validate($f
     module_load_include('inc', 'webform', 'includes/webform.submissions');
 
     if (!$finished && $limit_exceeded = _webform_submission_limit_check($node)) {
-      $error = theme('webform_view_messages', $node, 0, 1, 0, $limit_exceeded, array_keys(user_roles()));
+      $error = theme('webform_view_messages', $node, 0, 1, 0, $limit_exceeded, array_keys(user_roles()), FALSE);
       form_set_error('', $error);
       return;
     }
Index: includes/webform.pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform/includes/webform.pages.inc,v
retrieving revision 1.10.2.4
diff -u -p -r1.10.2.4 webform.pages.inc
--- includes/webform.pages.inc	17 Oct 2010 18:53:09 -0000	1.10.2.4
+++ includes/webform.pages.inc	21 Dec 2010 13:45:58 -0000
@@ -7,10 +7,13 @@
  * Menu callbacks and functions for configuring and editing webforms.
  */
 
+require_once ('./' . drupal_get_path('module', 'webform') . '/components/date.inc');
+
 /**
  * Main configuration form for editing a webform node.
  */
 function webform_configure_form(&$form_state, $node) {
+  $format = webform_date_format('short');
   $form = array();
 
   // Add CSS and JS. Don't preprocess because these files are used rarely.
@@ -113,6 +116,25 @@ function webform_configure_form(&$form_s
     '#parents' => array('submit_interval'),
   );
 
+  $form['submission']['start_date'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Submission Start Date'),
+    '#default_value' => $node->webform['start_date'] != 0 ? date($format, $node->webform['start_date']) : '',
+    '#description' => t('Begin date for submissions - the form will display "Submissions for this form are currently closed." prior to the date entered. Leaving this field blank will activate the form immediately.'). '<br />' . t('Accepts any date in any <a href="http://www.gnu.org/software/tar/manual/html_chapter/Date-input-formats.html">GNU Date Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 are all valid.'),
+    '#parents' => array('start_date'),
+    '#size' => 60,
+    '#maxlength' => 127,
+  );
+
+  $form['submission']['end_date'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Submission End Date'),
+    '#default_value' => $node->webform['end_date'] != 0 ? date($format, $node->webform['end_date']) : '',
+    '#description' => t('End date for submissions - the form will display "Submissions for this form are current closed." after the date entered. Leave this field blank for no expiration.'). '<br />' . t('Accepts any date in any <a href="http://www.gnu.org/software/tar/manual/html_chapter/Date-input-formats.html">GNU Date Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 are all valid.'),
+    '#parents' => array('end_date'),
+    '#size' => 60,
+    '#maxlength' => 127,
+  );
   /* End Edit Form */
 
   /* Start per-role submission control */
@@ -225,12 +247,26 @@ function webform_configure_form_validate
   else {
     form_set_value($form['submission']['redirection']['redirect_url'], '<none>', $form_state);
   }
+
+  if (!empty($form_state['values']['start_date']) && strtotime($form_state['values']['start_date']) === false) {
+    form_error($form['submission']['start_date'], t('Please enter a valid start date.'));
+  }
+
+  if (!empty($form_state['values']['end_date']) && strtotime($form_state['values']['end_date']) === false) {
+    form_error($form['submission']['end_date'], t('Please enter a valid end date.'));
+  }
+
+  if (!empty($form_state['values']['start_date']) && !empty($form_state['values']['end_date']) && (strtotime($form_state['values']['end_date']) < strtotime($form_state['values']['start_date']))) {
+    form_error($form['submission']['start_date'], t('Start date cannot be after end date. Please adjust one or the other.'));
+  }
 }
 
 /**
  * Submit handler for webform_configure_form().
  */
 function webform_configure_form_submit($form, &$form_state) {
+  $current_tz = date_default_timezone_get();
+  date_default_timezone_set('UTC');
   $node = node_load($form_state['values']['nid']);
 
   // Save the confirmation.
@@ -262,6 +298,21 @@ function webform_configure_form_submit($
     $node->webform['submit_interval'] = $form_state['values']['submit_interval'];
   }
 
+  // Save date fields.
+  if (!empty($form_state['values']['start_date'])) {
+    $node->webform['start_date'] = strtotime($form_state['values']['start_date']);
+  }
+  else {
+    $node->webform['start_date'] = '';
+  }
+
+  if (!empty($form_state['values']['end_date'])) {
+    $node->webform['end_date'] = strtotime($form_state['values']['end_date']);
+  }
+  else {
+    $node->webform['end_date'] = '';
+  }
+
   // Set submit notice.
   $node->webform['submit_notice'] = $form_state['values']['submit_notice'];
 
