mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
improve script for downloading exports
This commit is contained in:
parent
dd05fa0be7
commit
587b248549
@ -412,9 +412,29 @@ class ExportController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function downloadResultAction(Request $request, $alias)
|
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
|
'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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -437,16 +437,16 @@ class ExportManager
|
|||||||
|
|
||||||
$result = $export->getResult($query, $data[ExportType::EXPORT_KEY]);
|
$result = $export->getResult($query, $data[ExportType::EXPORT_KEY]);
|
||||||
|
|
||||||
if (!is_array($result)) {
|
if (!is_iterable($result)) {
|
||||||
throw new \UnexpectedValueException(
|
throw new \UnexpectedValueException(
|
||||||
sprintf(
|
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)
|
gettype($result)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @var $formatter Formatter\CSVFormatter */
|
/* @var $formatter FormatterInterface */
|
||||||
$formatter = $this->getFormatter($this->getFormatterAlias($data));
|
$formatter = $this->getFormatter($this->getFormatterAlias($data));
|
||||||
$filtersData = array();
|
$filtersData = array();
|
||||||
$aggregatorsData = array();
|
$aggregatorsData = array();
|
||||||
|
67
Resources/public/modules/download-report/download-report.js
Normal file
67
Resources/public/modules/download-report/download-report.js
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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;
|
18
Resources/public/modules/download-report/index.js
Normal file
18
Resources/public/modules/download-report/index.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
chill.download_report = require("./download-report.js");
|
@ -27,50 +27,8 @@ window.addEventListener("DOMContentLoaded", function(e) {
|
|||||||
query = window.location.search,
|
query = window.location.search,
|
||||||
container = document.querySelector("#download_container")
|
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);
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@ -79,6 +37,6 @@ window.addEventListener("DOMContentLoaded", function(e) {
|
|||||||
|
|
||||||
<h1>{{ "Download export"|trans }}</h1>
|
<h1>{{ "Download export"|trans }}</h1>
|
||||||
|
|
||||||
<div id="download_container"><span id="waiting_text">{{ "Waiting for your report"|trans }}...</span></div>
|
<div id="download_container" data-alias="{{ alias|escape('html_attr') }}" {% if mime_type is defined %}data-mime-type="{{ mime_type|escape('html_attr') }}"{% endif %} data-download-text="{{ "Download your report"|trans|escape('html_attr') }}"><span id="waiting_text">{{ "Waiting for your report"|trans }}...</span></div>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
@ -23,6 +23,7 @@ require('./Resources/public/css/chillmain.css');
|
|||||||
require('./Resources/public/css/pikaday.css');
|
require('./Resources/public/css/pikaday.css');
|
||||||
require('./Resources/public/js/collection/collections.js');
|
require('./Resources/public/js/collection/collections.js');
|
||||||
require('./Resources/public/modules/breadcrumb/index.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/scratch.css');
|
||||||
//require('./Resources/public/css/select2/select2.css');
|
//require('./Resources/public/css/select2/select2.css');
|
||||||
require('select2/dist/css/select2.css');
|
require('select2/dist/css/select2.css');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user