*/ $page->layout = 'admin'; if (!User::require_admin()) { $this->redirect('/admin'); } if (!isset($_GET['id'])) { $this->redirect('/form/admin'); } $f = new form\Form($_GET['id']); if ($f->error) { $this->redirect('/form/admin'); } $limit = 20; $_GET['offset'] = isset($_GET['offset']) ? $_GET['offset'] : 0; $results = form\Results::query()->where('form_id', $_GET['id'])->order('ts desc')->fetch($limit, $_GET['offset']); $count = form\Results::query()->where('form_id', $_GET['id'])->count(); // determine which fields to display as columns // as well as the column names $labels = $f->labels(); if ($count > 0) { if (count($labels) === 1) { $field_one = current(array_keys($labels)); $field_one_name = current(array_values($labels)); $field_two_name = ''; foreach ($results as $k => $v) { $res = $v->results; $results[$k]->field_one = is_array($res->{$field_one}) ? join(', ', $res->{$field_one}) : $res->{$field_one}; $results[$k]->field_two = ''; } } elseif (count($labels) > 1) { $keys = array_keys($labels);
/** * Get a count of the results for a particular form. */ function form_results_count($id) { return form\Results::query()->where('form_id', $id)->count(); }
*/ if (!User::require_admin()) { $this->redirect('/admin'); } if (!isset($_GET['id'])) { $this->redirect('/form/admin'); } $f = new form\Form($_GET['id']); if ($f->error) { $this->redirect('/form/admin'); } $page->layout = false; header('Cache-control: private'); header('Content-Type: text/plain'); header('Content-Disposition: attachment; filename=' . preg_replace('/[^a-z0-9_-]+/', '-', strtolower($f->title)) . '-' . gmdate('Y-m-d') . '.csv'); $results = form\Results::query()->order('ts desc')->fetch_orig(); $labels = $f->labels(); echo 'Submitted,IP Address,' . join(',', $labels) . "\n"; foreach ($results as $row) { $sep = ''; echo $row->ts . ',' . $row->ip . ','; $res = json_decode($row->results); foreach ($res as $k => $v) { if (is_array($v)) { $v = join(', ', $v); } $v = str_replace('"', '""', $v); if (strpos($v, ',') !== false) { $v = '"' . $v . '"'; } $v = str_replace(array("\n", "\r"), array('\\n', '\\r'), $v);