-
Notifications
You must be signed in to change notification settings - Fork 46
/
exportcsv.php
137 lines (118 loc) · 3.93 KB
/
exportcsv.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* This software is governed by the CeCILL-B license. If a copy of this license
* is not distributed with this file, you can obtain one at
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
*
* Authors of STUdS (initial project): Guilhem BORGHESI ([email protected]) and Raphaël DROZ
* Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
*
* =============================
*
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
*
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI ([email protected]) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
use Framadate\Services\LogService;
use Framadate\Services\PollService;
use Framadate\Services\SecurityService;
use Framadate\Utils;
include_once __DIR__ . '/app/inc/init.php';
ob_start();
/* Variables */
/* --------- */
$poll_id = null;
$poll = null;
/* Services */
/*----------*/
$logService = new LogService();
$pollService = new PollService($logService);
$securityService = new SecurityService();
/* Globals */
global $smarty;
global $date_format;
/* PAGE */
/* ---- */
if (!empty($_GET['poll'])) {
$poll_id = filter_input(INPUT_GET, 'poll', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => POLL_REGEX]]);
$poll = $pollService->findById($poll_id);
} else if (!empty($_GET['admin'])) {
$admin_id = filter_input(INPUT_GET, 'admin', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => ADMIN_POLL_REGEX]]);
$poll = $pollService->findByAdminId($admin_id);
if ($poll) {
$poll_id = $poll->id;
}
}
if (!$poll) {
$smarty->assign('error', __('Error', 'This poll doesn\'t exist !'));
$smarty->display('error.tpl');
exit;
}
if (empty($admin_id)) {
$forbiddenBecauseOfPassword = !$poll->results_publicly_visible && !$securityService->canAccessPoll($poll);
$resultsAreHidden = $poll->hidden;
if ($resultsAreHidden || $forbiddenBecauseOfPassword) {
$smarty->assign('error', __('Error', 'Forbidden!'));
$smarty->display('error.tpl');
exit;
}
}
$slots = $pollService->allSlotsByPoll($poll);
$votes = $pollService->allVotesByPollId($poll_id);
// CSV header
if ($poll->format === 'D') {
$titles_line = ',';
$moments_line = ',';
foreach ($slots as $slot) {
$title = Utils::csvEscape(formatDate($date_format['txt_date'], $slot->title));
$moments = explode(',', $slot->moments);
$titles_line .= str_repeat($title . ',', count($moments));
$moments_line .= implode(',', array_map('\Framadate\Utils::csvEscape', $moments)) . ',';
}
echo $titles_line . "\r\n";
echo $moments_line . "\r\n";
} else {
echo ',';
foreach ($slots as $slot) {
echo Utils::csvEscape(Utils::markdown($slot->title, true)) . ',';
}
echo "\r\n";
}
// END - CSV header
// Vote lines
foreach ($votes as $vote) {
echo Utils::csvEscape($vote->name) . ',';
$choices = str_split($vote->choices);
foreach ($choices as $choice) {
switch ($choice) {
case 0:
$text = __('Generic', 'No');
break;
case 1:
$text = __('Generic', 'Ifneedbe');
break;
case 2:
$text = __('Generic', 'Yes');
break;
default:
$text = __('Generic', 'Unknown');
}
echo Utils::csvEscape($text);
echo ',';
}
echo "\r\n";
}
// END - Vote lines
// HTTP headers
$content = ob_get_clean();
$filesize = strlen($content);
$filename = Utils::cleanFilename($poll->title) . '.csv';
header('Content-Type: text/csv; charset=utf-8');
header('Content-Length: ' . $filesize);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Cache-Control: max-age=10');
// END - HTTP headers
echo $content;