Made to work with magic quotes on or off.
[openemr.git] / interface / forms / CAMOS / content_parser.php
blobcdef87bbd191daf826b472035f1341b7457dc135
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 if(get_magic_quotes_gpc()) {
83 $replacement_text = stripslashes($replacement_text);
85 $replacement_text = mysql_real_escape_string($replacement_text);
86 $string_to_process = str_replace($val,$replacement_text,$string_to_process);
89 else {$replace_finished = TRUE;}
91 //date_add is a function to add a given number of days to the date of the current encounter
92 //this will be useful for saving templates of prescriptions with 'do not fill until' dates
93 //I am going to implement with mysql date functions.
94 //I am putting this before other functions just like replace function because it is replacing text
95 //needs to be here.
96 if (preg_match("/\/\*\s*date_add\s*::\s*(.*?)\s*\*\//",$string_to_process, $matches)) {
97 $to_replace = $matches[0];
98 $days = $matches[1];
99 $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'];
100 $statement = sqlStatement($query);
101 if ($result = sqlFetchArray($statement)){
102 $string_to_process = str_replace($to_replace,$result['date'],$string_to_process);
105 if (preg_match("/\/\*\s*date_sub\s*::\s*(.*?)\s*\*\//",$string_to_process, $matches)) {
106 $to_replace = $matches[0];
107 $days = $matches[1];
108 $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'];
109 $statement = sqlStatement($query);
110 if ($result = sqlFetchArray($statement)){
111 $string_to_process = str_replace($to_replace,$result['date'],$string_to_process);
116 //end of special case of replace function
117 $return_value = 0;
118 $camos_return_data = array(); // to be filled with additional camos form submissions if any embedded
119 $command_array = array(); //to be filled with potential commands
120 $matches= array(); //to be filled with potential commands
121 if (!preg_match_all("/\/\*.*?\*\//s",$string_to_process, $matches)) {return $return_value;}
122 $command_array = $matches[0];
123 foreach($command_array as $val) {
124 //process each command
125 $comm = preg_replace("/(\/\*)|(\*\/)/","",$val);
126 $comm_array = split('::', $comm); //array where first element is command and rest are args
127 //Here is where we process particular commands
128 if (trim($comm_array[0])== 'billing') {
129 array_shift($comm_array); //couldn't do it in 'if' or would lose element 0 for next if
130 //insert data into the billing table, see, easy!
131 $type = trim(array_shift($comm_array));
132 $code = trim(array_shift($comm_array));
133 $text = trim(array_shift($comm_array));
134 $modifier = trim(array_shift($comm_array));
135 $units = trim(array_shift($comm_array));
136 $fee = sprintf("%01.2f",trim(array_shift($comm_array)));
137 //in function call 'addBilling' note last param is the remainder of the array. we will look for justifications here...
138 addBilling2($encounter, $type, $code, $text, $modifier,$units,$fee,$comm_array);
140 if (trim($comm_array[0])== 'appt') {
141 array_shift($comm_array);
142 $days = trim(array_shift($comm_array));
143 $time = trim(array_shift($comm_array));
144 addAppt($days, $time);
146 if (trim($comm_array[0])== 'vitals') {
147 array_shift($comm_array);
148 $weight = trim(array_shift($comm_array));
149 $height = trim(array_shift($comm_array));
150 $systolic = trim(array_shift($comm_array));
151 $diastolic = trim(array_shift($comm_array));
152 $pulse = trim(array_shift($comm_array));
153 $temp = trim(array_shift($comm_array));
154 addVitals($weight, $height, $systolic, $diastolic, $pulse, $temp);
156 $command_count = 0;
157 if (trim($comm_array[0]) == 'camos') {
158 $command_count++;
159 //data to be submitted as separate camos forms
160 //this is for embedded prescriptions, test orders etc... usually within a soap note or something
161 //data collected here will be returned so that save.php can give it special treatment and insert
162 //into the database after the main form data is submitted so it will be in a sensible order
163 array_push($camos_return_data,
164 array("category" => trim($comm_array[1]),
165 "subcategory" => trim($comm_array[2]),
166 "item" => trim($comm_array[3]),
167 "content" => trim($comm_array[4])));
170 $string_to_process = remove_comments($string_to_process);
171 return $return_value;
173 // I was using this for debugging. touch logging, chmod 777 logging, then can use.
174 //file_put_contents('./logging',$string_to_process."\n\n*************\n\n",FILE_APPEND);//DEBUG
176 function replace($pid, $enc, $content) { //replace placeholders with values
177 $name= '';
178 $fname = '';
179 $mname = '';
180 $lname = '';
181 $dob = '';
182 $date = '';
183 $age = '';
184 $gender = '';
185 $doctorname = '';
186 $query1 = sqlStatement(
187 "select t1.fname, t1.mname, t1.lname, " .
188 "t1.sex as gender, " .
189 "t1.DOB, " .
190 "t2.date " .
191 "from patient_data as t1 join form_encounter as t2 on " .
192 "(t1.pid = t2.pid) " .
193 "where t2.pid = ".$pid." and t2.encounter = ".$enc);
194 if ($results = mysql_fetch_array($query1, MYSQL_ASSOC)) {
195 $fname = $results['fname'];
196 $mname = $results['mname'];
197 $lname = $results['lname'];
198 if ($mname) {$name = $fname.' '.$mname.' '.$lname;}
199 else {$name = $fname.' '.$lname;}
200 $dob = $results['DOB'];
201 $date = $results['date'];
202 $age = patient_age($dob, $date);
203 $gender = $results['gender'];
205 $query1 = sqlStatement("select t1.lname from users as t1 join forms as " .
206 "t2 on (t1.username like t2.user) where t2.encounter = ".$_SESSION['encounter']);
207 if ($results = mysql_fetch_array($query1, MYSQL_ASSOC)) {
208 $doctorname = "Dr. ".$results['lname'];
210 $ret = preg_replace(array("/patientname/i","/patientage/i","/patientgender/i","/doctorname/i"),
211 array($name,$age,strtolower($gender),$doctorname), $content);
212 return $ret;
214 function patient_age($birthday, $date) { //calculate age from birthdate and a given later date
215 list($birth_year,$birth_month,$birth_day) = explode("-",$birthday);
216 list($date_year,$date_month,$date_day) = explode("-",$date);
217 $year_diff = $date_year - $birth_year;
218 $month_diff = $date_month - $birth_month;
219 $day_diff = $date_day - $birth_day;
220 if ($month_diff < 0) $year_diff--;
221 elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
222 return $year_diff;