-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileUploadEmbed.php
118 lines (96 loc) · 4.69 KB
/
FileUploadEmbed.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
<?php
namespace UIOWA\FileUploadEmbed;
class FileUploadEmbed extends \ExternalModules\AbstractExternalModule {
function redcap_module_system_enable($version) {
$key = 'allowed-file-extension';
// if no file types already exist, set default
if (!$this->getSystemSetting($key)) {
$this->setSystemSetting($key, array(
'pdf',
'jpg',
'jpeg',
'png'
));
}
}
function redcap_data_entry_form($project_id, $record, $instrument, $event_id, $group_id, $repeat_instance)
{
// only embed if record has been saved
if (isset($record)) {
$enabledFields = $this->getProjectSetting('allowed-upload-field');
$supportedFiletypes = $this->getSystemSetting('allowed-file-extension');
// get doc_id(s)
$data = \REDCap::getData(array(
'records' => $record,
'fields' => $enabledFields,
'events' => $event_id,
'groups' => $group_id,
'return_format' => 'json'
));
$data = json_decode($data, true);
// get specific instance if repeatable
if (isset($repeat_instance)) {
$data = $data[$repeat_instance - 1]; // todo better way to get instance id?
}
foreach($data as $field => $doc_id) {
// remove any fields that are not File Upload type
if (\REDCap::getFieldType($field) !== 'file') {
unset($data[$field]);
}
else {
// Returns array of "mime_type" (string), "doc_name" (string), and "contents" (string) or FALSE if failed
$docInfo = \Files::getEdocContentsAttributes($doc_id);
$uploadedFileType = \Files::get_file_extension_by_mime_type($docInfo[0]);
// exit if file type isn't whitelisted in module config or couldn't lookup mime type
if (!in_array($uploadedFileType, $supportedFiletypes) || !$uploadedFileType) {
$this->exitAfterHook();
}
// generate verification hash
$hash = \Files::docIdHash($doc_id);
// get embed url
$data[$field] = $this->getUrl("index.php?id=" . $doc_id . '&doc_id_hash=' . $hash);
}
}
// exit if no valid fields found
if ($data == NULL || !count($data)) {
$this->exitAfterHook();
}
$data = htmlspecialchars(json_encode($data),ENT_QUOTES);
?>
<script>
$(document).ready(function() {
const data = <?= json_encode($data); ?>;
const dataParsed = JSON.parse(data.replaceAll(""", '"').replaceAll("&", "&"))
$.each(dataParsed, function(field, url) {
const splitUrl = url.split("&")
const getIdIndex = splitUrl.findIndex(function(item){
return item.indexOf("id=")!==-1;
});
const getId = splitUrl[getIdIndex].split("=")[1]
if(getId !== "") {
let $uploadTr = $("[sq_id='" + field + "']");
// if field appears on page, add embed
if ($uploadTr.length) {
let embedId = 'fileUploadEmbed_' + field;
const fieldLabel = $('div[data-mlm-field="'+field+'"]').text();
const isHidden = $(`#${field}-tr`).hasClass("@HIDDEN")
let newLabel = ""
if(isHidden) {
newLabel = "<tr><td colspan='2' style='background-color:#F0F0F0'><b>"+fieldLabel+"</b></td></tr>"
}
$uploadTr.after(newLabel+"<tr id='" + embedId + "'></tr>");
$('#' + embedId).html(
"<td colspan='2'>" +
"<object id='embeddedFile_" + field + "' data='" + url + "#toolbar=0&navpanes=0&scrollbar=0" + "' style='width:100%;height:800px'></object>" +
"</td>"
);
}
}
});
});
</script>
<?php
}
}
}
?>