Skip to content

Commit 577d6bd

Browse files
committed
Remove unnecessary ViewableUserRank and ViewableUserRankList class and move the code into the UserRank and UserRankList.
1 parent 796ea99 commit 577d6bd

File tree

6 files changed

+115
-138
lines changed

6 files changed

+115
-138
lines changed

wcfsetup/install/files/lib/data/TMultilingualContentList.class.php renamed to wcfsetup/install/files/lib/data/MultilingualContentList.class.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,21 @@
1212
* @since 6.2
1313
*
1414
* @template-covariant TDatabaseObject of DatabaseObject|DatabaseObjectDecorator<DatabaseObject>
15-
* @mixin DatabaseObjectList<TDatabaseObject>
15+
* @extends DatabaseObjectList<TDatabaseObject>
1616
*/
17-
trait TMultilingualContentList
17+
abstract class MultilingualContentList extends DatabaseObjectList
1818
{
19-
public readonly int $langaugeID;
19+
public readonly int $preferredLanguageID;
20+
21+
public function __construct(?int $preferredLanguageID = null)
22+
{
23+
parent::__construct();
24+
25+
if ($preferredLanguageID === null) {
26+
$preferredLanguageID = WCF::getLanguage()->languageID;
27+
}
28+
$this->preferredLanguageID = $preferredLanguageID;
29+
}
2030

2131
#[\Override]
2232
public function countObjects()
@@ -101,7 +111,7 @@ public function readObjects()
101111
protected function getSubSelectQuery(): string
102112
{
103113
return $this->createSubSelectQuery(
104-
$this->langaugeID,
114+
$this->preferredLanguageID,
105115
LanguageFactory::getInstance()->getDefaultLanguageID()
106116
);
107117
}

wcfsetup/install/files/lib/data/user/rank/UserRank.class.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ class UserRank extends DatabaseObject implements ITitledObject
3232

3333
/**
3434
* @var array<int, string>
35+
*
36+
* @since 6.2
3537
*/
36-
protected array $content;
38+
protected array $titles;
3739

3840
/**
3941
* Returns the image of this user rank.
@@ -60,39 +62,47 @@ public function getImage()
6062
*/
6163
public function getTitle(): string
6264
{
63-
$this->loadContent();
65+
$this->loadTitles();
6466

65-
if ($this->content === []) {
67+
if ($this->titles === []) {
6668
// Backwards compatibility
6769
return WCF::getLanguage()->get($this->rankTitle);
6870
}
6971

70-
if ($this->isMultilingual === 0) {
71-
return \reset($this->content);
72-
} else {
73-
return $this->content[WCF::getLanguage()->languageID]
74-
?? $this->content[LanguageFactory::getInstance()->getDefaultLanguageID()]
75-
?? \reset($this->content);
76-
}
72+
return $this->titles[WCF::getLanguage()->languageID]
73+
?? $this->titles[LanguageFactory::getInstance()->getDefaultLanguageID()]
74+
?? \reset($this->titles);
7775
}
7876

79-
protected function loadContent(): void
77+
/**
78+
* @since 6.2
79+
*/
80+
protected function loadTitles(): void
8081
{
81-
if (isset($this->content)) {
82+
if (isset($this->titles)) {
8283
return;
8384
}
8485

8586
$sql = "SELECT languageID, title
8687
FROM wcf1_user_rank_content
8788
WHERE rankID = ?";
8889

89-
$statement = WCF::getDB()->prepare($sql, 1);
90+
$statement = WCF::getDB()->prepare($sql);
9091
$statement->execute([$this->rankID]);
9192

92-
$this->content = [];
93-
while ($row = $statement->fetchArray()) {
94-
$this->content[$row['languageID'] ?? 0] = $row['title'];
93+
$this->titles = $statement->fetchMap('languageID', 'title');
94+
}
95+
96+
/**
97+
* @since 6.2
98+
*/
99+
public function setRankTitle(int $languageID, string $title): void
100+
{
101+
if (!isset($this->titles)) {
102+
$this->titles = [];
95103
}
104+
105+
$this->titles[$languageID] = $title;
96106
}
97107

98108
/**

wcfsetup/install/files/lib/data/user/rank/UserRankList.class.php

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,80 @@
22

33
namespace wcf\data\user\rank;
44

5-
use wcf\data\DatabaseObjectDecorator;
6-
use wcf\data\DatabaseObjectList;
5+
use wcf\data\MultilingualContentList;
6+
use wcf\system\database\util\PreparedStatementConditionBuilder;
7+
use wcf\system\WCF;
78

89
/**
910
* Represents a list of user ranks.
1011
*
11-
* @author Marcel Werk
12-
* @copyright 2001-2019 WoltLab GmbH
12+
* @author Olaf Braun, Marcel Werk
13+
* @copyright 2001-2025 WoltLab GmbH
1314
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
1415
*
15-
* @template-covariant TDatabaseObject of UserRank|DatabaseObjectDecorator<UserRank> = UserRank
16-
* @extends DatabaseObjectList<TDatabaseObject>
16+
* @extends MultilingualContentList<UserRank>
1717
*/
18-
class UserRankList extends DatabaseObjectList
18+
class UserRankList extends MultilingualContentList
1919
{
20-
/**
21-
* @inheritDoc
22-
*/
23-
public $className = UserRank::class;
20+
public function __construct(?int $preferredLanguageID = null)
21+
{
22+
parent::__construct($preferredLanguageID);
23+
24+
$alias = $this->getDatabaseTableAlias();
25+
$this->sqlJoins = $this->sqlConditionJoins = "
26+
LEFT JOIN wcf1_user_rank_content userRankContent
27+
ON {$alias}.rankID = userRankContent.rankID
28+
AND {$alias}.languageID = userRankContent.languageID";
29+
$this->sqlSelects = "userRankContent.title";
30+
}
31+
32+
#[\Override]
33+
public function readObjects()
34+
{
35+
parent::readObjects();
36+
37+
if ($this->objectIDs !== []) {
38+
$this->loadRankTitles();
39+
}
40+
}
41+
42+
private function loadRankTitles(): void
43+
{
44+
$conditionBuilder = new PreparedStatementConditionBuilder();
45+
$conditionBuilder->add("rankID IN(?)", [$this->objectIDs]);
46+
47+
$sql = "SELECT *
48+
FROM wcf1_user_rank_content
49+
{$conditionBuilder}";
50+
$statement = WCF::getDB()->prepare($sql);
51+
$statement->execute($conditionBuilder->getParameters());
52+
53+
while ($row = $statement->fetchArray()) {
54+
$this->objects[$row['rankID']]->setRankTitle($row['languageID'], $row['title']);
55+
}
56+
}
57+
58+
#[\Override]
59+
protected function createSubSelectQuery(int $preferredLanguageID, int $defaultLanguageID): string
60+
{
61+
$alias = $this->getDatabaseTableAlias();
62+
$tableName = $this->getDatabaseTableName();
63+
64+
return <<<SQL
65+
(
66+
SELECT {$alias}.*, (
67+
SELECT languageID
68+
FROM wcf1_user_rank_content userRankContent
69+
WHERE userRankContent.rankID = {$alias}.rankID
70+
ORDER BY CASE
71+
WHEN languageID = {$preferredLanguageID} THEN -2
72+
WHEN languageID = {$defaultLanguageID} THEN -1
73+
ELSE languageID
74+
END ASC
75+
LIMIT 1
76+
) AS languageID
77+
FROM {$tableName} {$alias}
78+
)
79+
SQL;
80+
}
2481
}

wcfsetup/install/files/lib/data/user/rank/ViewableUserRank.class.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

wcfsetup/install/files/lib/data/user/rank/ViewableUserRankList.class.php

Lines changed: 0 additions & 68 deletions
This file was deleted.

wcfsetup/install/files/lib/system/gridView/admin/UserRankGridView.class.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use wcf\acp\form\UserRankEditForm;
66
use wcf\data\DatabaseObject;
77
use wcf\data\user\group\UserGroup;
8-
use wcf\data\user\rank\ViewableUserRank;
9-
use wcf\data\user\rank\ViewableUserRankList;
8+
use wcf\data\user\rank\UserRank;
9+
use wcf\data\user\rank\UserRankList;
1010
use wcf\event\gridView\admin\UserRankGridViewInitialized;
1111
use wcf\system\gridView\AbstractGridView;
1212
use wcf\system\gridView\filter\SelectFilter;
@@ -31,7 +31,7 @@
3131
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
3232
* @since 6.2
3333
*
34-
* @extends AbstractGridView<ViewableUserRank, ViewableUserRankList>
34+
* @extends AbstractGridView<UserRank, UserRankList>
3535
*/
3636
final class UserRankGridView extends AbstractGridView
3737
{
@@ -51,7 +51,7 @@ public function __construct()
5151
new class extends DefaultColumnRenderer {
5252
public function render(mixed $value, DatabaseObject $row): string
5353
{
54-
\assert($row instanceof ViewableUserRank);
54+
\assert($row instanceof UserRank);
5555

5656
return '<span class="badge label' . ($row->cssClassName ? ' ' . $row->cssClassName : '') . '">'
5757
. StringUtil::encodeHTML($row->getTitle())
@@ -66,7 +66,7 @@ public function render(mixed $value, DatabaseObject $row): string
6666
new class extends DefaultColumnRenderer {
6767
public function render(mixed $value, DatabaseObject $row): string
6868
{
69-
\assert($row instanceof ViewableUserRank);
69+
\assert($row instanceof UserRank);
7070

7171
return $row->rankImage ? $row->getImage() : '';
7272
}
@@ -127,9 +127,9 @@ public function isAccessible(): bool
127127
}
128128

129129
#[\Override]
130-
protected function createObjectList(): ViewableUserRankList
130+
protected function createObjectList(): UserRankList
131131
{
132-
return new ViewableUserRankList();
132+
return new UserRankList();
133133
}
134134

135135
#[\Override]

0 commit comments

Comments
 (0)