-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSaveFileToField.php
77 lines (62 loc) · 2.94 KB
/
SaveFileToField.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
<?php
/**
* Initialize Custom Template Engine object, and call method to save report to a field.
*/
require_once "vendor/autoload.php";
use Dompdf\Dompdf;
$customTemplateEngine = new \BCCHR\CustomTemplateEngine\CustomTemplateEngine();
$customTemplateEngine->setPaths();
$header = REDCap::filterHtml(preg_replace("/ /", " ", $_POST["header-editor"]));
$footer = REDCap::filterHtml(preg_replace("/ /", " ", $_POST["footer-editor"]));
$main = REDCap::filterHtml(preg_replace("/ /", " ", $_POST["editor"]));
$filename = filter_input(INPUT_POST, 'filename', FILTER_SANITIZE_SPECIAL_CHARS);
$record = filter_input(INPUT_POST, 'record', FILTER_SANITIZE_SPECIAL_CHARS);
if (isset($main) && !empty($main))
{
$event_name = htmlspecialchars($_POST["save-report-to-event-val"], ENT_QUOTES);
$field_name = htmlspecialchars($_POST["save-report-to-field-val"], ENT_QUOTES);
if (empty($field_name))
{
print json_encode(array("error" => "Cannot save to an empty field!"));
return;
}
if (!empty($event_name))
{
$event_id = REDCap::getEventIdFromUniqueEvent($event_name);
}
else
{
// SQL to retrieve the first event ID of the first event in the project.
$sql = "SELECT event_id FROM redcap_events_metadata
join redcap_events_arms on
redcap_events_metadata.arm_id = redcap_events_arms.arm_id
where redcap_events_arms.project_id = ?
order by event_id asc
limit 1";
$result = $customTemplateEngine->query($sql, array($customTemplateEngine->getProjectId()));
if (!$result)
{
$error_msg = "Trouble retrieving first event ID for saving report to a record field, for a classic project.";
REDCap::logEvent("Custom Template Engine - $error_msg", "", "", $record);
print json_encode(array("error" => $error_msg));
return;
}
$row = $result->fetch_assoc();
$event_id = $row["event_id"];
}
// If longitudinal, check that field exists on chosen event.
if (REDCap::isLongitudinal() && !$customTemplateEngine->checkFieldInEvent($field_name, $event_id))
{
print json_encode(array("error" => "$field_name is not a valid field on " . (empty($event_name) ? "the first event" : $event_name)));
return;
}
$dompdf = new Dompdf();
$pdf_content = $customTemplateEngine->createPDF($dompdf, $header, $footer, $main);
if (!$customTemplateEngine->saveFileToField($filename, $pdf_content, $field_name, $record, $event_id))
{
REDCap::logEvent("Custom Template Engine - Failed to Save Report to Field!", "Field name: $field_name", "", $record);
print json_encode(array("error" => "An unknown error occured. Please contact the BCCHR REDCap team."));
return;
}
print json_encode(array("success" => true));
}