Index: webform_report.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform_report/Attic/webform_report.inc,v
retrieving revision 1.1.2.27.2.10
diff -u -r1.1.2.27.2.10 webform_report.inc
--- webform_report.inc	9 Nov 2010 21:10:01 -0000	1.1.2.27.2.10
+++ webform_report.inc	12 Nov 2010 20:21:26 -0000
@@ -7,7 +7,7 @@
  * This file contains common functions and functions required
  * to ouput a report for the webform report module
  */
- 
+
 /**
  * Search for nodes of type webform and returns their nids and titles
  * in an associative array.
@@ -113,12 +113,49 @@
         );
       }
     }
+    // check for session values - override original values if found
+    $sess_values =  $_SESSION['filter_values'][$node->nid];
+    if (is_array($sess_values)) {
+      foreach($sess_values as $index => $val) {
+        $filters[$index]['value'] = $val;
+      }
+    }
   }
   
   return $filters;
 }
 
 /**
+ * return an array containing the webform report filter options
+ *
+ * @return 
+ *   an array of webform report filter options
+ */
+function _webform_report_get_filter_options($datetimeonly = FALSE) {
+
+  $opt = array();
+  
+  $opt[0] = t('none');
+  if (!$datetimeonly) {
+    $opt[1] = t('begins with');
+    $opt[2] = t('does not begin with');
+    $opt[3] = t('contains');
+    $opt[4] = t('does not contain');
+  }
+  $opt[5] = t('equals');
+  $opt[6] = t('does not equal');
+  $opt[7] = t('is empty');
+  $opt[8] = t('is not empty');
+  $opt[9] = t('greater than');
+  $opt[10] = t('less than');
+  $opt[11] = t('greater than or equal');
+  $opt[12] = t('less than or equal');
+  $opt[13] = t('between');
+  
+  return $opt;
+}  
+
+/**
  * return an associative array containing the webform report sort criteria
  *
  * @param node
@@ -187,16 +224,22 @@
  * @return 
  *   a string of text or a themed table
  */
