diff --git a/includes/webform.export.inc b/includes/webform.export.inc index 265cdf4..b5f02aa 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'; } diff --git a/includes/webform.report.inc b/includes/webform.report.inc index a4334a1..9e0f870 100644 --- a/includes/webform.report.inc +++ b/includes/webform.report.inc @@ -344,6 +344,17 @@ function webform_results_download_form($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'), @@ -411,6 +422,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); } @@ -516,6 +528,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_keys' => 0, 'select_format' => 'separate', + 'compress' => 0, ); } else { @@ -565,6 +578,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; @@ -601,6 +615,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)); } @@ -623,7 +640,24 @@ 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)); + } + } + $zh->addFile(realpath($file_name), $export_name . 'csv'); + $zh->close(); + @readfile($zipfile); + @unlink($zipfile); + } + else { + @readfile($file_name); // The @ makes it silent. + } @unlink($file_name); // Clean up, the @ makes it silent. exit(); }