2 // Copyright (C) 2012 Rod Roark <rod@sunsetsystems.com>
3 // Sponsored by David Eschelbacher, MD
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 require_once("../../globals.php");
12 $popup = empty($_REQUEST['popup']) ?
0 : 1;
14 // With the ColReorder or ColReorderWithResize plug-in, the expected column
15 // ordering may have been changed by the user. So we cannot depend on
16 // list_options to provide that.
18 $aColumns = explode(',', $_GET['sColumns']);
20 // Paging parameters. -1 means not applicable.
22 $iDisplayStart = isset($_GET['iDisplayStart' ]) ?
0 +
$_GET['iDisplayStart' ] : -1;
23 $iDisplayLength = isset($_GET['iDisplayLength']) ?
0 +
$_GET['iDisplayLength'] : -1;
25 if ($iDisplayStart >= 0 && $iDisplayLength >= 0) {
26 $limit = "LIMIT " . escape_limit($iDisplayStart) . ", " . escape_limit($iDisplayLength);
29 // Column sorting parameters.
32 if (isset($_GET['iSortCol_0'])) {
33 for ($i = 0; $i < intval($_GET['iSortingCols']); ++
$i) {
34 $iSortCol = intval($_GET["iSortCol_$i"]);
35 if ($_GET["bSortable_$iSortCol"] == "true") {
36 $sSortDir = escape_sort_order($_GET["sSortDir_$i"]); // ASC or DESC
37 // We are to sort on column # $iSortCol in direction $sSortDir.
38 $orderby .= $orderby ?
', ' : 'ORDER BY ';
40 if ($aColumns[$iSortCol] == 'name') {
41 $orderby .= "lname $sSortDir, fname $sSortDir, mname $sSortDir";
43 $orderby .= "`" . escape_sql_column_name($aColumns[$iSortCol], array('patient_data')) . "` $sSortDir";
52 if (isset($_GET['sSearch']) && $_GET['sSearch'] !== "") {
53 $sSearch = add_escape_custom($_GET['sSearch']);
54 foreach ($aColumns as $colname) {
55 $where .= $where ?
"OR " : "WHERE ( ";
56 if ($colname == 'name') {
58 "lname LIKE '$sSearch%' OR " .
59 "fname LIKE '$sSearch%' OR " .
60 "mname LIKE '$sSearch%' ";
62 $where .= "`" . escape_sql_column_name($colname, array('patient_data')) . "` LIKE '$sSearch%' ";
71 // Column-specific filtering.
73 for ($i = 0; $i < count($aColumns); ++
$i) {
74 $colname = $aColumns[$i];
75 if (isset($_GET["bSearchable_$i"]) && $_GET["bSearchable_$i"] == "true" && $_GET["sSearch_$i"] != '') {
76 $where .= $where ?
' AND' : 'WHERE';
77 $sSearch = add_escape_custom($_GET["sSearch_$i"]);
78 if ($colname == 'name') {
80 "lname LIKE '$sSearch%' OR " .
81 "fname LIKE '$sSearch%' OR " .
82 "mname LIKE '$sSearch%' )";
84 $where .= " `" . escape_sql_column_name($colname, array('patient_data')) . "` LIKE '$sSearch%'";
89 // Compute list of column names for SELECT clause.
90 // Always includes pid because we need it for row identification.
93 foreach ($aColumns as $colname) {
94 if ($colname == 'pid') {
99 if ($colname == 'name') {
100 $sellist .= "lname, fname, mname";
102 $sellist .= "`" . escape_sql_column_name($colname, array('patient_data')) . "`";
106 // Get total number of rows in the table.
108 $row = sqlQuery("SELECT COUNT(id) AS count FROM patient_data");
109 $iTotal = $row['count'];
111 // Get total number of rows in the table after filtering.
113 $row = sqlQuery("SELECT COUNT(id) AS count FROM patient_data $where");
114 $iFilteredTotal = $row['count'];
116 // Build the output data array.
119 "sEcho" => intval($_GET['sEcho']),
120 "iTotalRecords" => $iTotal,
121 "iTotalDisplayRecords" => $iFilteredTotal,
124 $query = "SELECT $sellist FROM patient_data $where $orderby $limit";
125 $res = sqlStatement($query);
126 while ($row = sqlFetchArray($res)) {
127 // Each <tr> will have an ID identifying the patient.
128 $arow = array('DT_RowId' => 'pid_' . $row['pid']);
129 foreach ($aColumns as $colname) {
130 if ($colname == 'name') {
131 $name = $row['lname'];
132 if ($name && $row['fname']) {
137 $name .= $row['fname'];
141 $name .= ' ' . $row['mname'];
145 } else if ($colname == 'DOB' ||
$colname == 'regdate' ||
$colname == 'ad_reviewed' ||
$colname == 'userdate1') {
146 $arow[] = oeFormatShortDate($row[$colname]);
148 $arow[] = $row[$colname];
152 $out['aaData'][] = $arow;
155 // error_log($query); // debugging
157 // Dump the output array as JSON.
159 echo json_encode($out);