forked from gbishop/TarHeelReaderTheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections.php
97 lines (83 loc) · 2.12 KB
/
collections.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
<?php
/*
Template Name: Collections
Handle reading collections of books.
*/
?>
<?php
$collections_table = $wpdb->prefix . 'book_collections';
$where = array();
$page = 1;
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$query = getParam('q', '');
$terms = array();
if ($query) {
$words = array();
$i = preg_match_all('/[\w\'*]+/', $query, $words);
foreach($words[0] as $word) {
if (strpos($word, "'") !== false) {
$terms[] = '+"' . $word . '"';
} else {
$terms[] = '+' . $word;
}
}
}
if (count($terms) > 0) {
$qstring = mysql_real_escape_string(implode(' ', $terms));
$where[] = "MATCH(title,description) AGAINST('$qstring' IN BOOLEAN MODE)";
}
$page = getParam('cpage', '1', '/\d+/');
$page = intval($page);
}
// construct the query
if (count($where) > 0) {
$where = 'WHERE ' . implode(' AND ', $where);
} else {
$where = '';
}
$count = 24; // set small to force paging, should be much larger
$cp1 = $count + 1; // ask for one more to determine if there are more
$offset = ($page - 1) * $count;
$sql = "
SELECT *
FROM $collections_table
$where
ORDER BY title
LIMIT $offset,$cp1";
$rows = $wpdb->get_results($sql);
$nrows = $wpdb->num_rows;
$view = array();
$view['has_collections'] = $nrows > 0;
if ($nrows > $count) {
$more = 1;
$nrows = $count;
} else {
$more = 0;
}
$collections = array();
for($i=0; $i<$nrows; $i++) {
$row = $rows[$i];
$collections[] = array(
'title' => $row->title,
'description' => $row->description,
'slug' => $row->slug);
}
$view['collections'] = $collections;
// assemble the next and previous links
$params = array();
if ($query) {
$params['search'] = $query;
}
if ($page > 1) {
$params['cpage'] = $page - 1;
$view['prevLink'] = '/collections/?' . http_build_query($params);
}
if ($more) {
$params['cpage'] = $page + 1;
$view['nextLink'] = '/collections/?' . http_build_query($params);
}
// finally render the page
thr_header('collections-page');
echo template_render('collections', $view);
thr_footer();
?>