minor bug fix
[openemr.git] / library / classes / class.Parser_HL7v2.php
blob7101a93f93d81668668dd1209fe7a7f332cd03b0
1 <?php
2 // $Id$
3 // $Author$
4 // HL7 Parser
6 class Parser_HL7v2 {
8 var $field_separator;
9 var $map;
10 var $message;
11 var $message_type;
13 var $MSH;
14 var $EVN;
16 function Parser_HL7v2 ( $message, $_options = NULL ) {
17 // Assume separator is a pipe
18 $this->message = $message;
19 $this->field_separator = '|';
20 if (is_array($_options)) {
21 $this->options = $_options;
24 function parse () {
25 $message = $this->message;
26 // Split HL7v2 message into lines
27 $segments = explode("\n", $message);
28 // Fail if there are no or one segments
29 if (count($segments) <= 1) {
30 return false;
33 // Loop through messages
34 $count = 0;
35 foreach ($segments AS $__garbage => $segment) {
36 $count++;
38 // Determine segment ID
39 $type = substr($segment, 0, 3);
40 switch ($type) {
41 case 'MSH':
42 case 'EVN':
43 $this->message_type = trim($type);
44 call_user_func_array(
45 array(&$this, '_'.$type),
46 array(
47 // All but type
48 substr(
49 $segment,
50 -(strlen($segment)-3)
54 $this->map[$count]['type'] = $type;
55 $this->map[$count]['position'] = 0;
56 break;
58 default:
59 $this->message_type = trim($type);
60 $this->__default_segment_parser($segment);
61 $this->map[$count]['type'] = $type;
62 $this->map[$count]['position'] = count($this->message[$type]);
63 break;
64 } // end switch type
67 // Depending on message type, handle differently
68 switch ($this->message_type) {
69 default:
70 return ('Message type '.$this->message_type.' is '.
71 'currently unhandled'."<br/>\n");
72 break;
73 } // end switch
74 } // end constructor Parser_HL7v2
76 function Handle() {
77 // Set to handle current method
78 $type = str_replace('^', '_', $this->MSH['message_type']);
80 // Check for an appropriate handler
81 $handler = CreateObject('_FreeMED.Handler_HL7v2_'.$type, $this);
83 // Error out if the handler doesn't exist
84 if (!is_object($handler)) {
85 if ($this->options['debug']) {
86 print "<b>Could not load class ".
87 "_FreeMED.Handler_HL7v2_".$type.
88 "</b><br/>\n";
90 return false;
93 // Run appropriate handler
94 return $handler->Handle();
95 } // end method Handle
97 //----- All handlers go below here
99 function _EVN ($segment) {
100 $composites = $this->__parse_segment ($segment);
101 if ($this->options['debug']) {
102 print "<b>EVN segment</b><br/>\n";
103 foreach ($composites as $k => $v) {
104 print "composite[$k] = ".prepare($v)."<br/>\n";
108 list (
109 $this->EVN['event_type_code'],
110 $this->EVN['event_datetime'],
111 $this->EVN['event_planned'],
112 $this->EVN['event_reason'],
113 $this->EVN['operator_id']
114 ) = $composites;
115 } // end method _EVN
117 function _MSH ($segment) {
118 // Get separator
119 $this->field_separator = substr($segment, 0, 1);
120 $composites = $this->__parse_segment ($segment);
121 if ($this->options['debug']) {
122 print "<b>MSH segment</b><br/>\n";
123 foreach ($composites as $k => $v) {
124 print "composite[$k] = ".prepare($v)."<br/>\n";
128 // Assign values
129 list (
130 $__garbage, // Skip index [0], it's the separator
131 $this->MSH['encoding_characters'],
132 $this->MSH['sending_application'],
133 $this->MSH['sending_facility'] ,
134 $this->MSH['receiving_application'],
135 $this->MSH['receiving_facility'],
136 $this->MSH['message_datetime'],
137 $this->MSH['security'],
138 $this->MSH['message_type'],
139 $this->MSH['message_control_id'],
140 $this->MSH['processing_id'],
141 $this->MSH['version_id'],
142 $this->MSH['sequence_number'],
143 $this->MSH['confirmation_pointer'],
144 $this->MSH['accept_ack_type'],
145 $this->MSH['application_ack_type'],
146 $this->MSH['country_code']
147 ) = $composites;
149 // TODO: Extract $this->MSH['encoding_characters'] and use
150 // it instead of assuming the defaults.
151 } // end method _MSH
153 //----- Truly internal functions
155 function __default_segment_parser ($segment) {
156 $composites = $this->__parse_segment($segment);
158 // The first composite is always the message type
159 $type = $composites[0];
161 // Debug
162 if ($this->options['debug']) {
163 print "<b>".$type." segment</b><br/>\n";
164 foreach ($composites as $k => $v) {
165 print "composite[$k] = ".prepare($v)."<br/>\n";
169 // Try to parse composites
170 foreach ($composites as $key => $composite) {
171 // If it is a composite ...
172 if (!(strpos($composite, '^') === false)) {
173 $composites[$key] = $this->__parse_composite($composite);
177 $pos = 0;
179 // Find out where we are
180 if (is_array($this->message[$type])) {
181 $pos = count($this->message[$type]);
184 //Ramesh Nagul - EnSoftek commented line out as it is throwing an error in parsing.
185 //$this->message[$type][$pos] = $composites;
186 // Add parsed segment to message
187 //Fix is below
188 $this->map[$pos][$type] = $composites;
189 } // end method __default_segment_parser
191 function __parse_composite ($composite) {
192 return explode('^', $composite);
193 } // end method __parse_composite
195 function __parse_segment ($segment) {
196 return explode($this->field_separator, $segment);
197 } // end method __parse_segment
199 function composite_array() {
200 $cmp = array();
201 $cmp["MSH"] = $this->MSH;
202 $cmp["EVN"] = $this->EVN;
203 return $cmp;
205 } // end class Parser_HL7v2