Support for text insert embedded function, named 'replace' added.
[openemr.git] / interface / forms / CAMOS / content_parser.php
blobc4f7b21d3f73202a89ea9b6daf2d483be9032cf3
1 <?
2 include_once("../../globals.php");
3 include_once("../../../library/sql.inc");
5 //This function was copied from billing.inc and altered to support 'justify'
6 function addBilling2($encounter, $code_type, $code, $code_text, $modifier="",$units="",$fee="0.00",$justify)
8 $justify_string = '';
9 if ($justify)
11 $justify_string = implode(":",$justify).":";
13 $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')";
15 return sqlInsert($sql);
19 function content_parser($input) {
21 // parse content field
22 $content = $input;
24 // comments should really be removed in save.php
25 // $content = remove_comments($content);
27 //reduce more than two empty lines to no more than two.
28 $content = preg_replace("/([^\n]\r[^\n]){2,}/","\r\r",$content);
29 $content = preg_replace("/([^\r]\n[^\r]){2,}/","\n\n",$content);
30 $content = preg_replace("/(\r\n){2,}/","\r\n\r\n",$content);
33 return $content;
36 // implement C style comments ie remove anything between /* and */
37 function remove_comments($string_to_process) {
38 return preg_replace("/\/\*.*?\*\//","",$string_to_process);
40 // This function is useless for now, don't use it
41 function remove_dangling_comments($string_to_process) {
42 return preg_replace("/(\/\*)|(\*\/)/","",$string_to_process);
45 //process commands embedded in C style comments where function name is first
46 //followed by args separated by :: delimiter and nothing else
48 function process_commands(&$string_to_process, &$camos_return_data) {
50 //First, handle replace function as special case. full depth of inserts should be evaluated prior
51 //to evaluating other functions in final string assembly.
52 $replace_finished = FALSE;
53 while (!$replace_finished) {
54 if (preg_match_all("/\/\*\s*replace\s*::.*?\*\//",$string_to_process, $matches)) {
55 foreach($matches[0] as $val) {
56 $comm = preg_replace("/(\/\*)|(\*\/)/","",$val);
57 $comm_array = split('::', $comm); //array where first element is command and rest are args
58 $replacement_item = trim($comm_array[1]); //this is the item name to search for in the database. easy.
59 $replacement_text = '';
60 $query = "SELECT content FROM form_CAMOS_item WHERE item like '".$replacement_item."'";
61 $statement = sqlStatement($query);
62 if ($result = sqlFetchArray($statement)) {$replacement_text = $result['content'];}
63 $string_to_process = str_replace($val,$replacement_text,$string_to_process);
66 else {$replace_finished = TRUE;}
70 //end of special case of replace function
71 $return_value = 0;
72 $camos_return_data = array(); // to be filled with additional camos form submissions if any embedded
73 $command_array = array(); //to be filled with potential commands
74 $matches= array(); //to be filled with potential commands
75 if (!preg_match_all("/\/\*.*?\*\//",$string_to_process, $matches)) {return $return_value;}
76 $command_array = $matches[0];
77 foreach($command_array as $val) {
78 //process each command
79 $comm = preg_replace("/(\/\*)|(\*\/)/","",$val);
80 $comm_array = split('::', $comm); //array where first element is command and rest are args
81 //Here is where we process particular commands
82 if (trim($comm_array[0])== 'billing') {
83 array_shift($comm_array); //couldn't do it in 'if' or would lose element 0 for next if
84 //insert data into the billing table, see, easy!
85 $type = trim(array_shift($comm_array));
86 $code = trim(array_shift($comm_array));
87 $text = trim(array_shift($comm_array));
88 $modifier = trim(array_shift($comm_array));
89 $units = trim(array_shift($comm_array));
90 $fee = sprintf("%01.2f",trim(array_shift($comm_array)));
91 //in function call 'addBilling' note last param is the remainder of the array. we will look for justifications here...
92 addBilling2($encounter, $type, $code, $text, $modifier,$units,$fee,$comm_array);
94 if (trim($comm_array[0]) == 'camos') {
95 $command_count++;
96 //data to be submitted as separate camos forms
97 //this is for embedded prescriptions, test orders etc... usually within a soap note or something
98 //data collected here will be returned so that save.php can give it special treatment and insert
99 //into the database after the main form data is submitted so it will be in a sensible order
100 array_push($camos_return_data,
101 array("category" => trim($comm_array[1]),
102 "subcategory" => trim($comm_array[2]),
103 "item" => trim($comm_array[3]),
104 "content" => trim($comm_array[4])));
107 $string_to_process = remove_comments($string_to_process);
108 return $return_value;