-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.php
More file actions
285 lines (255 loc) · 12.5 KB
/
print.php
File metadata and controls
285 lines (255 loc) · 12.5 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* file: print.php
*
* Printer-friendly view of the timeline.
*
* Generates a simplified, printable list of all events and relations for the logged-in user.
*/
require_once 'auth.php';
require_once 'config.php';
require_once 'language_loader.php';
require_once 'src/SecurityService.php';
$sec = new SecurityService($pdo, ENCRYPTION_KEY);
// Haal de ingelogde gebruiker's ID op uit de sessie
$user_id = $_SESSION['user_id'];
// --- 1. Haal gebruikersinformatie op ---
$user_stmt = $pdo->prepare("SELECT full_name, date_of_birth FROM users WHERE id = ?");
$user_stmt->execute([$user_id]);
$user = $user_stmt->fetch();
$user_full_name = $sec->decrypt($user['full_name']) ?? $lang['unknown_user'];
$decrypted_dob = $sec->decrypt($user['date_of_birth']);
$user_dob_year = $decrypted_dob ? (int)date('Y', strtotime($decrypted_dob)) : 1982;
// --- 2. Data ophalen voor Gebeurtenissen (specifiek voor de ingelogde user) ---
$stmt = $pdo->prepare("SELECT * FROM gebeurtenissen WHERE user_id = ? ORDER BY datum ASC");
$stmt->execute([$user_id]);
$events = $stmt->fetchAll();
// --- 3. Data ophalen voor Verbanden (specifiek voor de ingelogde user) ---
$stmt_links = $pdo->prepare("SELECT * FROM verbanden WHERE user_id = ?");
$stmt_links->execute([$user_id]);
$links = $stmt_links->fetchAll();
// Helper functie om datum naar decimaal jaar om te zetten
function dateToDecimalYear($date) {
$timestamp = strtotime($date);
$year = (int)date('Y', $timestamp);
$dayOfYear = (int)date('z', $timestamp);
$daysInYear = (int)date('L', $timestamp) ? 366 : 365;
return $year + ($dayOfYear / $daysInYear);
}
// --- 4. Raw data array bouwen EN een map maken ---
$rawData = [];
$counter = 1;
$db_id_to_chart_data = []; // Map [db_id] => [chart_data_point]
foreach ($events as $event) {
$decimalYear = dateToDecimalYear($event['datum']);
$score = (int)$event['score'];
$vid = str_pad($counter, 3, '0', STR_PAD_LEFT);
$dataPoint = [
'year' => $decimalYear,
'score' => $score,
'title' => htmlspecialchars($sec->decrypt($event['titel'])),
'details' => nl2br(htmlspecialchars($sec->decrypt($event['details']))),
'type' => ($score >= 0) ? 'pos' : 'neg',
'id' => $vid, // Visuele ID
'real_date' => date('d-m-Y', strtotime($event['datum'])),
'db_id' => (int)$event['id'] // ECHTE DATABASE ID
];
$rawData[] = $dataPoint;
// Sla de mapping op van de database ID naar het volledige data-object
$db_id_to_chart_data[$event['id']] = $dataPoint;
$counter++;
}
// --- 5. Verbanden (links) data voorbereiden ---
$linksData = [];
foreach ($links as $link) {
$id_begin = $link['gebeurtenis_id_begin'];
$id_eind = $link['gebeurtenis_id_eind'];
// Controleer of beide punten (nog) bestaan in onze data
if (isset($db_id_to_chart_data[$id_begin]) && isset($db_id_to_chart_data[$id_eind])) {
$point_begin = $db_id_to_chart_data[$id_begin];
$point_eind = $db_id_to_chart_data[$id_eind];
$linksData[] = [
'begin' => ['x' => $point_begin['year'], 'y' => $point_begin['score']],
'end' => ['x' => $point_eind['year'], 'y' => $point_eind['score']],
'desc' => htmlspecialchars($link['link_desc']),
'vid_begin' => $point_begin['id'], // Visuele ID voor tooltip
'vid_eind' => $point_eind['id'] // Visuele ID voor tooltip
];
}
}
// Bepaal start en eindjaar voor de grafiek as
$startYear = $user_dob_year; // Gebruik geboortejaar van gebruiker
$endYear = (int)date('Y') + 1;
if (!empty($rawData)) {
$minYearInData = floor(min(array_column($rawData, 'year')));
// De start van de grafiek is altijd het geboortejaar, tenzij er data van daarvoor is.
$startYear = min($startYear, $minYearInData);
$maxYearInData = ceil(max(array_column($rawData, 'year')));
$endYear = max($endYear, $maxYearInData);
}
?>
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<title><?php echo sprintf($lang['page_title_print'], htmlspecialchars($user_full_name)); ?></title>
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
<link rel="manifest" href="site.webmanifest">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2.0.0"></script>
<style>
body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; background-color: #fff; color: #111827; }
.print-container { max-width: 1600px; margin: 2rem auto; padding: 2rem; }
h1 { font-size: 2.25rem; font-weight: 700; text-align: center; margin-bottom: 0.5rem; color: #111827; }
p.subtitle { text-align: center; font-size: 1.125rem; color: #6b7280; margin-bottom: 3rem; }
#timelineChart { max-height: 70vh; }
.events-list { margin-top: 4rem; }
.event-item { page-break-inside: avoid; border-left: 3px solid #d1d5db; padding: 1rem 1.5rem; margin-bottom: 1.5rem; }
.event-item.positive { border-left-color: #16a34a; }
.event-item.negative { border-left-color: #dc2626; }
.event-header { display: flex; align-items: baseline; justify-content: space-between; }
.event-title { font-size: 1.25rem; font-weight: 600; color: #111827; }
.event-meta { font-size: 0.9rem; font-weight: 500; color: #4b5563; }
.event-details { margin-top: 0.5rem; color: #374151; line-height: 1.6; }
.event-id { font-weight: 700; color: #6b7280; margin-right: 0.75rem; font-size: 1rem; }
@media print {
body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.print-container { margin: 0; padding: 0; max-width: none; }
h1, p.subtitle { margin-bottom: 1.5rem; }
.events-list { margin-top: 2rem; }
}
</style>
</head>
<body>
<div class="print-container">
<h1><?php echo sprintf($lang['index_heading'], htmlspecialchars($user_full_name)); ?></h1>
<p class="subtitle"><?php echo $lang['print_subtitle']; ?></p>
<div style="width: 100%; margin: 0 auto;">
<canvas id="timelineChart"></canvas>
</div>
<div class="events-list">
<?php foreach ($rawData as $event): ?>
<div class="event-item <?php echo $event['score'] >= 0 ? 'positive' : 'negative'; ?>">
<div class="event-header">
<h2 class="event-title">
<span class="event-id"><?php echo $event['id']; ?>.</span>
<?php echo $event['title']; ?>
</h2>
<span class="event-meta">
<?php echo $event['real_date']; ?> | <?php echo $lang['print_score']; ?>: <?php echo $event['score']; ?>
</span>
</div>
<p class="event-details"><?php echo $event['details']; ?></p>
</div>
<?php endforeach; ?>
</div>
</div>
<script>
const rawData = <?php echo json_encode($rawData); ?>;
const linksData = <?php echo json_encode($linksData); ?>;
const startYear = <?php echo $startYear; ?>;
const endYear = <?php echo $endYear; ?>;
const posBandData = [];
const negBandData = [];
const netTrendData = [];
// Trendberekeningen
for (let year = startYear; year <= endYear; year += 0.25) {
const eventsUntilNow = rawData.filter(d => d.year <= year);
let currentPosScoreSum = 0, currentNegScoreSum = 0, netScoreSum = 0;
eventsUntilNow.forEach(event => {
if (event.score > 0) {
currentPosScoreSum += event.score;
netScoreSum += event.score;
} else {
currentNegScoreSum += Math.abs(event.score);
netScoreSum += event.score;
}
});
const yearsPassed = Math.max(1, year - startYear);
posBandData.push({ x: year, y: (currentPosScoreSum / yearsPassed) });
negBandData.push({ x: year, y: -(currentNegScoreSum / yearsPassed) });
netTrendData.push({ x: year, y: (netScoreSum / yearsPassed) });
}
const POSITIVE_COLOR = 'rgb(22, 163, 74)';
const NEGATIVE_COLOR = 'rgb(220, 38, 38)';
const LINE_COLOR = 'rgba(107, 114, 128, 0.8)';
const POS_FILL_COLOR = 'rgba(74, 222, 128, 0.2)';
const NEG_FILL_COLOR = 'rgba(248, 113, 113, 0.2)';
// Plugin voor verbandenlijnen
const linksPlugin = {
id: 'drawLinks',
beforeDatasetsDraw: (chart) => {
const ctx = chart.ctx;
const xAxis = chart.scales.x;
const yAxis = chart.scales.y;
ctx.save();
ctx.strokeStyle = 'rgba(59, 130, 246, 0.8)';
ctx.lineWidth = 1.5;
ctx.setLineDash([4, 4]);
linksData.forEach(link => {
ctx.beginPath();
ctx.moveTo(xAxis.getPixelForValue(link.begin.x), yAxis.getPixelForValue(link.begin.y));
ctx.lineTo(xAxis.getPixelForValue(link.end.x), yAxis.getPixelForValue(link.end.y));
ctx.stroke();
});
ctx.restore();
}
};
Chart.register(linksPlugin, ChartDataLabels);
const ctx = document.getElementById('timelineChart').getContext('2d');
const chart = new Chart(ctx, {
type: 'line',
data: {
datasets: [
{ label: '<?php echo $lang['index_chart_label_positive']; ?>', data: posBandData, borderColor: 'transparent', backgroundColor: POS_FILL_COLOR, fill: 'origin', pointRadius: 0, tension: 0.4 },
{ label: '<?php echo $lang['index_chart_label_negative']; ?>', data: negBandData, borderColor: 'transparent', backgroundColor: NEG_FILL_COLOR, fill: 'origin', pointRadius: 0, tension: 0.4 },
{
label: '<?php echo $lang['index_chart_label_net']; ?>', data: netTrendData,
segment: { borderColor: ctx => ctx.p0.parsed.y >= 0 ? POSITIVE_COLOR : NEGATIVE_COLOR },
borderWidth: 2.5, pointRadius: 0, tension: 0.4, fill: false
},
{
label: '<?php echo $lang['index_chart_label_events']; ?>',
data: rawData.map(d => ({ x: d.year, y: d.score, raw: d })),
borderColor: LINE_COLOR, borderWidth: 1.5, tension: 0.1,
pointBackgroundColor: rawData.map(d => d.score >= 0 ? POSITIVE_COLOR : NEGATIVE_COLOR),
pointBorderColor: '#fff', pointBorderWidth: 1, pointRadius: 5
}
]
},
options: {
responsive: true, maintainAspectRatio: true,
scales: {
x: {
type: 'linear', min: startYear, max: endYear,
ticks: { stepSize: 2, font: { size: 10 } },
grid: { color: '#e5e7eb' },
title: { display: true, text: '<?php echo $lang['index_chart_axis_year']; ?>', font: { weight: 'bold' } }
},
y: {
min: -11, max: 11, ticks: { stepSize: 2 },
grid: { color: ctx => ctx.tick.value === 0 ? '#9ca3af' : '#e5e7eb', lineWidth: ctx => ctx.tick.value === 0 ? 1.5 : 1 },
title: { display: true, text: '<?php echo $lang['index_chart_axis_impact']; ?>', font: { weight: 'bold' } }
}
},
plugins: {
legend: { display: false },
tooltip: { enabled: false },
datalabels: {
align: 'top', offset: 4, font: { size: 9, weight: '600' },
color: context => context.dataset.data[context.dataIndex].y >= 0 ? '#14532d' : '#991b1b',
formatter: (value, context) => context.datasetIndex === 3 ? rawData[context.dataIndex].id : ''
}
},
animation: {
onComplete: () => {
window.print();
}
}
}
});
</script>
</body>
</html>