Skip to content

Commit 190c3f2

Browse files
committed
download as csv
1 parent fbc0b05 commit 190c3f2

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

app/routers/form.router.php

+34
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,35 @@
165165
})->name('getFormResponses');
166166

167167

168+
// /form/responses/:id_form/download/ controller
169+
// Download responses as csv format
170+
171+
// We create the csv files on the fly
172+
173+
$app->get('/form/responses/:id_form/download', function ($id_form) use ($app) {
174+
175+
$c = array();
176+
177+
// We grab the form
178+
$form = models\Form::find($id_form);
179+
180+
// We grab its fields (will be referenced by their ids on the responses)
181+
$fields = $form->fields;
182+
183+
// We grab its responses
184+
$responses = models\ModelBuilder::fromTable('responses_'.$id_form)->all();
185+
186+
$c['responses'] = $responses;
187+
$c['fields'] = $fields;
188+
189+
$app->response()->header('Content-Type', 'text/csv');
190+
$app->response()->header('Content-Disposition', 'attachment;filename=form'.$id_form.'_export.csv');
191+
192+
$app->render('pages/csv.html', $c);
193+
194+
})->name('getFormResponsesDownload');
195+
196+
168197

169198
// /form/responses/:id_form/delete/:id_response controller
170199
// Delete responses associated to a form
@@ -182,6 +211,11 @@
182211
})->name('getFormResponseDelete');
183212

184213

214+
215+
216+
217+
218+
185219
/*
186220
* POST CONTROLLERS
187221
*/

app/templates/pages/responses.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ <h3>Form details - <a href="{{ base_path }}/index.php/form/edit/{{ form.id }}">E
2727

2828
</dl>
2929

30-
<h3>Form responses</h3>
30+
<h3>Form responses - <a href="{{ base_path }}/index.php/form/responses/{{ form.id }}/download">Download as csv</a></h3>
3131

3232
{% for r in responses %}
3333
<div class="row">

0 commit comments

Comments
 (0)