migrated ubiquitous libraries to composer autoloader (#421)
[openemr.git] / interface / forms / CAMOS / content_parser.php
blobde463ca948ab841fb6cc8c6c8116b9eb6165e3d9
1 <?php
3 include_once("../../globals.php");
4 include_once("../../../library/sql.inc");
6 function addAppt($days,$time) {
7 $days = formDataCore($days);
8 $time = formDataCore($time);
10 $sql = "insert into openemr_postcalendar_events (pc_pid, pc_eventDate," .
11 "pc_comments, pc_aid,pc_startTime) values (" .
12 $_SESSION['pid'] . ", date_add(current_date(), interval " . $days .
13 " day),'from CAMOS', " . $_SESSION['authId'] . ",'$time')";
14 return sqlInsert($sql);
16 function addVitals($weight, $height, $systolic, $diastolic, $pulse, $temp) {
17 //This is based on code from /openemr/interface/forms/vitals/C_FormVitals.class.php
18 //if it doesn't work, look there for changes.
19 $_POST['process'] = 'true';
20 $_POST['weight'] = $weight;
21 $_POST['height'] = $height;
22 $_POST['bps'] = $systolic;
23 $_POST['bpd'] = $diastolic;
24 $_POST['pulse'] = $pulse;
25 $_POST['temperature'] = $temp;
26 include_once("../../../library/api.inc");
27 require ("../vitals/C_FormVitals.class.php");
28 $c = new C_FormVitals();
29 echo $c->default_action_process($_POST);
32 //This function was copied from billing.inc and altered to support 'justify'
33 function addBilling2($encounter, $code_type, $code, $code_text, $modifier="",$units="",$fee="0.00",$justify)
35 $justify_string = '';
36 if ($justify)
38 //trim eahc entry
39 foreach ($justify as $temp_justify) {
40 $justify_trimmed[] = trim($temp_justify);
42 //format it
43 $justify_string = implode(":",$justify_trimmed).":";
45 $code_type = formDataCore($code_type);
46 $code = formDataCore($code);
47 $code_text = formDataCore($code_text);
48 $modifier = formDataCore($modifier);
49 $units = formDataCore($units);
50 $fee = formDataCore($fee);
51 $justify_string = formDataCore($justify_string);
53 // set to authorize billing codes as default - bm
54 // could place logic here via acls to control who
55 // can authorize as a feature in the future
56 $authorized=1;
58 $sql = "insert into billing (date, encounter, code_type, code, code_text, pid, authorized, user, groupname,activity,billed,provider_id,modifier,units,fee,justify) values (NOW(), '".$_SESSION['encounter']."', '$code_type', '$code', '$code_text', '".$_SESSION['pid']."', '$authorized', '" . $_SESSION['authId'] . "', '" . $_SESSION['authProvider'] . "',1,0,".$_SESSION['authUserID'].",'$modifier','$units','$fee','$justify_string')";
60 return sqlInsert($sql);
64 function content_parser($input) {
66 // parse content field
67 $content = $input;
69 // comments should really be removed in save.php
70 // $content = remove_comments($content);
72 //reduce more than two empty lines to no more than two.
73 $content = preg_replace("/([^\n]\r[^\n]){2,}/","\r\r",$content);
74 $content = preg_replace("/([^\r]\n[^\r]){2,}/","\n\n",$content);
75 $content = preg_replace("/(\r\n){2,}/","\r\n\r\n",$content);
78 return $content;
81 // implement C style comments ie remove anything between /* and */
82 function remove_comments($string_to_process) {
83 return preg_replace("/\/\*.*?\*\//s","",$string_to_process);
86 //process commands embedded in C style comments where function name is first
87 //followed by args separated by :: delimiter and nothing else
89 function process_commands(&$string_to_process, &$camos_return_data) {
91 //First, handle replace function as special case. full depth of inserts should be evaluated prior
92 //to evaluating other functions in final string assembly.
93 $replace_finished = FALSE;
94 while (!$replace_finished) {
95 if (preg_match_all("/\/\*\s*replace\s*::.*?\*\//",$string_to_process, $matches)) {
96 foreach($matches[0] as $val) {
97 $comm = preg_replace("/(\/\*)|(\*\/)/","",$val);
98 $comm_array = explode('::', $comm); //array where first element is command and rest are args
99 $replacement_item = trim($comm_array[1]); //this is the item name to search for in the database. easy.
100 $replacement_text = '';
101 $query = "SELECT content FROM form_CAMOS_item WHERE item like '".$replacement_item."'";
102 $statement = sqlStatement($query);
103 if ($result = sqlFetchArray($statement)) {$replacement_text = $result['content'];}
104 $replacement_text = formDataCore($replacement_text);
105 $string_to_process = str_replace($val,$replacement_text,$string_to_process);
108 else {$replace_finished = TRUE;}
110 //date_add is a function to add a given number of days to the date of the current encounter
111 //this will be useful for saving templates of prescriptions with 'do not fill until' dates
112 //I am going to implement with mysql date functions.
113 //I am putting this before other functions just like replace function because it is replacing text
114 //needs to be here.
115 if (preg_match("/\/\*\s*date_add\s*::\s*(.*?)\s*\*\//",$string_to_process, $matches)) {
116 $to_replace = $matches[0];
117 $days = $matches[1];
118 $query = "select date_format(date_add(date, interval $days day),'%W, %m-%d-%Y') as date from form_encounter where " . "pid = " . $_SESSION['pid'] . " and encounter = " . $_SESSION['encounter'];
119 $statement = sqlStatement($query);
120 if ($result = sqlFetchArray($statement)){
121 $string_to_process = str_replace($to_replace,$result['date'],$string_to_process);
124 if (preg_match("/\/\*\s*date_sub\s*::\s*(.*?)\s*\*\//",$string_to_process, $matches)) {
125 $to_replace = $matches[0];
126 $days = $matches[1];
127 $query = "select date_format(date_sub(date, interval $days day),'%W, %m-%d-%Y') as date from form_encounter where " . "pid = " . $_SESSION['pid'] . " and encounter = " . $_SESSION['encounter'];
128 $statement = sqlStatement($query);
129 if ($result = sqlFetchArray($statement)){
130 $string_to_process = str_replace($to_replace,$result['date'],$string_to_process);
135 //end of special case of replace function
136 $return_value = 0;
137 $camos_return_data = array(); // to be filled with additional camos form submissions if any embedded
138 $command_array = array(); //to be filled with potential commands
139 $matches= array(); //to be filled with potential commands
140 if (!preg_match_all("/\/\*.*?\*\//s",$string_to_process, $matches)) {return $return_value;}
141 $command_array = $matches[0];
142 foreach($command_array as $val) {
143 //process each command
144 $comm = preg_replace("/(\/\*)|(\*\/)/","",$val);
145 $comm_array = explode('::', $comm); //array where first element is command and rest are args
146 //Here is where we process particular commands
147 if (trim($comm_array[0])== 'billing') {
148 array_shift($comm_array); //couldn't do it in 'if' or would lose element 0 for next if
149 //insert data into the billing table, see, easy!
150 $type = trim(array_shift($comm_array));
151 $code = trim(array_shift($comm_array));
152 $text = trim(array_shift($comm_array));
153 $modifier = trim(array_shift($comm_array));
154 $units = trim(array_shift($comm_array));
155 //make default units 1 if left blank - bm
156 if ($units == '') {
157 $units = 1;
159 $fee = sprintf("%01.2f",trim(array_shift($comm_array)));
160 //make default fee 0.00 if left blank
161 if ($fee == '') {
162 $fee = sprintf("%01.2f",'0.00');
164 //in function call 'addBilling' note last param is the remainder of the array. we will look for justifications here...
165 addBilling2($encounter, $type, $code, $text, $modifier,$units,$fee,$comm_array);
167 if (trim($comm_array[0])== 'appt') {
168 array_shift($comm_array);
169 $days = trim(array_shift($comm_array));
170 $time = trim(array_shift($comm_array));
171 addAppt($days, $time);
173 if (trim($comm_array[0])== 'vitals') {
174 array_shift($comm_array);
175 $weight = trim(array_shift($comm_array));
176 $height = trim(array_shift($comm_array));
177 $systolic = trim(array_shift($comm_array));
178 $diastolic = trim(array_shift($comm_array));
179 $pulse = trim(array_shift($comm_array));
180 $temp = trim(array_shift($comm_array));
181 addVitals($weight, $height, $systolic, $diastolic, $pulse, $temp);
183 $command_count = 0;
184 if (trim($comm_array[0]) == 'camos') {
185 $command_count++;
186 //data to be submitted as separate camos forms
187 //this is for embedded prescriptions, test orders etc... usually within a soap note or something
188 //data collected here will be returned so that save.php can give it special treatment and insert
189 //into the database after the main form data is submitted so it will be in a sensible order
190 array_push($camos_return_data,
191 array("category" => trim($comm_array[1]),
192 "subcategory" => trim($comm_array[2]),
193 "item" => trim($comm_array[3]),
194 "content" => trim($comm_array[4])));
197 $string_to_process = remove_comments($string_to_process);
198 return $return_value;
200 // I was using this for debugging. touch logging, chmod 777 logging, then can use.
201 //file_put_contents('./logging',$string_to_process."\n\n*************\n\n",FILE_APPEND);//DEBUG
203 function replace($pid, $enc, $content) { //replace placeholders with values
204 $name= '';
205 $fname = '';
206 $mname = '';
207 $lname = '';
208 $dob = '';
209 $date = '';
210 $age = '';
211 $gender = '';
212 $doctorname = '';
213 $query1 = sqlStatement(
214 "select t1.fname, t1.mname, t1.lname, " .
215 "t1.sex as gender, " .
216 "t1.DOB, " .
217 "t2.date " .
218 "from patient_data as t1 join form_encounter as t2 on " .
219 "(t1.pid = t2.pid) " .
220 "where t2.pid = ".$pid." and t2.encounter = ".$enc);
221 if ($results = sqlFetchArray($query1)) {
222 $fname = $results['fname'];
223 $mname = $results['mname'];
224 $lname = $results['lname'];
225 if ($mname) {$name = $fname.' '.$mname.' '.$lname;}
226 else {$name = $fname.' '.$lname;}
227 $dob = $results['DOB'];
228 $date = $results['date'];
229 $age = patient_age($dob, $date);
230 $gender = $results['gender'];
232 $query1 = sqlStatement("select t1.lname from users as t1 join forms as " .
233 "t2 on (t1.username like t2.user) where t2.encounter = ".$enc);
234 if ($results = sqlFetchArray($query1)) {
235 $doctorname = "Dr. ".$results['lname'];
237 $ret = preg_replace(array("/patientname/i","/patientage/i","/patientgender/i","/doctorname/i"),
238 array($name,$age,strtolower($gender),$doctorname), $content);
239 return $ret;
241 function patient_age($birthday, $date) { //calculate age from birthdate and a given later date
242 list($birth_year,$birth_month,$birth_day) = explode("-",$birthday);
243 list($date_year,$date_month,$date_day) = explode("-",$date);
244 $year_diff = $date_year - $birth_year;
245 $month_diff = $date_month - $birth_month;
246 $day_diff = $date_day - $birth_day;
247 if ($month_diff < 0) $year_diff--;
248 elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
249 return $year_diff;