-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscussionsearch.php
More file actions
268 lines (216 loc) · 10.5 KB
/
discussionsearch.php
File metadata and controls
268 lines (216 loc) · 10.5 KB
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
<?php
/*
Copyright (C) 2008-2009 Gilles Dubuc (www.kouiskas.com - gilles@dubuc.fr)
Search results on the discussion boards
*/
require_once(dirname(__FILE__).'/entities/community.php');
require_once(dirname(__FILE__).'/entities/communitymembership.php');
require_once(dirname(__FILE__).'/entities/discussionpost.php');
require_once(dirname(__FILE__).'/entities/discussionpostindexlist.php');
require_once(dirname(__FILE__).'/entities/discussionpostlist.php');
require_once(dirname(__FILE__).'/entities/discussionthread.php');
require_once(dirname(__FILE__).'/entities/discussionthreadindexlist.php');
require_once(dirname(__FILE__).'/entities/discussionthreadlist.php');
require_once(dirname(__FILE__).'/entities/insightfulmarklist.php');
require_once(dirname(__FILE__).'/entities/user.php');
require_once(dirname(__FILE__).'/entities/userpaging.php');
require_once(dirname(__FILE__).'/utilities/page.php');
require_once(dirname(__FILE__).'/utilities/string.php');
require_once(dirname(__FILE__).'/constants.php');
$user = User::getSessionUser();
$page = new Page('DISCUSS', 'COMMUNITIES', $user);
$page->startHTML();
$all_posts = array();
$all_posts_nid = array();
function getAllPostsByNid($nid) {
global $all_posts_nid;
global $user;
global $DISCUSSION_POST_STATUS;
global $USER_STATUS;
if (isset($all_posts_nid[$nid]))
return $all_posts_nid[$nid];
try {
$all_posts_nid[$nid] = DiscussionPostList::getByNidAndStatus($nid, $DISCUSSION_POST_STATUS['POSTED']);
} catch (DiscussionPostListException $e) {
}
if ($user->getStatus() == $USER_STATUS['UNREGISTERED']) {
try {
$anonymous_posts = DiscussionPostList::getByUidAndStatus($user->getUid(), $DISCUSSION_POST_STATUS['ANONYMOUS']);
foreach ($anonymous_posts as $anonymous_oid => $anonymous_timestamp) {
$anonymous_post = DiscussionPost::get($anonymous_oid);
if ($anonymous_post->getNid() == $nid) $all_posts_nid[$nid][$anonymous_oid] = $anonymous_timestamp;
}
} catch (DiscussionPostListException $e) {
}
} elseif($user->getStatus() == $USER_STATUS['BANNED']) {
try {
$anonymous_posts = DiscussionPostList::getByUidAndStatus($user->getUid(), $DISCUSSION_POST_STATUS['BANNED']);
foreach ($anonymous_posts as $anonymous_oid => $anonymous_timestamp) {
$anonymous_post = DiscussionPost::get($anonymous_oid);
if ($anonymous_post->getNid() == $nid) $all_posts_nid[$nid][$anonymous_oid] = $anonymous_timestamp;
}
} catch (DiscussionPostListException $e) {
}
}
asort($all_posts_nid[$nid]);
return $all_posts_nid[$nid];
}
$page_offset = isset($_REQUEST['page'])?$_REQUEST['page']:1;
if (!isset($_REQUEST['search'])) {
header('Location: '.$PAGE['DISCUSS'].'?lid='.$user->getLid());
exit(0);
}
$page->setTitle('<translate id="DISCUSSION_SEARCH_PAGE_TITLE">Search results for "<string value="'.String::fromaform(stripslashes($_REQUEST['search'])).'"/>" on the discussion boards of inspi.re</translate>');
$searchwordlist = String::wordlist(stripslashes($_REQUEST['search']));
$results = array();
$multiplier = array();
$communitylist = $user->getCommunityList();
$communitylist []= null;
foreach ($communitylist as $xid) {
$discussionpostindexlist = array();
$emptyresult = false;
foreach ($searchwordlist as $searchword) {
if (!$emptyresult) {
$newlist = DiscussionPostIndexList::getByXidAndWord($xid, $searchword);
if (empty($newlist)) {
$discussionpostindexlist = array();
$emptyresult = true;
}
} else $newlist = array();
if (empty($discussionpostindexlist) && !empty($newlist)) $discussionpostindexlist = $newlist;
else {
$keys = array_intersect($discussionpostindexlist, $newlist);
foreach ($newlist as $oid => $count) if (isset($discussionpostindexlist[$oid]))
$discussionpostindexlist[$oid] += $count;
foreach ($discussionpostindexlist as $oid => $count)
if (!isset($newlist[$oid])) unset($discussionpostindexlist[$oid]);
}
}
foreach ($discussionpostindexlist as $oid => $count) {
if (!isset($results[$oid])) $results[$oid] = $count;
else $results[$oid] += $count;
}
}
foreach ($communitylist as $xid) {
$discussionthreadindexlist = array();
$emptyresult = false;
foreach ($searchwordlist as $searchword) {
if (!$emptyresult) {
$newlist = DiscussionThreadIndexList::getByXidAndWord($xid, $searchword);
if (empty($newlist)) {
$discussionthreadindexlist = array();
$emptyresult = true;
}
} else $newlist = array();
if (empty($discussionthreadindexlist) && !empty($newlist)) $discussionthreadindexlist = $newlist;
else {
$keys = array_intersect($discussionthreadindexlist, $newlist);
foreach ($newlist as $nid => $count) if (isset($discussionthreadindexlist[$nid]))
$discussionthreadindexlist[$nid] += $count;
foreach ($discussionthreadindexlist as $nid => $count)
if (!isset($newlist[$nid])) unset($discussionpostindexlist[$nid]);
}
}
foreach ($discussionthreadindexlist as $nid => $count) {
$discussionpostlist = DiscussionPostList::getByNidAndStatus($nid, $DISCUSSION_POST_STATUS['POSTED']);
foreach ($discussionpostlist as $oid => $creation_time)
if (!isset($results[$oid])) $results[$oid] = $count * 3; else $results[$oid] += $count * 3;
}
}
arsort($results);
$results_count = count($results);
$amount_per_page = UserPaging::getPagingValue($user->getUid(), 'DISCUSSION_THREAD_POSTS');
$page_count = ceil($results_count / $amount_per_page);
echo '<div class="hint '.($page_count <= 1?'hintbigmargin':'').'">';
echo '<div class="hint_title">';
echo '<translate id="DISCUSSION_SEARCH_HINT_TITLE">';
echo 'Results in your discussion boards';
echo '</translate>';
echo '</div> <!-- hint_title -->';
if ($results_count == 0) {
echo '<translate id="DISCUSSION_SEARCH_HINT_BODY_ZERO">';
echo 'No search results for "<string value="'.String::fromaform(stripslashes($_REQUEST['search'])).'"/>"';
echo '</translate>';
} elseif ($results_count == 1) {
echo '<translate id="DISCUSSION_SEARCH_HINT_BODY_SINGULAR">';
echo '1 search result for "<string value="'.String::fromaform(stripslashes($_REQUEST['search'])).'"/>"';
echo '</translate>';
} else {
echo '<translate id="DISCUSSION_SEARCH_HINT_BODY">';
echo '<integer value="'.$results_count.'"/> search results for "<string value="'.String::fromaform(stripslashes($_REQUEST['search'])).'"/>"';
echo '</translate>';
}
echo '</div> <!-- hint -->';
function RenderDiscussionSearchLink($i, $page_offset, $page_count) {
global $PAGE;
global $user;
global $_REQUEST;
return ($i == $page_offset?'<b>'.$i.'</b>':'<a href="'.$PAGE['DISCUSSION_SEARCH'].'?lid='.$user->getLid().'&page='.$i.'&search='.urlencode(stripslashes($_REQUEST['search'])).'">'.$i.'</a>').($i == $page_count?'':' ');
}
echo UI::RenderPaging($page_offset, $page_count, 'RenderDiscussionSearchLink');
echo '<ad ad_id="DISCUSSION_SEARCH"/>';
$results = array_slice($results, $amount_per_page * ($page_offset - 1), $amount_per_page, true);
echo '<div id="search_result_list">';
if (empty($results)) {
echo '<div class="listing_item clearboth">';
echo '<div class="listing_header">';
echo '<translate id="DISCUSSION_SEARCH_EMPTY_RESULTS">';
echo 'There are no results in your discussion boards for that query.';
echo '</translate>';
echo '</div> <!-- listing_header -->';
echo '</div> <!-- listing_item -->';
} else foreach ($results as $oid => $count) {
$post = DiscussionPost::get($oid);
$nid = $post->getNid();
if (!isset($threads[$nid]))
$threads[$nid] = DiscussionThread::get($nid);
$xid = $threads[$nid]->getXid();
$key = array_search($oid, array_keys(getAllPostsByNid($nid)));
if ($key === false) $key = 0;
$post_page_offset = ceil(($key + 1) / $amount_per_page);
if ($xid !== null) {
$community = Community::get($xid);
$community_pid = $community->getPid();
} else $community_pid = null;
$marked = count(InsightfulMarkList::getByOid($oid));
$style = $marked > 0?'insightful_header':'';
echo '<div class="listing_item clearboth">';
echo '<picture href="'.$PAGE['BOARD'].'?lid='.$user->getLid().($xid === null ?'':'&xid='.$xid).'" category="community" class="listing_thumbnail" size="small" '.($community_pid === null?'':'pid="'.$community_pid.'"').' />';
echo '<profile_picture class="listing_thumbnail" uid="'.$post->getUid().'" size="small"/>';
echo '<div class="listing_header recent_header '.$style.'">';
$thread_title = String::htmlentities($threads[$nid]->getTitle());
foreach ($searchwordlist as $searchword) {
$thread_title = mb_ereg_replace('([\s,.:;?!¿¡()><*]+)('.String::htmlentities($searchword).')([\s,.:;?!¿¡()><*]+)', '\\1<span class="search_highlight">\\2</span>\\3', $thread_title, 'i');
$thread_title = mb_ereg_replace('^('.String::htmlentities($searchword).')([\s,.:;?!¿¡()><*]+)', '<span class="search_highlight">\\1</span>\\2', $thread_title, 'i');
$thread_title = mb_ereg_replace('([\s,.:;?!¿¡()><*]+)('.String::htmlentities($searchword).')$', '\\1<span class="search_highlight">\\2</span>', $thread_title, 'i');
}
$thread_title = String::fromaform($thread_title, false);
echo '<a href="'.$PAGE['DISCUSSION_THREAD'].'?lid='.$user->getLid().'&nid='.$nid.'&page='.$post_page_offset.'&scrollto=post_'.$oid.'">'.$thread_title.'</a>';
echo '</div> <!-- listing_header -->';
echo '<div class="listing_subheader recent_subheader">';
if ($xid === null) {
echo '<translate id="DISCUSS_RECENT_THREAD_HEADER_GENERAL">';
echo '<duration value="'.(time() - $post->getCreationTime()).'" /> ago <user_name uid="'.$post->getUid().'"/> wrote the following in General Discussion';
echo '</translate>';
} else {
echo '<translate id="DISCUSS_RECENT_THREAD_HEADER">';
echo '<duration value="'.(time() - $post->getCreationTime()).'" /> ago <user_name uid="'.$post->getUid().'"/> wrote the following in <community_name link="true" xid="'.$xid.'"/>';
echo '</translate>';
}
echo '</div> <!-- post_header -->';
echo '<div class="search_result_post">';
$post_text = String::htmlentities($post->getText());
foreach ($searchwordlist as $searchword) {
$post_text = mb_ereg_replace('([\s,.:;?!¿¡()><*]+)('.String::htmlentities($searchword).')([\s,.:;?!¿¡()><*]+)', '\\1<span class="search_highlight">\\2</span>\\3', $post_text, 'i');
$post_text = mb_ereg_replace('^('.String::htmlentities($searchword).')([\s,.:;?!¿¡()><*]+)', '<span class="search_highlight">\\1</span>\\2', $post_text, 'i');
$post_text = mb_ereg_replace('([\s,.:;?!¿¡()><*]+)('.String::htmlentities($searchword).')$', '\\1<span class="search_highlight">\\2</span>', $post_text, 'i');
}
$post_text = String::fromaform($post_text, false);
echo $post_text;
echo '</div> <!-- search_result_post -->';
echo '</div> <!-- listing_item -->';
}
echo '</div> <!-- search_result_list -->';
$page->endHTML();
$page->render();
?>