You are here

function webform_results_download_form in Webform 7.4

Same name and namespace in other branches
  1. 5.2 webform_report.inc \webform_results_download_form()
  2. 6.3 includes/webform.report.inc \webform_results_download_form()
  3. 6.2 webform_report.inc \webform_results_download_form()
  4. 7.3 includes/webform.report.inc \webform_results_download_form()

Form to configure the download of CSV files.

1 string reference to 'webform_results_download_form'
webform_menu in ./webform.module
Implements hook_menu().

File

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

Code

function webform_results_download_form($form, &$form_state, $node) {
  module_load_include('inc', 'webform', 'includes/webform.export');
  module_load_include('inc', 'webform', 'includes/webform.components');
  $form['#attached']['js'][] = drupal_get_path('module', 'webform') . '/js/webform-admin.js';

  // If an export is waiting to be downloaded, redirect the user there after
  // the page has finished loading.
  if (isset($_SESSION['webform_export_info'])) {
    $download_url = url('node/' . $node->nid . '/webform-results/download-file', array(
      'absolute' => TRUE,
    ));
    $form['#attached']['js'][] = array(
      'data' => array(
        'webformExport' => $download_url,
      ),
      'type' => 'setting',
    );
  }
  $form['node'] = array(
    '#type' => 'value',
    '#value' => $node,
  );
  $form['format'] = array(
    '#type' => 'radios',
    '#title' => t('Export format'),
    '#options' => webform_export_list(),
    '#default_value' => webform_variable_get('webform_export_format'),
  );
  $form['delimited_options'] = array(
    '#type' => 'container',
    'warning' => array(
      '#markup' => '<p>' . t('<strong>Warning:</strong> Opening delimited text files with spreadsheet applications may expose you to <a href="!link">formula injection</a> or other security vulnerabilities. When the submissions contain data from untrusted users and the downloaded file will be used with spreadsheets, use Microsoft Excel format.', array(
        '!link' => url('https://www.google.com/search?q=spreadsheet+formula+injection'),
      )) . '</p>',
    ),
    'delimiter' => array(
      '#type' => 'select',
      '#title' => t('Delimited text format'),
      '#description' => t('This is the delimiter used in the CSV/TSV file when downloading Webform results. Using tabs in the export is the most reliable method for preserving non-latin characters. You may want to change this to another character depending on the program with which you anticipate importing results.'),
      '#default_value' => webform_variable_get('webform_csv_delimiter'),
      '#options' => array(
        ',' => t('Comma (,)'),
        '\\t' => t('Tab (\\t)'),
        ';' => t('Semicolon (;)'),
        ':' => t('Colon (:)'),
        '|' => t('Pipe (|)'),
        '.' => t('Period (.)'),
        ' ' => t('Space ( )'),
      ),
    ),
    '#states' => array(
      'visible' => array(
        ':input[name=format]' => array(
          'value' => 'delimited',
        ),
      ),
    ),
  );
  $form['header_keys'] = array(
    '#type' => 'radios',
    '#title' => t('Column header format'),
    '#options' => array(
      -1 => t('None'),
      0 => t('Label'),
      1 => t('Form Key'),
    ),
    '#default_value' => 0,
    '#description' => t('Choose whether to show the label or form key in each column header.'),
  );
  $form['select_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Select list options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['select_options']['select_keys'] = array(
    '#type' => 'radios',
    '#title' => t('Select keys'),
    '#options' => array(
      0 => t('Full, human-readable options (values)'),
      1 => t('Short, raw options (keys)'),
    ),
    '#default_value' => 0,
    '#description' => t('Choose which part of options should be displayed from key|value pairs.'),
  );
  $form['select_options']['select_format'] = array(
    '#type' => 'radios',
    '#title' => t('Select list format'),
    '#options' => array(
      'separate' => t('Separate'),
      'compact' => t('Compact'),
    ),
    '#default_value' => 'separate',
    '#attributes' => array(
      'class' => array(
        'webform-select-list-format',
      ),
    ),
    '#theme' => 'webform_results_download_select_format',
  );
  $csv_components = array(
    'info' => t('Submission information'),
  );

  // Prepend information fields with "-" to indent.
  foreach (webform_results_download_submission_information($node) as $key => $title) {
    $csv_components[$key] = '-' . $title;
  }
  $csv_components += webform_component_list($node, 'csv', TRUE);
  $form['components'] = array(
    '#type' => 'select',
    '#title' => t('Included export components'),
    '#options' => $csv_components,
    '#default_value' => array_keys($csv_components),
    '#multiple' => TRUE,
    '#size' => 10,
    '#description' => t('The selected components will be included in the export.'),
    '#process' => array(
      'webform_component_select',
    ),
  );
  $form['range'] = array(
    '#type' => 'fieldset',
    '#title' => t('Download range options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
    '#theme' => 'webform_results_download_range',
    '#element_validate' => array(
      'webform_results_download_range_validate',
    ),
    '#after_build' => array(
      'webform_results_download_range_after_build',
    ),
  );
  $form['range']['range_type'] = array(
    '#type' => 'radios',
    '#options' => array(
      'all' => t('All submissions'),
      'new' => t('Only new submissions since your last download'),
      'latest' => t('Only the latest'),
      'range_serial' => t('All submissions starting from'),
      'range_date' => t('All submissions by date'),
    ),
    '#default_value' => 'all',
  );
  $form['range']['latest'] = array(
    '#type' => 'textfield',
    '#size' => 5,
    '#maxlength' => 8,
  );
  $form['range']['start'] = array(
    '#type' => 'textfield',
    '#size' => 5,
    '#maxlength' => 8,
  );
  $form['range']['end'] = array(
    '#type' => 'textfield',
    '#size' => 5,
    '#maxlength' => 8,
  );
  $date_attributes = array(
    'placeholder' => format_date(REQUEST_TIME, 'custom', webform_date_format('short')),
  );
  $form['range']['start_date'] = array(
    '#type' => 'textfield',
    '#size' => 20,
    '#attributes' => $date_attributes,
  );
  $form['range']['end_date'] = array(
    '#type' => 'textfield',
    '#size' => 20,
    '#attributes' => $date_attributes,
  );

  // If drafts are allowed, provide options to filter download based on draft
  // status.
  $form['range']['completion_type'] = array(
    '#type' => 'radios',
    '#title' => t('Included submissions'),
    '#default_value' => 'all',
    '#options' => array(
      'all' => t('Finished and draft submissions'),
      'finished' => t('Finished submissions only'),
      'draft' => t('Drafts only'),
    ),
    '#access' => $node->webform['allow_draft'] || $node->webform['auto_save'],
  );

  // By default results are downloaded. User can override this value if
  // programmatically submitting this form.
  $form['download'] = array(
    '#type' => 'value',
    '#default_value' => TRUE,
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Download'),
  );
  return $form;
}