-function _webform_report_get_body_content($node, $formatcsv = FALSE) {
+function _webform_report_get_body_content($node, $teaser, $page, $formatcsv = FALSE) {
 
   $output = '';
   
+  
   // query submissions  
   $rs = _webform_report_get_data($node);  
 
   // if any submissions
   if (!empty($rs)) {
-  
+ 
+    // display filter form if desired
+    if ($node->options['filter_form'] && $page) {
+      $output .= drupal_get_form('webform_report_filter_form', $node);
+    }
+    
     // get report criteria
     $components = _webform_report_get_components($node->wnid);
     $columns = _webform_report_get_columns($node, $components);
@@ -260,7 +303,7 @@
           if ($csid != 0) {
           
             // test submission against filters
-            if (_webform_report_test_filters($data, $filters)) {
+            if (_webform_report_test_filters($data, $filters, $columns)) {
               // output row if filters pass
               $rows[] = _webform_report_output($data, $columns);
             }
@@ -328,14 +371,14 @@
       }
       // no submissions met criteria
       else {
-        $output = t('There are no submissions that match the criteria for the selected webform.');
+        $output .= t('There are no submissions that match the criteria for the selected webform.');
       }
       
     }   // end - if (count($columns) > 0)...
     
     // webform report does not have any criteria
     else {
-      $output = t('It appears that no criteria have been specified for this report. 
+      $output .= t('It appears that no criteria have been specified for this report. 
         Please click on the Criteria tab to add webform data to your report.');
     }
     
@@ -362,7 +405,7 @@
  * @return 
  *   TRUE if filters passed, otherwise FALSE
  */
-function _webform_report_test_filters($data, $filters) {
+function _webform_report_test_filters($data, $filters, $columns) {
 
   // filter result, return true if no filters
   $ok = TRUE;
@@ -383,17 +426,32 @@
         $raw = $data[$filter['cid']];
         
         // format value
-        $value = _webform_report_format_data($raw, $filter['cid'], $filter['type']);
+        foreach ($columns as $col) {
+          if ($col['cid'] == $filter['cid']) {
+            break;
+          }
+        }
+        $value = _webform_report_format_data($raw, $col);
         
         // prepare filter values
         $filter_data = strip_tags(strtolower(trim($value['data'])));
         $filter_value = strtolower(trim($filter['value']));
         
-        // flag eq, ne, gt, lt, ge, le compares
+        // extract filter values for between
+        if ($filter['ftype'] == 13) {
+            $filter_value = explode('|', $filter_value);
+            // should be two values
+            if (count($filter_value) == 1) {
+              $filter_value[] = '';
+            }
+        }
+        
+        // flag eq, ne, gt, lt, ge, le, between compares
         $cmpflag = (
           $filter['ftype'] == 5 || $filter['ftype'] == 6 ||
           $filter['ftype'] == 9 || $filter['ftype'] == 10 ||
-          $filter['ftype'] == 11 || $filter['ftype'] == 12
+          $filter['ftype'] == 11 || $filter['ftype'] == 12 || 
+          $filter['ftype'] == 13
         );
         
         // see if special date and time processing is required
@@ -406,8 +464,6 @@
           else {
             $filter_data = strtotime(date('m/d/Y', $raw[0]));
           }
-          // convert date value to timestamp
-          $filter_value = strtotime($filter_value);
         }
         if ($filter['type'] == 'time' && $cmpflag) {
           // user entered time
@@ -418,8 +474,16 @@
           else {
             $filter_data = strtotime(date('H:m', $raw[0]));
           }
-          // convert time value to timestamp
-          $filter_value = strtotime($filter_value);
+        }
+        // convert filter value to timestamp if necessary
+        if (($filter['type'] == 'date' || $filter['type'] == 'time') && $cmpflag) {
+          if (is_array($filter_value)) {
+            $filter_value[0] = strtotime($filter_value[0]); 
+            $filter_value[1] = strtotime($filter_value[1]); 
+          }
+          else {
+            $filter_value = strtotime($filter_value);
+          }
         }
         
         // apply filter
@@ -445,14 +509,14 @@
             
           // Contains x.
           case 3:
-            if (strpos($filter_data, $filter_value) !== FALSE) {
+            if (!$filter_value || strpos($filter_data, $filter_value) !== FALSE) {
               $ok = TRUE;
             }
             break;
             
           // Does not contain x.
           case 4:
-            if (strpos($filter_data, $filter_value) === FALSE) {
+            if (!$filter_value || strpos($filter_data, $filter_value) === FALSE) {
               $ok = TRUE;
             }
             break;
@@ -512,6 +576,15 @@
               $ok = TRUE;
             }
             break;
+          
+          // between
+          case 13:
+            // negate filter if both values are empty
+            if ((!$filter_value[0] && !$filter_value[1]) ||       
+                ($filter_data >= $filter_value[0] && $filter_data <= $filter_value[1])) {
+              $ok = TRUE;
+            }
+            break;
           }   // end - switch($filter['type'])...
           
         }   // end - if (array_key_exists($filter['cid'], $data)...
@@ -971,7 +1044,7 @@
  */
 function webform_report_csv($node) {
 
-  $output = _webform_report_get_body_content($node, TRUE);
+  $output = _webform_report_get_body_content($node, FALSE, FALSE, TRUE);
       
   $fname = 'wfr_export.csv';
   header('Content-Type: text/plain');
Index: webform_report.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform_report/webform_report.module,v
retrieving revision 1.2.2.55.2.43.2.3
diff -u -r1.2.2.55.2.43.2.3 webform_report.module
--- webform_report.module	9 Nov 2010 21:06:58 -0000	1.2.2.55.2.43.2.3
+++ webform_report.module	12 Nov 2010 20:18:57 -0000
@@ -181,9 +181,8 @@
 function webform_report_theme() {
   
   $theme = array(
-    'webform_report_criteria_form' => array(
-      'arguments' => array('form' => NULL),
-    )
+    'webform_report_criteria_form' => array('arguments' => array('form' => NULL)),
+    'webform_report_filter_form' => array('arguments' => array('form' => NULL))
   );
   
   return $theme;
@@ -219,7 +218,10 @@
   }
   module_load_include('inc', 'webform_report', 'webform_report');
 
-  $output = _webform_report_get_body_content($node);
+  // include filter form
+  module_load_include('inc', 'webform_report', 'webform_report_criteria');
+
+  $output = _webform_report_get_body_content($node, $teaser, $page);
   
   $node->content['body'] = array('#value' => check_markup($node->body, $node->format, FALSE));
   $node->content['webform_report'] = array('#value' => $output, '#weight' => 10);
Index: webform_report_criteria.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform_report/Attic/webform_report_criteria.inc,v
retrieving revision 1.1.2.3
diff -u -r1.1.2.3 webform_report_criteria.inc
--- webform_report_criteria.inc	9 Nov 2010 21:06:58 -0000	1.1.2.3
+++ webform_report_criteria.inc	12 Nov 2010 20:18:44 -0000
@@ -139,24 +139,15 @@
       '#return_value' => 'columns_add',
       '#submit' => array('webform_report_criteria_form_list_submit')
     );
-  
+
+    // make sure we get report values  
+    unset($_SESSION['filter_values'][$node->nid]);
+    
     $filters = _webform_report_get_filters($node, $webform_components);
+
+    $filter_options = _webform_report_get_filter_options();
     
-    $filter_options = array(
-      0 => t('none'), 
-      1 => t('begins with'), 
-      2 => t('does not begin with'), 
-      3 => t('contains'), 
-      4 => t('does not contain'), 
-      5 => t('equals'), 
-      6 => t('does not equal'), 
-      7 => t('is empty'), 
-      8 => t('is not empty'),
-      9 => t('greater than'), 
-      10 => t('less than'),
-      11 => t('greater than or equal'),
-      12 => t('less than or equal')
-    );
+    $filter_options_datetime = _webform_report_get_filter_options(TRUE);
     
     // filters fieldset  
     $form['filters'] = array(
@@ -169,10 +160,13 @@
     // form list of report filters
     $cc = count($filters);
     $i = 0;
-    
+
     for ($i=0; $i<$cc; $i++) {
   
       $cid = $filters[$i]['cid'];
+      $type = $filters[$i]['type'];
+      $ftype = $filters[$i]['ftype'];
+      $value = $filters[$i]['value'];
   
       $form['filters'][$i]['name'] = array(
         '#type' => 'markup',
@@ -180,19 +174,24 @@
       );
       $form['filters'][$i]['type'] = array(
         '#type' => 'markup',
-        '#value' => $filters[$i]['type']
+        '#value' => $type
       );
   
       $form['filters'][$i]['ftype'] = array(
         '#type' => 'select',
-        '#options' => $filter_options,
-        '#default_value' => $filters[$i]['ftype']
-      );
-  
-      $form['filters'][$i]['value'] = array(
-        '#type' => 'textfield',
-        '#default_value' => $filters[$i]['value']
+        '#default_value' => $ftype
       );
+
+      // use reduced options for date/time
+      if ($type == 'date' || $type == 'time') {
+        $form['filters'][$i]['ftype']['#options'] = $filter_options_datetime;
+      }
+      else {
+        $form['filters'][$i]['ftype']['#options'] = $filter_options;
+      }
+
+      // insert value field(s)      
+      webform_report_criteria_filter_value_field($form, $i, $type, $ftype, $value);
       
       webform_report_criteria_form_list_buttons($form, $img_path, 'filters', $cid, $i, $cc);
     }
@@ -307,6 +306,16 @@
       '#default_value' => $default,
     );
     
+    $default = FALSE;  
+    if (array_key_exists('filter_form', $node->options)) {
+      $default = $node->options['filter_form'];
+    }
+    $form['options']['filter_form'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Display Report Filter Form'),
+      '#default_value' => $default,
+    );
+    
     $form['update'] = array(
       '#type' => 'submit',
       '#value' => t('Update'),
@@ -321,6 +330,84 @@
 }
 
 /**
+ * Insert filter value field(s)
+ *
+ * @param form
+ *   the form to add the fields to
+ * @param index
+ *   the filter index
+ * @param type
+ *   the filter column type
+ * @param ftype
+ *   the filter type
+ * @param value
+ *   the filter value
+ */
+function webform_report_criteria_filter_value_field(&$form, $index, $type, $ftype, $value) {
+  
+  // use a date popup for dates if date_popup module is enabled
+  $fieldtype = 'textfield';
+  if ($type == 'date') {
+    if (module_exists('date_popup')) {
+      $fieldtype = 'date_popup';
+    }
+  }
+  
+  // if filter type is 'between', extract the two values
+  if ($ftype == 13) {
+    $value = explode('|', $value);
+    if (count($value) == 1) {
+      $value[] = '';
+    }
+  }
+  
+  // if between, add two fields
+  if (is_array($value)) {
+    $form['filters'][$index]['value1'] = array(
+      '#type' => $fieldtype,
+      '#default_value' => $value[0]
+    );
+    if ($fieldtype == 'date_popup') {
+      $form['filters'][$index]['value1']['#date_format'] = 'm/d/Y';
+    }
+    // add a size if a textbox
+    else {
+      $form['filters'][$index]['value1']['#size'] = 15;
+    }
+    $form['filters'][$index]['value2'] = array(
+      '#type' => $fieldtype,
+      '#default_value' => $value[1]
+    );
+          
+    // if using popup, format the value correctly
+    if ($fieldtype == 'date_popup') {
+      $form['filters'][$index]['value2']['#date_format'] = 'm/d/Y';
+    }
+    // add a size if a textbox
+    else {
+      $form['filters'][$index]['value2']['#size'] = 15;
+    }
+  }
+  // otherwise, just add one field
+  else {
+    $form['filters'][$index]['value'] = array(
+      '#type' => $fieldtype,
+      '#default_value' => $value
+    );
+        
+    // if using popup, format the value correctly
+    if ($fieldtype == 'date_popup') {
+      $form['filters'][$index]['value']['#date_format'] = 'm/d/Y';
+    }
+    // add a size if a textbox
+    else {
+      $form['filters'][$index]['value']['#size'] = 40;
+    }
+  }   // end - if (is_array($value))...
+        
+}        
+
+/**
  * Add list buttons to a form
  *
  * @param form
@@ -457,32 +544,55 @@
   
   if (array_key_exists('filters', $form)) {
   
+    // see if an extra value column is present
+    $hasextra = FALSE;
+    foreach (element_children($form['filters']) as $index) {
+      if (array_key_exists('value1', $form['filters'][$index])) {
+        $hasextra = TRUE;
+        break;
+      }
+    }
     // theme filter list
-    $header = array(
-      t('Name'), 
-      t('Type'), 
-      t('Filter Type'), 
-      t('Filter Value'), 
-      array('data' => t('Operations'), 'colspan' => '5')
-    );
-    
     $rows = array();
-    
     foreach (element_children($form['filters']) as $index) {
   
-      $rows[] = array(
-        drupal_render($form['filters'][$index]['name']),
-        drupal_render($form['filters'][$index]['type']),
-        drupal_render($form['filters'][$index]['ftype']),
-        drupal_render($form['filters'][$index]['value']),
-        drupal_render($form['filters'][$index]['delete']),    
-        drupal_render($form['filters'][$index]['top']), 
-        drupal_render($form['filters'][$index]['up']),
-        drupal_render($form['filters'][$index]['down']),
-        drupal_render($form['filters'][$index]['end'])    
-      );
+      $row = array();
+      $row[] = drupal_render($form['filters'][$index]['name']);
+      $row[] = drupal_render($form['filters'][$index]['type']);
+      $row[] = drupal_render($form['filters'][$index]['ftype']);
+      if (array_key_exists('value1', $form['filters'][$index])) {
+         $row[] = drupal_render($form['filters'][$index]['value1']);
+         $row[] = drupal_render($form['filters'][$index]['value2']);
+      }
+      else {
+        if ($hasextra) {
+          $row[] = array('data' => drupal_render($form['filters'][$index]['value']), 'colspan' => '2');
+        }
+        else {
+          $row[] = drupal_render($form['filters'][$index]['value']);
+        }
+      }
+      $row[] = drupal_render($form['filters'][$index]['delete']);
+      $row[] = drupal_render($form['filters'][$index]['top']);
+      $row[] = drupal_render($form['filters'][$index]['up']);
+      $row[] = drupal_render($form['filters'][$index]['down']);
+      $row[] = drupal_render($form['filters'][$index]['end']);  
+      
+      $rows[] = $row;
     }
-  
+
+    $header = array();
+    $header[] = t('Name');
+    $header[] = t('Type');
+    $header[] = t('Filter Type');
+    if ($hasextra) {  
+      $header[] = array('data' => t('Filter Value(s)'), 'colspan' => '2');
+    }
+    else {
+      $header[] = t('Filter Value');
+    }
+    $header[] = array('data' => t('Operations'), 'colspan' => '5');
+    
     $form['filters']['table'] = array(
       '#value' => theme('table', $header, $rows)
     );
@@ -744,7 +854,19 @@
     if (is_numeric($index)) {
       if (array_key_exists($index, $node->filters)) {
         $node->filters[$index]['type'] = $form_state['values']['filters'][$index]['ftype'];
-        $node->filters[$index]['value'] = $form_state['values']['filters'][$index]['value'];
+        // see if we have two values (between)
+        if (array_key_exists('value1', $form_state['values']['filters'][$index])) {
+          $v = array();
+          $v[] = $form_state['values']['filters'][$index]['value1'];
+          $v[] = $form_state['values']['filters'][$index]['value2'];
+          // combine the values
+          $value = implode('|', $v);
+        }
+        // just one value
+        else {
+          $value = $form_state['values']['filters'][$index]['value'];
+        }
+        $node->filters[$index]['value'] = $value;
       }
     }
   }
@@ -760,9 +882,208 @@
   // update options
   $node->options['results_per_page'] = $form_state['values']['results_per_page'];
   $node->options['hide_csv'] = $form_state['values']['hide_csv'];
+  $node->options['filter_form'] = $form_state['values']['filter_form'];
 
   // update the node  
   node_save($node);
 
   drupal_set_message(t('Webform Report has been updated.'));
-}
\ No newline at end of file
+}
+
+/**
+ * Generate a form for specifying report filter data
+ *
+ * @param form_state
+ *   drupal form state
+ * *param node
+ *   current node object
+ * @return 
+ *   an array of form elements
+ */
+ function webform_report_filter_form($form_state, $node) {
+  
+  $form = array();
+  
+  if (isset($node->wnid)) {  
+  
+    $webform_components = _webform_report_get_components($node->wnid);
+    
+    $filters = _webform_report_get_filters($node, $webform_components);
+    
+    $filter_options = _webform_report_get_filter_options();
+    
+    // filters fieldset  
+    $form['filters'] = array(
+      '#type' => 'fieldset', 
+      '#title' => t('Report Filters'),
+      '#description' => t('All filters are ANDed together.'),
+      '#collapsible' => TRUE,
+      '#collapsed' => TRUE,
+      '#tree' => TRUE
+    );
+  
+    $form['filters']['nid'] = array(
+      '#type' => 'hidden',
+      '#value' => $node->nid
+    );
+  
+    // form list of report filters
+    $cc = count($filters);
+    $i = 0;
+    
+    for ($i=0; $i<$cc; $i++) {
+  
+      $cid = $filters[$i]['cid'];
+      $type = $filters[$i]['type'];
+      $ftype = $filters[$i]['ftype'];
+      $value = $filters[$i]['value'];
+      
+      if ($ftype > 0) {
+  
+        $form['filters'][$i]['name'] = array(
+          '#type' => 'markup',
+          '#value' => $filters[$i]['name']
+        );
+        $form['filters'][$i]['type'] = array(
+          '#type' => 'markup',
+          '#value' => $type
+        );
+    
+        $form['filters'][$i]['ftype'] = array(
+          '#type' => 'markup',
+          '#value' => $filter_options[$ftype]
+        );
+        
+        webform_report_criteria_filter_value_field($form, $i, $type, $ftype, $value);
+  
+      }   // end - if ($ftype > 0)...
+    }   // end - for ($i=0; $i<$cc; $i++)...
+
+    $form['filters']['refresh'] = array(
+      '#type' => 'submit',
+      '#value' => t('Refresh'),
+      '#weight' => 1
+    );
+  
+    $form['filters']['reset'] = array(
+      '#type' => 'submit',
+      '#value' => t('Reset'),
+      '#weight' => 2
+    );
+    
+  }   // end -   if (isset($node->wnid))...
+  
+  return $form;
+}
+
+/**
+ * Theme the report filter form
+ *
+ * @param form
+ *   the form to theme
+ * @return
+ *   the rendered form
+ */
+function theme_webform_report_filter_form($form) {
+
+  if (array_key_exists('filters', $form)) {
+  
+    // see if an extra value column is present
+    $hasextra = FALSE;
+    foreach (element_children($form['filters']) as $index) {
+      if (array_key_exists('value1', $form['filters'][$index])) {
+        $hasextra = TRUE;
+        break;
+      }
+    }
+    // theme filter list
+    $rows = array();
+    foreach (element_children($form['filters']) as $index) {
+  
+      $row = array();
+      $row[] = drupal_render($form['filters'][$index]['name']);
+      $row[] = drupal_render($form['filters'][$index]['type']);
+      $row[] = drupal_render($form['filters'][$index]['ftype']);
+      if (array_key_exists('value1', $form['filters'][$index])) {
+         $row[] = drupal_render($form['filters'][$index]['value1']);
+         $row[] = drupal_render($form['filters'][$index]['value2']);
+      }
+      else {
+        if ($hasextra) {
+          $row[] = array('data' => drupal_render($form['filters'][$index]['value']), 'colspan' => '2');
+        }
+        else {
+          $row[] = drupal_render($form['filters'][$index]['value']);
+        }
+      }
+      
+      $rows[] = $row;
+    }
+
+    $header = array();
+    $header[] = t('Name');
+    $header[] = t('Type');
+    $header[] = t('Filter Type');
+    if ($hasextra) {  
+      $header[] = array('data' => t('Filter Value(s)'), 'colspan' => '2');
+    }
+    else {
+      $header[] = t('Filter Value');
+    }
+    
+    $form['filters']['table'] = array(
+      '#value' => theme('table', $header, $rows)
+    );
+  }
+  
+  return drupal_render($form);
+}
+
+/**
+ * Implementation of hook_submit for the report filter form
+ *
+ * @param form_id
+ *   drupal form id
+ * @param form_state
+ *   drupal form state and values
+ */
+function webform_report_filter_form_submit($form_id, $form_state) {
+
+  // get current node id
+  $nid = $form_state['values']['filters']['nid'];
+  
+  // refresh
+  if ($form_state['values']['op'] == 'Refresh') {
+  
+    $filter_values = array();
+  
+    foreach (element_children($form_state['values']['filters']) as $index) {
+    
+      // make sure it's a filter row
+      if (is_array($form_state['values']['filters'][$index])) {
+      
+        // see if we have two values (between)
+        if (array_key_exists('value1', $form_state['values']['filters'][$index])) {
+          $v = array();
+          $v[] = $form_state['values']['filters'][$index]['value1'];
+          $v[] = $form_state['values']['filters'][$index]['value2'];
+          // combine the values
+          $value = implode('|', $v);
+        }
+        // just one value
+        else {
+          $value = $form_state['values']['filters'][$index]['value'];
+        }
+        $filter_values[$index] = $value;
+      }
+    }
+    
+    $_SESSION['filter_values'][$nid] = $filter_values;
+  }
+  
+  // reset
+  else {
+    unset($_SESSION['filter_values'][$nid]);
+  }
+  
+}

