update
[openemr.git] / interface / forms / CAMOS / content_parser.php
blob755a18dc4b29229704e950a9ab40124719fc6edb
1 <?
2 include_once("../../globals.php");
3 include_once("../../../library/sql.inc");
5 function addAppt($days,$time) {
6 $sql = "insert into openemr_postcalendar_events (pc_pid, pc_eventDate," .
7 "pc_comments, pc_aid,pc_startTime) values (" .
8 $_SESSION['pid'] . ", date_add(current_date(), interval " . $days .
9 " day),'from CAMOS', " . $_SESSION['authId'] . ",'$time')";
10 return sqlInsert($sql);
12 function addVitals($weight, $height, $systolic, $diastolic, $pulse, $temp) {
13 //This is based on code from /openemr/interface/forms/vitals/C_FormVitals.class.php
14 //if it doesn't work, look there for changes.
15 $_POST['process'] = 'true';
16 $_POST['weight'] = $weight;
17 $_POST['height'] = $height;
18 $_POST['bps'] = $systolic;
19 $_POST['bpd'] = $diastolic;
20 $_POST['pulse'] = $pulse;
21 $_POST['temperature'] = $temp;
22 include_once("../../../library/api.inc");
23 require ("../vitals/C_FormVitals.class.php");
24 $c = new C_FormVitals();
25 echo $c->default_action_process($_POST);
28 //This function was copied from billing.inc and altered to support 'justify'
29 function addBilling2($encounter, $code_type, $code, $code_text, $modifier="",$units="",$fee="0.00",$justify)
31 $justify_string = '';
32 if ($justify)
34 $justify_string = implode(":",$justify).":";
36 $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')";
38 return sqlInsert($sql);
42 function content_parser($input) {
44 // parse content field
45 $content = $input;
47 // comments should really be removed in save.php
48 // $content = remove_comments($content);
50 //reduce more than two empty lines to no more than two.
51 $content = preg_replace("/([^\n]\r[^\n]){2,}/","\r\r",$content);
52 $content = preg_replace("/([^\r]\n[^\r]){2,}/","\n\n",$content);
53 $content = preg_replace("/(\r\n){2,}/","\r\n\r\n",$content);
56 return $content;
59 // implement C style comments ie remove anything between /* and */
60 function remove_comments($string_to_process) {
61 return preg_replace("/\/\*.*?\*\//s","",$string_to_process);
64 //process commands embedded in C style comments where function name is first
65 //followed by args separated by :: delimiter and nothing else
67 function process_commands(&$string_to_process, &$camos_return_data) {
69 //First, handle replace function as special case. full depth of inserts should be evaluated prior
70 //to evaluating other functions in final string assembly.
71 $replace_finished = FALSE;
72 while (!$replace_finished) {
73 if (preg_match_all("/\/\*\s*replace\s*::.*?\*\//",$string_to_process, $matches)) {
74 foreach($matches[0] as $val) {
75 $comm = preg_replace("/(\/\*)|(\*\/)/","",$val);
76 $comm_array = split('::', $comm); //array where first element is command and rest are args
77 $replacement_item = trim($comm_array[1]); //this is the item name to search for in the database. easy.
78 $replacement_text = '';
79 $query = "SELECT content FROM form_CAMOS_item WHERE item like '".$replacement_item."'";
80 $statement = sqlStatement($query);
81 if ($result = sqlFetchArray($statement)) {$replacement_text = $result['content'];}
82 $string_to_process = str_replace($val,$replacement_text,$string_to_process);
85 else {$replace_finished = TRUE;}
87 //date_add is a function to add a given number of days to the date of the current encounter
88 //this will be useful for saving templates of prescriptions with 'do not fill until' dates
89 //I am going to implement with mysql date functions.
90 //I am putting this before other functions just like replace function because it is replacing text
91 //needs to be here.
92 if (preg_match("/\/\*\s*date_add\s*::\s*(.*?)\s*\*\//",$string_to_process, $matches)) {
93 $to_replace = $matches[0];
94 $days = $matches[1];
95 $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'];
96 $statement = sqlStatement($query);
97 if ($result = sqlFetchArray($statement)){
98 $string_to_process = str_replace($to_replace,$result['date'],$string_to_process);
101 if (preg_match("/\/\*\s*date_sub\s*::\s*(.*?)\s*\*\//",$string_to_process, $matches)) {
102 $to_replace = $matches[0];
103 $days = $matches[1];
104 $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'];
105 $statement = sqlStatement($query);
106 if ($result = sqlFetchArray($statement)){
107 $string_to_process = str_replace($to_replace,$result['date'],$string_to_process);
112 //end of special case of replace function
113 $return_value = 0;
114 $camos_return_data = array(); // to be filled with additional camos form submissions if any embedded
115 $command_array = array(); //to be filled with potential commands
116 $matches= array(); //to be filled with potential commands
117 if (!preg_match_all("/\/\*.*?\*\//s",$string_to_process, $matches)) {return $return_value;}
118 $command_array = $matches[0];
119 foreach($command_array as $val) {
120 //process each command
121 $comm = preg_replace("/(\/\*)|(\*\/)/","",$val);
122 $comm_array = split('::', $comm); //array where first element is command and rest are args
123 //Here is where we process particular commands
124 if (trim($comm_array[0])== 'billing') {
125 array_shift($comm_array); //couldn't do it in 'if' or would lose element 0 for next if
126 //insert data into the billing table, see, easy!
127 $type = trim(array_shift($comm_array));
128 $code = trim(array_shift($comm_array));
129 $text = trim(array_shift($comm_array));
130 $modifier = trim(array_shift($comm_array));
131 $units = trim(array_shift($comm_array));
132 $fee = sprintf("%01.2f",trim(array_shift($comm_array)));
133 //in function call 'addBilling' note last param is the remainder of the array. we will look for justifications here...
134 addBilling2($encounter, $type, $code, $text, $modifier,$units,$fee,$comm_array);
136 if (trim($comm_array[0])== 'appt') {
137 array_shift($comm_array);
138 $days = trim(array_shift($comm_array));
139 $time = trim(array_shift($comm_array));
140 addAppt($days, $time);
142 if (trim($comm_array[0])== 'vitals') {
143 array_shift($comm_array);
144 $weight = trim(array_shift($comm_array));
145 $height = trim(array_shift($comm_array));
146 $systolic = trim(array_shift($comm_array));
147 $diastolic = trim(array_shift($comm_array));
148 $pulse = trim(array_shift($comm_array));
149 $temp = trim(array_shift($comm_array));
150 addVitals($weight, $height, $systolic, $diastolic, $pulse, $temp);
152 $command_count = 0;
153 if (trim($comm_array[0]) == 'camos') {
154 $command_count++;
155 //data to be submitted as separate camos forms
156 //this is for embedded prescriptions, test orders etc... usually within a soap note or something
157 //data collected here will be returned so that save.php can give it special treatment and insert
158 //into the database after the main form data is submitted so it will be in a sensible order
159 array_push($camos_return_data,
160 array("category" => trim($comm_array[1]),
161 "subcategory" => trim($comm_array[2]),
162 "item" => trim($comm_array[3]),
163 "content" => trim($comm_array[4])));
166 $string_to_process = remove_comments($string_to_process);
167 return $return_value;
169 // I was using this for debugging. touch logging, chmod 777 logging, then can use.
170 //file_put_contents('./logging',$string_to_process."\n\n*************\n\n",FILE_APPEND);//DEBUG
172 function replace($pid, $enc, $content) { //replace placeholders with values
173 $name= '';
174 $fname = '';
175 $mname = '';
176 $lname = '';
177 $dob = '';
178 $date = '';
179 $age = '';
180 $gender = '';
181 $query1 = sqlStatement(
182 "select t1.fname, t1.mname, t1.lname, " .
183 "t1.sex as gender, " .
184 "t1.DOB, " .
185 "t2.date " .
186 "from patient_data as t1 join form_encounter as t2 on " .
187 "(t1.pid = t2.pid) " .
188 "where t2.pid = ".$pid." and t2.encounter = ".$enc);
189 if ($results = mysql_fetch_array($query1, MYSQL_ASSOC)) {
190 $fname = $results['fname'];
191 $mname = $results['mname'];
192 $lname = $results['lname'];
193 if ($mname) {$name = $fname.' '.$mname.' '.$lname;}
194 else {$name = $fname.' '.$lname;}
195 $dob = $results['DOB'];
196 $date = $results['date'];
197 $age = patient_age($dob, $date);
198 $gender = $results['gender'];
200 $ret = preg_replace(array("/patientname/i","/patientage/i","/patientgender/i"),
201 array($name,$age,strtolower($gender)), $content);
202 return $ret;
204 function patient_age($birthday, $date) { //calculate age from birthdate and a given later date
205 list($birth_year,$birth_month,$birth_day) = explode("-",$birthday);
206 list($date_year,$date_month,$date_day) = explode("-",$date);
207 $year_diff = $date_year - $birth_year;
208 $month_diff = $date_month - $birth_month;
209 $day_diff = $date_day - $birth_day;
210 if ($month_diff < 0) $year_diff--;
211 elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
212 return $year_diff;