You are here

function webform_results_download_range_validate in Webform 7.4

Same name and namespace in other branches
  1. 6.3 includes/webform.report.inc \webform_results_download_range_validate()
  2. 7.3 includes/webform.report.inc \webform_results_download_range_validate()

FormAPI element validate function for the range fieldset.

1 string reference to 'webform_results_download_range_validate'
webform_results_download_form in includes/webform.report.inc
Form to configure the download of CSV files.

File

includes/webform.report.inc, line 631
This file includes helper functions for creating reports for webform.module.

Code

function webform_results_download_range_validate($element, $form_state) {
  switch ($element['range_type']['#value']) {
    case 'latest':

      // Download latest x submissions.
      if ($element['latest']['#value'] == '') {
        form_error($element['latest'], t('Latest number of submissions field is required.'));
      }
      else {
        if (!is_numeric($element['latest']['#value'])) {
          form_error($element['latest'], t('Latest number of submissions must be numeric.'));
        }
        else {
          if ($element['latest']['#value'] <= 0) {
            form_error($element['latest'], t('Latest number of submissions must be greater than 0.'));
          }
        }
      }
      break;
    case 'range_serial':

      // Download Start-End range of submissions.
      // Start submission number.
      if ($element['start']['#value'] == '') {
        form_error($element['start'], t('Start submission number is required.'));
      }
      else {
        if (!is_numeric($element['start']['#value'])) {
          form_error($element['start'], t('Start submission number must be numeric.'));
        }
        else {
          if ($element['start']['#value'] <= 0) {
            form_error($element['start'], t('Start submission number must be greater than 0.'));
          }
        }
      }

      // End submission number.
      if ($element['end']['#value'] != '') {
        if (!is_numeric($element['end']['#value'])) {
          form_error($element['end'], t('End submission number must be numeric.'));
        }
        else {
          if ($element['end']['#value'] <= 0) {
            form_error($element['end'], t('End submission number must be greater than 0.'));
          }
          else {
            if ($element['end']['#value'] < $element['start']['#value']) {
              form_error($element['end'], t('End submission number must not be less than Start submission number.'));
            }
          }
        }
      }
      break;
    case 'range_date':

      // Download Start-end range of submissions.
      // Start submission time.
      $format = webform_date_format('short');
      $start_date = DateTime::createFromFormat($format, $element['start_date']['#value']);
      if ($element['start_date']['#value'] == '') {
        form_error($element['start_date'], t('Start date range is required.'));
      }
      elseif ($start_date === FALSE) {
        form_error($element['start_date'], t('Start date range is not in a valid format.'));
      }

      // End submission time.
      $end_date = DateTime::createFromFormat($format, $element['end_date']['#value']);
      if ($element['end_date']['#value'] != '') {
        if ($end_date === FALSE) {
          form_error($element['end_date'], t('End date range is not in a valid format.'));
        }
        elseif ($start_date !== FALSE && $start_date > $end_date) {
          form_error($element['end_date'], t('End date range must not be before the Start date.'));
        }
      }
      break;
  }

  // Check that the range will return something at all.
  $range_options = array(
    'range_type' => $element['range_type']['#value'],
    'start' => $element['start']['#value'],
    'end' => $element['end']['#value'],
    'latest' => $element['latest']['#value'],
    'start_date' => $element['start_date']['#value'],
    'end_date' => $element['end_date']['#value'],
    'completion_type' => $element['completion_type']['#value'],
    'batch_size' => 1,
    'batch_number' => 0,
  );
  if (!form_get_errors() && !webform_download_sids_count($form_state['values']['node']->nid, $range_options)) {
    form_error($element['range_type'], t('The specified range will not return any results.'));
  }
}