-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpage-export.php
229 lines (194 loc) · 7.86 KB
/
page-export.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
include( 'constants.php' );
$format = $_GET['format'];
$oipa_url = OIPA_URL . '/activities/';
function maybeEncodeCSVField($string) {
if(strpos($string, ',') !== false || strpos($string, '"') !== false || strpos($string, "\n") !== false) {
$string = '"' . str_replace('"', '""', $string) . '"';
}
return $string;
}
switch ($_GET['type']) {
case 'activity-list':
$oipa_url .= '?page_size=1000&fields=all&reporting_organisation_identifier=XM-DAC-7&format=' . $format;
$oipa_filter = $_GET['filters'];
$url = $oipa_url . urldecode($oipa_filter);
break;
case 'activity-detail':
$url = $oipa_url . '?format=' . $format . '&id=' . $_GET['detail'] . '&fields=all';
break;
case 'aggregated-list':
// get the json, if format = csv re-format to csv here
$aggregation_url = $_GET['aggregation_url'];
$url = $oipa_url . urldecode($aggregation_url);
if($format == 'csv'){
// get json data
$url = str_replace('format=csv', 'format=json', $url);
$json_str = file_get_contents($url);
$json_obj = json_decode($json_str, true);
// reformat to csv
$group_by = $_GET['aggregation_group'];
if($group_by == 'participating_organisation'){
// init csv with headers
$csv = array("code,name,count\n");
// add csv data
foreach ($json_obj['results'] as $row) {
$code = maybeEncodeCSVField($row['ref']);
$name = maybeEncodeCSVField($row['name']);
$count = maybeEncodeCSVField($row['count']);
$row_arr = array();
array_push($row_arr, $code);
array_push($row_arr, $name);
array_push($row_arr, $count);
$row_str = implode(",", $row_arr) . "\n";
array_push($csv, $row_str);
}
} else {
// init csv with headers
$csv = array("code,name,count,budget\n");
// add csv data
foreach ($json_obj['results'] as $row) {
$code = maybeEncodeCSVField($row[$group_by]['code']);
$name = maybeEncodeCSVField($row[$group_by]['name']);
$count = maybeEncodeCSVField($row['count']);
$incoming_fund = maybeEncodeCSVField($row['incoming_fund']);
$row_arr = array();
array_push($row_arr, $code);
array_push($row_arr, $name);
array_push($row_arr, $count);
array_push($row_arr, $incoming_fund);
$row_str = implode(",", $row_arr) . "\n";
array_push($csv, $row_str);
}
}
// // return to user
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=export.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo implode('', $csv);
exit();
}
break;
case 'sector-detail':
$name = urldecode($_GET['sector_name']);
$code = $_GET['detail'];
$budget = $_GET['budget'];
$expenditure = $_GET['expenditure'];
if($format == 'json'){
$json = array(
'code'=>$code,
'name'=>$name,
'budget'=>$budget,
'expenditure'=>$expenditure
);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.json");
echo json_encode($json);
exit();
} else {
// header row
$row_arr = "code,name,budget,expenditure\n";
$row = array();
array_push($row, $code);
array_push($row, $name);
array_push($row, $budget);
array_push($row, $expenditure);
$row = implode(',', $row) . "\n";
$csv = array($row_arr, $row);
// // return to user
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=export.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo implode('', $csv);
exit();
}
break;
case 'receiver-org-detail':
$code = $_GET['detail'];
$budget = $_GET['budget'];
$expenditure = $_GET['expenditure'];
if($format == 'json'){
$json = array(
'name'=>$code,
'budget'=>$budget,
'expenditure'=>$expenditure
);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.json");
echo json_encode($json);
exit();
} else {
// header row
$row_arr = "name,budget,expenditure\n";
$row = array();
array_push($row, $code);
array_push($row, $budget);
array_push($row, $expenditure);
$row = implode(',', $row) . "\n";
$csv = array($row_arr, $row);
// // return to user
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=export.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo implode('', $csv);
exit();
}
break;
case 'country-detail':
$endpoint = $_GET['endpoint'];
$fields = $_GET['fields'];
$code = $_GET['detail'];
$budget = $_GET['budget'];
$expenditure = $_GET['expenditure'];
$url = $oipa_url = OIPA_URL . '/' . $endpoint . '/?format=json&fields=' . $fields . '&code=' . $code;
$json_str = file_get_contents($url);
$json_obj = json_decode($json_str, true);
$fields_arr = explode(',', $fields);
if($format == 'json'){
$json_obj['results'][0]['budget'] = $budget;
$json_obj['results'][0]['expenditure'] = $expenditure;
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.json");
echo json_encode($json_obj['results'][0]);
exit();
} else {
// header row
$row_arr = array();
foreach($fields_arr as $field){
array_push($row_arr, $field);
}
array_push($row_arr, 'budget');
array_push($row_arr, 'expenditure');
$row = implode(',', $row_arr) . "\n";
$csv = array($row);
// reformat to csv
foreach ($json_obj['results'] as $row) {
$row_arr = array();
foreach($fields_arr as $field){
$field_value = maybeEncodeCSVField($row[$field]);
array_push($row_arr, $field_value);
}
$budget = maybeEncodeCSVField($budget);
$expenditure = maybeEncodeCSVField($expenditure);
array_push($row_arr, $budget);
array_push($row_arr, $expenditure);
$row_str = implode(",", $row_arr) . "\n";
array_push($csv, $row_str);
// // return to user
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=export.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo implode('', $csv);
exit();
}
}
break;
}
$filename = 'export.' . $format;
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . $filename);
readfile($url);