fix: use temp files dir for htmlpurify serializer path (#7476)
[openemr.git] / interface / patient_file / transaction / print_referral.php
blobcb784f28d4084142bd071cea2906abe77f50f7a6
1 <?php
3 /**
4 * print_referral.php
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2008-2017 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
12 * @copyright Copyright (c) 2019 Stephen Waite <stephen.waite@cmsvt.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 require_once("../../globals.php");
17 require_once("$srcdir/transactions.inc.php");
18 require_once("$srcdir/options.inc.php");
19 require_once("$srcdir/patient.inc.php");
21 $template_file = $GLOBALS['OE_SITE_DIR'] . "/referral_template.html";
23 $TEMPLATE_LABELS = array(
24 'label_clinic_id' => xlt('Clinic ID'),
25 'label_client_id' => xlt('Client ID'),
26 'label_control_no' => xlt('Control No.'),
27 'label_date' => xlt('Date'),
28 'label_webpage_title' => xlt('Referral Form'),
29 'label_form1_title' => xlt('Referral Form'),
30 'label_name' => xlt('Name'),
31 'label_age' => xlt('Age'),
32 'label_gender' => xlt('Gender'),
33 'label_address' => xlt('Address'),
34 'label_postal' => xlt('Postal'),
35 'label_phone' => xlt('Phone'),
36 'label_ref_reason' => xlt('Reference Reason'),
37 'label_diagnosis' => xlt('Diagnosis'),
38 'label_ref_class' => xlt('Reference classification (risk level)'),
39 'label_dr_name_sig' => xlt('Doctor\'s name and signature'),
40 'label_refer_to' => xlt('Referred to'),
41 'label_clinic' => xlt('Health centre/clinic'),
42 'label_history_summary' => xlt('Client medical history summary'),
43 'label_bp' => xlt('Blood pressure'),
44 'label_ht' => xlt('Height'),
45 'label_wt' => xlt('Weight'),
46 'label_ref_name_sig' => xlt('Referer name and signature'),
47 'label_special_name_sig' => xlt('Specialist name and signature'),
48 'label_form2_title' => xlt('Counter Referral Form'),
49 'label_findings' => xlt('Findings'),
50 'label_final_diagnosis' => xlt('Final Diagnosis'),
51 'label_services_provided' => xlt('Services provided'),
52 'label_recommendations' => xlt('Recommendations and treatment'),
53 'label_scripts_and_referrals' => xlt('Prescriptions and other referrals'),
54 'label_subhead_clinic' => xlt('Clinic Copy'),
55 'label_subhead_patient' => xlt('Client Copy'),
56 'label_subhead_referred' => xlt('For Referred Organization/Practitioner'),
57 'label_ins_name' => xlt('Insurance'),
58 'label_ins_plan_name' => xlt('Plan'),
59 'label_ins_policy' => xlt('Policy'),
60 'label_ins_group' => xlt('Group'),
61 'label_ins_date' => xlt('Effective Date')
64 if (!is_file($template_file)) {
65 die(text($template_file) . " does not exist!");
68 $transid = empty($_REQUEST['transid']) ? 0 : $_REQUEST['transid'] + 0;
70 // if (!$transid) die("Transaction ID is missing!");
72 if ($transid) {
73 $trow = getTransById($transid);
74 $patient_id = $trow['pid'];
75 $refer_date = empty($trow['refer_date']) ? date('Y-m-d') : $trow['refer_date'];
76 } else {
77 if (empty($_REQUEST['patient_id'])) {
78 // If no transaction ID or patient ID, this will be a totally blank form.
79 $patient_id = 0;
80 $refer_date = '';
81 } else {
82 $patient_id = $_REQUEST['patient_id'] + 0;
83 $refer_date = date('Y-m-d');
86 $trow = array('id' => '', 'pid' => $patient_id, 'refer_date' => $refer_date);
89 if ($patient_id) {
90 $patdata = getPatientData($patient_id);
91 $patient_age = getPatientAge(str_replace('-', '', $patdata['DOB']));
92 $insurancedata = getInsuranceData($patient_id);
93 } else {
94 $patdata = array('DOB' => '');
95 $patient_age = '';
96 $ins_name = '';
99 if (empty($trow['refer_from'])) {
100 $trow['refer_from'] = 0;
103 if (empty($trow['refer_to' ])) {
104 $trow['refer_to' ] = 0;
107 $frrow = sqlQuery("SELECT * FROM users WHERE id = ?", array($trow['refer_from']));
108 if (empty($frrow)) {
109 $frrow = array();
112 $torow = sqlQuery("SELECT * FROM users WHERE id = ?", array($trow['refer_to']));
113 if (empty($torow)) {
114 $torow = array(
115 'organization' => '',
116 'street' => '',
117 'city' => '',
118 'state' => '',
119 'zip' => '',
120 'phone' => '',
124 $vrow = sqlQuery("SELECT * FROM form_vitals WHERE " .
125 "pid = ? AND date <= ? " .
126 "ORDER BY date DESC LIMIT 1", array($patient_id, $refer_date . " 23:59:59"));
127 if (empty($vrow)) {
128 $vrow = array(
129 'bps' => '',
130 'bpd' => '',
131 'weight' => '',
132 'height' => '',
136 // $facrow = sqlQuery("SELECT name, facility_npi FROM facility ORDER BY " .
137 // "service_location DESC, billing_location DESC, id ASC LIMIT 1");
138 $facrow = getFacility(-1);
140 // Make some items HTML-friendly if they are empty.
141 if (empty($trow['id'])) {
142 $trow['id'] = '&nbsp;';
145 if (empty($patient_id)) {
146 $patient_id = '&nbsp;';
149 if (empty($facrow['facility_npi'])) {
150 $facrow['facility_npi'] = '&nbsp;';
153 // Generate link to MA logo if it exists.
154 $logo = "";
155 $ma_logo_path = "sites/" . $_SESSION['site_id'] . "/images/ma_logo.png";
156 if (is_file("$webserver_root/$ma_logo_path")) {
157 $logo = "$web_root/$ma_logo_path";
160 $s = '';
161 $fh = fopen($template_file, 'r');
162 while (!feof($fh)) {
163 $s .= fread($fh, 8192);
166 fclose($fh);
168 $s = str_replace("{header1}", genFacilityTitle($TEMPLATE_LABELS['label_form1_title'], -1, $logo), $s);
169 $s = str_replace("{header2}", genFacilityTitle($TEMPLATE_LABELS['label_form2_title'], -1, $logo), $s);
170 $s = str_replace("{fac_name}", text($facrow['name'] ?? ''), $s);
171 $s = str_replace("{fac_facility_npi}", text($facrow['facility_npi']), $s);
172 $s = str_replace("{ref_id}", text($trow['id']), $s);
173 $s = str_replace("{ref_pid}", text($patient_id), $s);
174 $s = str_replace("{pt_age}", text($patient_age), $s);
177 $fres = sqlStatement("SELECT * FROM layout_options " .
178 "WHERE form_id = 'LBTref' ORDER BY group_id, seq");
179 while ($frow = sqlFetchArray($fres)) {
180 $data_type = $frow['data_type'];
181 $field_id = $frow['field_id'];
182 $currvalue = '';
183 if (isset($trow[$field_id])) {
184 $currvalue = $trow[$field_id];
187 $s = str_replace(
188 "{ref_$field_id}",
189 generate_display_field($frow, $currvalue),
194 foreach ($patdata as $key => $value) {
195 if ($key == "sex") {
196 $s = str_replace("{pt_$key}", generate_display_field(array('data_type' => '1','list_id' => 'sex'), $value), $s);
197 } else {
198 $s = str_replace("{pt_$key}", text($value), $s);
202 foreach ($frrow as $key => $value) {
203 $s = str_replace("{from_$key}", text($value), $s);
206 foreach ($torow as $key => $value) {
207 $s = str_replace("{to_$key}", text($value), $s);
210 foreach ($vrow as $key => $value) {
211 $s = str_replace("{v_$key}", text($value), $s);
214 foreach ($TEMPLATE_LABELS as $key => $value) {
215 $s = str_replace("{" . $key . "}", $value, $s);
218 if (!empty($insurancedata)) {
219 foreach ($insurancedata as $key => $value) {
220 $s = str_replace("{insurance_$key}", text($value), $s);
224 // A final pass to clear any unmatched variables:
225 $s = preg_replace('/\{\S+\}/', '', $s);
227 echo $s;