* @copyright Copyright (c) 2016-2021 Jerry Padgett * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3 */ require_once("../interface/globals.php"); use OpenEMR\Common\Acl\AclMain; use OpenEMR\Common\Csrf\CsrfUtils; use OpenEMR\Core\Header; use OpenEMR\Services\DocumentTemplates\DocumentTemplateService; if (!(isset($GLOBALS['portal_onsite_two_enable'])) || !($GLOBALS['portal_onsite_two_enable'])) { echo xlt('Patient Portal is turned off'); exit; } $authUploadTemplates = AclMain::aclCheckCore('admin', 'forms'); $templateService = new DocumentTemplateService(); $patient = json_decode($_POST['upload_pid'] ?? ''); $template_content = null; if (($_POST['mode'] ?? null) === 'save_profiles') { $profiles = json_decode($_POST['profiles'], true); $rtn = $templateService->saveAllProfileTemplates($profiles); if ($rtn) { echo xlt("Profiles successfully saved."); } else { echo xlt('Error! Profiles save failed. Check your Profile lists.'); } exit; } if (($_REQUEST['mode'] ?? null) === 'render_profile') { echo renderProfileHtml(); exit; } if (($_REQUEST['mode'] ?? null) === 'getPdf') { if ($_REQUEST['docid']) { $template = $templateService->fetchTemplate($_REQUEST['docid']); echo "data:application/pdf;base64," . base64_encode($template['template_content']); exit(); } die(xlt('Invalid File')); } if ($_POST['mode'] === 'get') { if ($_REQUEST['docid']) { $template = $templateService->fetchTemplate($_POST['docid']); echo $template['template_content']; exit(); } die(xlt('Invalid File')); } if (($_POST['mode'] ?? null) === 'send_profiles') { if (!empty($_POST['checked'])) { $profiles = json_decode($_POST['checked']) ?: []; $last_id = $templateService->setProfileActiveStatus($profiles); if ($last_id) { echo xlt('Profile Templates Successfully set to Active in portal.'); } else { echo xlt('Error. Problem setting one or more profiles.'); } exit; } die(xlt('Invalid Request')); } if (($_POST['mode'] ?? null) === 'send') { if (!empty($_POST['docid'])) { $pids_array = json_decode($_POST['docid']) ?: ['0']; // profiles are in an array with flag to indicate a group of template id's $ids = json_decode($_POST['checked']) ?: []; $master_ids = []; foreach ($ids as $id) { if (is_array($id)) { if ($id[1] !== true) { continue; } $profile = $id[0]; // get all template ids for this profile $rtn_ids = sqlStatement('SELECT `template_id` as id FROM `document_template_profiles` WHERE `profile` = ? AND `template_id` > "0"', array($profile)); while ($rtn_id = sqlFetchArray($rtn_ids)) { $master_ids[$rtn_id['id']] = $profile; } continue; } $master_ids[$id] = ''; } $last_id = $templateService->sendTemplate($pids_array, $master_ids, $_POST['category']); if ($last_id) { echo xlt('Templates Successfully sent to Locations.'); } else { echo xlt('Error. Problem sending one or more templates.'); } exit; } die(xlt('Invalid Request')); } if (($_POST['mode'] ?? null) === 'save') { if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"], 'import-template-save')) { CsrfUtils::csrfNotVerified(); } if (!$authUploadTemplates) { die(xlt('Not authorized to edit template')); } if ($_POST['docid']) { if (stripos($_POST['content'], "updateTemplateContent($_POST['docid'], $_POST['content']); if ($_POST['service'] === 'window') { echo ""; } } else { die(xlt('Invalid Content')); } } else { die(xlt('Invalid File')); } } elseif (($_POST['mode'] ?? null) === 'delete') { if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"], 'import-template-delete')) { CsrfUtils::csrfNotVerified(); } if (!$authUploadTemplates) { die(xlt('Not authorized to delete template')); } if ($_POST['docid']) { $template = $templateService->deleteTemplate($_POST['docid'], ($_POST['template'] ?? null)); exit($template); } die(xlt('Invalid File')); } elseif (($_POST['mode'] ?? null) === 'update_category') { if ($_POST['docid']) { $template = $templateService->updateTemplateCategory($_POST['docid'], $_POST['category']); echo xlt('Template Category successfully changed to new Category') . ' ' . text($_POST['category']); exit; } die(xlt('Invalid Request Parameters')); } elseif (!empty($_FILES["template_files"])) { if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"], 'import-template-upload')) { CsrfUtils::csrfNotVerified(); } if (!$authUploadTemplates) { xlt("Not Authorized to Upload Templates"); exit; } // so it is a template file import. create record(s). $import_files = $_FILES["template_files"]; $total = count($_FILES['template_files']['name']); for ($i = 0; $i < $total; $i++) { if ($_FILES['template_files']['error'][$i] !== UPLOAD_ERR_OK) { header('refresh:3;url= import_template_ui.php'); echo '' . xlt('Error') . " ...

" . xlt('An error occurred: Missing file to upload. Returning to form.') . '

'; exit; } // parse out what we need $name = preg_replace("/[^A-Z0-9.]/i", " ", $_FILES['template_files']['name'][$i]); if (preg_match("/(.*)\.(php|php7|php8|doc|docx)$/i", $name) !== 0) { die(xlt('Invalid file type.')); } $parts = pathinfo($name); $name = ucwords(strtolower($parts["filename"])); if (empty($patient)) { $patient = ['-1']; } // get em and dispose $success = $templateService->uploadTemplate($name, $_POST['template_category'], $_FILES['template_files']['tmp_name'][$i], $patient); if (!$success) { echo "

" . xlt("Unable to save files. Use back button!") . "

"; exit; } } header("location: " . $_SERVER['HTTP_REFERER']); die(); } if ($_REQUEST['mode'] === 'editor_render_html') { if ($_REQUEST['docid']) { $content = $templateService->fetchTemplate($_REQUEST['docid']); $template_content = $content['template_content']; if ($content['mime'] === 'application/pdf') { $content = ""; echo $content; exit; } renderEditorHtml($_REQUEST['docid'], $template_content); } else { die(xlt('Invalid File')); } } elseif (!empty($_GET['templateHtml'] ?? null)) { renderEditorHtml($_REQUEST['docid'], $_GET['templateHtml']); } /** * @param $template_id * @param $content */ function renderEditorHtml($template_id, $content) { global $authUploadTemplates; $lists = [ '{ParseAsHTML}', '{SignaturesRequired}', '{TextInput}', '{sizedTextInput:120px}', '{smTextInput}', '{TextBox:03x080}', '{CheckMark}', '{ynRadioGroup}', '{TrueFalseRadioGroup}', '{DatePicker}', '{DateTimePicker}', '{StandardDatePicker}', '{CurrentDate:"global"}', '{CurrentTime}', '{DOS}', '{ReferringDOC}', '{PatientID}', '{PatientName}', '{PatientSex}', '{PatientDOB}', '{PatientPhone}', '{Address}', '{City}', '{State}', '{Zip}', '{PatientSignature}', '{AdminSignature}', '{WitnessSignature}', '{AcknowledgePdf:pdf name or id:title}', '{EncounterForm:LBF}', '{Medications}', '{ProblemList}', '{Allergies}', '{ChiefComplaint}', '{DEM: }', '{HIS: }', '{LBF: }', '{GRP}{/GRP}' ]; ?>
    '; } ?>
fetchDefaultCategories(); $profile_list = $templateService->fetchDefaultProfiles(); ?>
getTemplateListAllCategories(-1, true); //$templates = $templateService->getTemplateListUnique(); // Reserved TBD future use ?>
    $files) { if (empty($cat)) { $cat = xlt('General'); } foreach ($files as $file) { $template_id = attr($file['id']); $title = $category_list[$cat]['title'] ?: $cat; $title_esc = attr($title); $this_name = attr($file['template_name']); if ($file['mime'] === 'application/pdf') { continue; } echo "
  • " . "" . text($file['template_name']) . '' . ' ' . xlt('in category') . ' ' . '' . text($title) . '' . '
  • ' . "\n"; } } ?>
$profiles) { $profile_items_list = $templateService->getTemplateListByProfile($profile); $profile_esc = attr($profile); $events = $templateService->fetchAllProfileEvents(); $recurring = attr($events[$profile]['recurring'] ?? ''); $trigger = attr($events[$profile]['event_trigger'] ?? ''); $days = attr($events[$profile]['period'] ?? ''); ?>
\n"; foreach ($profile_items_list as $cat => $files) { if (empty($cat)) { $cat = xlt('General'); } foreach ($files as $file) { $template_id = attr($file['id']); $this_cat = attr($file['category']); $title = $category_list[$file['category']]['title'] ?: $cat; $this_name = attr($file['template_name']); if ($file['mime'] === 'application/pdf') { continue; } echo "
  • " . text($file['template_name']) . ' ' . xlt('in category') . ' ' . text($title) . "
  • \n"; } } echo "\n"; } ?>