Skip to content

Commit

Permalink
Support multi buttons per form
Browse files Browse the repository at this point in the history
  • Loading branch information
aanunez committed Dec 9, 2024
1 parent e2bb0d9 commit bd936dd
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 73 deletions.
85 changes: 47 additions & 38 deletions FormFill.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,60 +19,69 @@ public function redcap_every_page_top($project_id)
public function redcap_data_entry_form($project_id, $record, $instrument, $event_id)
{
$settings = $this->getProjectSettings();
$settingIndex = -1;
$validSettings = [];

// Note: We only support one form fill per page here
foreach ($settings['instrument'] as $index => $instrumentList) {
if (in_array($instrument, $instrumentList))
$settingIndex = $index;
$validSettings[] = $index;
}

if ($settingIndex == -1)
if (empty($validSettings))
return;

$this->initGlobal();
$parsed = [];
$dd = REDCap::getDataDictionary('array');

foreach ($settings as $name => $valueArray) {
if (in_array($name, ['enabled', 'filable-instance', 'instrument', 'filable-field', 'event']))
continue;

if ($name == 'pdf') {
$doc_id = $valueArray[$settingIndex];
if (!empty($doc_id)) {
list($mimeType, $docName, $fileContent) = REDCap::getFile($doc_id);
$file = unpack("C*", $fileContent);
$files = [];
$parsedSettings = [];

foreach ($validSettings as $settingIndex) {
$parsed = [];

foreach ($settings as $name => $valueArray) {
if (in_array($name, ['enabled', 'filable-instance', 'instrument', 'filable-field', 'event']))
continue;

if ($name == 'pdf') {
$doc_id = $valueArray[$settingIndex];
if (!empty($doc_id)) {
list($mimeType, $docName, $fileContent) = REDCap::getFile($doc_id);
$file = unpack("C*", $fileContent);
}
continue;
}
continue;
}

if ($name == 'fill-value') {
$fetched = [];
$defaultEvent = $settings['event'][$settingIndex];
foreach ($valueArray[$settingIndex] as $index => $field) {
$data = REDCap::getData($project_id, 'array', $record, $field)[$record];
$default = empty($data[$defaultEvent][$field]) ? reset($data)[$field] : $data[$defaultEvent][$field];
$data = empty($data[$event_id][$field]) ? $default : $data[$event_id][$field];
$type = $dd[$field]['field_type'];
$validation = $dd[$field]['text_validation_type_or_show_slider_number'];
if ($type == 'checkbox') // Only look at first check box
$data = reset($data) ? true : false;
if ($type == 'radio' || $type == 'yesno' || $type == 'dropdown') // Code blank, 0, and negatives as false
$data = $data == '' || $data == '0' ? false : !(intval($data) < 0);
$fetched[$index] = $this->escape($data);
if ($name == 'fill-value') {
$fetched = [];
$defaultEvent = $settings['event'][$settingIndex];
foreach ($valueArray[$settingIndex] as $index => $field) {
$data = REDCap::getData($project_id, 'array', $record, $field)[$record];
$default = empty($data[$defaultEvent][$field]) ? reset($data)[$field] : $data[$defaultEvent][$field];
$data = empty($data[$event_id][$field]) ? $default : $data[$event_id][$field];
$type = $dd[$field]['field_type'];
$validation = $dd[$field]['text_validation_type_or_show_slider_number'];
if ($type == 'checkbox') // Only look at first check box
$data = reset($data) ? true : false;
if ($type == 'radio' || $type == 'yesno' || $type == 'dropdown') // Code blank, 0, and negatives as false
$data = $data == '' || $data == '0' ? false : !(intval($data) < 0);
$fetched[$index] = $this->escape($data);
}
$parsed[$name] = $fetched;
$parsed['redcap-fields'] = $valueArray[$settingIndex];
continue;
}
$parsed[$name] = $fetched;
$parsed['redcap-fields'] = $valueArray[$settingIndex];
continue;

$parsed[$name] = $valueArray[$settingIndex];
}

$parsed[$name] = $valueArray[$settingIndex];
if (!empty($file)) {
$files[] = $file;
$parsedSettings[] = $parsed;
}
}

if (!empty($file)) {
$this->passArgument('pdf_base64', $file);
$this->passArgument('settings', $parsed);
if (!empty($files)) {
$this->passArgument('pdf_base64', $files);
$this->passArgument('settings', $parsedSettings);
$this->includeJs('pdf-lib.min.js');
$this->includeJs('formfill.js');
}
Expand Down
78 changes: 43 additions & 35 deletions formfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@

attachEvents = () => {
if (initSuccess) return;
if ((module.settings.destination == 'email' && !module.settings.email) ||
(module.settings.destination == 'fax' && (!module.settings.phone || !module.fax))) {
$(module.settings.button).on('click', issue);
}
else {
$(module.settings.button).on('click', fillPDF);
}
$.each(module.settings, function (index, btnSetting) {
if ((btnSetting.destination == 'email' && !btnSetting.email) ||
(btnSetting.destination == 'fax' && (!btnSetting.phone || !module.fax))) {
$(btnSetting.button).on('click', issue);
return;
}
$(btnSetting.button).on('click', function () {
fillPDF(index);
});
})
initSuccess = true;
}

fillPDF = async () => {
fillPDF = async (settingsIndex) => {

// Grab any blank values that might be on this form and do any formatting
let localVals = module.settings['fill-value'];
$.each(module.settings['redcap-fields'], function (index, name) {
// Grab any blank values that might be on this form and do any formatting
const btnSettings = module.settings[settingsIndex];
const pdfDoc = module.pdfDoc[settingsIndex];
let localVals = btnSettings['fill-value'];
$.each(btnSettings['redcap-fields'], function (index, name) {

// Grab local values
if ($(`*[name=${name}]`).length > 0) {
Expand All @@ -55,10 +60,10 @@
});

// Flip though all fields on the form
let form = module.pdfDoc.getForm();
let form = pdfDoc.getForm();
let fields = form.getFields();
$.each(fields, function (_, pdfField) {
let index = module.settings['pdf-field-name'].indexOf(pdfField.getName());
let index = btnSettings['pdf-field-name'].indexOf(pdfField.getName());
if (index == -1)
return true;
if (pdfField.check) {
Expand All @@ -78,59 +83,59 @@
});

// Save and send
let pdf = null;
switch (module.settings.destination) {
let pdf = await pdfDoc.saveAsBase64();
let uri = await pdfDoc.saveAsBase64({ dataUri: true });
switch (btnSettings.destination) {
case 'email':
pdf = await module.pdfDoc.saveAsBase64();
let emails = module.settings.email.replaceAll(' ', '').split(',');
let emails = btnSettings.email.replaceAll(' ', '').split(',');
emails.forEach(async (email) => {
await send(module.from, email, module.settings.subject || "", pdf, module.settings.body);
await send(settingsIndex, module.from, email, btnSettings.subject || "", pdf, uri, btnSettings.body);
});
break;
case 'fax':
pdf = await module.pdfDoc.saveAsBase64();
let phones = module.settings.phone.replace(/[-() ]/g, '').split(',');
let phones = btnSettings.phone.replace(/[-() ]/g, '').split(',');
phones.forEach(async (phone) => {
phone = phone.length != 11 ? '1' + phone : phone;
await send(module.from, phone + "@" + module.fax, module.settings.regarding || "", pdf, module.settings.cover);
await send(settingsIndex, module.from, phone + "@" + module.fax, btnSettings.regarding || "", pdf, uri, btnSettings.cover);
});
break;
case 'download':
let pdfBytes = await module.pdfDoc.save();
let pdfBytes = await pdfDoc.save();
download(pdfBytes, $("#dataEntryTopOptionsButtons").next('div').text().trim() + ".pdf", "application/pdf");
break;
}
}

send = async (from, to, subject, pdf, body) => {
send = async (from, to, subject, pdf, uri, body) => {
const msg = `To: ${to}\nSubject: ${subject}`

module.ajax("email", {
from: from,
to: to,
attachment: pdf,
subject: subject,
message: body
}).then(function (response) {
}).then(async function (response) {
console.log(response.text);
if (response.sent) {
Swal.fire({
icon: 'success',
title: 'Document Sent',
text: 'The completed document has been successfully ' + module.settings.destination + 'ed!',
text: 'The completed document has been successfully sent!',
});
log('Form sent', 'To: ' + to + '\nSubject: ' + subject);
} else {
failsafeDownload();
log('Form send failed', 'To: ' + to + '\nSubject: ' + subject);
log('Form sent', msg);
return;
}
}).catch(function (err) {
failsafeDownload(uri);
log('Form send failed', msg);
}).catch(async function (err) {
console.log(err);
failsafeDownload();
log('Form send failed', 'To: ' + to + '\nSubject: ' + subject);
failsafeDownload(uri);
log('Form send failed', msg);
});
}

failsafeDownload = async () => {
let uri = await module.pdfDoc.saveAsBase64({ dataUri: true });
failsafeDownload = async (uri) => {
Swal.fire({
icon: 'error',
title: 'Issue Sending Fax/Email',
Expand Down Expand Up @@ -164,7 +169,10 @@
}

$(document).ready(async () => {
module.pdfDoc = await PDFLib.PDFDocument.load(Uint8Array.from(Object.values(module.pdf_base64)));
module.pdfDoc = []
$.each(module.pdf_base64, async function (index, pdf) {
module.pdfDoc[index] = await PDFLib.PDFDocument.load(Uint8Array.from(Object.values(pdf)));
});
// Load the config, play nice w/ Shazam
if (typeof Shazam == "object") {
let oldCallback = Shazam.beforeDisplayCallback;
Expand Down

0 comments on commit bd936dd

Please sign in to comment.