Eye Progress 1 (#4219)
[openemr.git] / ccr / createCCR.php
blob3324d119f7bf7e49f14670fb3722c8e7d6172fb9
1 <?php
3 /**
4 * CCR Script.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Garden State Health Systems <http://www.gshsys.com/>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2010 Garden State Health Systems <http://www.gshsys.com/>
11 * @copyright Copyright (c) 2018-2019 Brady Miller <brady.g.miller@gmail.com>
12 * @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'])) {
18 $landingpage = "../portal/index.php";
20 // Will start the (patient) portal OpenEMR session/cookie.
21 require_once(dirname(__FILE__) . "/../src/Common/Session/SessionUtil.php");
22 OpenEMR\Common\Session\SessionUtil::portalSessionStart();
24 if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite_two'])) {
25 $pid = $_SESSION['pid'];
26 $ignoreAuth = true;
27 global $ignoreAuth;
28 } else {
29 OpenEMR\Common\Session\SessionUtil::portalSessionCookieDestroy();
30 header('Location: ' . $landingpage . '?w');
31 exit;
35 require_once(dirname(__FILE__) . "/../interface/globals.php");
36 require_once(dirname(__FILE__) . "/../library/sql-ccr.inc");
37 require_once(dirname(__FILE__) . "/uuid.php");
38 require_once(dirname(__FILE__) . "/transmitCCD.php");
39 require_once(dirname(__FILE__) . "/../custom/code_types.inc.php");
41 use PHPMailer\PHPMailer\PHPMailer;
43 function createCCR($action, $raw = "no", $requested_by = "")
46 $authorID = getUuid();
47 $patientID = getUuid();
48 $sourceID = getUuid();
49 $oemrID = getUuid();
51 $result = getActorData();
52 while ($res = sqlFetchArray($result[2])) {
53 ${"labID{$res['id']}"} = getUuid();
56 $ccr = new DOMDocument('1.0', 'UTF-8');
57 $e_styleSheet = $ccr->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="stylesheet/ccr.xsl"');
58 $ccr->appendChild($e_styleSheet);
60 $e_ccr = $ccr->createElementNS('urn:astm-org:CCR', 'ContinuityOfCareRecord');
61 $ccr->appendChild($e_ccr);
63 /////////////// Header
65 require_once("createCCRHeader.php");
66 $e_Body = $ccr->createElement('Body');
67 $e_ccr->appendChild($e_Body);
69 /////////////// Problems
71 $e_Problems = $ccr->createElement('Problems');
72 require_once("createCCRProblem.php");
73 $e_Body->appendChild($e_Problems);
75 /////////////// Alerts
77 $e_Alerts = $ccr->createElement('Alerts');
78 require_once("createCCRAlerts.php");
79 $e_Body->appendChild($e_Alerts);
81 ////////////////// Medication
83 $e_Medications = $ccr->createElement('Medications');
84 require_once("createCCRMedication.php");
85 $e_Body->appendChild($e_Medications);
87 ///////////////// Immunization
89 $e_Immunizations = $ccr->createElement('Immunizations');
90 require_once("createCCRImmunization.php");
91 $e_Body->appendChild($e_Immunizations);
94 /////////////////// Results
96 $e_Results = $ccr->createElement('Results');
97 require_once("createCCRResult.php");
98 $e_Body->appendChild($e_Results);
101 /////////////////// Procedures
103 //$e_Procedures = $ccr->createElement('Procedures');
104 //require_once("createCCRProcedure.php");
105 //$e_Body->appendChild($e_Procedures);
107 //////////////////// Footer
109 // $e_VitalSigns = $ccr->createElement('VitalSigns');
110 // $e_Body->appendChild($e_VitalSigns);
112 /////////////// Actors
114 $e_Actors = $ccr->createElement('Actors');
115 require_once("createCCRActor.php");
116 $e_ccr->appendChild($e_Actors);
118 if ($action == "generate") {
119 gnrtCCR($ccr, $raw, $requested_by);
122 if ($action == "viewccd") {
123 viewCCD($ccr, $raw, $requested_by);
127 function gnrtCCR($ccr, $raw = "no", $requested_by = "")
129 global $pid;
131 $ccr->preserveWhiteSpace = false;
132 $ccr->formatOutput = true;
134 if ($raw == "yes") {
135 // simply send the xml to a textarea (nice debugging tool)
136 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
137 echo $ccr->saveXml();
138 echo "</textarea>";
139 return;
140 } elseif ($raw == "hybrid") {
141 // send a file that contains a hybrid file of the raw xml and the xsl stylesheet
142 createHybridXML($ccr);
143 } elseif ($raw == "pure") {
144 // send a zip file that contains a separate xml data file and xsl stylesheet
145 if (! (class_exists('ZipArchive'))) {
146 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
147 return;
150 $tempDir = $GLOBALS['temporary_files_dir'];
151 $zipName = $tempDir . "/" . getReportFilename() . "-ccr.zip";
152 if (file_exists($zipName)) {
153 unlink($zipName);
156 $zip = new ZipArchive();
157 if (!($zip)) {
158 displayError(xl("ERROR: Unable to Create Zip Archive."));
159 return;
162 if ($zip->open($zipName, ZipArchive::CREATE)) {
163 $zip->addFile("stylesheet/ccr.xsl", "stylesheet/ccr.xsl");
164 $xmlName = $tempDir . "/" . getReportFilename() . "-ccr.xml";
165 if (file_exists($xmlName)) {
166 unlink($xmlName);
169 $ccr->save($xmlName);
170 $zip->addFile($xmlName, basename($xmlName));
171 $zip->close();
172 header("Pragma: public");
173 header("Expires: 0");
174 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
175 header("Content-Type: application/force-download");
176 header("Content-Length: " . filesize($zipName));
177 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
178 header("Content-Description: File Transfer");
179 readfile($zipName);
180 unlink($zipName);
181 unlink($xmlName);
182 exit(0);
183 } else {
184 displayError(xl("ERROR: Unable to Create Zip Archive."));
185 return;
187 } elseif (substr($raw, 0, 4) == "send") {
188 $recipient = trim(stripslashes(substr($raw, 5)));
189 $result = transmitCCD($ccr, $recipient, $requested_by, "CCR");
190 echo htmlspecialchars($result, ENT_NOQUOTES);
191 return;
192 } else {
193 header("Content-type: application/xml");
194 echo $ccr->saveXml();
198 function viewCCD($ccr, $raw = "no", $requested_by = "")
200 global $pid;
202 $ccr->preserveWhiteSpace = false;
203 $ccr->formatOutput = true;
205 if (file_exists(dirname(__FILE__) . '/generatedXml')) {
206 $ccr->save(dirname(__FILE__) . '/generatedXml/ccrForCCD.xml');
209 $xmlDom = new DOMDocument();
210 $xmlDom->loadXML($ccr->saveXML());
212 $ccr_ccd = new DOMDocument();
213 $ccr_ccd->load(dirname(__FILE__) . '/ccd/ccr_ccd.xsl');
215 $xslt = new XSLTProcessor();
216 $xslt->importStylesheet($ccr_ccd);
218 $ccd = new DOMDocument();
219 $ccd->preserveWhiteSpace = false;
220 $ccd->formatOutput = true;
222 $ccd->loadXML($xslt->transformToXML($xmlDom));
224 if (file_exists(dirname(__FILE__) . '/generatedXml')) {
225 $ccd->save(dirname(__FILE__) . '/generatedXml/ccdDebug.xml');
228 if ($raw == "yes") {
229 // simply send the xml to a textarea (nice debugging tool)
230 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
231 echo $ccd->saveXml();
232 echo "</textarea>";
233 return;
236 if ($raw == "pure") {
237 // send a zip file that contains a separate xml data file and xsl stylesheet
238 if (! (class_exists('ZipArchive'))) {
239 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
240 return;
243 $tempDir = $GLOBALS['temporary_files_dir'];
244 $zipName = $tempDir . "/" . getReportFilename() . "-ccd.zip";
245 if (file_exists($zipName)) {
246 unlink($zipName);
249 $zip = new ZipArchive();
250 if (!($zip)) {
251 displayError(xl("ERROR: Unable to Create Zip Archive."));
252 return;
255 if ($zip->open($zipName, ZipArchive::CREATE)) {
256 $zip->addFile("stylesheet/cda.xsl", "stylesheet/cda.xsl");
257 $xmlName = $tempDir . "/" . getReportFilename() . "-ccd.xml";
258 if (file_exists($xmlName)) {
259 unlink($xmlName);
262 $e_styleSheet = $ccd->createProcessingInstruction(
263 'xml-stylesheet',
264 'type="text/xsl" href="stylesheet/cda.xsl"'
266 $ccd->insertBefore($e_styleSheet, $ccd->firstChild);
267 $ccd->save($xmlName);
268 $zip->addFile($xmlName, basename($xmlName));
269 $zip->close();
270 header("Pragma: public");
271 header("Expires: 0");
272 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
273 header("Content-Type: application/force-download");
274 header("Content-Length: " . filesize($zipName));
275 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
276 header("Content-Description: File Transfer");
277 readfile($zipName);
278 unlink($zipName);
279 unlink($xmlName);
280 exit(0);
281 } else {
282 displayError(xl("ERROR: Unable to Create Zip Archive."));
283 return;
287 if (substr($raw, 0, 4) == "send") {
288 $recipient = trim(stripslashes(substr($raw, 5)));
289 $result = transmitCCD($ccd, $recipient, $requested_by);
290 echo htmlspecialchars($result, ENT_NOQUOTES);
291 return;
294 $ss = new DOMDocument();
295 $ss->load(dirname(__FILE__) . "/stylesheet/cda.xsl");
297 $xslt->importStyleSheet($ss);
299 $html = $xslt->transformToXML($ccd);
301 echo $html;
305 function sourceType($ccr, $uuid)
308 $e_Source = $ccr->createElement('Source');
310 $e_Actor = $ccr->createElement('Actor');
311 $e_Source->appendChild($e_Actor);
313 $e_ActorID = $ccr->createElement('ActorID', $uuid);
314 $e_Actor->appendChild($e_ActorID);
316 return $e_Source;
320 function displayError($message)
322 echo '<script>alert("' . addslashes($message) . '");</script>';
326 function createHybridXML($ccr)
329 // save the raw xml
330 $main_xml = $ccr->saveXml();
332 // save the stylesheet
333 $main_stylesheet = file_get_contents('stylesheet/ccr.xsl');
335 // replace stylesheet link in raw xml file
336 $substitute_string = '<?xml-stylesheet type="text/xsl" href="#style1"?>
337 <!DOCTYPE ContinuityOfCareRecord [
338 <!ATTLIST xsl:stylesheet id ID #REQUIRED>
341 $replace_string = '<?xml-stylesheet type="text/xsl" href="stylesheet/ccr.xsl"?>';
342 $main_xml = str_replace($replace_string, $substitute_string, $main_xml);
344 // remove redundant xml declaration from stylesheet
345 $replace_string = '<?xml version="1.0" encoding="UTF-8"?>';
346 $main_stylesheet = str_replace($replace_string, '', $main_stylesheet);
348 // embed the stylesheet in the raw xml file
349 $replace_string = '<ContinuityOfCareRecord xmlns="urn:astm-org:CCR">';
350 $main_stylesheet = $replace_string . $main_stylesheet;
351 $main_xml = str_replace($replace_string, $main_stylesheet, $main_xml);
353 // insert style1 id into the stylesheet parameter
354 $substitute_string = 'xsl:stylesheet id="style1" exclude-result-prefixes';
355 $replace_string = 'xsl:stylesheet exclude-result-prefixes';
356 $main_xml = str_replace($replace_string, $substitute_string, $main_xml);
358 // prepare the filename to use
359 // LASTNAME-FIRSTNAME-PID-DATESTAMP-ccr.xml
360 $main_filename = getReportFilename() . "-ccr.xml";
362 // send the output as a file to the user
363 header("Content-type: text/xml");
364 header("Content-Disposition: attachment; filename=" . $main_filename . "");
365 echo $main_xml;
368 if ($_POST['ccrAction']) {
369 $raw = $_POST['raw'];
370 /* If transmit requested, fail fast if the recipient address fails basic validation */
371 if (substr($raw, 0, 4) == "send") {
372 $send_to = trim(stripslashes(substr($raw, 5)));
373 if (!PHPMailer::ValidateAddress($send_to)) {
374 echo(htmlspecialchars(xl('Invalid recipient address. Please try again.'), ENT_QUOTES));
375 return;
378 createCCR($_POST['ccrAction'], $raw, $_POST['requested_by']);
379 } else {
380 createCCR($_POST['ccrAction'], $raw);