New version of save.php for submitting via ajax without leaving form page
[openemr.git] / interface / forms / CAMOS / content_parser.php
blob12cb98fc1353702e3f0a468670032a577b24290e
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 $authorized = 1;
14 $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')";
16 return sqlInsert($sql);
20 function content_parser($input) {
22 // parse content field
23 $content = $input;
25 // comments should really be removed in save.php
26 // $content = remove_comments($content);
28 //reduce more than two empty lines to no more than two.
29 $content = preg_replace("/([^\n]\r[^\n]){2,}/","\r\r",$content);
30 $content = preg_replace("/([^\r]\n[^\r]){2,}/","\n\n",$content);
31 $content = preg_replace("/(\r\n){2,}/","\r\n\r\n",$content);
34 return $content;
37 // implement C style comments ie remove anything between /* and */
38 function remove_comments($string_to_process) {
39 return preg_replace("/\/\*.*?\*\//","",$string_to_process);
41 // This function is useless for now, don't use it
42 function remove_dangling_comments($string_to_process) {
43 return preg_replace("/(\/\*)|(\*\/)/","",$string_to_process);
46 //process commands embedded in C style comments where function name is first
47 //followed by args separated by :: delimiter and nothing else
49 function process_commands(&$string_to_process, &$camos_return_data) {
51 //First, handle replace function as special case. full depth of inserts should be evaluated prior
52 //to evaluating other functions in final string assembly.
53 $replace_finished = FALSE;
54 while (!$replace_finished) {
55 if (preg_match_all("/\/\*\s*replace\s*::.*?\*\//",$string_to_process, $matches)) {
56 foreach($matches[0] as $val) {
57 $comm = preg_replace("/(\/\*)|(\*\/)/","",$val);
58 $comm_array = split('::', $comm); //array where first element is command and rest are args
59 $replacement_item = trim($comm_array[1]); //this is the item name to search for in the database. easy.
60 $replacement_text = '';
61 $query = "SELECT content FROM form_CAMOS_item WHERE item like '".$replacement_item."'";
62 $statement = sqlStatement($query);
63 if ($result = sqlFetchArray($statement)) {$replacement_text = $result['content'];}
64 $string_to_process = str_replace($val,$replacement_text,$string_to_process);
67 else {$replace_finished = TRUE;}
71 //end of special case of replace function
72 $return_value = 0;
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;
112 function clean_multibox_array (&$multibox_array, &$camos_array) {
113 foreach($multibox_array as $key => $var) {
114 $head = '';
115 $body = '';
116 $tail = '';
117 $all = '';
118 if (preg_match('/^(\/\* camos :: .*? :: .*? :: .*? :: )(.*?)(\*\/)$/s', $var, $matches) > 0) {
119 $head = $matches[1];
120 $body = $matches[2];
121 $tail = $matches[3];
122 process_commands($body, $camos_array);
124 $all = $head.$body.$tail;
125 if (preg_match('/^\/\* camos :: (.*?) :: (.*?) :: (.*?) :: (.*?) \*\/$/s',
126 $all, $matches)) {
127 array_push($camos_array, array('category' => $matches[1], 'subcategory' => $matches[2],
128 'item' => $matches[3], 'content' => $matches[4]));
132 function create_multibox_array (&$string_to_parse, &$multibox_array) {
133 if (preg_match_all('/\/\*\[begin.+?\]\*\/\s*(\/\* camos.+?)\s*\/\*\[end.+?\]/s',
134 $string_to_parse, $matches,PREG_SET_ORDER)) {
135 foreach($matches as $match) {
136 array_push($multibox_array, $match[1]);
140 function remove_multibox_data (&$string_to_parse) {
141 $string_to_parse = preg_replace('/\/\*\[begin.+?\].*?\[end.+?\]\*\//s', '', $string_to_parse);