Skip to content

Commit 94561d2

Browse files
committed
initial commit
0 parents  commit 94561d2

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed

FileUploadEmbed.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
namespace UIOWA\FileUploadEmbed;
3+
4+
class FileUploadEmbed extends \ExternalModules\AbstractExternalModule {
5+
6+
function redcap_module_system_enable($version) {
7+
$key = 'allowed-file-extension';
8+
9+
// if no file types already exist, set default
10+
if (!$this->getSystemSetting($key)) {
11+
$this->setSystemSetting($key, array(
12+
'pdf',
13+
'jpg',
14+
'jpeg',
15+
'png'
16+
));
17+
}
18+
}
19+
20+
function redcap_data_entry_form($project_id, $record, $instrument, $event_id, $group_id, $repeat_instance)
21+
{
22+
// only embed if record has been saved
23+
if (isset($record)) {
24+
$enabledFields = $this->getProjectSetting('allowed-upload-field');
25+
$supportedFiletypes = $this->getSystemSetting('allowed-file-extension');
26+
27+
// get doc_id(s)
28+
$data = \REDCap::getData(array(
29+
'records' => $record,
30+
'fields' => $enabledFields,
31+
'events' => $event_id,
32+
'groups' => $group_id,
33+
'return_format' => 'json'
34+
));
35+
36+
$data = json_decode($data, true);
37+
38+
// get specific instance if repeatable
39+
if (isset($repeat_instance)) {
40+
$data = $data[$repeat_instance - 1]; // todo better way to get instance id?
41+
}
42+
43+
foreach($data as $field => $doc_id) {
44+
// remove any fields that are not File Upload type
45+
if (\REDCap::getFieldType($field) !== 'file') {
46+
unset($data[$field]);
47+
}
48+
else {
49+
// Returns array of "mime_type" (string), "doc_name" (string), and "contents" (string) or FALSE if failed
50+
$docInfo = \Files::getEdocContentsAttributes($doc_id);
51+
$uploadedFileType = \Files::get_file_extension_by_mime_type($docInfo[0]);
52+
53+
// exit if file type isn't whitelisted in module config or couldn't lookup mime type
54+
if (!in_array($uploadedFileType, $supportedFiletypes) || !$uploadedFileType) {
55+
exit();
56+
}
57+
58+
// generate verification hash
59+
$hash = \Files::docIdHash($doc_id);
60+
61+
// get embed url
62+
$data[$field] = $this->getUrl("index.php?id=" . $doc_id . '&doc_id_hash=' . $hash);
63+
}
64+
}
65+
66+
// exit if no valid fields found
67+
if (!count($data)) {
68+
exit();
69+
}
70+
?>
71+
<script>
72+
$(document).ready(function() {
73+
$.each(<?= json_encode($data) ?>, function(field, url) {
74+
let $uploadTr = $(`[sq_id="${field}"]`);
75+
76+
// if field appears on page, add embed
77+
if ($uploadTr.length) {
78+
let embedId = 'fileUploadEmbed_' + field;
79+
80+
$uploadTr.after(`<tr id="${embedId}"></tr>`);
81+
82+
$('#' + embedId).html(`
83+
<td colspan="2">
84+
<object id="embeddedFile_${field}" data="${url}" style="width:100%;height:800px"></object>
85+
</td>
86+
`);
87+
}
88+
});
89+
});
90+
</script>
91+
<?
92+
}
93+
}
94+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 University of Iowa ICTS
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# File Upload Embed
2+
3+
### Description
4+
This module allows File Upload fields to display the uploaded file as an inline embed on a data entry form.
5+
6+
### Setup
7+
No system-level configuration is required, but you may want to customize the whitelisted file extensions. By default, the only file types that will display as embedded elements are PDF, JPG, JPEG, and PNG. This module has only been tested extensively with PDFs.
8+
9+
Once enabled on a project, you need to define which field(s) can be embedded via the project-level configuration. The selected fields must be the "File Upload" type.
10+
11+
### Usage
12+
If a file with a whitelisted extension is uploaded to a valid File Upload field, it will appear below the File Upload field after saving and reloading the data entry form. The File Upload field can be hidden using an action tag, but the embed will still display (helpful for restricting the ability for a user to re-upload or delete files).

config.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "File Upload Embed",
3+
4+
"namespace": "UIOWA\\FileUploadEmbed",
5+
6+
"description": "Allow user uploaded files to appear embedded in data entry forms",
7+
8+
"documentation": "README.md",
9+
10+
"authors": [
11+
{
12+
"name": "Izzy Neuhaus",
13+
"email": "[email protected]",
14+
"institution": "University of Iowa Institute for Clinical and Translational Science"
15+
}
16+
],
17+
18+
"permissions": [
19+
"redcap_module_system_enable",
20+
"redcap_data_entry_form"
21+
],
22+
23+
"system-settings": [
24+
{
25+
"key": "allowed-file-extension",
26+
"name": "Allowed file extension for embedding (e.g. pdf, png, txt)",
27+
"type": "text",
28+
"repeatable": true,
29+
"required": true
30+
}
31+
],
32+
33+
"project-settings": [
34+
{
35+
"key": "allowed-upload-field",
36+
"name": "File Upload fields to display as embeds",
37+
"type": "field-list",
38+
"repeatable": true,
39+
"required": true
40+
}
41+
]
42+
}

index.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/** @var FileUploadEmbed $module */
3+
4+
use UIOWA\FileUploadEmbed\FileUploadEmbed;
5+
6+
// exit if hash doesn't match doc_id
7+
if (!isset($_GET['doc_id_hash']) || (isset($_GET['doc_id_hash']) && $_GET['doc_id_hash'] != \Files::docIdHash($_GET['id']))) {
8+
exit("{$lang['global_01']}!");
9+
}
10+
11+
// Returns array of "mime_type" (string), "doc_name" (string), and "contents" (string) or FALSE if failed
12+
$docInfo = \Files::getEdocContentsAttributes($_GET['id']);
13+
14+
// set mime type and file contents
15+
header('Content-type: ' . $docInfo[0]);
16+
echo $docInfo[2];

0 commit comments

Comments
 (0)