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