-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
69 lines (60 loc) · 2.61 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
<?php require($config->paths->templates.'layouts/sidebar-right.inc') ?>
<div data-pw-id="main-content" class="text-page">
<?php
// search.php template file
// See README.txt for more information.
// look for a GET variable named 'q' and sanitize it
$q = $sanitizer->text($input->get->q);
// did $q have anything in it?
if($q) {
// Send our sanitized query 'q' variable to the whitelist where it will be
// picked up and echoed in the search box by _main.php file. Now we could just use
// another variable initialized in _init.php for this, but it's a best practice
// to use this whitelist since it can be read by other modules. That becomes
// valuable when it comes to things like pagination.
$input->whitelist('q', $q);
// Sanitize for placement within a selector string. This is important for any
// values that you plan to bundle in a selector string like we are doing here.
$q = $sanitizer->selectorValue($q);
// Search the title and body fields for our query text.
// Limit the results to 50 pages.
$selector = "template=basic-page|blog-post,title|blog_body~=$q, limit=50";
// If user has access to admin pages, lets exclude them from the search results.
// Note that 2 is the ID of the admin page, so this excludes all results that have
// that page as one of the parents/ancestors. This isn't necessary if the user
// doesn't have access to view admin pages. So it's not technically necessary to
// have this here, but we thought it might be a good way to introduce has_parent.
if($user->isLoggedin()) $selector .= ", has_parent!=2";
// Find pages that match the selector
$matches = $pages->find($selector);
// did we find any matches?
if($matches->count) {
// yes we did
$content = "<h2>Found $matches->count pages matching your query:</h2>";
// we'll use our renderNav function (in _func.php) to render the navigation
$content .= renderSearchResults($matches,$sanitizer);
} else {
// we didn't find any
$content = "<h2>Sorry, no results were found.</h2>";
}
} else {
// no search terms provided
$content = "<h2>Please enter a search term in the search box (upper right corner)</h2>";
}
echo $content;
function renderSearchResults($matches, $sanitizer) {
$out = '';
foreach ($matches as $result) {
$out .= "<h3><a href='$result->url'>$result->title</a></h3>";
$out .= "<p>".$sanitizer->truncate($result->blog_body)." <a href='".$result->url."'>read more</a></p>";
}
return $out;
}
?>
</div>
<!-- END MAIN CONTENT -->
<!-- START SIDEBAR -->
<div data-pw-id="right-sidebar">
<?php require($config->paths->templates.'includes/blog-sidebar.inc') ?>
</div>
<!-- END SIDEBAR -->