Skip to content

Commit 67db90a

Browse files
authored
Merge pull request #9101 from jdarwood007/3.0/searchBoards
[3.0] Move search board loading logic into Board
2 parents dd497c0 + ccabfa2 commit 67db90a

2 files changed

Lines changed: 64 additions & 83 deletions

File tree

Sources/Actions/Search.php

Lines changed: 11 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use SMF\ActionInterface;
1919
use SMF\ActionRouter;
2020
use SMF\ActionTrait;
21-
use SMF\Category;
21+
use SMF\Board;
2222
use SMF\Config;
2323
use SMF\Db\DatabaseApi as Db;
2424
use SMF\ErrorHandler;
@@ -70,6 +70,7 @@ public function execute(): void
7070
// Don't load this in XML mode.
7171
if (!isset($_REQUEST['xml'])) {
7272
Theme::loadTemplate('Search');
73+
Theme::loadTemplate('GenericControls');
7374
Theme::loadJavaScriptFile('suggest.js', ['defer' => false, 'minimize' => true], 'smf_suggest');
7475
}
7576

@@ -174,91 +175,18 @@ public function execute(): void
174175
}
175176
}
176177

177-
// Find all the boards this user is allowed to see.
178-
$request = Db::$db->query(
179-
'SELECT b.id_cat, c.name AS cat_name, b.id_board, b.name, b.child_level
180-
FROM {db_prefix}boards AS b
181-
LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
182-
WHERE {query_see_board}
183-
AND redirect = {string:empty_string}',
184-
[
185-
'empty_string' => '',
186-
],
187-
identifier: 'order_by_board_order',
188-
);
189-
Utils::$context['num_boards'] = Db::$db->num_rows($request);
190-
Utils::$context['boards_check_all'] = true;
191-
Utils::$context['categories'] = [];
192-
193-
while ($row = Db::$db->fetch_assoc($request)) {
194-
// This category hasn't been set up yet..
195-
if (!isset(Utils::$context['categories'][$row['id_cat']])) {
196-
Utils::$context['categories'][$row['id_cat']] = [
197-
'id' => $row['id_cat'],
198-
'name' => $row['cat_name'],
199-
'boards' => [],
200-
];
201-
}
202-
203-
$is_recycle_board = !empty(Config::$modSettings['recycle_enable']) && $row['id_board'] == Config::$modSettings['recycle_board'];
204-
205-
// Set this board up, and let the template know when it's a child. (indent them..)
206-
Utils::$context['categories'][$row['id_cat']]['boards'][$row['id_board']] = [
207-
'id' => $row['id_board'],
208-
'name' => $row['name'],
209-
'child_level' => $row['child_level'],
210-
];
211-
212-
// If user selected some particular boards, is this one of them?
213-
if (!empty(Utils::$context['search_params']['brd'])) {
214-
Utils::$context['categories'][$row['id_cat']]['boards'][$row['id_board']]['selected'] = \in_array($row['id_board'], Utils::$context['search_params']['brd']);
215-
}
216-
// User didn't select any boards, so select all except ignored and recycle boards.
217-
else {
218-
Utils::$context['categories'][$row['id_cat']]['boards'][$row['id_board']]['selected'] = !$is_recycle_board && !\in_array($row['id_board'], User::$me->ignoreboards);
219-
}
220-
221-
// If a board wasn't checked that probably should have been ensure the board selection is selected, yo!
222-
if (!Utils::$context['categories'][$row['id_cat']]['boards'][$row['id_board']]['selected'] && !$is_recycle_board) {
223-
Utils::$context['boards_check_all'] = false;
224-
}
225-
}
226-
Db::$db->free_result($request);
227-
228-
Category::sort(Utils::$context['categories']);
229-
230-
// Now, let's sort the list of categories into the boards for templates that like that.
231-
$temp_boards = [];
232-
233-
foreach (Utils::$context['categories'] as $category) {
234-
$temp_boards[] = [
235-
'name' => $category['name'],
236-
'child_ids' => array_keys($category['boards']),
237-
];
238-
$temp_boards = array_merge($temp_boards, array_values($category['boards']));
239-
240-
// Include a list of boards per category for easy toggling.
241-
Utils::$context['categories'][$category['id']]['child_ids'] = array_keys($category['boards']);
178+
// If user selected some particular boards, is this one of them?
179+
if (!empty(Utils::$context['search_params']['brd'])) {
180+
$boards = Utils::$context['search_params']['brd'];
242181
}
243-
244-
$max_boards = ceil(\count($temp_boards) / 2);
245-
246-
if ($max_boards == 1) {
247-
$max_boards = 2;
182+
// User didn't select any boards, so select all except ignored and recycle boards.
183+
elseif (!empty(Config::$modSettings['recycle_enable']) && !empty(Config::$modSettings['recycle_board'])) {
184+
$boards = array_merge(User::$me->ignoreboards, [(int) Config::$modSettings['recycle_board']]);
185+
} else {
186+
$boards = User::$me->ignoreboards;
248187
}
249188

250-
// Now, alternate them so they can be shown left and right ;).
251-
Utils::$context['board_columns'] = [];
252-
253-
for ($i = 0; $i < $max_boards; $i++) {
254-
Utils::$context['board_columns'][] = $temp_boards[$i];
255-
256-
if (isset($temp_boards[$i + $max_boards])) {
257-
Utils::$context['board_columns'][] = $temp_boards[$i + $max_boards];
258-
} else {
259-
Utils::$context['board_columns'][] = [];
260-
}
261-
}
189+
Utils::$context['categories'] = Board::getUserVisibleBoards($boards);
262190

263191
if (!empty($_REQUEST['topic'])) {
264192
Utils::$context['search_params']['topic'] = (int) $_REQUEST['topic'];

Sources/Board.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,59 @@ public static function getMsgMemberID(int $messageID): int
11081108
return (int) $memberID;
11091109
}
11101110

1111+
/**
1112+
* Fetches the list of boards the user is allowed to see and organizes them by categories.
1113+
*
1114+
* This function queries the database for boards visible to the current user, grouped by categories.
1115+
* It returns a structured array of categories and their associated boards.
1116+
*
1117+
* @param array $boards An array of board IDs to mark as selected.
1118+
* @return array The structured array of categories and boards.
1119+
*/
1120+
public static function getUserVisibleBoards(array $boards): array
1121+
{
1122+
// Query to fetch boards visible to the user.
1123+
$request = Db::$db->query(
1124+
'order_by_board_order',
1125+
'SELECT id_board, b.name, child_level, c.name AS cat_name, id_cat
1126+
FROM {db_prefix}boards AS b
1127+
JOIN {db_prefix}categories AS c USING (id_cat)
1128+
WHERE {query_see_board}
1129+
AND redirect = {string:empty_string}
1130+
ORDER BY board_order',
1131+
[
1132+
'empty_string' => '',
1133+
],
1134+
);
1135+
1136+
$categories = [];
1137+
$categoryTracker = [];
1138+
$currentCategoryIndex = -1;
1139+
1140+
// Process the results and group boards by categories.
1141+
while ($row = Db::$db->fetch_assoc($request)) {
1142+
if (!isset($categoryTracker[$row['id_cat']])) {
1143+
$categories[++$currentCategoryIndex] = [
1144+
'id' => (int) $row['id_cat'],
1145+
'name' => $row['cat_name'],
1146+
'boards' => [],
1147+
];
1148+
$categoryTracker[$row['id_cat']] = true;
1149+
}
1150+
1151+
$categories[$currentCategoryIndex]['boards'][] = [
1152+
'id' => (int) $row['id_board'],
1153+
'name' => $row['name'],
1154+
'child_level' => (int) $row['child_level'],
1155+
'selected' => \in_array($row['id_board'], $boards),
1156+
];
1157+
}
1158+
1159+
Db::$db->free_result($request);
1160+
1161+
return $categories;
1162+
}
1163+
11111164
/**
11121165
* Modify the settings and position of a board.
11131166
* Used by ManageBoards.php to change the settings of a board.

0 commit comments

Comments
 (0)