|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* NewBB 5.0x, the forum module for XOOPS project |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
|
6
|
|
|
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
|
7
|
|
|
* @author Taiwen Jiang (phppp or D.J.) <[email protected]>, irmtfan <[email protected]> |
|
8
|
|
|
* @since 4.3 |
|
9
|
|
|
* @package module::newbb |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
use XoopsModules\Newbb; |
|
13
|
|
|
|
|
14
|
|
|
// completely rewrite by irmtfan - remove hardcode database access, solve order issues, add post_text & topic_id, add highlight and reduce queries |
|
15
|
|
|
|
|
16
|
|
|
require_once $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php'); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param $queryarray |
|
20
|
|
|
* @param $andor |
|
21
|
|
|
* @param $limit |
|
22
|
|
|
* @param $offset |
|
23
|
|
|
* @param $userid |
|
24
|
|
|
* @param int $forums |
|
25
|
|
|
* @param int|string $sortby |
|
26
|
|
|
* @param string $searchin |
|
27
|
|
|
* @param \CriteriaCompo $criteriaExtra |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
function newbb_search( |
|
31
|
|
|
$queryarray, |
|
32
|
|
|
$andor, |
|
33
|
|
|
$limit, |
|
34
|
|
|
$offset, |
|
35
|
|
|
$userid, |
|
36
|
|
|
$forums = 0, |
|
37
|
|
|
$sortby = 0, |
|
38
|
|
|
$searchin = 'both', |
|
39
|
|
|
\CriteriaCompo $criteriaExtra = null |
|
40
|
|
|
) { |
|
41
|
|
|
global $myts, $xoopsDB; |
|
42
|
|
|
// irmtfan - in XOOPSCORE/search.php $GLOBALS['xoopsModuleConfig'] is not set |
|
43
|
|
|
if (!isset($GLOBALS['xoopsModuleConfig'])) { |
|
44
|
|
|
$GLOBALS['xoopsModuleConfig'] = newbbLoadConfig(); |
|
45
|
|
|
} |
|
46
|
|
|
// irmtfan - in XOOPSCORE/search.php $xoopsModule is not set |
|
47
|
|
|
if (!is_object($GLOBALS['xoopsModule']) && is_object($GLOBALS['module']) |
|
48
|
|
|
&& 'newbb' === $GLOBALS['module']->getVar('dirname')) { |
|
49
|
|
|
$GLOBALS['xoopsModule'] = $GLOBALS['module']; |
|
50
|
|
|
} |
|
51
|
|
|
/** @var Newbb\ForumHandler $forumHandler */ |
|
52
|
|
|
$forumHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum'); |
|
53
|
|
|
$validForums = $forumHandler->getIdsByValues($forums); // can we use view permission? $forumHandler->getIdsByValues($forums, "view") |
|
54
|
|
|
|
|
55
|
|
|
$criteriaPost = new \CriteriaCompo(); |
|
56
|
|
|
$criteriaPost->add(new \Criteria('p.approved', 1), 'AND'); // only active posts |
|
57
|
|
|
|
|
58
|
|
|
$forum_list = []; // get forum lists just for forum names |
|
59
|
|
|
if (count($validForums) > 0) { |
|
60
|
|
|
$criteriaPermissions = new \CriteriaCompo(); |
|
61
|
|
|
$criteriaPermissions->add(new \Criteria('p.forum_id', '(' . implode(',', $validForums) . ')', 'IN'), 'AND'); |
|
62
|
|
|
$forum_list = $forumHandler->getAll(new \Criteria('forum_id', '(' . implode(', ', $validForums) . ')', 'IN'), 'forum_name', false); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (is_numeric($userid) && 0 !== $userid) { |
|
66
|
|
|
$criteriaUser = new \CriteriaCompo(); |
|
67
|
|
|
$criteriaUser->add(new \Criteria('p.uid', $userid), 'OR'); |
|
68
|
|
|
} elseif ($userid && is_array($userid)) { |
|
69
|
|
|
$userid = array_map('\intval', $userid); |
|
70
|
|
|
$criteriaUser = new \CriteriaCompo(); |
|
71
|
|
|
$criteriaUser->add(new \Criteria('p.uid', '(' . implode(',', $userid) . ')', 'IN'), 'OR'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$count = 0; |
|
75
|
|
|
if (is_array($queryarray)) { |
|
76
|
|
|
$count = count($queryarray); |
|
77
|
|
|
} |
|
78
|
|
|
$highlightKey = ''; |
|
79
|
|
|
if ($count > 0) { |
|
80
|
|
|
$criteriaKeywords = new \CriteriaCompo(); |
|
81
|
|
|
foreach ($queryarray as $queryTerm) { |
|
82
|
|
|
$termCriteria = new \CriteriaCompo(); |
|
83
|
|
|
$queryTermLike = '%' . $xoopsDB->escape($queryTerm) . '%'; |
|
84
|
|
|
if ('title' === $searchin || 'both' === $searchin) { |
|
85
|
|
|
$termCriteria->add(new \Criteria('p.subject', $queryTermLike, 'LIKE'), 'OR'); |
|
86
|
|
|
} |
|
87
|
|
|
if ('text' === $searchin || 'both' === $searchin) { |
|
88
|
|
|
$termCriteria->add(new \Criteria('t.post_text', $queryTermLike, 'LIKE'), 'OR'); |
|
89
|
|
|
} |
|
90
|
|
|
$criteriaKeywords->add($termCriteria, $andor); |
|
91
|
|
|
} |
|
92
|
|
|
// add highlight keywords to post links |
|
93
|
|
|
$highlightKey = '&keywords=' . implode(' ', $queryarray); |
|
94
|
|
|
$highlightKey = str_replace(' ', '+', $highlightKey); |
|
95
|
|
|
} |
|
96
|
|
|
$criteria = new \CriteriaCompo(); |
|
97
|
|
|
$criteria->add($criteriaPost, 'AND'); |
|
98
|
|
|
if (null !== $criteriaPermissions) { |
|
99
|
|
|
$criteria->add($criteriaPermissions, 'AND'); |
|
100
|
|
|
} |
|
101
|
|
|
if (isset($criteriaUser)) { |
|
102
|
|
|
$criteria->add($criteriaUser, 'AND'); |
|
103
|
|
|
} |
|
104
|
|
|
if (isset($criteriaKeywords)) { |
|
105
|
|
|
$criteria->add($criteriaKeywords, 'AND'); |
|
106
|
|
|
} |
|
107
|
|
|
if (isset($criteriaExtra)) { |
|
108
|
|
|
$criteria->add($criteriaExtra, 'AND'); |
|
109
|
|
|
} |
|
110
|
|
|
//$criteria->setLimit($limit); // no need for this |
|
111
|
|
|
//$criteria->setStart($offset); // no need for this |
|
112
|
|
|
|
|
113
|
|
|
if (empty($sortby)) { |
|
114
|
|
|
$sortby = 'p.post_time'; |
|
115
|
|
|
} |
|
116
|
|
|
$criteria->setSort($sortby); |
|
117
|
|
|
$order = 'ASC'; |
|
118
|
|
|
if ('p.post_time' === $sortby) { |
|
119
|
|
|
$order = 'DESC'; |
|
120
|
|
|
} |
|
121
|
|
|
$criteria->setOrder($order); |
|
122
|
|
|
|
|
123
|
|
|
/** @var Newbb\PostHandler $postHandler */ |
|
124
|
|
|
$postHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Post'); |
|
125
|
|
|
$posts = $postHandler->getPostsByLimit($criteria, $limit, $offset); |
|
126
|
|
|
|
|
127
|
|
|
$ret = []; |
|
128
|
|
|
$i = 0; |
|
129
|
|
|
foreach (array_keys($posts) as $id) { |
|
130
|
|
|
/** @var Newbb\Post $post */ |
|
131
|
|
|
$post = $posts[$id]; |
|
132
|
|
|
$post_data = $post->getPostBody(); |
|
133
|
|
|
$ret[$i]['topic_id'] = $post->getVar('topic_id'); |
|
134
|
|
|
$ret[$i]['link'] = XOOPS_URL . '/modules/newbb/viewtopic.php?post_id=' . $post->getVar('post_id') . $highlightKey; // add highlight key |
|
135
|
|
|
$ret[$i]['title'] = $post_data['subject']; |
|
136
|
|
|
$ret[$i]['time'] = $post_data['date']; |
|
137
|
|
|
$ret[$i]['forum_name'] = $myts->htmlSpecialChars($forum_list[$post->getVar('forum_id')]['forum_name']); |
|
138
|
|
|
$ret[$i]['forum_link'] = XOOPS_URL . '/modules/newbb/viewforum.php?forum=' . $post->getVar('forum_id'); |
|
139
|
|
|
$ret[$i]['post_text'] = $post_data['text']; |
|
140
|
|
|
$ret[$i]['uid'] = $post->getVar('uid'); |
|
141
|
|
|
$ret[$i]['poster'] = $post->getVar('uid') ? '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $ret[$i]['uid'] . '">' . $post_data['author'] . '</a>' : $post_data['author']; |
|
142
|
|
|
++$i; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $ret; |
|
146
|
|
|
} |
|
147
|
|
|
|