diff --git a/Sources/Actions/Admin/Maintenance.php b/Sources/Actions/Admin/Maintenance.php index 089aba225f..b632577ff2 100644 --- a/Sources/Actions/Admin/Maintenance.php +++ b/Sources/Actions/Admin/Maintenance.php @@ -16,10 +16,10 @@ namespace SMF\Actions\Admin; use SMF\ActionInterface; +use SMF\Actions\MessageIndex; use SMF\Actions\TopicRemove; use SMF\ActionTrait; use SMF\Cache\CacheApi; -use SMF\Category; use SMF\Config; use SMF\Db\DatabaseApi as Db; use SMF\Draft; @@ -233,37 +233,10 @@ public function members(): void public function topics(): void { // Let's load up the boards in case they are useful. - Utils::$context['categories'] = []; - - $result = Db::$db->query( - 'SELECT b.id_board, b.name, b.child_level, c.name AS cat_name, c.id_cat - FROM {db_prefix}boards AS b - LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat) - WHERE {query_see_board} - AND redirect = {string:blank_redirect}', - [ - 'blank_redirect' => '', - ], - identifier: 'order_by_board_order', - ); - - while ($row = Db::$db->fetch_assoc($result)) { - if (!isset(Utils::$context['categories'][$row['id_cat']])) { - Utils::$context['categories'][$row['id_cat']] = [ - 'name' => $row['cat_name'], - 'boards' => [], - ]; - } - - Utils::$context['categories'][$row['id_cat']]['boards'][$row['id_board']] = [ - 'id' => $row['id_board'], - 'name' => $row['name'], - 'child_level' => $row['child_level'], - ]; - } - Db::$db->free_result($result); - - Category::sort(Utils::$context['categories']); + Utils::$context['categories'] = MessageIndex::getBoardList([ + 'use_permissions' => true, + 'not_redirection' => true, + ]); if (isset($_GET['done']) && $_GET['done'] == 'purgeold') { Utils::$context['maintenance_finished'] = Lang::getTxt('maintain_old', file: 'ManageMaintenance'); @@ -274,6 +247,8 @@ public function topics(): void /** * Oh noes! I'd document this but that would give it away. + * + * @internal */ public function destroy(): void { diff --git a/Sources/Actions/MessageIndex.php b/Sources/Actions/MessageIndex.php index a9bc6acb60..42b2cd0adc 100644 --- a/Sources/Actions/MessageIndex.php +++ b/Sources/Actions/MessageIndex.php @@ -19,7 +19,6 @@ use SMF\ActionRouter; use SMF\ActionTrait; use SMF\Board; -use SMF\Category; use SMF\Config; use SMF\Db\DatabaseApi as Db; use SMF\ErrorHandler; @@ -188,16 +187,16 @@ public static function getBoardList(array $boardListOptions = []): array } $where = []; - $where_parameters = []; + $params = []; if (isset($boardListOptions['excluded_boards'])) { $where[] = 'b.id_board NOT IN ({array_int:excluded_boards})'; - $where_parameters['excluded_boards'] = $boardListOptions['excluded_boards']; + $params['excluded_boards'] = $boardListOptions['excluded_boards']; } if (isset($boardListOptions['included_boards'])) { $where[] = 'b.id_board IN ({array_int:included_boards})'; - $where_parameters['included_boards'] = $boardListOptions['included_boards']; + $params['included_boards'] = $boardListOptions['included_boards']; } if (!empty($boardListOptions['ignore_boards'])) { @@ -208,43 +207,52 @@ public static function getBoardList(array $boardListOptions = []): array if (!empty($boardListOptions['not_redirection'])) { $where[] = 'b.redirect = {string:blank_redirect}'; - $where_parameters['blank_redirect'] = ''; + $params['blank_redirect'] = ''; } - $request = Db::$db->query( - 'SELECT c.name AS cat_name, c.id_cat, b.id_board, b.name AS board_name, b.child_level, b.redirect - FROM {db_prefix}boards AS b - LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)' . (empty($where) ? '' : ' - WHERE ' . implode(' - AND ', $where)), - $where_parameters, - identifier: 'order_by_board_order', - ); + $selects = [ + 'c.name AS cat_name', + 'c.id_cat', + 'b.id_board', + 'b.name AS board_name', + 'b.child_level', + 'b.redirect', + ]; + + $joins = [ + 'LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)', + ]; + + $order = [ + 'b.board_order', + ]; $return_value = []; + $selected = null; - if (Db::$db->num_rows($request) !== 0) { - while ($row = Db::$db->fetch_assoc($request)) { - if (!isset($return_value[$row['id_cat']])) { - $return_value[$row['id_cat']] = [ - 'id' => $row['id_cat'], - 'name' => $row['cat_name'], - 'boards' => [], - ]; - } + if (isset($boardListOptions['selected_boards']) && \is_array($boardListOptions['selected_boards'])) { + $selected = array_flip($boardListOptions['selected_boards']); + } elseif (isset($boardListOptions['selected_board'])) { + $selected = [$boardListOptions['selected_board'] => true]; + } - $return_value[$row['id_cat']]['boards'][$row['id_board']] = [ - 'id' => $row['id_board'], - 'name' => $row['board_name'], - 'child_level' => $row['child_level'], - 'redirect' => $row['redirect'], - 'selected' => isset($boardListOptions['selected_board']) && $boardListOptions['selected_board'] == $row['id_board'], + foreach (Board::queryData($selects, $params, $joins, $where, $order) as $row) { + if (!isset($return_value[$row['id_cat']])) { + $return_value[$row['id_cat']] = [ + 'id' => $row['id_cat'], + 'name' => $row['cat_name'], + 'boards' => [], ]; } - } - Db::$db->free_result($request); - Category::sort($return_value); + $return_value[$row['id_cat']]['boards'][$row['id_board']] = [ + 'id' => $row['id_board'], + 'name' => $row['board_name'], + 'child_level' => $row['child_level'], + 'redirect' => $row['redirect'], + 'selected' => $selected !== null && isset($selected[$row['id_board']]), + ]; + } return $return_value; } diff --git a/Sources/Actions/Profile/IgnoreBoards.php b/Sources/Actions/Profile/IgnoreBoards.php index 69c0908d37..cedbea0f5a 100644 --- a/Sources/Actions/Profile/IgnoreBoards.php +++ b/Sources/Actions/Profile/IgnoreBoards.php @@ -16,10 +16,10 @@ namespace SMF\Actions\Profile; use SMF\ActionInterface; +use SMF\Actions\MessageIndex; use SMF\ActionTrait; use SMF\Category; use SMF\Config; -use SMF\Db\DatabaseApi as Db; use SMF\ErrorHandler; use SMF\Profile; use SMF\Utils; @@ -47,52 +47,20 @@ public function execute(): void // Find all the boards this user is allowed to see. Utils::$context['num_boards'] = 0; - Utils::$context['categories'] = []; - - $request = Db::$db->query( - 'SELECT b.id_cat, c.name AS cat_name, b.id_board, b.name, b.child_level, - ' . (!empty(Profile::$member->data['ignore_boards']) ? 'b.id_board IN ({array_int:ignore_boards})' : '0') . ' AS is_ignored - FROM {db_prefix}boards AS b - LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat) - WHERE {query_see_board} - AND redirect = {string:empty_string}', - [ - 'ignore_boards' => !empty(Profile::$member->data['ignore_boards']) ? explode(',', Profile::$member->data['ignore_boards']) : [], - 'empty_string' => '', - ], - identifier: 'order_by_board_order', - ); - - while ($row = Db::$db->fetch_assoc($request)) { - Utils::$context['num_boards']++; - - // This category hasn't been set up yet.. - if (!isset(Utils::$context['categories'][$row['id_cat']])) { - Utils::$context['categories'][$row['id_cat']] = [ - 'id' => $row['id_cat'], - 'name' => $row['cat_name'], - 'boards' => [], - ]; - } - - // Set this board up, and let the template know when it's a child. (indent them..) - Utils::$context['categories'][$row['id_cat']]['boards'][$row['id_board']] = [ - 'id' => $row['id_board'], - 'name' => $row['name'], - 'child_level' => $row['child_level'], - 'selected' => $row['is_ignored'], - ]; - } - Db::$db->free_result($request); - - Category::sort(Utils::$context['categories']); + Utils::$context['categories'] = MessageIndex::getBoardList([ + 'use_permissions' => true, + 'not_redirection' => true, + 'selected_boards' => !empty(Profile::$member->data['ignore_boards']) + ? explode(',', Profile::$member->data['ignore_boards']) + : [], + ]); // Now, let's sort the list of categories into the boards for templates that like that. $temp_boards = []; - foreach (Utils::$context['categories'] as $category) { + foreach (Utils::$context['categories'] as $cat_id => $category) { // Include a list of boards per category for easy toggling. - Utils::$context['categories'][$category['id']]['child_ids'] = array_keys($category['boards']); + Utils::$context['categories'][$cat_id]['child_ids'] = array_keys($category['boards']); $temp_boards[] = [ 'name' => $category['name'], @@ -100,6 +68,8 @@ public function execute(): void ]; $temp_boards = array_merge($temp_boards, array_values($category['boards'])); + + Utils::$context['num_boards'] += \count($category['boards']); } $max_boards = max(2, ceil(\count($temp_boards) / 2));