Advertisement
Guest User

Untitled

a guest
May 28th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. <?php
  2.  
  3. class HL7Parser
  4. {
  5. var $messageElements = array("MSH" => 18, "PID" => 30, "PV1" => 52, "IN1" => 49, "ORC" => 19, "OBR" => 43);
  6.  
  7. function getDatabaseInput($hl7message)
  8. {
  9. if ($hl7message) {
  10. // hl7message exists
  11.  
  12. $hl7ausgabe = $this->parsemsg($hl7message);
  13.  
  14. $selectedInformation = array();
  15.  
  16. $selectedInformation["patientID"] = $this->getDatabaseElement($hl7ausgabe, array('PID', 2));
  17.  
  18. $selectedInformation["patientLastName"] = $this->getDatabaseElement($hl7ausgabe,array('PID', 4, 0));
  19.  
  20. $selectedInformation["patientFirstname"] = $this->getDatabaseElement($hl7ausgabe,array('PID', 4, 1));
  21.  
  22. if ($selectedInformation["patientFirstname"] = '' && $selectedInformation["patientLastName"] = '' ){
  23.  
  24. $selectedInformation["patientLastName"] = $this->getDatabaseElement($hl7ausgabe,array('PID', 4));
  25. }
  26.  
  27. $selectedInformation["patientCode"] = $this->getDatabaseElement($hl7ausgabe,array('OBR', 3, 0));
  28.  
  29. $selectedInformation["patientDateOfBirth"] = $this->parseDateFormat($this->getDatabaseElement($hl7ausgabe,array('PID', 6)));
  30.  
  31. $selectedInformation["patientWard"] = "Found in Second IN1 segment 36 at 21012.hl7";
  32.  
  33. $selectedInformation["patientCaseNr"] = $this->getDatabaseElement($hl7ausgabe,array('PV1', 19));
  34.  
  35. $selectedInformation["patientKlAu-Nr"] = $this->getDatabaseElement($hl7ausgabe,array('ORC', 4));
  36.  
  37. $selectedInformation["patientKlAu-Dat"] = $this->parseDateTimeFormat($this->getDatabaseElement($hl7ausgabe,array('OBR', 7)));
  38.  
  39. return $selectedInformation;
  40.  
  41. }else{
  42. //no hl7 message found
  43. return false;
  44. }
  45.  
  46.  
  47. }
  48.  
  49. function getDatabaseElement($array, $segments)
  50. {
  51. foreach ($segments as $segment) {
  52.  
  53. if (is_array($array) && array_key_exists($segment, $array)) {
  54.  
  55. $array = $array[$segment];
  56.  
  57. } else {
  58.  
  59. return "";
  60. }
  61. }
  62.  
  63. return $array;
  64.  
  65. }
  66.  
  67. function parseDateFormat($string)
  68. {
  69. if ($string == ''){
  70. return '';
  71. }
  72. $string = substr_replace($string, '-', 4, 0);
  73. $string = substr_replace($string, '-', 7, 0);
  74. return $string;
  75. }
  76.  
  77. function parseDateTimeFormat($string)
  78. {
  79. $string = utf8_encode($string);
  80. $string = str_replace(" ", "", $string);
  81. $string = str_replace("\t", "", $string);
  82.  
  83. if ($string == ''){
  84. return '';
  85. }
  86. $date = $this->parseDateFormat(substr($string, 0, 8));
  87. $time = substr($string, 8);
  88. $time = substr_replace($time, ':', 4, 0);
  89. $time = substr_replace($time, ':', 2, 0);
  90. return $date . " " . $time;
  91. }
  92.  
  93. function parsemsg($string)
  94. {
  95. //looks for the closing bar of the delimiting characters
  96. if (substr($string, 0, 3) != 'MSH') {
  97. $out['ERROR'][0] = 'Invalid HL7 Message.';
  98. $out['ERROR'][1] = 'Must start with MSH';
  99. return $out;
  100. }
  101.  
  102. //get delimiting characters
  103.  
  104. $delbarpos = strpos($string, '|', 4);
  105. $delchar = substr($string, 4, ($delbarpos - 4));
  106.  
  107. if (!defined('FLD_SEP')) {
  108. define('FLD_SEP', substr($delchar, 0, 1));
  109. }
  110. if (!defined('SFLD_SEP')) {
  111. define('SFLD_SEP', substr($delchar, 1, 1));
  112. }
  113. if (!defined('REP_SEP')) {
  114. define('REP_SEP', substr($delchar, 2, 1));
  115. }
  116. if (!defined('ESC_CHAR')) {
  117. define('ESC_CHAR', substr($delchar, 3, 1));
  118. }
  119.  
  120. $out = array();
  121.  
  122. while (strlen($string) > 0) {
  123. $segname = substr($string, 0, strpos($string, '|'));
  124. if (array_key_exists($segname, $this->messageElements)) {
  125. //valid message element name
  126. $string = substr($string, 3);
  127. $out[$segname] = array();
  128. for ($elementCount = 0; $elementCount < $this->messageElements[$segname]; $elementCount++) {
  129. $delbarposCurrent = strpos($string, '|');
  130. $delbarposNext = strpos($string, '|', $delbarposCurrent + 1);
  131. if (($elementCount + 1) == $this->messageElements[$segname]) {
  132. //end of segment, next segment must be manually seperated
  133. if ($delbarposNext) {
  134. //next segment exists
  135. $delbarposNext -= 3;
  136. } else {
  137. //no next segment -> end of message
  138. $delbarposNext = strlen($string);
  139. }
  140.  
  141. }
  142.  
  143. $element = substr($string, $delbarposCurrent + 1, $delbarposNext - 1);
  144.  
  145. if (strpos($element, FLD_SEP) == false) {
  146. //no field seperator -> only one information in element
  147. $out[$segname][] = $element;
  148.  
  149. } else {
  150. //field seperator found -> element contains multiple information
  151. $elements = array();
  152.  
  153. $sf = explode(FLD_SEP, $element);
  154.  
  155. foreach ($sf as $f) {
  156. $elements[] = $f;
  157. }
  158. $out[$segname][] = $elements;
  159. }
  160. $string = substr($string, $delbarposNext);
  161. }
  162. } else {
  163. $out['ERROR'][0] = 'Invalid HL7 Message.';
  164. $out['ERROR'][1] = $segname . " is not a valid Segment";
  165. return $out;
  166. }
  167.  
  168.  
  169. }
  170. return $out;
  171. }
  172. }
  173.  
  174. function getFiles($directory)
  175. {
  176. //check directory for new hl7 messages
  177. $handle = opendir($directory);
  178. $files = array();
  179. while ($file = readdir($handle)) {
  180. if ($file != "." && $file != ".." && !is_dir($file) && $file != 'Description') {
  181. $files[] = $file;
  182. }
  183. }
  184. return $files;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement