diff --git a/Controller/ExportController.php b/Controller/ExportController.php index c25a25e03..b29849585 100644 --- a/Controller/ExportController.php +++ b/Controller/ExportController.php @@ -412,9 +412,29 @@ class ExportController extends Controller } public function downloadResultAction(Request $request, $alias) - { - return $this->render("ChillMainBundle:Export:download.html.twig", [ + { + /* @var $exportManager \Chill\MainBundle\Export\ExportManager */ + $exportManager = $this->get('chill.main.export_manager'); + $formCenters = $this->createCreateFormExport($alias, 'generate_centers'); + $formCenters->handleRequest($request); + $dataCenters = $formCenters->getData(); + + $formExport = $this->createCreateFormExport($alias, 'generate_export', $dataCenters); + $formExport->handleRequest($request); + $dataExport = $formExport->getData(); + dump($dataExport); + $formatterAlias = $exportManager->getFormatterAlias($dataExport['export']); + $formater = $exportManager->getFormatter($formatterAlias); + + $viewVariables = [ 'alias' => $alias - ]); + ]; + + if ($formater instanceof \Chill\MainBundle\Export\Formatter\CSVListFormatter) { + // due to a bug in php, we add the mime type in the download view + $viewVariables['mime_type'] = 'text/csv'; + } + + return $this->render("ChillMainBundle:Export:download.html.twig", $viewVariables); } } diff --git a/Export/ExportManager.php b/Export/ExportManager.php index 85c2b4280..3db2d138c 100644 --- a/Export/ExportManager.php +++ b/Export/ExportManager.php @@ -437,16 +437,16 @@ class ExportManager $result = $export->getResult($query, $data[ExportType::EXPORT_KEY]); - if (!is_array($result)) { + if (!is_iterable($result)) { throw new \UnexpectedValueException( sprintf( - 'The result of the export should be an array, %s given', + 'The result of the export should be an iterable, %s given', gettype($result) ) ); } - /* @var $formatter Formatter\CSVFormatter */ + /* @var $formatter FormatterInterface */ $formatter = $this->getFormatter($this->getFormatterAlias($data)); $filtersData = array(); $aggregatorsData = array(); diff --git a/Resources/public/modules/download-report/download-report.js b/Resources/public/modules/download-report/download-report.js new file mode 100644 index 000000000..1900c04cf --- /dev/null +++ b/Resources/public/modules/download-report/download-report.js @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2018 Champs Libres Cooperative + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +var mime = require('mime-types') + +var download_report = (url, container) => { + var download_text = container.dataset.downloadText, + alias = container.dataset.alias; + + window.fetch(url, { credentials: 'same-origin' }) + .then(response => { + if (!response.ok) { + throw Error(response.statusText); + } + + return response.blob(); + }).then(blob => { + + var content = URL.createObjectURL(blob), + link = document.createElement("a"), + type = blob.type, + hasForcedType = 'mimeType' in container.dataset, + extension; + + if (hasForcedType) { + // force a type + type = container.dataset.mimeType; + blob = new Blob([ blob ], { 'type': type }); + content = URL.createObjectURL(blob); + } + + extension = mime.extension(type); + + link.appendChild(document.createTextNode(download_text)); + link.classList.add("sc-button", "btn-action"); + link.href = content; + link.download = alias; + if (extension !== false) { + link.download = link.download + '.' + extension; + } + container.innerHTML = ""; + container.appendChild(link); + }).catch(function(error) { + console.log(error); + var problem_text = + document.createTextNode("Problem during download"); + + container + .replaceChild(problem_text, container.firstChild); + }); +}; + +module.exports = download_report; \ No newline at end of file diff --git a/Resources/public/modules/download-report/index.js b/Resources/public/modules/download-report/index.js new file mode 100644 index 000000000..1b519e6c2 --- /dev/null +++ b/Resources/public/modules/download-report/index.js @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2018 Champs Libres Cooperative + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +chill.download_report = require("./download-report.js"); diff --git a/Resources/views/Export/download.html.twig b/Resources/views/Export/download.html.twig index d1f670d74..87c8b7989 100644 --- a/Resources/views/Export/download.html.twig +++ b/Resources/views/Export/download.html.twig @@ -27,50 +27,8 @@ window.addEventListener("DOMContentLoaded", function(e) { query = window.location.search, container = document.querySelector("#download_container") ; - - window.fetch(url+query, { credentials: 'same-origin' }) - .then(function(response) { - if (!response.ok) { - throw Error(response.statusText); - } - - return response.blob(); - }).then(function(blob) { - var content = URL.createObjectURL(blob), - link = document.createElement("a"), - suffix_file; - - switch (blob.type) { - case 'application/vnd.oasis.opendocument.spreadsheet': - suffix_file = '.ods'; - break; - case 'text/csv': - suffix_file = '.csv'; - break; - case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': - suffix_file = '.xlsx'; - break; - default: - suffix_file = ''; - } - - link.appendChild(document.createTextNode("{{ "Download your report"|trans }}")); - link.classList.add("sc-button", "btn-action"); - link.href = content; - link.download = "{{ alias }}"+suffix_file; - let waiting_text = container.querySelector("#waiting_text"); - container.removeChild(waiting_text); - container.appendChild(link); - }).catch(function(error) { - var problem_text = - document.createTextNode("{{ "Problem during download"|trans }}"); - - container - .replaceChild(problem_text, container.firstChild); - }) - ; - + chill.download_report(url+query, container); }); {% endblock %} @@ -79,6 +37,6 @@ window.addEventListener("DOMContentLoaded", function(e) {

{{ "Download export"|trans }}

-
{{ "Waiting for your report"|trans }}...
+
{{ "Waiting for your report"|trans }}...
{% endblock %} \ No newline at end of file diff --git a/chill.webpack.config.js b/chill.webpack.config.js index ae87294e0..13bb15998 100644 --- a/chill.webpack.config.js +++ b/chill.webpack.config.js @@ -23,6 +23,7 @@ require('./Resources/public/css/chillmain.css'); require('./Resources/public/css/pikaday.css'); require('./Resources/public/js/collection/collections.js'); require('./Resources/public/modules/breadcrumb/index.js'); +require('./Resources/public/modules/download-report/index.js'); //require('./Resources/public/css/scratch.css'); //require('./Resources/public/css/select2/select2.css'); require('select2/dist/css/select2.css');