ebe0f6f2957d23a8af3f46ab5a137ddd270241a2
[openemr.git] / interface / main / finder / dynamic_finder_ajax.php
blobebe0f6f2957d23a8af3f46ab5a137ddd270241a2
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/formdata.inc.php");
17 require_once("$srcdir/formatting.inc.php");
19 $popup = empty($_REQUEST['popup']) ? 0 : 1;
21 // With the ColReorder or ColReorderWithResize plug-in, the expected column
22 // ordering may have been changed by the user. So we cannot depend on
23 // list_options to provide that.
25 $aColumns = explode(',', $_GET['sColumns']);
27 // Paging parameters. -1 means not applicable.
29 $iDisplayStart = isset($_GET['iDisplayStart' ]) ? 0 + $_GET['iDisplayStart' ] : -1;
30 $iDisplayLength = isset($_GET['iDisplayLength']) ? 0 + $_GET['iDisplayLength'] : -1;
31 $limit = '';
32 if ($iDisplayStart >= 0 && $iDisplayLength >= 0) {
33 $limit = "LIMIT $iDisplayStart, $iDisplayLength";
36 // Column sorting parameters.
38 $orderby = '';
39 if (isset($_GET['iSortCol_0'])) {
40 for ($i = 0; $i < intval($_GET['iSortingCols']); ++$i) {
41 $iSortCol = intval($_GET["iSortCol_$i"]);
42 if ($_GET["bSortable_$iSortCol"] == "true" ) {
43 $sSortDir = add_escape_custom($_GET["sSortDir_$i"]); // ASC or DESC
44 // We are to sort on column # $iSortCol in direction $sSortDir.
45 $orderby .= $orderby ? ', ' : 'ORDER BY ';
47 if ($aColumns[$iSortCol] == 'name') {
48 $orderby .= "lname $sSortDir, fname $sSortDir, mname $sSortDir";
50 else {
51 $orderby .= "`" . add_escape_custom($aColumns[$iSortCol]) . "` $sSortDir";
57 // Global filtering.
59 $where = '';
60 if (isset($_GET['sSearch']) && $_GET['sSearch'] !== "") {
61 $sSearch = add_escape_custom($_GET['sSearch']);
62 foreach ($aColumns as $colname) {
63 $where .= $where ? "OR " : "WHERE ( ";
64 if ($colname == 'name') {
65 $where .=
66 "lname LIKE '$sSearch%' OR " .
67 "fname LIKE '$sSearch%' OR " .
68 "mname LIKE '$sSearch%' ";
70 else {
71 $where .= "`" . add_escape_custom($colname) . "` LIKE '$sSearch%' ";
74 if ($where) $where .= ")";
77 // Column-specific filtering.
79 for ($i = 0; $i < count($aColumns); ++$i) {
80 $colname = $aColumns[$i];
81 if (isset($_GET["bSearchable_$i"]) && $_GET["bSearchable_$i"] == "true" && $_GET["sSearch_$i"] != '') {
82 $where .= $where ? ' AND' : 'WHERE';
83 $sSearch = add_escape_custom($_GET["sSearch_$i"]);
84 if ($colname == 'name') {
85 $where .= " ( " .
86 "lname LIKE '$sSearch%' OR " .
87 "fname LIKE '$sSearch%' OR " .
88 "mname LIKE '$sSearch%' )";
90 else {
91 $where .= " `" . add_escape_custom($colname) . "` LIKE '$sSearch%'";
96 // Compute list of column names for SELECT clause.
97 // Always includes pid because we need it for row identification.
99 $sellist = 'pid';
100 foreach ($aColumns as $colname) {
101 if ($colname == 'pid') continue;
102 $sellist .= ", ";
103 if ($colname == 'name') {
104 $sellist .= "lname, fname, mname";
106 else {
107 $sellist .= "`" . add_escape_custom($colname) . "`";
111 // Get total number of rows in the table.
113 $row = sqlQuery("SELECT COUNT(id) AS count FROM patient_data");
114 $iTotal = $row['count'];
116 // Get total number of rows in the table after filtering.
118 $row = sqlQuery("SELECT COUNT(id) AS count FROM patient_data $where");
119 $iFilteredTotal = $row['count'];
121 // Build the output data array.
123 $out = array(
124 "sEcho" => intval($_GET['sEcho']),
125 "iTotalRecords" => $iTotal,
126 "iTotalDisplayRecords" => $iFilteredTotal,
127 "aaData" => array()
129 $query = "SELECT $sellist FROM patient_data $where $orderby $limit";
130 $res = sqlStatement($query);
131 while ($row = sqlFetchArray($res)) {
132 // Each <tr> will have an ID identifying the patient.
133 $arow = array('DT_RowId' => 'pid_' . $row['pid']);
134 foreach ($aColumns as $colname) {
135 if ($colname == 'name') {
136 $name = $row['lname'];
137 if ($name && $row['fname']) $name .= ', ';
138 if ($row['fname']) $name .= $row['fname'];
139 if ($row['mname']) $name .= ' ' . $row['mname'];
140 $arow[] = $name;
142 else if ($colname == 'DOB' || $colname == 'regdate' || $colname == 'ad_reviewed' || $colname == 'userdate1') {
143 $arow[] = oeFormatShortDate($row[$colname]);
145 else {
146 $arow[] = $row[$colname];
149 $out['aaData'][] = $arow;
152 // error_log($query); // debugging
154 // Dump the output array as JSON.
156 echo json_encode($out);