diff --git a/includes/webform.export.inc b/includes/webform.export.inc index 91204a7..2b6588d 100644 --- a/includes/webform.export.inc +++ b/includes/webform.export.inc @@ -102,6 +102,9 @@ class webform_exporter_delimited extends webform_exporter { if ($this->delimiter == '\t') { $this->delimiter = "\t"; } + if ($options['compress'] == 1) { + $this->compress = 1; + } } function bof(&$file_handle) { @@ -135,8 +138,11 @@ class webform_exporter_delimited extends webform_exporter { function set_headers($filename) { parent::set_headers($filename); - // Convert tabs. - if ($this->delimiter == "\t") { + if ($this->compress == 1) { + $extension = 'zip'; + $content_type = 'application/octet-stream'; + } + elseif ($this->delimiter == "\t") { $extension = 'tsv'; $content_type = 'text/tab-separated-values'; } @@ -162,9 +168,14 @@ class webform_exporter_excel extends webform_exporter_delimited { } function set_headers($filename) { - drupal_set_header('Content-Type: application/x-msexcel'); - drupal_set_header("Content-Disposition: attachment; filename=$filename.xls"); - drupal_set_header('Pragma: public'); - drupal_set_header('Cache-Control: max-age=0'); + if ($this->compress == 1) { + parent::set_headers($filename); + } + else { + drupal_add_http_header('Content-Type', 'application/x-msexcel'); + drupal_add_http_header('Content-Disposition', "attachment; filename=$filename.xls"); + drupal_add_http_header('Pragma', 'public'); + drupal_add_http_header('Cache-Control', 'max-age=0'); + } } } diff --git a/includes/webform.report.inc b/includes/webform.report.inc index 0c2f698..53de0de 100644 --- a/includes/webform.report.inc +++ b/includes/webform.report.inc @@ -330,6 +330,17 @@ function webform_results_download_form(&$form_state, $node) { ), ); + $form['compress'] = array( + '#type' => 'checkbox', + '#title' => t('Include files'), + '#description' => t('All files in the webform results will be bundled in a .zip archive along with the exported results.'), + ); + + if (!class_exists('ZipArchive')) { + $form['compress']['#description'] = t('Unable to load ZipArchive class. This usually happens because the PHP interpreter is not compiled with zlib.'); + $form['compress']['#disabled'] = TRUE; + } + $form['select_options'] = array( '#type' => 'fieldset', '#title' => t('Select list options'), @@ -397,6 +408,7 @@ function webform_results_download_form_submit(&$form, &$form_state) { 'components' => array_keys(array_filter($form_state['values']['components'])), 'select_keys' => $form_state['values']['select_keys'], 'select_format' => $form_state['values']['select_format'], + 'compress' => $form_state['values']['compress'], ); webform_results_download($form_state['values']['node'], $form_state['values']['format'], $options); } @@ -500,6 +512,7 @@ function webform_results_download($node, $format = 'delimited', $options = array 'components' => array_merge(array_keys($submission_information), array_keys(webform_component_list($node, 'csv', TRUE))), 'select_display' => 'value', 'select_format' => 'separate', + 'compress' => 0, ); } else { @@ -549,6 +562,7 @@ function webform_results_download($node, $format = 'delimited', $options = array // Get all the submissions for the node. $submissions = webform_get_submissions($node->nid); + $attached_files = array(); // Generate a row for each submission. $row_count = 0; @@ -585,6 +599,9 @@ function webform_results_download($node, $format = 'delimited', $options = array $raw_data = isset($submission->data[$cid]['value']) ? $submission->data[$cid]['value'] : NULL; if (webform_component_feature($component['type'], 'csv')) { $data = webform_component_invoke($component['type'], 'csv_data', $component, $options, $raw_data); + if ($component['type'] == 'file') { + $attached_files[] = webform_get_file($raw_data[0])->filepath; + } if (is_array($data)) { $row = array_merge($row, array_values($data)); } @@ -607,7 +624,28 @@ function webform_results_download($node, $format = 'delimited', $options = array $export_name = _webform_safe_name($node->title); $exporter->set_headers($export_name); - @readfile($file_name); // The @ makes it silent. + + if ($options['compress'] == 1) { + $zh = new ZipArchive(); + $zipfile = variable_get('file_directory_temp', file_directory_temp()) . '/webform' . time() . '.zip'; + $zh->open($zipfile, ZIPARCHIVE::CREATE); + foreach ($attached_files as $file) { + if (!empty($file)) { + $zh->addFile(realpath($base_path . $file), basename($file)); + } + } + $extension = '.csv'; + if ($exporter->delimiter == "\t") { + $extension = '.tsv'; + } + $zh->addFile(realpath($file_name), $export_name . $extension); + $zh->close(); + @readfile($zipfile); + @unlink($zipfile); + } + else { + @readfile($file_name); // The @ makes it silent. + } @unlink($file_name); // Clean up, the @ makes it silent. exit(); }