-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
executable file
·269 lines (261 loc) · 10.4 KB
/
index.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<html lang="et">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Aruanded · Eesti Kommunismiohvrid 1940–1991</title>
<link
href="https://cloud.typography.com/6935656/6118392/css/fonts.css"
rel="stylesheet"
type="text/css"
/>
<link href="styles.css" rel="stylesheet" />
</head>
<?php
$env = parse_ini_file('.env');
$servername = $env['DB_SERVERNAME'] ?? null;
$username = $env['DB_USERNAME'] ?? null;
$password = $env['DB_PASSWORD'] ?? null;
$db = $env['DB_NAME'] ?? null;
$jsonFilters = $env['JSON_FILTERS'];
$jsonTableRows = $env['JSON_TABLEROWS'];
require_once ('SSHMysql.php');
$sshMySQL = new SSHMysql();
$jsonFilters = file_get_contents($jsonFilters);
$filtersArray = json_decode($jsonFilters, true);
$SQL = $filtersArray['sql'];
$filters = $filtersArray['filters'];
$jsonTableRows = file_get_contents($jsonTableRows);
$tableRows = json_decode($jsonTableRows, true);
$subelements = $definedColumns = [];
$currentYear = 1941;
$definedColumns['default'] = $filtersArray['columns'];
$columns = $definedColumns;
foreach ($filters as $group) {
foreach ($group as $id => $filter) {
if (isset($filter['occurrence']) && $filter['occurrence'] > $currentYear) {
$currentYear = $filter['occurrence']; // age is calculated by latest occurrence
}
$definedColumns[$id] = $group['data']['columns'];
$subelements[$id] = $filter;
}
}
$filterParams = ['select', 'join', 'where'];
foreach ($_GET as $parameter => $value) {
if (array_key_exists($parameter, $subelements)) {
if ($value) {
$columns[] = $definedColumns[$parameter];
}
$filterSQL = $subelements[$parameter]['sql'];
foreach ($filterParams as $filterParam) {
if (array_key_exists($filterParam, $filterSQL) && $value !== '') {
if (is_array($filterSQL[$filterParam])) {
foreach ($filterSQL[$filterParam] as $key => $filter) {
$filter = str_replace('{{value}}', $value, $filter);
$SQL[$filterParam][$key][] = $filter;
}
} else {
$filterSQL[$filterParam] = str_replace('{{value}}', $value, $filterSQL[$filterParam]);
$SQL[$filterParam][] = $filterSQL[$filterParam];
}
}
}
if (array_key_exists('extra_sql', $subelements[$parameter]) && $subelements[$parameter] !== '') {
$SQL['extra_sql'] = $subelements[$parameter]['extra_sql'];
}
}
}
$columnCount = 0;
foreach ($columns as $column) {
if (count($column) > $columnCount) {
$shownColumns = $column;
$columnCount = count($column);
}
}
foreach (['repressions', 'arrested', 'fled', 'military', 'gender'] as $subcriteria) {
if (isset($SQL['where'][$subcriteria])) {
$SQL['where'][$subcriteria] = '(' . implode(' OR ' , $SQL['where'][$subcriteria]) . ')';
}
}
$select = implode(', ' , $SQL['select']);
$from = implode(', ' , $SQL['from']);
$join = (isset($SQL['join'])) ? implode(' ' , $SQL['join']) : '';
$where = (isset($SQL['where']) && count($SQL['where'])) ? 'WHERE ' . implode(' AND ' , $SQL['where']) : '';
$group_by = implode(', ' , $SQL['group_by']);
$order_by = implode(', ' , $SQL['order_by']);
$sql = "SELECT $select FROM $from $join $where GROUP BY $group_by ORDER BY $order_by ASC";
$result = $sshMySQL->query($sql);
$resultArray = [];
$sshError = null;
if ($result['status'] === 'success') {
$resultArray = $result['dataset'];
} elseif(isset($result['errorset'])) {
$sshError = $result['errorset'];
} else {
$sshError = $result['msg'];
}
$ages = $tableRows;
$total['repressed'] = $totalFaulty['repressed'] = 0;
$total['arrested'] = $totalFaulty['arrested'] = 0;
$total['deported'] = $totalFaulty['deported'] = 0;
foreach ($resultArray as $item) {
$age = $currentYear - (int) $item->year;
$floor = roundDownToFive($age);
if (array_key_exists((string) $floor, $ages)) {
$ages[$floor]['repressed'] += $item->records;
} else {
$totalFaulty['repressed'] += $item->records;
}
$total['repressed'] += $item->records;
}
if (isset($SQL['extra_sql']) && count($columns) === 2) {
foreach ($SQL['extra_sql'] as $key => $extraSQL) {
$extraResult = $sshMySQL->query($extraSQL);
$extraResult = $extraResult['dataset'];
foreach ($extraResult as $item) {
$age = $currentYear - (int) $item->year;
$floor = roundDownToFive($age);
if (array_key_exists((string) $floor, $ages)) {
$ages[$floor][$key] += $item->records;
} else {
$totalFaulty[$key] += $item->records;
}
$total[$key] += $item->records;
}
}
}
function roundDownToFive($n,$x=5) {
return floor( $n / $x ) * $x;
}
?>
<body style="/*user-select: none*/">
<div class="col-12">
<div class="reports-content-wrapper">
<div class="reports-wrapper">
<div class="reports-totals reports-totals-by-age-group">
<h5 class="repressed-total-by-age-group">Represseeritute/põgenike üldarv vanusegrupi järgi</h5>
<?php
if ($sshError) {
?>
<h5 class="repressed-total-by-age-group"><?=$sshError?></h5>
<?php
}
?>
</div>
<div class="reports-table-wrapper">
<table class="reports-table">
<thead class="reports-table-head">
<tr class="reports-table-row">
<th class="reports-table-age-group">Vanusegrupp</th>
<?php
foreach ($shownColumns as $columnId => $columnText) {
?>
<th class="reports-table-number-of-<?=$columnId?>"><?=$columnText?></th>
<?php
}
?>
</tr>
</thead>
<tbody class="reports-table-body">
<?php
foreach ($ages as $age => $value) {
?>
<tr class="reports-table-row">
<td><?=$value['text']?></td>
<?php
foreach ($shownColumns as $columnId => $columnText) {
?>
<td class="reports-table-data"><?=$value[$columnId];?></td>
<?php
}
?>
</tr>
<?php
}
?>
<tr class="reports-table-row">
<td>Andmed puuduvad:</td>
<?php
foreach ($shownColumns as $columnId => $columnText) {
?>
<td class="reports-table-total-number-of-<?=$columnId?> reports-table-data"><?=$totalFaulty[$columnId];?></td>
<?php
}
?>
</tr>
<tr class="reports-table-row">
<td class="reports-table-total-text">Kokku:</td>
<?php
foreach ($shownColumns as $columnId => $columnText) {
?>
<td class="reports-table-total-number-<?=$columnId?> reports-table-data"><?=$total[$columnId];?></td>
<?php
}
?>
</tr>
</tbody>
</table>
</div>
</div>
<div class="filters-wrapper">
<form
class="reports-filters-form"
action="index.php"
method="GET"
>
<h5>Filtrid:</h5>
<div class="report-filter-inputs-wrapper">
<?php
foreach ($filters as $groupId => $group) {
?>
<div id="<?=$groupId?>">
<h6><?=$group['data']['header']?></h6>
<?php
foreach ($group as $id => $filter) {
if ($id === 'data' || (isset($filter['visible']) && $filter['visible'] === 'false')) {
continue;
}
$label = (isset($filter['label'])) ? $filter['label'] : '';
$placeholder = (isset($filter['placeholder'])) ? 'placeholder="' . $filter['placeholder'] . '"' : '';
switch ($filter['type']) {
case 'checkbox':
$class = 'report-filter-checkbox';
$type = 'checkbox';
$wrapperDiv = '<div class="report-filter-checkbox-wrapper">';
$wrapperEnd = '</div>';
$value = (isset($_GET[$id]) && $_GET[$id] === 'on') ? 'checked="checked"' : '';
break;
case 'input':
$class = 'report-filter-text-input';
$type = 'search';
$wrapperDiv = $wrapperEnd = '';
$value = (isset($_GET[$id]) && $_GET[$id] !== '') ? 'value="' . $_GET[$id] . '"' : '';
}
?>
<?=$wrapperDiv?>
<input
class="<?=$class?>"
type="<?=$type?>"
<?=(isset($filter['disabled']) && $filter['disabled'] === 'true') ? 'disabled' : '';?>
name="<?=$id?>"
<?=$placeholder?>
<?=$value?>
/>
<label for="<?=$id?>"><?=$label?></label>
<?=$wrapperEnd?>
<?php
}
?>
</div>
<?php
}
?>
<button class="report-filter-submit-button" type="submit">
Filtreeri
</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>