-
Notifications
You must be signed in to change notification settings - Fork 0
/
advance_search_result.php
295 lines (269 loc) · 13.1 KB
/
advance_search_result.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
include('db.php');
$operatorMap = array("eq"=>"=", "ne"=>"<>", "lt"=>"<", "lte"=>"<=", "gt"=>">", "gte"=>">=");
$keyTypes = array("AssayType"=>"C","Country"=>"C","Instrument"=>"C","Year"=>"Y","IsolationSource"=>"C","Disease"=>"C");
$dbAttributeNameMap = array(
"AssayType"=>"run.AssayType","Country"=>"run.Country","Instrument"=>"run.Instrument",
"Year"=>"run.ReleaseYear","IsolationSource"=>"run.IsolationSource","Disease"=>"disease.Grp"
);
$possibleValues = array(
"AssayType"=> array("0"=>"Amplicon", "1"=>"WMS"),
"Country"=> array(
'0'=> 'Australia', '1'=> 'Bangladesh', '2'=> 'Belgium', '3'=> 'Brazil',
'4'=> 'China', '5'=> 'Czech Republic', '6'=> 'Germany', '7'=> 'Hungary',
'8'=> 'India', '9'=> 'Italy', '10'=> 'Japan', '11'=> 'Mali', '12'=> 'Morocco',
'13'=> 'Nepal', '14'=> 'Netherlands', '15'=> 'Peru', '16'=> 'Poland',
'17'=> 'Russia', '18'=> 'South Africa', '19'=> 'South Korea', '20'=> 'Spain',
'21'=> 'Srilanka', '22'=> 'Switzerland', '23'=> 'Taiwan', '24'=> 'United Kingdom',
'25'=> 'USA'
),
"Instrument"=> array(
"0"=>"HiSeq 2000", "1"=>"HiSeq 2500", "2"=>"HiSeq 4000", "3"=>"MiSeq",
"4"=>"NovaSeq 6000", "5"=>"NextSeq 500","6"=>"NextSeq 550"
),
"IsolationSource"=> array(
'0'=> 'BAL', '1'=> 'Bronchial Brush', '2'=> 'Bronchial Mucosa', '3'=> 'Colon Mucus',
'4'=> 'Endotracheal Aspirate', '5'=> 'Lung Biopsy', '6'=> 'Lung Tissue',
'7'=> 'Lung Tumour Tissue', '8'=> 'Sputum', '9'=> 'Stool', '10'=> 'Supraglottic Swab'
),
"Disease"=> array(
"0"=>"Asthma", "1"=>"COPD", "2"=>"COVID-19", "3"=>"Cystic Fibrosis", "4"=>"Lung cancer",
"5"=>"Pneumonia", "6"=>"Tuberculosis"
)
);
function assertLogicalOperator($lop) {
return ($lop === "OR" || $lop === "AND") ? true : false;
}
function assertKey($key) {
global $keyTypes;
return array_key_exists($key, $keyTypes);
}
function assertOperator($op, $key) {
global $keyTypes;
if($keyTypes[$key] === "C")
return ($op === "eq" || $op === "ne") ? true : false;
if($keyTypes[$key] === "S")
return ($op === "eq" || $op === "ne") ? true : false;
if($keyTypes[$key] === "N" || $keyTypes[$key] === "Y")
return ($op === "eq" || $op === "ne" || $op === "lt" || $op === "lte" || $op === "gt" || $op === "gte") ? true : false;
}
function assertValue($val, $key) {
global $possibleValues;
global $keyTypes;
if($keyTypes[$key] === "N")
return is_numeric($val);
if($keyTypes[$key] === "Y")
return is_numeric($val) && (intval($val) >= 1900) && (intval($val) <= date("Y"));
if($keyTypes[$key] === "C")
return array_key_exists($val, $possibleValues[$key]);
if($keyTypes[$key] === "S")
return true;
}
function validate($predicateCount, $data, &$logicalOperators, &$keys, &$operators, &$values) {
global $keyTypes;
global $possibleValues;
for($i=0; $i<$predicateCount; ++$i) {
$pass = false;
$k = $_POST["k".strval($i)];
$op = $_POST["op".strval($i)];
$val = $_POST["v".strval($i)];
// echo $lo.",".assertLogicalOperator($lo)."<br/>";
// echo $k.",".assertKey($k)."<br/>";
// echo $op.",".assertOperator($op,$k)."<br/>";
// echo $val.",".assertValue($val, $k)."<br/>";
if(assertKey($k) && assertOperator($op, $k) && assertValue($val, $k)) {
array_push($keys, $k);
array_push($operators, $op);
if ($keyTypes[$k] === "C")
array_push($values, $possibleValues[$k][$val]);
elseif ($keyTypes[$k] === "N" || $keyTypes[$k] === "Y")
array_push($values, intval($val));
else
array_push($values, $val);
if($i > 0) {
$lo = $_POST["lo".strval($i)];
if(assertLogicalOperator($lo)) {
array_push($logicalOperators, $lo);
} else {
return false;
}
}
} else {
return false;
}
}
return true;
}
function createPredicateString($keys, $operators, $logicalOperators) {
global $operatorMap;
global $dbAttributeNameMap;
$pred = "(".$dbAttributeNameMap[$keys[0]].$operatorMap[$operators[0]]."?)";
for ($i=1; $i<count($keys); ++$i) {
$currPred = "(".$dbAttributeNameMap[$keys[$i]].$operatorMap[$operators[$i]]."?)";
$pred = "(".$pred.$logicalOperators[$i-1].$currPred.")";
}
return $pred;
}
function createParamString($keys) {
global $keyTypes;
$typeString = "";
foreach($keys as $k) {
if($keyTypes[$k] === "S")
$typeString = $typeString."s";
elseif($keyTypes[$k] === "C")
$typeString = $typeString."s";
elseif($keyTypes[$k] === "N" || $keyTypes[$k] === "Y")
$typeString = $typeString."i";
// elseif($keyTypes[$k] === "D")
// $typeString = $typeString."s";
// elseif($keyTypes[$k] === "F")
// $typeString = $typeString."d";
}
return $typeString;
}
function refValues($arr){
$refs = array();
for($i=0; $i<count($arr); ++$i)
$refs[$i] = &$arr[$i];
return $refs;
}
$predicateCount = $_POST["total_count"];
$logicalOperators = array();
$keys = array();
$operators = array();
$values = array();
if (validate($predicateCount, $_POST, $logicalOperators, $keys, $operators, $values) == false) {
echo "Validation failed !!!<br/>";
////TODO Redirect to error page
}
// echo implode(",", $logicalOperators)."<br/>".implode(",", $keys)."<br/>".implode(",", $operators)."<br/>".implode(",", $values)."<br/>";
$pred = createPredicateString($keys, $operators, $logicalOperators);
$paramString = createParamString($keys);
// $query = "select ".implode(",", $viewAttributes)." from run,bioproject where (run.BioProject=bioproject.BioProject)AND".$pred.";";
$query = "select ".implode(",", $viewAttributes)." from ((run inner join bioproject on run.BioProject=bioproject.BioProject) inner join disease on run.SubGroup=disease.SubGroup) where ".$pred." order by run.Run;";
// echo $pred."<br/>";
// echo $paramString."<br/>";
// echo $query."<br/>";
$conn = connect();
$stmt = $conn->prepare($query);
array_unshift($values, $paramString);
call_user_func_array(
array($stmt, "bind_param"),
refValues($values)
);
$stmt->execute();
// $stmt->bind_param($paramString, ...$values);
// $stmt->execute();
// $result = $stmt->get_result();
// $rows = $result->fetch_all(MYSQLI_ASSOC);
$rows = execute_and_fetch_assoc($stmt);
// foreach($rows as $row) {
// echo implode(",", $row)."<br/>";
// }
$rowsJSON = json_encode($rows);
// echo $result->num_rows." ".$result->field_count."<br/>";
$stmt->close();
closeConnection($conn);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home - MDPD</title>
<link rel = "stylesheet" type = "text/css" href = "css/main.css" />
<script type = "text/javascript" src = "js/advance_search_result.js"></script>
<script>
var dataJSON = '<?php echo $rowsJSON; ?>';
initializeData(dataJSON);
</script>
</head>
<body>
<div class = "section_header">
<center><p class="title">MDPD - Microbiome Database of Pulmonary Diseases</p></center>
</div>
<div class = "section_menu">
<center>
<table cellpadding="3px">
<tr class="nav">
<td class="nav"><a href="index.php" class="side_nav">Home</a></td>
<td class="nav"><a href="browse.php" class="side_nav">Browse</a></td>
<td class="nav"><a href="statistics.php" class="side_nav">Statistics</a></td>
<td class="nav"><a href="about.php" class="side_nav">About</a></td>
<td class="nav"><a href="help.html" class="side_nav">Help</a></td>
<td class="nav"><a href="team.html" class="side_nav">Team</a></td>
</tr>
</table>
</center>
</div>
<!--<div class = "section_left"></div>-->
<div class = "section_middle">
<?php
if (count($rows) < 1)
echo "<br/><center>No entries found in the database for the given query.</center>";
else {
?>
<div id="download_div" style="width:100%; text-align:center; margin-top:20px;">
<a id="download_button" onclick="createDownloadLink()" download="search_result.csv">
<button type="button" style="margin:2px;">Download table</button>
</a>
</div>
<p>Total number of entries found in the database = <?php echo count($rows);?></p>
<table border="0" style="width:100%; border:4px solid #392d37;">
<tr>
<td style="width:25%;">
Go to Page:
<input id="page_no_top" type="number" size="2" min="1" style="width:50px;" />
<button type="button" class="round" onclick="goto_page('result_display', 'page_no_top')" />Go</button>
</td>
<td style="width:15%;text-align:right;">
<button type="button" class="round" onclick="displayFirstPage('result_display')">First</button>
<button type="button" class="round" onclick="displayPrevPage('result_display')">Prev</button>
</td>
<td style="width:20%;">
<p id="pages_top" style="text-align:center;"></p>
</td>
<td style="width:15%;">
<button type="button" class="round" onclick="displayNextPage('result_display')">Next</button>
<button type="button" class="round" onclick="displayLastPage('result_display')">Last</button>
</td>
<td style="width:25%;"></td>
</tr>
</table>
<div id="result_display">
</div>
<table border="0" style="width:100%; border:4px solid #392d37;">
<tr>
<td style="width:25%;">
Go to Page:
<input id="page_no_bottom" type="number" size="2" min="1" style="width:50px;" />
<button type="button" class="round" onclick="goto_page('result_display', 'page_no_bottom')" />Go</button>
</td>
<td style="width:15%;text-align:right;">
<button type="button" class="round" onclick="displayFirstPage('result_display')">First</button>
<button type="button" class="round" onclick="displayPrevPage('result_display')">Prev</button>
</td>
<td style="width:20%;">
<p id="pages_bottom" style="text-align:center;"></p>
</td>
<td style="width:15%;">
<button type="button" class="round" onclick="displayNextPage('result_display')">Next</button>
<button type="button" class="round" onclick="displayLastPage('result_display')">Last</button>
</td>
<td style="width:25%;"></td>
</tr>
</table>
<script>
displayFirstPage('result_display');
</script>
<?php
}
?>
<br/><hr/>
<p style="font-size:0.9em;text-align:center;">
© 2023 Bose Institute. All rights reserved. For queries, please contact Dr. Sudipto Saha
(<a style="color:#003325;" href="mailto:[email protected]">[email protected]</a>,
<a style="color:#003325;" href="mailto:[email protected]">[email protected]</a>).
</p>
</div>
</body>
</html>