-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.php
218 lines (201 loc) · 6.88 KB
/
actions.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
<?php
/*
* These functions are registered with "actions", they could be called by themes using do_action()
*/
/*
* Wrapper to get new messages and sticky messages for displaying on front page
* @max: Maximium number of total messages to get
* @cat: Category to search in
* @include_sticky: Whether to include sticky messages
* return: Array of message objects
*/
function get_newmsg($max, $cat='all', $include_sticky=false)
{
include_once('dboperator.php');
return DBOperator::get_newmsg($max, $cat, $include_sticky);
}
add_action('get_newmsg', 'get_newmsg', 10, 3);
// Function to search and show the result by title
function get_bt_search_by_title($query, $curr_page)
{
global $wpdb;
$numrows = $wpdb->get_var("SELECT COUNT(msg_id) as rows FROM " . WP_BTAEON_TABLE . " WHERE msg_title LIKE '%$query%'");
$max = 15;
$numpages = ceil($numrows / $max);
$offset = ($curr_page - 1) * $max;
// Print the links to access each page
$nav = '';
for ( $spage = 1; $spage <= $numpages; $spage++ )
{
// With few pages, print all the links
if ( $numpages < 4 )
{
if ( $spage == $curr_page ) $nav .= $spage; // No need to create a link to current page
else $nav .= ' <a href="?btp=' . $spage . '&type=bt&title=' . $query . '">' . $spage . '</a> ';
} else {
if ( $spage == $curr_page )$nav .= $spage; // No need to create a link to current page
elseif ( $spage == 1 || $spage == $numpages ) $nav .= ''; // No need to create first and last (they are created by the first and last links afterwards)
else {
// Print links that are close to the current page (< 3 steps away)
if ( $spage < ($curr_page + 3) && $spage > ($curr_page - 3) )
$nav .= ' <a href="?btp=' . $spage . '&type=bt&title=' . $query . '">' . $spage . '</a> ';
}
}
}
// print first, last, next, previous links
if ( $curr_page > 1 )
{
$spage = $curr_page - 1;
if ( $numpages >= 4 )
$first = ' <a href="?btp=1&type=bt&title=' . $query . '">«首頁</a> ';
else
$first = '';
} else {
$prev = ' '; // We're on page one, no need to print previous link
if ( $numpages >= 4 )
$first = ' ';
else
$first = '';
}
if ( $curr_page < $numpages )
{
$spage = $curr_page + 1;
if ( $numpages >= 4 )
$last = ' <a href="?btp=' . $numpages . '&type=bt&title=' . $query . '">末頁»</a> ';
else
$last = '';
} else {
$next = ' '; // We're on the last page
if ( $numpages >= 4 )
$last = ' ';
else
$last = '';
}
// Grab the search results
$rows = $wpdb->get_results("SELECT msg_id, msg_time, msg_owner, msg_title FROM " . WP_BTAEON_TABLE . " WHERE msg_title LIKE '%$query%' ORDER BY msg_time DESC LIMIT $offset, $max");
if ( !empty($rows) )
{
$out = '<table>';
foreach ( $rows as $row )
{
// Display display_name rather than login name
$userinfo = get_user_by('login', $row->msg_owner);
$out .= '<tr>
<td>' . convert_timestamp($row->msg_time) . '</td>
<td>' . $userinfo->display_name . '</td>
<td><a href="' . get_option("bt_showall_url") . '?mid=' . $row->msg_id . '">' . stripslashes($row->msg_title) . '</a></td>
</tr>
';
}
$out .= '</table>';
} else {
$out = '<p>找不到符合條件的公告</p>';
}
echo $first . $nav . $last . $out . $first . $nav . $last;
}
add_action('get_bt_search_by_title', 'get_bt_search_by_title', 10, 2);
// Function to search and show the result by owner
function get_bt_search_by_owner($query, $curr_page)
{
global $wpdb;
$numrows = $wpdb->get_var("SELECT COUNT(msg_id) as rows FROM " . WP_BTAEON_TABLE . " WHERE msg_owner LIKE '$query'");
$max = 15;
$numpages = ceil($numrows / $max);
$offset = ($curr_page - 1) * $max;
// Print the links to access each page
$nav = '';
for ( $spage = 1; $spage <= $numpages; $spage++ )
{
// With few pages, print all the links
if ( $numpages < 4 )
{
if ( $spage == $curr_page ) $nav .= $spage; // No need to create a link to current page
else $nav .= ' <a href="?btp=' . $spage . '&type=bt&owner=' . $query . '">' . $spage . '</a> ';
} else {
if ( $spage == $curr_page )$nav .= $spage; // No need to create a link to current page
elseif ( $spage == 1 || $spage == $numpages ) $nav .= ''; // No need to create first and last (they are created by the first and last links afterwards)
else {
// Print links that are close to the current page (< 3 steps away)
if ( $spage < ($curr_page + 3) && $spage > ($curr_page - 3) )
$nav .= ' <a href="?btp=' . $spage . '&type=bt&owner=' . $query . '">' . $spage . '</a> ';
}
}
}
// print first, last, next, previous links
if ( $curr_page > 1 )
{
$spage = $curr_page - 1;
if ( $numpages >= 4 )
$first = ' <a href="?btp=1&type=bt&owner=' . $query . '">«首頁</a> ';
else
$first = '';
} else {
$prev = ' '; // We're on page one, no need to print previous link
if ( $numpages >= 4 )
$first = ' ';
else
$first = '';
}
if ( $curr_page < $numpages )
{
$spage = $curr_page + 1;
if ( $numpages >= 4 )
$last = ' <a href="?btp=' . $numpages . '&type=bt&owner=' . $query . '">末頁»</a> ';
else
$last = '';
} else {
$next = ' '; // We're on the last page
if ( $numpages >= 4 )
$last = ' ';
else
$last = '';
}
// Grab the search results
$rows = $wpdb->get_results("SELECT msg_id, msg_time, msg_owner, msg_title FROM " . WP_BTAEON_TABLE . " WHERE msg_owner LIKE '$query' ORDER BY msg_time DESC LIMIT $offset, $max");
if ( !empty($rows) )
{
$out = '<table>';
foreach ( $rows as $row )
{
// Display display_name rather than login name
$userinfo = get_user_by('login', $row->msg_owner);
$out .= '<tr>
<td>' . convert_timestamp($row->msg_time) . '</td>
<td>' . $userinfo->display_name . '</td>
<td><a href="' . get_option("bt_showall_url") . '?mid=' . $row->msg_id . '">' . stripslashes($row->msg_title) . '</a></td>
</tr>
';
}
$out .= '</table>';
} else {
$out = '<p>找不到符合條件的公告</p>';
}
echo $first . $nav . $last . $out . $first . $nav . $last;
}
add_action('get_bt_search_by_owner', 'get_bt_search_by_owner', 10, 2);
function get_allmsg($max)
{
global $wpdb;
$msgs = array();
if ( !is_int($max) ) $max = 10;
// Get categories
$sql = "SELECT * FROM " . WP_BTAEON_CATEGORIES_TABLE;
$cat_rows = $wpdb->get_results($sql);
foreach ( $cat_rows as $cat_row )
{
$msgs[$cat_row->category_id]['name'] = $cat_row->category_name;
$msgs[$cat_row->category_id]['link'] = $cat_row->category_link;
$sql = "SELECT msg_id, msg_time, msg_title FROM " . WP_BTAEON_TABLE . " WHERE msg_category='$cat_row->category_id' ORDER BY msg_time DESC LIMIT $max";
$msg_rows = $wpdb->get_results($sql);
foreach ( $msg_rows as $msg_row )
{
$msg_row->msg_time = convert_timestamp($msg_row->msg_time);
}
$msg_array = (array) $msg_rows;
$msgs[$cat_row->category_id]['msgs'] = $msg_array;
}
krsort($msgs);
echo json_encode($msgs, JSON_FORCE_OBJECT);
}
add_action('bt_get_allmsg', 'get_allmsg', 10, 1);
?>