fix new encounter RTL (#1811)
[openemr.git] / ccr / createCCR.php
blobe096c361d8ba8a471d94949c23bdef49fc0cdd90
1 <?php
2 /**
3 * CCR Script.
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Garden State Health Systems <http://www.gshsys.com/>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2010 Garden State Health Systems <http://www.gshsys.com/>
10 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 // check if using the patient portal
16 //(if so, then use the portal authorization)
17 if (isset($_GET['portal_auth']) || isset($_GET['portal_auth_two'])) {
18 if (isset($_GET['portal_auth'])) {
19 $landingpage = "../patients/index.php";
20 } else { // isset($_GET['portal_auth_two'])
21 $landingpage = "../portal/index.php";
24 session_start();
25 if (isset($_SESSION['pid']) && (isset($_SESSION['patient_portal_onsite']) || isset($_SESSION['patient_portal_onsite_two']))) {
26 $pid = $_SESSION['pid'];
27 $ignoreAuth=true;
28 global $ignoreAuth;
29 } else {
30 session_destroy();
31 header('Location: '.$landingpage.'?w');
32 exit;
36 require_once(dirname(__FILE__) . "/../interface/globals.php");
37 require_once(dirname(__FILE__) . "/../library/sql-ccr.inc");
38 require_once(dirname(__FILE__) . "/uuid.php");
39 require_once(dirname(__FILE__) . "/transmitCCD.php");
40 require_once(dirname(__FILE__) . "/../custom/code_types.inc.php");
42 function createCCR($action, $raw = "no", $requested_by = "")
45 $authorID = getUuid();
46 $patientID = getUuid();
47 $sourceID = getUuid();
48 $oemrID = getUuid();
50 $result = getActorData();
51 while ($res = sqlFetchArray($result[2])) {
52 ${"labID{$res['id']}"} = getUuid();
55 $ccr = new DOMDocument('1.0', 'UTF-8');
56 $e_styleSheet = $ccr->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="stylesheet/ccr.xsl"');
57 $ccr->appendChild($e_styleSheet);
59 $e_ccr = $ccr->createElementNS('urn:astm-org:CCR', 'ContinuityOfCareRecord');
60 $ccr->appendChild($e_ccr);
62 /////////////// Header
64 require_once("createCCRHeader.php");
65 $e_Body = $ccr->createElement('Body');
66 $e_ccr->appendChild($e_Body);
68 /////////////// Problems
70 $e_Problems = $ccr->createElement('Problems');
71 require_once("createCCRProblem.php");
72 $e_Body->appendChild($e_Problems);
74 /////////////// Alerts
76 $e_Alerts = $ccr->createElement('Alerts');
77 require_once("createCCRAlerts.php");
78 $e_Body->appendChild($e_Alerts);
80 ////////////////// Medication
82 $e_Medications = $ccr->createElement('Medications');
83 require_once("createCCRMedication.php");
84 $e_Body->appendChild($e_Medications);
86 ///////////////// Immunization
88 $e_Immunizations = $ccr->createElement('Immunizations');
89 require_once("createCCRImmunization.php");
90 $e_Body->appendChild($e_Immunizations);
93 /////////////////// Results
95 $e_Results = $ccr->createElement('Results');
96 require_once("createCCRResult.php");
97 $e_Body->appendChild($e_Results);
100 /////////////////// Procedures
102 //$e_Procedures = $ccr->createElement('Procedures');
103 //require_once("createCCRProcedure.php");
104 //$e_Body->appendChild($e_Procedures);
106 //////////////////// Footer
108 // $e_VitalSigns = $ccr->createElement('VitalSigns');
109 // $e_Body->appendChild($e_VitalSigns);
111 /////////////// Actors
113 $e_Actors = $ccr->createElement('Actors');
114 require_once("createCCRActor.php");
115 $e_ccr->appendChild($e_Actors);
117 if ($action=="generate") {
118 gnrtCCR($ccr, $raw, $requested_by);
121 if ($action == "viewccd") {
122 viewCCD($ccr, $raw, $requested_by);
126 function gnrtCCR($ccr, $raw = "no", $requested_by = "")
128 global $pid;
130 $ccr->preserveWhiteSpace = false;
131 $ccr->formatOutput = true;
133 if ($raw == "yes") {
134 // simply send the xml to a textarea (nice debugging tool)
135 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
136 echo $ccr->saveXml();
137 echo "</textarea>";
138 return;
139 } else if ($raw == "hybrid") {
140 // send a file that contains a hybrid file of the raw xml and the xsl stylesheet
141 createHybridXML($ccr);
142 } else if ($raw == "pure") {
143 // send a zip file that contains a separate xml data file and xsl stylesheet
144 if (! (class_exists('ZipArchive'))) {
145 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
146 return;
149 $tempDir = $GLOBALS['temporary_files_dir'];
150 $zipName = $tempDir . "/" . getReportFilename() . "-ccr.zip";
151 if (file_exists($zipName)) {
152 unlink($zipName);
155 $zip = new ZipArchive();
156 if (!($zip)) {
157 displayError(xl("ERROR: Unable to Create Zip Archive."));
158 return;
161 if ($zip->open($zipName, ZIPARCHIVE::CREATE)) {
162 $zip->addFile("stylesheet/ccr.xsl", "stylesheet/ccr.xsl");
163 $xmlName = $tempDir . "/" . getReportFilename() . "-ccr.xml";
164 if (file_exists($xmlName)) {
165 unlink($xmlName);
168 $ccr->save($xmlName);
169 $zip->addFile($xmlName, basename($xmlName));
170 $zip->close();
171 header("Pragma: public");
172 header("Expires: 0");
173 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
174 header("Content-Type: application/force-download");
175 header("Content-Length: " . filesize($zipName));
176 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
177 header("Content-Description: File Transfer");
178 readfile($zipName);
179 unlink($zipName);
180 unlink($xmlName);
181 exit(0);
182 } else {
183 displayError(xl("ERROR: Unable to Create Zip Archive."));
184 return;
186 } else if (substr($raw, 0, 4)=="send") {
187 $recipient = trim(stripslashes(substr($raw, 5)));
188 $result=transmitCCD($ccr, $recipient, $requested_by, "CCR");
189 echo htmlspecialchars($result, ENT_NOQUOTES);
190 return;
191 } else {
192 header("Content-type: application/xml");
193 echo $ccr->saveXml();
197 function viewCCD($ccr, $raw = "no", $requested_by = "")
199 global $pid;
201 $ccr->preserveWhiteSpace = false;
202 $ccr->formatOutput = true;
204 if (file_exists(dirname(__FILE__) .'/generatedXml')) {
205 $ccr->save(dirname(__FILE__) . '/generatedXml/ccrForCCD.xml');
208 $xmlDom = new DOMDocument();
209 $xmlDom->loadXML($ccr->saveXML());
211 $ccr_ccd = new DOMDocument();
212 $ccr_ccd->load(dirname(__FILE__) .'/ccd/ccr_ccd.xsl');
214 $xslt = new XSLTProcessor();
215 $xslt->importStylesheet($ccr_ccd);
217 $ccd = new DOMDocument();
218 $ccd->preserveWhiteSpace = false;
219 $ccd->formatOutput = true;
221 $ccd->loadXML($xslt->transformToXML($xmlDom));
223 if (file_exists(dirname(__FILE__) .'/generatedXml')) {
224 $ccd->save(dirname(__FILE__) . '/generatedXml/ccdDebug.xml');
227 if ($raw == "yes") {
228 // simply send the xml to a textarea (nice debugging tool)
229 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
230 echo $ccd->saveXml();
231 echo "</textarea>";
232 return;
235 if ($raw == "pure") {
236 // send a zip file that contains a separate xml data file and xsl stylesheet
237 if (! (class_exists('ZipArchive'))) {
238 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
239 return;
242 $tempDir = $GLOBALS['temporary_files_dir'];
243 $zipName = $tempDir . "/" . getReportFilename() . "-ccd.zip";
244 if (file_exists($zipName)) {
245 unlink($zipName);
248 $zip = new ZipArchive();
249 if (!($zip)) {
250 displayError(xl("ERROR: Unable to Create Zip Archive."));
251 return;
254 if ($zip->open($zipName, ZIPARCHIVE::CREATE)) {
255 $zip->addFile("stylesheet/cda.xsl", "stylesheet/cda.xsl");
256 $xmlName = $tempDir . "/" . getReportFilename() . "-ccd.xml";
257 if (file_exists($xmlName)) {
258 unlink($xmlName);
261 $e_styleSheet = $ccd->createProcessingInstruction(
262 'xml-stylesheet',
263 'type="text/xsl" href="stylesheet/cda.xsl"'
265 $ccd->insertBefore($e_styleSheet, $ccd->firstChild);
266 $ccd->save($xmlName);
267 $zip->addFile($xmlName, basename($xmlName));
268 $zip->close();
269 header("Pragma: public");
270 header("Expires: 0");
271 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
272 header("Content-Type: application/force-download");
273 header("Content-Length: " . filesize($zipName));
274 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
275 header("Content-Description: File Transfer");
276 readfile($zipName);
277 unlink($zipName);
278 unlink($xmlName);
279 exit(0);
280 } else {
281 displayError(xl("ERROR: Unable to Create Zip Archive."));
282 return;
286 if (substr($raw, 0, 4)=="send") {
287 $recipient = trim(stripslashes(substr($raw, 5)));
288 $result=transmitCCD($ccd, $recipient, $requested_by);
289 echo htmlspecialchars($result, ENT_NOQUOTES);
290 return;
293 $ss = new DOMDocument();
294 $ss->load(dirname(__FILE__) ."/stylesheet/cda.xsl");
296 $xslt->importStyleSheet($ss);
298 $html = $xslt->transformToXML($ccd);
300 echo $html;
304 function sourceType($ccr, $uuid)
307 $e_Source = $ccr->createElement('Source');
309 $e_Actor = $ccr->createElement('Actor');
310 $e_Source->appendChild($e_Actor);
312 $e_ActorID = $ccr->createElement('ActorID', $uuid);
313 $e_Actor->appendChild($e_ActorID);
315 return $e_Source;
319 function displayError($message)
321 echo '<script type="text/javascript">alert("' . addslashes($message) . '");</script>';
325 function createHybridXML($ccr)
328 // save the raw xml
329 $main_xml = $ccr->saveXml();
331 // save the stylesheet
332 $main_stylesheet = file_get_contents('stylesheet/ccr.xsl');
334 // replace stylesheet link in raw xml file
335 $substitute_string = '<?xml-stylesheet type="text/xsl" href="#style1"?>
336 <!DOCTYPE ContinuityOfCareRecord [
337 <!ATTLIST xsl:stylesheet id ID #REQUIRED>
340 $replace_string = '<?xml-stylesheet type="text/xsl" href="stylesheet/ccr.xsl"?>';
341 $main_xml = str_replace($replace_string, $substitute_string, $main_xml);
343 // remove redundant xml declaration from stylesheet
344 $replace_string = '<?xml version="1.0" encoding="UTF-8"?>';
345 $main_stylesheet = str_replace($replace_string, '', $main_stylesheet);
347 // embed the stylesheet in the raw xml file
348 $replace_string ='<ContinuityOfCareRecord xmlns="urn:astm-org:CCR">';
349 $main_stylesheet = $replace_string.$main_stylesheet;
350 $main_xml = str_replace($replace_string, $main_stylesheet, $main_xml);
352 // insert style1 id into the stylesheet parameter
353 $substitute_string = 'xsl:stylesheet id="style1" exclude-result-prefixes';
354 $replace_string = 'xsl:stylesheet exclude-result-prefixes';
355 $main_xml = str_replace($replace_string, $substitute_string, $main_xml);
357 // prepare the filename to use
358 // LASTNAME-FIRSTNAME-PID-DATESTAMP-ccr.xml
359 $main_filename = getReportFilename()."-ccr.xml";
361 // send the output as a file to the user
362 header("Content-type: text/xml");
363 header("Content-Disposition: attachment; filename=" . $main_filename . "");
364 echo $main_xml;
367 if ($_POST['ccrAction']) {
368 $raw=$_POST['raw'];
369 /* If transmit requested, fail fast if the recipient address fails basic validation */
370 if (substr($raw, 0, 4)=="send") {
371 $send_to = trim(stripslashes(substr($raw, 5)));
372 if (!PHPMailer::ValidateAddress($send_to)) {
373 echo(htmlspecialchars(xl('Invalid recipient address. Please try again.'), ENT_QUOTES));
374 return;
377 createCCR($_POST['ccrAction'], $raw, $_POST['requested_by']);
378 } else {
379 createCCR($_POST['ccrAction'], $raw);