-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging_table.php
488 lines (453 loc) · 19.5 KB
/
logging_table.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
namespace YaleREDCap\UserRightsHistory;
?>
<div class="module-container">
<?php
if ( $module->getProjectSetting("disable-logging-table") == "1" ) {
header("Location: " . $module->getUrl("history_viewer.php"));
exit();
}
$module->showPageHeader("logging_table");
$module->initializeJavascriptModuleObject();
// If user is in a DAG and settings are appropriate, don't show the logging table.
$current_dag = $module->getCurrentDag($module->getProject()->getProjectId(), $module->getUser()->getUsername());
$prevent_dags_from_seeing_logs = $module->getProjectSetting("prevent_logs_for_dags");
if ( $current_dag != null && $prevent_dags_from_seeing_logs != "1" ) {
echo "<span style='color:#C00000; margin-bottom: 5px;'>Since you have been assigned to a Data Access Group, you are not able to view the User Rights History logs.</span><br>";
exit();
}
// Get User's Date Format
$date_format = \DateTimeRC::get_user_format_php();
$time_format = explode("_", \DateTimeRC::get_user_format_full(), 2)[1];
$datetime_format = $date_format . " " . ($time_format == 24 ? "H:i:S" : "h:i:S K");
[ $initialLogs, $recordsFiltered ] = $module->getLogs([
"start" => 0,
"length" => 10,
"search" => [
"value" => "",
"regex" => false
],
"order" => [
[
"column" => 0,
"dir" => "desc"
]
],
"columns" => [
[
"data" => "timestamp"
]
]
]);
// TODO: use
$event_types = [
"Initialize URH Module",
"Update Project",
"Add User(s)",
"Remove User(s)",
"Add User(s) and Remove User(s)"
];
$messages = [
'dags',
'instruments',
'module enabled by default status',
'module project status',
'module system status',
'module version',
'project_info',
'rights',
'roles',
'system',
'users'
];
$messages_pretty = [];
foreach ( $messages as $message ) {
$messages_pretty[$message] = ucwords(str_replace('_', ' ', $message));
}
?>
<script>
//TODO: Get rid of this nastiness
Array.from(document.styleSheets).forEach(ss => {
try {
if (!ss.href.includes('https://cdn.datatables.net')) {
return Array.from(ss.cssRules).forEach(rule => {
if (rule.cssText.toLowerCase().includes('datatable')) {
rule.style.removeProperty('all')
}
})
}
} catch (err) {
}
})
</script>
<link rel="stylesheet" type="text/css"
href="https://cdn.datatables.net/v/dt/jszip-2.5.0/dt-1.13.1/b-2.3.3/b-html5-2.3.3/b-print-2.3.3/date-1.2.0/rg-1.3.0/datatables.min.css" />
<script type="text/javascript"
src="https://cdn.datatables.net/v/dt/jszip-2.5.0/dt-1.13.1/b-2.3.3/b-html5-2.3.3/b-print-2.3.3/date-1.2.0/rg-1.3.0/datatables.min.js"></script>
<script type="text/javascript">
const module = <?= $module->getJavascriptModuleObjectName() ?>;
const totalRecords = "<?= $module->getTotalLogCount() ?>";
$(document).ready(function () {
module.dt = $('#history_logging_table').DataTable({
processing: true,
serverSide: true,
deferLoading: totalRecords,
searchDelay: 400,
buttons: [{
extend: 'excel',
text: '<i class="fas fa-file-excel"></i> Export to Excel',
action: function (e, dt, node, config) {
var origText = $(node).html();
$(node).html(`<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Loading...`);
var self = this;
var oldStart = dt.settings()[0]._iDisplayStart;
dt.one('preXhr', function (e, s, data) {
data.start = 0;
data.length = 2147483647;
dt.one('preDraw', function (e, settings) {
$.fn.dataTable.ext.buttons.excelHtml5.action.call(self, e, dt, node, config);
dt.one('preXhr', function (e, s, data) {
settings._iDisplayStart = oldStart;
data.start = oldStart;
$(node).html(origText);
});
setTimeout(dt.ajax.reload, 0);
return false;
});
});
dt.ajax.reload();
},
className: 'btn btn-success btn-sm mr-2',
init: function (api, node, config) {
$(node).removeClass('dt-button');
},
filename: `UserRightsHistory_Logging_PID${pid}_${new Date().toISOString().slice(0, 10)}`,
exportOptions: {
format: {
header: function (data, columnIdx, node) {
if (columnIdx == 0) {
return 'Timestamp';
} else if (columnIdx == 1) {
return 'Update Type';
} else if (columnIdx == 2) {
return 'Previous Value';
} else if (columnIdx == 3) {
return 'New Value';
}
return data;
}
}
}
}],
dom: 'Blfrtip',
lengthMenu: [
[10, 25, 50, 100, -1],
[10, 25, 50, 100, 'All']
],
scrollX: true,
scrollCollapse: true,
ajax: function (data, callback, settings) {
const payload = {
"draw": data.draw,
"search": data.search,
"start": data.start,
"length": data.length,
"order": data.order,
"columns": data.columns,
"minDate": $('.timestamp.min').val(),
"maxDate": $('.timestamp.max').val()
};
module.ajax('logging_table_ajax', payload)
.then(response => {
callback(response);
})
.catch(error => {
console.error(error);
});
},
columns: [{
data: 'timestamp',
searchable: false,
render: function (data, type) {
if (type === 'display') {
return flatpickr.formatDate(new Date(data), '<?= $datetime_format ?>');
}
return data;
},
width: "15%"
},
{
data: 'message',
render: function (data, type) {
if (type === 'display') {
return data.replace('_', ' ').replace(/\S+/gm, (match, offset) => {
return ((offset > 0 ? ' ' : '') + match[0].toUpperCase() + match.substr(1));
})
}
return data;
},
width: "20%"
},
{
data: 'previous',
width: "32.5%",
render: function (data, type) {
if (type === 'display') {
return syntaxHighlight(data);
}
return data;
}
},
{
data: 'current',
width: "32.5%",
render: function (data, type) {
if (type === 'display') {
return syntaxHighlight(data);
}
return data;
}
},
],
order: [
[0, 'desc']
],
initComplete: function () {
// Apply the search
let table = this;
this.api()
.columns()
.every(function () {
var that = this;
$('input', this.header()).not('.timestamp').on('keyup change clear', function () {
if (that.search() !== this.value) {
that.search(this.value).draw();
}
});
});
const fp_opts = {
enableTime: true,
enableSeconds: true,
allowInput: true,
altInput: true,
altFormat: "<?= $datetime_format ?>",
dateFormat: "Y-m-d H:i:S",
time_24hr: <?= $time_format == 24 ? "true" : "false" ?>
};
$('input.timestamp.min').flatpickr(Object.assign(fp_opts, {
onClose: function () {
$('#history_logging_table').DataTable().search('').draw();
const fp_max = document.querySelector('input.timestamp.max')._flatpickr;
const fp_min = document.querySelector('input.timestamp.min')._flatpickr;
fp_max.set('minDate', fp_min.selectedDates[0]);
}
}));
$('input.timestamp.max').flatpickr(Object.assign(fp_opts, {
onClose: function () {
$('#history_logging_table').DataTable().search('').draw();
const fp_max = document.querySelector('input.timestamp.max')._flatpickr;
const fp_min = document.querySelector('input.timestamp.min')._flatpickr;
fp_min.set('maxDate', fp_max.selectedDates[0]);
}
}));
$('input.timestamp').attr('onclick', "event.stopPropagation();");
$('.message-select').on('change', function (event) {
$('.message-select').css('color', 'rgb(73, 80, 87)');
const searchValue = event.target.value;
table.DataTable().column(event.target.parentElement).search(searchValue, true).draw();
});
table.DataTable().columns.adjust();
$('table').css('opacity', 1);
table.DataTable().rows().every(function () {
const rowNode = this.node();
const rowIndex = this.index();
$(rowNode).attr('data-dt-row', rowIndex);
});
$('.dataTable tbody tr').each((i, row) => {
row.onmouseenter = hover;
row.onmouseleave = dehover;
});
},
drawCallback: function (settings) {
const table = this.DataTable();
table.rows().every(function () {
const rowNode = this.node();
const rowIndex = this.index();
$(rowNode).attr('data-dt-row', rowIndex);
});
$('.dataTable tbody tr').each((i, row) => {
row.onmouseenter = hover;
row.onmouseleave = dehover;
});
}
});
});
function hover() {
const thisNode = $(this);
const rowIdx = thisNode.attr('data-dt-row');
$("tr[data-dt-row='" + rowIdx + "'] td").addClass("highlight"); // shade only the hovered row
}
function dehover() {
const thisNode = $(this);
const rowIdx = thisNode.attr('data-dt-row');
$("tr[data-dt-row='" + rowIdx + "'] td").removeClass("highlight"); // shade only the hovered row
}
function getIndex(str, char, n) {
return str.split(char).slice(0, n).join(char).length;
}
function getClosestIndex(str, char, max_instances, max_chars = 500) {
let index = Infinity;
let instance = max_instances;
let result;
while (index > max_chars && instance > 0) {
index = getIndex(str, char, instance--);
}
return index;
}
function syntaxHighlight(json) {
if (json === "") json = null;
json = JSON.stringify(JSON.parse(json), undefined, 4);
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
const result = json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
const lines = result.split("\n").length;
let final_result;
if (lines > 10 || result.length > 500) {
const result_preview = result.substring(0, getClosestIndex(result, '\n', 9));
const result_tail = result.substring(getClosestIndex(result, '\n', 9));
final_result = `<pre class="preview" style="margin-bottom:0px !important;padding-bottom:0px !important;">${result_preview}</pre><pre class="break"> ...</pre><pre class="tail" style="display:none; margin-top:0px !important;padding-top:0px !important;">${result_tail}</pre><button type="button" class="btn btn-outline-primary btn-sm" onclick="$(this).siblings('pre.break').toggle();$(this).siblings('pre.tail').slideToggle(500);$(this).text(($(this).text()=='Show More'?'Show Less':'Show More')); $(this).toggleClass('btn-outline-primary').toggleClass('btn-primary');$('#history_logging_table').DataTable().columns.adjust();">Show More</button>`;
} else {
final_result = `<pre>${result}</pre>`;
}
return final_result;
}
</script>
<p>
This page shows the changes to the project in a tabular form. This is useful when searching for a particular
user rights change.
</p>
<br>
<div class="container">
<div class="options">
</div>
<table id="history_logging_table" class="stripe compact cell-border" style="opacity: 0; width: 100%;">
<thead>
<tr>
<th>
Timestamp
<br><input onclick="event.stopPropagation();" class="timestamp min form-control-sm" type="text"
placeholder="Min Timestamp" />
<input onclick="event.stopPropagation();" class="timestamp max form-control-sm" type="text"
placeholder="Max Timestamp" />
</th>
<th>Update Type<br><select class="message-select form-control form-control-sm"
onclick="event.stopPropagation();">
<option hidden>Select Update Type</option>
<option value=""></option>
<?php foreach ( $messages_pretty as $value => $name ) { ?>
<option class="choice" value="<?= $value ?>">
<?= $name ?>
</option>
<?php } ?>
</select></th>
<th>Previous Value<br><input onclick="event.stopPropagation();" class="form-control form-control-sm"
type="text" placeholder="Search Previous Value"></th>
<th>New Value<br><input onclick="event.stopPropagation();" class="form-control form-control-sm"
type="text" placeholder="Search New Value"></th>
</tr>
</thead>
<tbody>
<?php foreach ( $initialLogs as $log ) { ?>
<tr>
<td>
<?= $log["timestamp"] ?>
</td>
<td>
<?= $log["message"] ?>
</td>
<td>
<?= $log["previous"] ?>
</td>
<td>
<?= $log["current"] ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<style>
.string {
color: green;
}
.number {
color: darkorange;
}
.boolean {
color: blue;
}
.null {
color: magenta;
}
.key {
color: darkred;
}
tr.odd {
background-color: transparent !important;
}
pre {
background-color: transparent !important;
border: none !important;
white-space: pre-wrap;
}
.ui-timepicker-div dl dd {
margin: 14px 10px 10px 40%;
}
div.container {
width: 100%;
margin: 0;
padding: 0;
}
table.dataTable#history_logging_table {
border-collapse: collapse !important;
}
.dataTables_processing {
z-index: 9000 !important;
}
.message-select,
.message-select:focus {
color: rgb(187, 187, 187);
}
.message-select .choice {
color: rgb(73, 80, 87) !important;
}
th {
border-top: 1px solid rgba(0, 0, 0, 0.15);
border-bottom: 1px solid rgba(0, 0, 0, 0.15);
border-right: 1px solid rgba(0, 0, 0, 0.15);
}
table#history_logging_table,
th:first-child {
border-left: 1px solid rgba(0, 0, 0, 0.15) !important;
}
.dataTable thead tr {
background-color: rgb(220, 220, 220) !important;
}
td.highlight {
background-color: #d9ebf5 !important;
}
</style>
</div>