|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
You may not change or alter any portion of this comment or credits |
|
4
|
|
|
of supporting developers from this source code or any supporting source code |
|
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
|
6
|
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
|
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
|
14
|
|
|
* @license https://www.fsf.org/copyleft/gpl.html GNU public license |
|
15
|
|
|
* @since 1.0 |
|
16
|
|
|
* @author trabis <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use XoopsModules\Publisher\Helper; |
|
20
|
|
|
use XoopsModules\Publisher\Item; |
|
21
|
|
|
use XoopsModules\Publisher\ItemHandler; |
|
22
|
|
|
|
|
23
|
|
|
/** @var ItemHandler $itemHandler */ |
|
24
|
|
|
require_once __DIR__ . '/common.php'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param array $queryArray |
|
28
|
|
|
* @param $andor |
|
29
|
|
|
* @param $limit |
|
30
|
|
|
* @param $offset |
|
31
|
|
|
* @param $userid |
|
32
|
|
|
* @param array $categories |
|
33
|
|
|
* @param int $sortby |
|
34
|
|
|
* @param string $searchin |
|
35
|
|
|
* @param string $extra |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
|
|
function publisher_search($queryArray, $andor, $limit, $offset, $userid, $categories = [], $sortby = 0, $searchin = '', $extra = '') |
|
40
|
|
|
{ |
|
41
|
|
|
$helper = Helper::getInstance(); |
|
42
|
|
|
$ret = $item = []; |
|
43
|
|
|
$hightlightKey = ''; |
|
44
|
|
|
|
|
45
|
|
|
if (is_array($queryArray)) { |
|
|
|
|
|
|
46
|
|
|
if (0 === count($queryArray)) { |
|
47
|
|
|
$hightlightKey = ''; |
|
48
|
|
|
} else { |
|
49
|
|
|
$keywords = implode('+', $queryArray); |
|
50
|
|
|
$hightlightKey = '&keywords=' . $keywords; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$itemHandler = $helper->getHandler('Item'); |
|
55
|
|
|
$itemsObjs = $itemHandler->getItemsFromSearch($queryArray, $andor, $limit, $offset, $userid, $categories, $sortby, $searchin, $extra); |
|
56
|
|
|
$withCategoryPath = $helper->getConfig('search_cat_path'); |
|
57
|
|
|
//xoops_load("xoopslocal"); |
|
58
|
|
|
$usersIds = []; |
|
59
|
|
|
/** @var Item $obj */ |
|
60
|
|
|
if (0 !== count($itemsObjs)) { |
|
61
|
|
|
foreach ($itemsObjs as $obj) { |
|
62
|
|
|
$item['image'] = 'assets/images/item_icon.gif'; |
|
63
|
|
|
$item['link'] = $obj->getItemUrl(); |
|
64
|
|
|
$item['link'] .= (!empty($hightlightKey) && (false === mb_strpos($item['link'], '.php?'))) ? '?' . ltrim($hightlightKey, '&') : $hightlightKey; |
|
65
|
|
|
if ($withCategoryPath) { |
|
66
|
|
|
$item['title'] = $obj->getCategoryPath(false) . ' > ' . $obj->getTitle(); |
|
67
|
|
|
} else { |
|
68
|
|
|
$item['title'] = $obj->getTitle(); |
|
69
|
|
|
} |
|
70
|
|
|
$item['time'] = $obj->getVar('datesub'); //must go has unix timestamp |
|
71
|
|
|
$item['uid'] = $obj->uid(); |
|
72
|
|
|
//"Fulltext search/highlight |
|
73
|
|
|
$text = $obj->getBody(); |
|
74
|
|
|
$sanitizedText = ''; |
|
75
|
|
|
$textLower = \mb_strtolower($text); |
|
76
|
|
|
$queryArray = is_array($queryArray) ? $queryArray : [$queryArray]; |
|
77
|
|
|
|
|
78
|
|
|
if ('' != $queryArray[0] && count($queryArray) > 0) { |
|
79
|
|
|
foreach ($queryArray as $query) { |
|
80
|
|
|
$pos = \mb_stripos($textLower, $query); //xoops_local("strpos", $textLower, \mb_strtolower($query)); |
|
81
|
|
|
$start = max($pos - 100, 0); |
|
82
|
|
|
$length = \mb_strlen($query) + 200; //xoops_local("strlen", $query) + 200; |
|
83
|
|
|
$context = $obj->highlight(xoops_substr($text, $start, $length, ' [...]'), $query); |
|
84
|
|
|
$sanitizedText .= '<p>[...] ' . $context . '</p>'; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
//End of highlight |
|
88
|
|
|
$item['text'] = $sanitizedText; |
|
89
|
|
|
$item['author'] = $obj->author_alias(); |
|
90
|
|
|
$item['datesub'] = $obj->getDatesub($helper->getConfig('format_date')); |
|
91
|
|
|
$objUid = $obj->uid(); |
|
92
|
|
|
$usersIds[$objUid] = $objUid; |
|
93
|
|
|
$ret[] = $item; |
|
94
|
|
|
unset($item, $sanitizedText); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
xoops_load('XoopsUserUtility'); |
|
98
|
|
|
$usersNames = \XoopsUserUtility::getUnameFromIds($usersIds, $helper->getConfig('format_realname'), true); |
|
99
|
|
|
foreach ($ret as $key => $item) { |
|
100
|
|
|
if ('' == $item['author']) { |
|
101
|
|
|
$ret[$key]['author'] = $usersNames[$item['uid']] ?? ''; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
unset($usersNames, $usersIds); |
|
105
|
|
|
|
|
106
|
|
|
return $ret; |
|
107
|
|
|
} |
|
108
|
|
|
|