Code type module improvements:
[openemr.git] / library / api.inc
blobc4b14cf7173c57d1cc2ab1506c1f798aa3e753e3
1 <?php
2 //our api for 3rd party developers
3 include_once("../../globals.php");
4 include_once("{$GLOBALS['srcdir']}/sql.inc");
5 include_once("{$GLOBALS['srcdir']}/billing.inc");
6 include_once("{$GLOBALS['srcdir']}/formdata.inc.php");
8 $GLOBALS['form_exit_url'] = $GLOBALS['concurrent_layout'] ?
9         "$rootdir/patient_file/encounter/encounter_top.php" :
10         "$rootdir/patient_file/encounter/patient_encounter.php";
12 function formHeader ($title = "My Form")
14         ?>
15         <html>
16         <head>
17 <?php html_header_show();?>
18         <link rel=stylesheet href="<?php echo $GLOBALS['css_header']?>" type="text/css">
19         <title><?php echo $title?></title>
20         </head>
21         <body background="<?php echo $GLOBALS['backpic']?>" topmargin=0 rightmargin=0 leftmargin=2 bottommargin=0 marginwidth=2 marginheight=0>
22         <?php
25 function formFooter ()
27         ?>
28         </body>
29         </html>
30         <?php
33 // This function will escape the $values when using the new security method (ie. $sanitize_all_escapes is TRUE).
34 //   Otherwise, this function expects the $values to already be escaped(original and legacy behavior).
35 function formSubmit ($tableName, $values, $id, $authorized = "0")
37         // Bring in $sanitize_all_escapes variable, which will decide
38         // the variable escaping method.
39         global $sanitize_all_escapes;
41         $sql = "insert into $tableName set pid = {$_SESSION['pid']},groupname='".$_SESSION['authProvider']."',user='".$_SESSION['authUser']."',authorized=$authorized,activity=1, date = NOW(),";
42         foreach ($values as $key => $value)
43                 if (strpos($key,"openemr_net_cpt") === 0) {
44                         //code to auto add cpt code
45                         if (!empty($value)) {
46                                 $code_array = split(" ",$value,2);
47                                 
48                                 addBilling(date("Ymd"), 'CPT4', $code_array[0], $code_array[1], $_SESSION['pid'], $authorized, $_SESSION['authUserID']);
49                         }
50                                         
51                 }
52                 //case where key looks like "[a-zA-Z]*diagnosis[0-9]" which is special, it is used to auto add ICD codes
53                 
54                 elseif (strpos($key,"diagnosis") == (strlen($key) -10) && !(strpos($key,"diagnosis")=== false )) {
55                         //icd auto add ICD9-CM
56                         if (!empty($value)) {
57                                 $code_array = split(" ",$value,2);
58                                 addBilling(date("Ymd"), 'ICD9-M', $code_array[0], $code_array[1], $_SESSION['pid'], $authorized, $_SESSION['authUserID']);
59                         }
60                 }
61                 else {
62                         if (isset($sanitize_all_escapes) && $sanitize_all_escapes) {
63                                 // using new security method, so escape the key and values here
64                                 $sql .= " " . add_escape_custom($key) . " = '" . add_escape_custom($value) . "',";
65                         }
66                         else {
67                                 // original method (rely on code to escape values before using this function)
68                                 $sql .= " $key = '$value',";
69                         }
70                 }
71         $sql = substr($sql, 0, -1);
72         return sqlInsert($sql);
76 function formUpdate ($tableName, $values, $id, $authorized = "0")
78         $sql = "update $tableName set pid = {$_SESSION['pid']},groupname='".$_SESSION['authProvider']."',user='".$_SESSION['authUser']."',authorized=$authorized,activity=1, date = NOW(),";
79         foreach ($values as $key => $value)
80                 $sql .= " $key = '$value',";
81         $sql = substr($sql, 0, -1);
82         $sql .= " where id=$id";
83         
84         return sqlInsert($sql);
88 function formJump ($address = "0")
90         $returnurl = $GLOBALS['concurrent_layout'] ? 'encounter_top.php' : 'patient_encounter.php';
91         if ($address == "0")
92                 $address = "{$GLOBALS['rootdir']}/patient_file/encounter/$returnurl";
93         echo "\n<script language='Javascript'>top.restoreSession();window.location='$address';</script>\n";
94         exit;
97 function formFetch ($tableName, $id, $cols="*", $activity="1")
99         return sqlQuery ( "select $cols from `$tableName` where id='$id' and pid = '{$GLOBALS['pid']}' and activity like '$activity' order by date DESC LIMIT 0,1" ) ;
102 function formGetIds ($tableName, $cols = "*", $limit='all', $start=0, $activity = "1")
104         if($limit == "all")
105         {
107                 $sql = "select $cols from `$tableName` where pid like '$pid' ";
108                 if ($activity != "all")
109                         $sql .= "and activity like '$activity' ";
110                 $sql .= "order by date DESC";
111         }
112         else
113         {
114                 $sql = "select $cols from pnotes where pid like '$pid' ";
115                 $sql .= " AND deleted != 1 "; // exclude ALL deleted notes
116                 if ($activity != "all")
117                         $sql .= "and activity like '$activity' ";
118                 $sql .= "order by date DESC LIMIT $start, $limit";
119         }
121         $res = sqlStatement($sql);
122         
123         for ($iter = 0;$row = sqlFetchArray($res);$iter++)
124                 $all[$iter] = $row;
125         return $all;
128 function formDisappear ($tableName, $id)
130         if (sqlStatement("update `$tableName` set activity = '0' where id='$id' and pid='$pid'")) return true;
131         return false;
134 function formReappear ($tableName, $id)
136         if (sqlStatement("update `$tableName` set activity = '1' where id='$id' and pid='$pid'")) return true;
137         return false;