chill-bundles/Resources/views/Export/download.html.twig
Julien Fastré b6d1f05e00 Improve type detection
We use blob.type property to detect the content-type
2017-04-19 14:24:34 +02:00

84 lines
3.2 KiB
Twig

{#
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
<info@champs-libres.coop> / <http://www.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/>.
#}
{% extends "ChillMainBundle::layoutWithVerticalMenu.html.twig" %}
{% block title "Download export"|trans %}
{% block js %}
<script type="text/javascript">
window.addEventListener("DOMContentLoaded", function(e) {
var url = "{{ path('chill_main_export_generate', { 'alias' : alias } ) }}",
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);
})
;
});
</script>
{% endblock %}
{% block layout_wvm_content %}
<h1>{{ "Download export"|trans }}</h1>
<div id="download_container"><span id="waiting_text">{{ "Waiting for your report"|trans }}...</span></div>
{% endblock %}