migrated ubiquitous libraries to composer autoloader (#421)
[openemr.git] / interface / main / finder / dynamic_finder_ajax.php
blob9f6026284f92f09c2b2173c811a1a067b85d6b24
1 <?php
2 // Copyright (C) 2012 Rod Roark <rod@sunsetsystems.com>
3 // Sponsored by David Eschelbacher, MD
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // Sanitize escapes and disable fake globals registration.
12 $sanitize_all_escapes = true;
13 $fake_register_globals = false;
15 require_once("../../globals.php");
16 require_once("$srcdir/formatting.inc.php");
18 $popup = empty($_REQUEST['popup']) ? 0 : 1;
20 // With the ColReorder or ColReorderWithResize plug-in, the expected column
21 // ordering may have been changed by the user. So we cannot depend on
22 // list_options to provide that.
24 $aColumns = explode(',', $_GET['sColumns']);
26 // Paging parameters. -1 means not applicable.
28 $iDisplayStart = isset($_GET['iDisplayStart' ]) ? 0 + $_GET['iDisplayStart' ] : -1;
29 $iDisplayLength = isset($_GET['iDisplayLength']) ? 0 + $_GET['iDisplayLength'] : -1;
30 $limit = '';
31 if ($iDisplayStart >= 0 && $iDisplayLength >= 0) {
32 $limit = "LIMIT " . escape_limit($iDisplayStart) . ", " . escape_limit($iDisplayLength);
35 // Column sorting parameters.
37 $orderby = '';
38 if (isset($_GET['iSortCol_0'])) {
39 for ($i = 0; $i < intval($_GET['iSortingCols']); ++$i) {
40 $iSortCol = intval($_GET["iSortCol_$i"]);
41 if ($_GET["bSortable_$iSortCol"] == "true" ) {
42 $sSortDir = escape_sort_order($_GET["sSortDir_$i"]); // ASC or DESC
43 // We are to sort on column # $iSortCol in direction $sSortDir.
44 $orderby .= $orderby ? ', ' : 'ORDER BY ';
46 if ($aColumns[$iSortCol] == 'name') {
47 $orderby .= "lname $sSortDir, fname $sSortDir, mname $sSortDir";
49 else {
50 $orderby .= "`" . escape_sql_column_name($aColumns[$iSortCol],array('patient_data')) . "` $sSortDir";
56 // Global filtering.
58 $where = '';
59 if (isset($_GET['sSearch']) && $_GET['sSearch'] !== "") {
60 $sSearch = add_escape_custom($_GET['sSearch']);
61 foreach ($aColumns as $colname) {
62 $where .= $where ? "OR " : "WHERE ( ";
63 if ($colname == 'name') {
64 $where .=
65 "lname LIKE '$sSearch%' OR " .
66 "fname LIKE '$sSearch%' OR " .
67 "mname LIKE '$sSearch%' ";
69 else {
70 $where .= "`" . escape_sql_column_name($colname,array('patient_data')) . "` LIKE '$sSearch%' ";
73 if ($where) $where .= ")";
76 // Column-specific filtering.
78 for ($i = 0; $i < count($aColumns); ++$i) {
79 $colname = $aColumns[$i];
80 if (isset($_GET["bSearchable_$i"]) && $_GET["bSearchable_$i"] == "true" && $_GET["sSearch_$i"] != '') {
81 $where .= $where ? ' AND' : 'WHERE';
82 $sSearch = add_escape_custom($_GET["sSearch_$i"]);
83 if ($colname == 'name') {
84 $where .= " ( " .
85 "lname LIKE '$sSearch%' OR " .
86 "fname LIKE '$sSearch%' OR " .
87 "mname LIKE '$sSearch%' )";
89 else {
90 $where .= " `" . escape_sql_column_name($colname,array('patient_data')) . "` LIKE '$sSearch%'";
95 // Compute list of column names for SELECT clause.
96 // Always includes pid because we need it for row identification.
98 $sellist = 'pid';
99 foreach ($aColumns as $colname) {
100 if ($colname == 'pid') continue;
101 $sellist .= ", ";
102 if ($colname == 'name') {
103 $sellist .= "lname, fname, mname";
105 else {
106 $sellist .= "`" . escape_sql_column_name($colname,array('patient_data')) . "`";
110 // Get total number of rows in the table.
112 $row = sqlQuery("SELECT COUNT(id) AS count FROM patient_data");
113 $iTotal = $row['count'];
115 // Get total number of rows in the table after filtering.
117 $row = sqlQuery("SELECT COUNT(id) AS count FROM patient_data $where");
118 $iFilteredTotal = $row['count'];
120 // Build the output data array.
122 $out = array(
123 "sEcho" => intval($_GET['sEcho']),
124 "iTotalRecords" => $iTotal,
125 "iTotalDisplayRecords" => $iFilteredTotal,
126 "aaData" => array()
128 $query = "SELECT $sellist FROM patient_data $where $orderby $limit";
129 $res = sqlStatement($query);
130 while ($row = sqlFetchArray($res)) {
131 // Each <tr> will have an ID identifying the patient.
132 $arow = array('DT_RowId' => 'pid_' . $row['pid']);
133 foreach ($aColumns as $colname) {
134 if ($colname == 'name') {
135 $name = $row['lname'];
136 if ($name && $row['fname']) $name .= ', ';
137 if ($row['fname']) $name .= $row['fname'];
138 if ($row['mname']) $name .= ' ' . $row['mname'];
139 $arow[] = $name;
141 else if ($colname == 'DOB' || $colname == 'regdate' || $colname == 'ad_reviewed' || $colname == 'userdate1') {
142 $arow[] = oeFormatShortDate($row[$colname]);
144 else {
145 $arow[] = $row[$colname];
148 $out['aaData'][] = $arow;
151 // error_log($query); // debugging
153 // Dump the output array as JSON.
155 echo json_encode($out);