/
search.php
420 lines (358 loc) · 12.7 KB
/
search.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
// $Id: search.php 2784 2013-11-21 10:48:22Z cimorrison $
require "defaultincludes.inc";
function generate_search_nav_html($search_pos, $total, $num_records, $search_str)
{
global $day, $month, $year;
global $search;
$html = '';
$has_prev = $search_pos > 0;
$has_next = $search_pos < ($total-$search["count"]);
if ($has_prev || $has_next)
{
$html .= "<div id=\"record_numbers\">\n";
$html .= get_vocab("records") . ($search_pos+1) . get_vocab("through") . ($search_pos+$num_records) . get_vocab("of") . $total;
$html .= "</div>\n";
$html .= "<div id=\"record_nav\">\n";
$base_query_string = "search_str=" . urlencode($search_str) . "&" .
"total=$total&" .
"from_year=$year&" .
"from_month=$month&" .
"from_day=$day";
// display a "Previous" button if necessary
if($has_prev)
{
$query_string = $base_query_string . "&search_pos=" . max(0, $search_pos-$search["count"]);
$html .= "<a href=\"search.php?$query_string\">";
}
$html .= get_vocab("previous");
if ($has_prev)
{
$html .= "</a>";
}
// add a separator for Next and Previous
$html .= (" | ");
// display a "Previous" button if necessary
if ($has_next)
{
$query_string = $base_query_string . "&search_pos=" . max(0, $search_pos+$search["count"]);
$html .= "<a href=\"search.php?$query_string\">";
}
$html .= get_vocab("next");
if ($has_next)
{
$html .= "</a>";
}
$html .= "</div>\n";
}
return $html;
}
function getRoomName($room_id){
$room_name = "";
$conn = new mysqli("localhost","mrbs","mrbs-password",'mrbs');
if($conn->connect_error){
die("connection failed.<>".$conn->connect_error);
}
// echo "connected :) ";
$query = "select room_name from mrbs_room where id = $room_id";
// print $query;
if($conn->query($query)==True){
// echo "here1";
$res = $conn->query($query);
// print ($res[1]);
foreach ($res as $room) {
// print ($room["room_name"]);
$room_name = $room["room_name"];
// print $room_name;
}
// print "room found";
// print $room_name;
return $room_name;
}
else{
return "Error in room_id: ".$room_id[0];
}
// return $room_name;
}
// print getRoomName(4);
function output_row($row)
{
global $ajax, $json_data;
// print_r($row);
$values = array();
// booking name
$html_name = htmlspecialchars($row['name']);
$values[] = "<a title=\"$html_name\" href=\"view_entry.php?id=" . $row['entry_id'] . "\">$html_name</a>";
// created by
$values[] = htmlspecialchars($row['create_by']);
// print [$row["room_id"]];
$values[] = htmlspecialchars(getRoomName([$row["room_id"]][0]));
// start time and link to day view
$date = getdate($row['start_time']);
$link = "<a href=\"day.php?day=$date[mday]&month=$date[mon]&year=$date[year]&area=".$row['area_id']."\">";
if(empty($row['enable_periods']))
{
$link_str = time_date_string($row['start_time']);
}
else
{
list(,$link_str) = period_date_string($row['start_time']);
}
$link .= "$link_str</a>";
// add a span with the numeric start time in the title for sorting
$values[] = "<span title=\"" . $row['start_time'] . "\"></span>" . $link;
// description
$values[] = htmlspecialchars($row['description']);
if ($ajax)
{
$json_data['aaData'][] = $values;
}
else
{
echo "<tr>\n<td>\n";
echo implode("</td>\n<td>", $values);
echo "</td>\n</tr>\n";
}
}
// Get non-standard form variables
$area = get_form_var('area', 'int');
$room = get_form_var('room', 'int');
$search_str = get_form_var('search_str', 'string');
$search_pos = get_form_var('search_pos', 'int');
$total = get_form_var('total', 'int');
// $advanced = get_form_var('advanced', 'int');
$ajax = get_form_var('ajax', 'int'); // Set if this is an Ajax request
$datatable = get_form_var('datatable', 'int'); // Will only be set if we're using DataTables
// Get the start day/month/year and make them the current day/month/year
$day = get_form_var('from_day', 'int');
$month = get_form_var('from_month', 'int');
$year = get_form_var('from_year', 'int');
// If we haven't been given a sensible date then use today's
if (!isset($day) || !isset($month) || !isset($year) || !checkdate($month, $day, $year))
{
$day = date("d");
$month = date("m");
$year = date("Y");
}
// Check the user is authorised for this page
checkAuthorised();
// Also need to know whether they have admin rights
$user = getUserName();
$is_admin = (isset($user) && authGetUserLevel($user)>=2);
// Set up for Ajax. We need to know whether we're capable of dealing with Ajax
// requests, which will only be if (a) the browser is using DataTables and (b)
// we can do JSON encoding. We also need to initialise the JSON data array.
$ajax_capable = $datatable && function_exists('json_encode');
if ($ajax)
{
$json_data['aaData'] = array();
}
if (!isset($search_str))
{
$search_str = '';
}
if (!$ajax)
{
print_header($day, $month, $year, $area, isset($room) ? $room : "");
// if (!empty($advanced))
// {
// ?>
<!-- <form class="form_general" id="search_form" method="get" action="search.php">
// <fieldset>
// <legend><?php echo get_vocab("advanced_search") ?></legend>
// <div id="div_search_str">
// <label for="search_str"><?php echo get_vocab("Booking Search") ?>:</label>
// <input type="search" id="search_str" name="search_str" required>
// </div>
// <div id="div_search_from"> -->
<?php
// echo "<label>" . get_vocab("from") . ":</label>\n";
// genDateSelector ("from_", $day, $month, $year);
// ?>
<!-- </div>
// <div id="div_search_to"> -->
<?php
// //echo "<label>" . get_vocab("to") . ":</label>\n";
// //genDateSelector ("to_", $day, $month, $year);
// ?>
<!-- </div>
// <div id="search_submit">
// <input class="submit" type="submit" value="<?php echo get_vocab("search_button") ?>">
// </div>
// </fieldset>
// </form> -->
<?php
// output_trailer();
// exit;
// }
if (!isset($search_str) || ($search_str == ''))
{
echo "<p class=\"error\">" . get_vocab("invalid_search") . "</p>";
output_trailer();
exit;
}
// now is used so that we only display entries newer than the current time
echo "<h3>";
echo get_vocab("search_results") . ": ";
echo "\"<span id=\"search_str\">" . htmlspecialchars($search_str) . "</span>\"";
echo "</h3>\n";
} // if (!$ajax)
$now = mktime(0, 0, 0, $month, $day, $year);
// This is the main part of the query predicate, used in both queries:
// NOTE: sql_syntax_caseless_contains() does the SQL escaping
$sql_pred = "( " . sql_syntax_caseless_contains("E.create_by", $search_str)
. " OR " . sql_syntax_caseless_contains("E.name", $search_str)
. " OR " . sql_syntax_caseless_contains("E.description", $search_str)
. " OR " . sql_syntax_caseless_contains("E.description", $search_str)
. " OR " . sql_syntax_caseless_contains("R.room_name", $search_str)
. " OR " . sql_syntax_caseless_contains("E.room_id", $search_str);
// Also need to search custom fields (but only those with character data,
// which can include fields that have an associative array of options)
$fields = sql_field_info($tbl_entry);
foreach ($fields as $field)
{
if (!in_array($field['name'], $standard_fields['entry']))
{
// If we've got a field that is represented by an associative array of options
// then we have to search for the keys whose values match the search string
if (isset($select_options["entry." . $field['name']]) &&
is_assoc($select_options["entry." . $field['name']]))
{
foreach($select_options["entry." . $field['name']] as $key => $value)
{
// We have to use strpos() rather than stripos() because we cannot
// assume PHP5
if (($key !== '') && (strpos(strtolower($value), strtolower($search_str)) !== FALSE))
{
$sql_pred .= " OR E." . $field['name'] . "='" . sql_escape($key) . "'";
}
}
}
elseif ($field['nature'] == 'character')
{
$sql_pred .= " OR " . sql_syntax_caseless_contains("E." . $field['name'], $search_str);
}
}
}
$sql_pred .= ") AND E.end_time > $now";
$sql_pred .= " AND E.room_id = R.id AND R.area_id = A.id";
// If we're not an admin (they are allowed to see everything), then we need
// to make sure we respect the privacy settings. (We rely on the privacy fields
// in the area table being not NULL. If they are by some chance NULL, then no
// entries will be found, which is at least safe from the privacy viewpoint)
// if (!$is_admin)
// {
// if (isset($user))
// {
// // if the user is logged in they can see:
// // - all bookings, if private_override is set to 'public'
// // - their own bookings, and others' public bookings if private_override is set to 'none'
// // - just their own bookings, if private_override is set to 'private'
// $sql_pred .= " AND ((A.private_override='public') OR
// (A.private_override='none' AND ((E.status&" . STATUS_PRIVATE . "=0) OR E.create_by = '" . sql_escape($user) . "')) OR
// (A.private_override='private' AND E.create_by = '" . sql_escape($user) . "'))";
// }
// else
// {
// // if the user is not logged in they can see:
// // - all bookings, if private_override is set to 'public'
// // - public bookings if private_override is set to 'none'
// $sql_pred .= " AND ((A.private_override='public') OR
// (A.private_override='none' AND (E.status&" . STATUS_PRIVATE . "=0)))";
// }
// }
// The first time the search is called, we get the total
// number of matches. This is passed along to subsequent
// searches so that we don't have to run it for each page.
if (!isset($total))
{
$total = sql_query1("SELECT count(*)
FROM $tbl_entry E, $tbl_room R, $tbl_area A
WHERE $sql_pred");
}
if ($total < 0)
{
trigger_error(sql_error(), E_USER_WARNING);
fatal_error(FALSE, get_vocab("fatal_db_error"));
}
if (($total <= 0) && !$ajax)
{
echo "<p id=\"nothing_found\">" . get_vocab("nothing_found") . "</p>\n";
output_trailer();
exit;
}
if(!isset($search_pos) || ($search_pos <= 0))
{
$search_pos = 0;
}
else if($search_pos >= $total)
{
$search_pos = $total - ($total % $search["count"]);
}
// If we're Ajax capable and this is not an Ajax request then don't ouput
// the table body, because that's going to be sent later in response to
// an Ajax request - so we don't need to do the query
if (!$ajax_capable || $ajax)
{
// Now we set up the "real" query
$sql = "SELECT E.id AS entry_id, E.create_by, E.name, E.description, E.start_time,
R.area_id, A.enable_periods, E.room_id
FROM $tbl_entry E, $tbl_room R, $tbl_area A
WHERE $sql_pred
ORDER BY E.start_time asc" . sql_syntax_limit($search["count"], $search_pos);
// If it's an Ajax query we want everything. Otherwise we use LIMIT to just get
// the stuff we want.
if (!$ajax)
{
$sql .= " " . sql_syntax_limit($search["count"], $search_pos);
}
// this is a flag to tell us not to display a "Next" link
$result = sql_query($sql);
if (! $result)
{
trigger_error(sql_error(), E_USER_WARNING);
fatal_error(FALSE, get_vocab("fatal_db_error"));
}
$num_records = sql_count($result);
}
if (!$ajax_capable)
{
echo generate_search_nav_html($search_pos, $total, $num_records, $search_str);
}
if (!$ajax)
{
echo "<div id=\"search_output\" class=\"datatable_container\">\n";
echo "<table id=\"search_results\" class=\"admin_table display\">\n";
echo "<thead>\n";
echo "<tr>\n";
// We give some columns an stype data value so that the JavaScript knows how to sort them
echo "<th>" . get_vocab("namebooker") . "</th>\n";
echo "<th>" . get_vocab("createdby") . "</th>\n";
echo "<th>" . get_vocab("room") . "</th>\n";
echo "<th><span class=\"normal\" data-stype=\"title-numeric\">" . get_vocab("start_date") . "</span></th>\n";
echo "<th>" . get_vocab("description") . "</th>\n";
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
}
// If we're Ajax capable and this is not an Ajax request then don't ouput
// the table body, because that's going to be sent later in response to
// an Ajax request
if (!$ajax_capable || $ajax)
{
for ($i = 0; ($row = sql_row_keyed($result, $i)); $i++)
{
output_row($row);
}
}
if ($ajax)
{
echo json_encode($json_data);
}
else
{
echo "</tbody>\n";
echo "</table>\n";
echo "</div>\n";
output_trailer();
}