From 55e5e87094f0978e7ea7db61add65ae737d4ac4b Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 7 Mar 2022 06:34:34 +1100 Subject: [PATCH 01/20] MCRI updates * Fill and save from file upload fields * Options for alternative paper size and landscape orientation --- CustomTemplateEngine.php | 213 +++++++++++++++++++++++++++++++++++++-- FillAndSave.php | 8 ++ README.md | 8 +- SaveFileToField.php | 2 +- config.json | 32 +++++- 5 files changed, 253 insertions(+), 10 deletions(-) create mode 100644 FillAndSave.php diff --git a/CustomTemplateEngine.php b/CustomTemplateEngine.php index 14617c9..bac0a6d 100644 --- a/CustomTemplateEngine.php +++ b/CustomTemplateEngine.php @@ -331,6 +331,7 @@ public function saveFileToField($filename, $file_contents, $field_name, $record, // Save file to edocs tables in the REDCap database $database_success = FALSE; $upload_success = FALSE; + $docs_id = 0; $dummy_file_name = $filename . ".pdf"; $dummy_file_name = preg_replace("/[^a-zA-Z-._0-9]/","_",$dummy_file_name); @@ -451,19 +452,17 @@ public function saveFileToField($filename, $file_contents, $field_name, $record, $current_timestamp = date("YmdHis"); $ip = $_SERVER["REMOTE_ADDR"]; $description = isset($instance) ? "[instance = $instance],\n$field_name = '$docs_id'" : "$field_name = '$docs_id'"; - $sql = str_replace("'", "''", $sql); + $sql = str_replace("'", "''", $query->getSQL()); $query = $this->framework->createQuery(); $query->add("INSERT INTO $redcap_log_event_table (ts, user, ip, page, project_id, event, object_type, sql_log, pk, event_id, data_values, description) VALUES (?, ?, ?, 'ExternalModules/index.php', ?, 'DOC_UPLOAD', 'redcap_data', ?, ?, ?, ?, 'Upload Document')", [$current_timestamp, $this->userid, $ip, $this->pid, $sql, $record, $event_id, $description]); $query->execute(); - - return true; } } } - return false; + return $docs_id; // 0 if could not upload file } /** @@ -1079,7 +1078,7 @@ public function saveTemplate() * * @since 3.1 */ - public function createPDF($dompdf_obj, $header, $footer, $main) + public function createPDF($dompdf_obj, $header, $footer, $main, $fileOrTemplateName) { $contents = $this->formatPDFContents($header, $footer, $main); @@ -1091,7 +1090,8 @@ public function createPDF($dompdf_obj, $header, $footer, $main) $dompdf_obj->set_option('isRemoteEnabled', TRUE); // Setup the paper size and orientation - $dompdf_obj->setPaper("letter", "portrait"); + list($paperSize, $paperOrientation) = $this->getPaperSettings($fileOrTemplateName); + $dompdf_obj->setPaper($paperSize, $paperOrientation); // Render the HTML as PDF $dompdf_obj->render(); @@ -1123,7 +1123,7 @@ public function downloadTemplate() if (isset($main) && !empty($main)) { $dompdf = new Dompdf(); - $pdf_content = $this->createPDF($dompdf, $header, $footer, $main); + $pdf_content = $this->createPDF($dompdf, $header, $footer, $main, $filename); if (!$this->getProjectSetting("save-report-to-repo")) { @@ -2495,6 +2495,8 @@ function getDropdownOptions($filter = false) foreach($records as $record) { $to_add = $record[$id_field]; + $previously_printed = (is_null($previously_printed)) ? [] : $previously_printed; // LS for PHP 8 + $participant_options = (is_null($participant_options)) ? [] : $participant_options; // LS for PHP 8 if (!in_array($to_add, $previously_printed)) { if (!in_array($to_add, array_keys($participant_options), true)) @@ -2828,4 +2830,201 @@ public function redcap_module_link_check_display($project_id, $link) return $link; } } + + /** + * getPaperSettings($name) + * Look for module project setting with name corresponding to the tempalte or file name provided + * @param String $name Name of template or pdf document file name + * @return Array Array with two elements: 1. paper size e.g. "Letter", "A4"; 2: paper orientation "Portrait" or "Landscape"" + */ + protected function getPaperSettings($fileOrTemplateName) + { + $paperSize = "letter"; + $paperOrientation = "portrait"; + + $templateSettings = $this->getSubSettings('template-options'); + + foreach ($templateSettings as $settings) { + // look for a template with name occurring within the file name of what's being generated + // (pretty horrid - will catch "MyTemplate" before "MyTemplate_New" - need better way of recording template names perhaps recording to project settings on create/save/delete and auto-generate template name for files system storage?) + if (strpos($fileOrTemplateName, $settings['template-name']) !== false) { + $paperSize = (empty($settings['option-paper-size'])) ? $paperSize : $settings['option-paper-size']; + $paperOrientation = ($settings['option-paper-orientation']) ? "landscape" : $paperOrientation; + break; + } + } + + return array($paperSize, $paperOrientation); + } + + public function redcap_data_entry_form_top($project_id, $record, $instrument, $event_id, $group_id, $repeat_instance) + { + global $Proj, $lang; + + // is there a file upload field on this form? + $ff = false; + foreach (array_keys($Proj->forms[$instrument]['fields']) as $f) { + if ($Proj->metadata[$f]['element_type'] === 'file') { + $ff = true; + break; + } + } + if (!$ff) return; // no file upload field -> return + + // any valid templates? + $all_templates = array_diff(scandir($this->templates_dir), array("..", ".")); + $valid_templates = array(); + $suffix = "_$this->pid.html"; + foreach($all_templates as $template) { + if (strpos($template, $suffix) !== FALSE) { + array_push($valid_templates, rtrim($template, $suffix)); + } + } + + if (count($valid_templates) === 0) return; // no templates -> return + + // make a select list of templates to include in the file upload dialog form for each field + $templateSelect = ''; + $uploadDialogContent = '
Custom Template Engine
Choose a file or generate from a template '.$templateSelect.'
'; + echo $uploadDialogContent; + + $fillAndSaveUrl = $this->getUrl('FillAndSave.php'); + $fillAndSaveUrl .= "&id=".urlencode($record)."&event_id=$event_id&instrument=$instrument&instance=$repeat_instance"; + + ?> + + templates_dir, $this->compiled_dir); + $filled_template = $template->fillTemplate($template_filename, $record); + + $doc = new DOMDocument(); + $doc->loadHTML($filled_template); + + $header = $doc->getElementsByTagName("header")->item(0); + $footer = $doc->getElementsByTagName("footer")->item(0); + $main = $doc->getElementsByTagName("main")->item(0); + + $filled_main = $doc->saveHTML($main); + $filled_header = empty($header) ? "" : $doc->saveHTML($header); + $filled_footer = empty($footer)? "" : $doc->saveHTML($footer); + + $dompdf = new Dompdf(); + $pdf_content = $this->createPDF($dompdf, $filled_header, $filled_footer, $filled_main, $template_name); + + $doc_size = strlen($pdf_content); + $doc_id = $this->saveFileToField($save_filename, $pdf_content, $field_name, $record, $event_id, $instance, true); + if ($doc_id) { + $result = 1; + } else { + $msg = "Failed to generate PDF and save to field.
Template=$template_name; Record=$record; Field=$field_name "; + \REDCap::logEvent("Custom Template Engine - Generate and Save Failed!", $msg); + $result = 0; + } + + + // return response for upload as per DataEntry/file_upload.php + // SURVEYS: Use the surveys/index.php page as a pass through for certain files (file uploads/downloads, etc.) + if (isset($_GET['s']) && !empty($_GET['s'])) + { + $file_download_page = APP_PATH_SURVEY . "index.php?pid=$project_id&__passthru=".urlencode("DataEntry/file_download.php"); + $file_delete_page = APP_PATH_SURVEY . "index.php?pid=$project_id&__passthru=".urlencode("DataEntry/file_delete.php"); + } + else + { + $file_download_page = APP_PATH_WEBROOT . "DataEntry/file_download.php?pid=$project_id"; + $file_delete_page = APP_PATH_WEBROOT . "DataEntry/file_delete.php?pid=$project_id&page=" . $_GET['instrument']; + } + + $return = array( + 'result' => $result, + 'field_name' => $field_name, + 'doc_id' => $doc_id, + 'save_filename' => $save_filename, + 'record' => js_escape($record), + 'doc_size' => " (" . round_up($doc_size/1024/1024) . " MB)", + 'event_id' => $event_id, + 'file_download_page' => $file_download_page, + 'file_delete_page' => $file_delete_page, + 'doc_id_hash' => \Files::docIdHash($doc_id), + 'instance' => $instance, + 'inlineActionTag' => (strpos($Proj->metadata[$field_name]['misc'], '@INLINE') !== false) + ); + return $return; + } } diff --git a/FillAndSave.php b/FillAndSave.php new file mode 100644 index 0000000..9ba377f --- /dev/null +++ b/FillAndSave.php @@ -0,0 +1,8 @@ +fillAndSave()); \ No newline at end of file diff --git a/README.md b/README.md index a8d7248..d388433 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,8 @@ When switching from the old module to the rebranded module, you must disable the - Images Folder: Required configuration that defines the publically accessible storage of images uploaded by users. ## Project Configurations -- Save Filled Templates to File Repository: This is specific to downloading a filled template, and will save a copy of the PDF to the File Repository. +- Save Filled Templates to File Repository: This is specific to downloading a filled template, and will save a copy of the PDF to the File Repository. +- [Optional] Enter templates by name and specify an alternative paper size, e.g. A4 (default is Letter), and/or select landscape orientation. ## Permissions @@ -41,6 +42,11 @@ Users must have access to data exports and reports in order to delete a template The module users the PHP template engine, Smarty, to fill in the templates with the apprpriate record data. Smarty will compile the template and store it in the previously configured compiled templates folder. The user is free to make edits to the template content before downloading. When the template is downloaded, if saving templates to the file repository has been configured, then the module will do so. +## File Upload Fields + +Templates are listed in a dropdown list in file upload field dialogs. The user may choose to select a file and upload it in the usual manner or selet a template. +When a template is selected, it will be filled and saved to the field. + ## Web Application Load Balancing WARNING: This module is not currently able to support REDCap instances using load balancers due to the requirement to save templates to the file system. diff --git a/SaveFileToField.php b/SaveFileToField.php index cde151d..4ee427c 100644 --- a/SaveFileToField.php +++ b/SaveFileToField.php @@ -63,7 +63,7 @@ } $dompdf = new Dompdf(); - $pdf_content = $customTemplateEngine->createPDF($dompdf, $header, $footer, $main); + $pdf_content = $customTemplateEngine->createPDF($dompdf, $header, $footer, $main, $filename); if (!$customTemplateEngine->saveFileToField($filename, $pdf_content, $field_name, $record, $event_id)) { diff --git a/config.json b/config.json index daac63b..3d6b09d 100644 --- a/config.json +++ b/config.json @@ -9,6 +9,9 @@ "institution": "BC Children's Hospital Research Institute" } ], + "permissions": [ + "redcap_data_entry_form_top" + ], "links": { "project": [ { @@ -64,7 +67,34 @@ "type": "checkbox", "repeatable": false, "super-users-only": true + }, + { + "key": "template-options", + "name": "Template configuration options", + "required": false, + "type": "sub_settings", + "repeatable": true, + "sub_settings": [ + { + "key": "template-name", + "name": "Template name", + "required": true, + "type": "text" + }, + { + "key": "option-paper-size", + "name": "Paper size
(Optional - specify if different to Letter e.g. A4)", + "required": false, + "type": "text" + }, + { + "key": "option-paper-orientation", + "name": "Use landscape orientation instead of portrait?", + "required": false, + "type": "checkbox" + } + ] } ], - "framework-version": 3 + "framework-version": 3 } \ No newline at end of file From 952baf0ad0973ca4e9b684842871cd611aee6c68 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 7 Mar 2022 06:38:16 +1100 Subject: [PATCH 02/20] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d388433..bc17313 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ The module users the PHP template engine, Smarty, to fill in the templates with ## File Upload Fields -Templates are listed in a dropdown list in file upload field dialogs. The user may choose to select a file and upload it in the usual manner or selet a template. +Templates are listed in a dropdown list in file upload field dialogs. The user may choose to select a file and upload it in the usual manner or select a template. When a template is selected, it will be filled and saved to the field. ## Web Application Load Balancing From 40e9b40be336007a8452fea820943947f288c5e3 Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:48:29 +1000 Subject: [PATCH 03/20] Update BrowseImages.php --- BrowseImages.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BrowseImages.php b/BrowseImages.php index 389a4e4..f2dc18f 100644 --- a/BrowseImages.php +++ b/BrowseImages.php @@ -3,5 +3,5 @@ * Create instance of Custom Report Buidler, * and display image gallery. */ -$customReportBuilder = new \BCCHR\CustomTemplateEngine\CustomTemplateEngine(); -$customReportBuilder->browseImages(); \ No newline at end of file +$customReportBuilder = new \MCRI\CustomTemplateEngine\CustomTemplateEngine(); +$customReportBuilder->browseImages(); From d687398bf2cd43ae6100c6872fb0ee85f421fe55 Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:48:49 +1000 Subject: [PATCH 04/20] Update CreateTemplate.php --- CreateTemplate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CreateTemplate.php b/CreateTemplate.php index 5b57aae..8dd3353 100644 --- a/CreateTemplate.php +++ b/CreateTemplate.php @@ -8,10 +8,10 @@ * Initialize Custom Template Engine object, and call method * to generate Create Template page. */ -$customTemplateEngine = new \BCCHR\CustomTemplateEngine\CustomTemplateEngine(); +$customTemplateEngine = new \MCRI\CustomTemplateEngine\CustomTemplateEngine(); $customTemplateEngine->generateCreateEditTemplatePage(); /** * Include REDCap footer. */ -require_once APP_PATH_DOCROOT . "ProjectGeneral/footer.php"; \ No newline at end of file +require_once APP_PATH_DOCROOT . "ProjectGeneral/footer.php"; From 2c98bc093c013170a32cca4c61a7536724c9e58a Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:49:09 +1000 Subject: [PATCH 05/20] Update CustomTemplateEngine.php --- CustomTemplateEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CustomTemplateEngine.php b/CustomTemplateEngine.php index bac0a6d..09cb2f1 100644 --- a/CustomTemplateEngine.php +++ b/CustomTemplateEngine.php @@ -1,6 +1,6 @@ Date: Wed, 30 Aug 2023 14:51:40 +1000 Subject: [PATCH 06/20] Update DeleteTemplate.php --- DeleteTemplate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DeleteTemplate.php b/DeleteTemplate.php index 8968d7f..d798616 100644 --- a/DeleteTemplate.php +++ b/DeleteTemplate.php @@ -3,7 +3,7 @@ * Initialize Custom Template Engine object, and call method to delete a * template. */ -$customTemplateEngine = new \BCCHR\CustomTemplateEngine\CustomTemplateEngine(); +$customTemplateEngine = new \MCRI\CustomTemplateEngine\CustomTemplateEngine(); $result = $customTemplateEngine->deleteTemplate(); /** @@ -17,4 +17,4 @@ else { header("Location:" . $customTemplateEngine->getUrl("index.php") . "&deleted=0"); -} \ No newline at end of file +} From f943e597874d3dd2adec90423adfdcbd9af28990 Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:51:51 +1000 Subject: [PATCH 07/20] Update DownloadFilledTemplate.php --- DownloadFilledTemplate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DownloadFilledTemplate.php b/DownloadFilledTemplate.php index ed8c67a..767644d 100644 --- a/DownloadFilledTemplate.php +++ b/DownloadFilledTemplate.php @@ -2,5 +2,5 @@ /** * Initialize Custom Template Engine object, and call method to download template. */ -$customTemplateEngine = new \BCCHR\CustomTemplateEngine\CustomTemplateEngine(); -$customTemplateEngine->downloadTemplate(); \ No newline at end of file +$customTemplateEngine = new \MCRI\CustomTemplateEngine\CustomTemplateEngine(); +$customTemplateEngine->downloadTemplate(); From e81e387870761f0748b505a343696ea009236fea Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:52:06 +1000 Subject: [PATCH 08/20] Update EditTemplate.php --- EditTemplate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EditTemplate.php b/EditTemplate.php index c04b8cb..725d447 100644 --- a/EditTemplate.php +++ b/EditTemplate.php @@ -8,10 +8,10 @@ * Inititalize Custom Template Engine object and display Edit Template * page. */ -$customTemplateEngine = new \BCCHR\CustomTemplateEngine\CustomTemplateEngine(); +$customTemplateEngine = new \MCRI\CustomTemplateEngine\CustomTemplateEngine(); $customTemplateEngine->generateCreateEditTemplatePage($_POST["template"]); /** * Include REDCap footer. */ -require_once APP_PATH_DOCROOT . "ProjectGeneral/footer.php"; \ No newline at end of file +require_once APP_PATH_DOCROOT . "ProjectGeneral/footer.php"; From 057de09c82698c3cca33230a8e7efe1341a3fa40 Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:52:27 +1000 Subject: [PATCH 09/20] Update FillAndSave.php --- FillAndSave.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FillAndSave.php b/FillAndSave.php index 9ba377f..dcb2a64 100644 --- a/FillAndSave.php +++ b/FillAndSave.php @@ -3,6 +3,6 @@ * REDCap External Module: Custom Template Engine * @author Luke Stevens lukestevens@hotmail.com https://github.com/lsgs/ */ -if (is_null($module) || !($module instanceof BCCHR\CustomTemplateEngine\CustomTemplateEngine)) { exit(); } +if (is_null($module) || !($module instanceof MCRI\CustomTemplateEngine\CustomTemplateEngine)) { exit(); } header("Content-Type: application/json"); -echo \json_encode($module->fillAndSave()); \ No newline at end of file +echo \json_encode($module->fillAndSave()); From b424313080c143e563a644a4162fd8bff4b0689c Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:52:50 +1000 Subject: [PATCH 10/20] Update FillTemplate.php --- FillTemplate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FillTemplate.php b/FillTemplate.php index 99dd869..5cab9e3 100644 --- a/FillTemplate.php +++ b/FillTemplate.php @@ -4,7 +4,7 @@ * Create instsance of Custom Template Engine, and display template * filled with REDcap data on Fill Template page. */ -$customTemplateEngine = new \BCCHR\CustomTemplateEngine\CustomTemplateEngine(); +$customTemplateEngine = new \MCRI\CustomTemplateEngine\CustomTemplateEngine(); if (sizeof($_POST["participantID"]) == 1) { @@ -23,4 +23,4 @@ else { $customTemplateEngine->batchFillReports(); -} \ No newline at end of file +} From 7ef7b5816f62f46ed720ba3e1a2aec3ac50ce2dc Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:53:35 +1000 Subject: [PATCH 11/20] Update SaveFileToField.php --- SaveFileToField.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SaveFileToField.php b/SaveFileToField.php index 4ee427c..d5c0a3f 100644 --- a/SaveFileToField.php +++ b/SaveFileToField.php @@ -7,7 +7,7 @@ use Dompdf\Dompdf; -$customTemplateEngine = new \BCCHR\CustomTemplateEngine\CustomTemplateEngine(); +$customTemplateEngine = new \MCRI\CustomTemplateEngine\CustomTemplateEngine(); $header = REDCap::filterHtml(preg_replace("/ /", " ", $_POST["header-editor"])); $footer = REDCap::filterHtml(preg_replace("/ /", " ", $_POST["footer-editor"])); @@ -68,9 +68,9 @@ 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.")); + print json_encode(array("error" => "An unknown error occured. Please contact the MCRI REDCap team.")); return; } print json_encode(array("success" => true)); -} \ No newline at end of file +} From 3d238819b28665b3500cb6c99be79c961d312a40 Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:53:46 +1000 Subject: [PATCH 12/20] Update SaveTemplate.php --- SaveTemplate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SaveTemplate.php b/SaveTemplate.php index 116b955..0ee16c1 100644 --- a/SaveTemplate.php +++ b/SaveTemplate.php @@ -3,6 +3,6 @@ * Create an instance of the Custom Template Engine class, * and save template. */ -$customTemplateEngine = new \BCCHR\CustomTemplateEngine\CustomTemplateEngine(); +$customTemplateEngine = new \MCRI\CustomTemplateEngine\CustomTemplateEngine(); $result = $customTemplateEngine->saveTemplate(); -print json_encode($result); \ No newline at end of file +print json_encode($result); From a6e565d729bcf7d75e5dc3728ff665e647beed1a Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:54:05 +1000 Subject: [PATCH 13/20] Update Template.php --- Template.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Template.php b/Template.php index 50132c2..08e29d6 100644 --- a/Template.php +++ b/Template.php @@ -1,6 +1,6 @@ Date: Wed, 30 Aug 2023 14:54:17 +1000 Subject: [PATCH 14/20] Update UploadImages.php --- UploadImages.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UploadImages.php b/UploadImages.php index 0ec972b..b8878cd 100644 --- a/UploadImages.php +++ b/UploadImages.php @@ -3,5 +3,5 @@ * Create instance of Custom Template Engine, * and upload image. */ -$customTemplateEngine = new \BCCHR\CustomTemplateEngine\CustomTemplateEngine(); -$customTemplateEngine->uploadImages(); \ No newline at end of file +$customTemplateEngine = new \MCRI\CustomTemplateEngine\CustomTemplateEngine(); +$customTemplateEngine->uploadImages(); From 7667bc275a82832e5fdbc36ab0a25e8137863501 Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:55:00 +1000 Subject: [PATCH 15/20] Update index.php --- index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index c1f781d..096a02c 100644 --- a/index.php +++ b/index.php @@ -8,10 +8,10 @@ * Initialize Custom Report Builder object, and call method * to generate Index page. */ -$customReportBuilder = new \BCCHR\CustomTemplateEngine\CustomTemplateEngine(); +$customReportBuilder = new \MCRI\CustomTemplateEngine\CustomTemplateEngine(); $customReportBuilder->generateIndexPage(); /** * Include REDCap footer. */ -require_once APP_PATH_DOCROOT . "ProjectGeneral/footer.php"; \ No newline at end of file +require_once APP_PATH_DOCROOT . "ProjectGeneral/footer.php"; From 9e52965ef50773d9cce15ff34385ddacec6cc62b Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:00:46 +1000 Subject: [PATCH 16/20] Update config.json --- config.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/config.json b/config.json index 3d6b09d..6fc81fe 100644 --- a/config.json +++ b/config.json @@ -1,12 +1,12 @@ { - "name": "Custom Template Engine", - "namespace": "BCCHR\\CustomTemplateEngine", + "name": "Custom Template Engine (MCRI)", + "namespace": "MCRI\\CustomTemplateEngine", "description": "This module allows you to create HTML templates and fill them with record data from your project. You can download the filled templates as a PDF, which are saved to the File Repository.", "authors": [ { - "name": "Ashley Lee", - "email": "alee2@bcchr.ca", - "institution": "BC Children's Hospital Research Institute" + "name": "Luke Stevens", + "email": "luke.stevens@mcri.edu.au", + "institution": "Murdoch Children's Research Institute" } ], "permissions": [ @@ -97,4 +97,4 @@ } ], "framework-version": 3 -} \ No newline at end of file +} From 32e9213d3ddfa334623e31ed13736434450c8394 Mon Sep 17 00:00:00 2001 From: harneetbhinder99 <49669617+harneetbhinder99@users.noreply.github.com> Date: Thu, 7 Sep 2023 11:16:51 +1000 Subject: [PATCH 17/20] Update CustomTemplateEngine.php Fix to show records in the dropdown menu --- CustomTemplateEngine.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CustomTemplateEngine.php b/CustomTemplateEngine.php index 09cb2f1..f6f89e7 100644 --- a/CustomTemplateEngine.php +++ b/CustomTemplateEngine.php @@ -2555,8 +2555,8 @@ public function generateIndexPage() } ?> - - + + From 59dec5e3c05a1050049472d0ef21d04759a99735 Mon Sep 17 00:00:00 2001 From: Luke Date: Fri, 3 Nov 2023 17:03:43 +1100 Subject: [PATCH 18/20] Paper size and orientation options for PR --- CustomTemplateEngine.php | 33 ++++++++++++++++++++++++++++++--- SaveFileToField.php | 2 +- config.json | 27 +++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/CustomTemplateEngine.php b/CustomTemplateEngine.php index 599140a..636f58f 100644 --- a/CustomTemplateEngine.php +++ b/CustomTemplateEngine.php @@ -1086,7 +1086,7 @@ public function saveTemplate() * * @since 3.1 */ - public function createPDF($dompdf_obj, $header, $footer, $main) + public function createPDF($dompdf_obj, $header, $footer, $main, $fileOrTemplateName="") { $contents = $this->formatPDFContents($header, $footer, $main); @@ -1098,7 +1098,8 @@ public function createPDF($dompdf_obj, $header, $footer, $main) $dompdf_obj->set_option('isRemoteEnabled', TRUE); // Setup the paper size and orientation - $dompdf_obj->setPaper("letter", "portrait"); + list($paperSize, $paperOrientation) = $this->getPaperSettings($fileOrTemplateName); + $dompdf_obj->setPaper($paperSize, $paperOrientation); // Render the HTML as PDF $dompdf_obj->render(); @@ -1130,7 +1131,7 @@ public function downloadTemplate() if (isset($main) && !empty($main)) { $dompdf = new Dompdf(); - $pdf_content = $this->createPDF($dompdf, $header, $footer, $main); + $pdf_content = $this->createPDF($dompdf, $header, $footer, $main, $filename); if (!$this->getProjectSetting("save-report-to-repo")) { @@ -2847,4 +2848,30 @@ public function redcap_module_link_check_display($project_id, $link) return $link; } } + + /** + * getPaperSettings($name) + * Look for module project setting with name corresponding to the tempalte or file name provided + * @param String $name Name of template or pdf document file name + * @return Array Array with two elements: 1. paper size e.g. "Letter", "A4"; 2: paper orientation "Portrait" or "Landscape"" + */ + protected function getPaperSettings($fileOrTemplateName) + { + $paperSize = "letter"; + $paperOrientation = "portrait"; + + $templateSettings = $this->getSubSettings('template-options'); + + foreach ($templateSettings as $settings) { + // look for a template with name occurring within the file name of what's being generated + // (pretty horrid - will catch "MyTemplate" before "MyTemplate_New" - need better way of recording template names perhaps recording to project settings on create/save/delete and auto-generate template name for files system storage?) + if (strpos($fileOrTemplateName, $settings['template-name']) !== false) { + $paperSize = (empty($settings['option-paper-size'])) ? $paperSize : $settings['option-paper-size']; + $paperOrientation = ($settings['option-paper-orientation']) ? "landscape" : $paperOrientation; + break; + } + } + + return array($paperSize, $paperOrientation); + } } diff --git a/SaveFileToField.php b/SaveFileToField.php index fb96335..107798f 100644 --- a/SaveFileToField.php +++ b/SaveFileToField.php @@ -63,7 +63,7 @@ } $dompdf = new Dompdf(); - $pdf_content = $customTemplateEngine->createPDF($dompdf, $header, $footer, $main); + $pdf_content = $customTemplateEngine->createPDF($dompdf, $header, $footer, $main, $filename); if (!$customTemplateEngine->saveFileToField($filename, $pdf_content, $field_name, $record, $event_id)) { diff --git a/config.json b/config.json index 7e43093..1d11f79 100644 --- a/config.json +++ b/config.json @@ -67,6 +67,33 @@ "type": "checkbox", "repeatable": false, "super-users-only": true + }, + { + "key": "template-options", + "name": "Template configuration options", + "required": false, + "type": "sub_settings", + "repeatable": true, + "sub_settings": [ + { + "key": "template-name", + "name": "Template name", + "required": true, + "type": "text" + }, + { + "key": "option-paper-size", + "name": "Paper size
(Optional - specify if different to Letter e.g. A4)", + "required": false, + "type": "text" + }, + { + "key": "option-paper-orientation", + "name": "Use landscape orientation instead of portrait?", + "required": false, + "type": "checkbox" + } + ] } ], "framework-version": 9 From cba44b0d926a50dcfba9f22c53c89b90db5afb71 Mon Sep 17 00:00:00 2001 From: Luke Date: Fri, 3 Nov 2023 17:16:20 +1100 Subject: [PATCH 19/20] Paper size and orientation options for PR --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 03a66b2..eb5e56a 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ When switching from the old module to the rebranded module, you must disable the ## Project Configurations - Save Filled Templates to File Repository: This is specific to downloading a filled template, and will save a copy of the PDF to the File Repository. +- [Optional] Enter templates by name and specify an alternative paper size, e.g. A4 (default is Letter), and/or select landscape orientation. ## Permissions From 810300a20b0fcc00609d77e1050efd9cb095002f Mon Sep 17 00:00:00 2001 From: Luke Date: Fri, 3 Nov 2023 17:28:33 +1100 Subject: [PATCH 20/20] Paper size and orientation options for PR --- CustomTemplateEngine.php | 33 ++++++++++++++++++++++++++++++--- README.md | 1 + SaveFileToField.php | 2 +- config.json | 27 +++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/CustomTemplateEngine.php b/CustomTemplateEngine.php index d98bbf3..b8f1dcd 100644 --- a/CustomTemplateEngine.php +++ b/CustomTemplateEngine.php @@ -1086,7 +1086,7 @@ public function saveTemplate() * * @since 3.1 */ - public function createPDF($dompdf_obj, $header, $footer, $main) + public function createPDF($dompdf_obj, $header, $footer, $main, $fileOrTemplateName="") { $contents = $this->formatPDFContents($header, $footer, $main); @@ -1098,7 +1098,8 @@ public function createPDF($dompdf_obj, $header, $footer, $main) $dompdf_obj->set_option('isRemoteEnabled', TRUE); // Setup the paper size and orientation - $dompdf_obj->setPaper("letter", "portrait"); + list($paperSize, $paperOrientation) = $this->getPaperSettings($fileOrTemplateName); + $dompdf_obj->setPaper($paperSize, $paperOrientation); // Render the HTML as PDF $dompdf_obj->render(); @@ -1130,7 +1131,7 @@ public function downloadTemplate() if (isset($main) && !empty($main)) { $dompdf = new Dompdf(); - $pdf_content = $this->createPDF($dompdf, $header, $footer, $main); + $pdf_content = $this->createPDF($dompdf, $header, $footer, $main, $filename); if (!$this->getProjectSetting("save-report-to-repo")) { @@ -2847,4 +2848,30 @@ public function redcap_module_link_check_display($project_id, $link) return $link; } } + + /** + * getPaperSettings($name) + * Look for module project setting with name corresponding to the tempalte or file name provided + * @param String $name Name of template or pdf document file name + * @return Array Array with two elements: 1. paper size e.g. "Letter", "A4"; 2: paper orientation "Portrait" or "Landscape"" + */ + protected function getPaperSettings($fileOrTemplateName) + { + $paperSize = "letter"; + $paperOrientation = "portrait"; + + $templateSettings = $this->getSubSettings('template-options'); + + foreach ($templateSettings as $settings) { + // look for a template with name occurring within the file name of what's being generated + // (pretty horrid - will catch "MyTemplate" before "MyTemplate_New" - need better way of recording template names perhaps recording to project settings on create/save/delete and auto-generate template name for files system storage?) + if (strpos($fileOrTemplateName, $settings['template-name']) !== false) { + $paperSize = (empty($settings['option-paper-size'])) ? $paperSize : $settings['option-paper-size']; + $paperOrientation = ($settings['option-paper-orientation']) ? "landscape" : $paperOrientation; + break; + } + } + + return array($paperSize, $paperOrientation); + } } diff --git a/README.md b/README.md index ca3c346..7e29ba0 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ When switching from the old module to the rebranded module, you must disable the ## Project Configurations - Save Filled Templates to File Repository: This is specific to downloading a filled template, and will save a copy of the PDF to the File Repository. +- [Optional] Enter templates by name and specify an alternative paper size, e.g. A4 (default is Letter), and/or select landscape orientation. ## Permissions diff --git a/SaveFileToField.php b/SaveFileToField.php index fcd1001..82e9e7a 100644 --- a/SaveFileToField.php +++ b/SaveFileToField.php @@ -63,7 +63,7 @@ } $dompdf = new Dompdf(); - $pdf_content = $customTemplateEngine->createPDF($dompdf, $header, $footer, $main); + $pdf_content = $customTemplateEngine->createPDF($dompdf, $header, $footer, $main, $filename); if (!$customTemplateEngine->saveFileToField($filename, $pdf_content, $field_name, $record, $event_id)) { diff --git a/config.json b/config.json index 0ea58ef..c16e5ae 100644 --- a/config.json +++ b/config.json @@ -67,6 +67,33 @@ "type": "checkbox", "repeatable": false, "super-users-only": true + }, + { + "key": "template-options", + "name": "Template configuration options", + "required": false, + "type": "sub_settings", + "repeatable": true, + "sub_settings": [ + { + "key": "template-name", + "name": "Template name", + "required": true, + "type": "text" + }, + { + "key": "option-paper-size", + "name": "Paper size
(Optional - specify if different to Letter e.g. A4)", + "required": false, + "type": "text" + }, + { + "key": "option-paper-orientation", + "name": "Use landscape orientation instead of portrait?", + "required": false, + "type": "checkbox" + } + ] } ], "framework-version": 9