Skip to content

Commit 46bbcbc

Browse files
committed
Overhaul sorting of label groups in the admin panel
Previously, `showOrder` was used for sorting in the admin panel. However, `showOrder` was only intended for sorting in the frontend and could led to chaotic sorting in the admin panel when you have a larger number of label groups in your installation. Instead, alphabetical sorting is now used consistently and the description is always displayed to make the label groups easier to distinguish.
1 parent 46293fb commit 46bbcbc

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

wcfsetup/install/files/lib/acp/form/LabelAddForm.class.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ protected function createForm()
5454
{
5555
parent::createForm();
5656

57-
$labelGroupList = new LabelGroupList();
58-
$labelGroupList->readObjects();
59-
if ($labelGroupList->count() === 0) {
57+
$labelGroups = $this->getAvailableLabelGroups();
58+
if ($labelGroups === []) {
6059
throw new NamedUserException(
6160
HtmlString::fromSafeHtml(WCF::getLanguage()->getDynamicVariable('wcf.acp.label.error.noGroups'))
6261
);
@@ -67,7 +66,7 @@ protected function createForm()
6766
->appendChildren([
6867
SelectFormField::create('groupID')
6968
->label('wcf.acp.label.group')
70-
->options($labelGroupList)
69+
->options($labelGroups, labelLanguageItems: false)
7170
->immutable($this->formAction !== 'create')
7271
->description('wcf.acp.label.group.permanentSelection')
7372
->required(),
@@ -84,6 +83,24 @@ protected function createForm()
8483
]);
8584
}
8685

86+
/**
87+
* @return array<int, string>
88+
*/
89+
protected function getAvailableLabelGroups(): array
90+
{
91+
$labelGroupList = new LabelGroupList();
92+
$labelGroupList->readObjects();
93+
$labelGroups = \array_map(static fn($group) => $group->getExtendedTitle(), $labelGroupList->getObjects());
94+
95+
$collator = new \Collator(WCF::getLanguage()->getLocale());
96+
\uasort(
97+
$labelGroups,
98+
static fn(string $groupA, string $groupB) => $collator->compare($groupA, $groupB)
99+
);
100+
101+
return $labelGroups;
102+
}
103+
87104
protected function getShowOrderField(): IFormField
88105
{
89106
if ($this->formAction === 'create') {

wcfsetup/install/files/lib/data/label/group/LabelGroup.class.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ public function __toString(): string
3737
return $this->getTitle();
3838
}
3939

40+
/**
41+
* Returns the title and, if available, the description as a combined string.
42+
*
43+
* @since 6.2
44+
*/
45+
public function getExtendedTitle(): string
46+
{
47+
if (!$this->groupDescription) {
48+
return $this->getTitle();
49+
}
50+
51+
return \sprintf(
52+
"%s / %s",
53+
$this->getTitle(),
54+
$this->groupDescription
55+
);
56+
}
57+
4058
/**
4159
* Callback for uasort() to sort label groups by show order and (if equal) group id.
4260
*

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use wcf\acp\form\LabelEditForm;
66
use wcf\data\DatabaseObject;
7+
use wcf\data\label\group\LabelGroupList;
78
use wcf\data\label\group\ViewableLabelGroup;
89
use wcf\data\label\I18nLabelList;
910
use wcf\data\label\Label;
@@ -77,25 +78,16 @@ public function render(mixed $value, DatabaseObject $row): string
7778
$group = $groups[$row->groupID];
7879
\assert($group instanceof ViewableLabelGroup);
7980

80-
if (empty($group->groupDescription)) {
81-
return StringUtil::encodeHTML($group->getTitle());
82-
}
83-
return \sprintf(
84-
"%s / %s",
85-
StringUtil::encodeHTML($group->getTitle()),
86-
StringUtil::encodeHTML($group->groupDescription)
87-
);
81+
return StringUtil::encodeHTML($group->getExtendedTitle());
8882
}
8983
}
9084
)
9185
->filter(
9286
new SelectFilter(
93-
\array_map(
94-
static fn(ViewableLabelGroup $group) => $group->getTitle(),
95-
LabelCacheBuilder::getInstance()->getData(arrayIndex: "groups"),
96-
),
87+
$this->getAvailableLabelGroups(),
9788
'groupID',
98-
'wcf.acp.label.group'
89+
'wcf.acp.label.group',
90+
labelLanguageItems: false
9991
)
10092
)
10193
->sortable(),
@@ -135,4 +127,22 @@ protected function getInitializedEvent(): LabelGridViewInitialized
135127
{
136128
return new LabelGridViewInitialized($this);
137129
}
130+
131+
/**
132+
* @return array<int, string>
133+
*/
134+
protected function getAvailableLabelGroups(): array
135+
{
136+
$labelGroupList = new LabelGroupList();
137+
$labelGroupList->readObjects();
138+
$labelGroups = \array_map(static fn($group) => $group->getExtendedTitle(), $labelGroupList->getObjects());
139+
140+
$collator = new \Collator(WCF::getLanguage()->getLocale());
141+
\uasort(
142+
$labelGroups,
143+
static fn(string $groupA, string $groupB) => $collator->compare($groupA, $groupB)
144+
);
145+
146+
return $labelGroups;
147+
}
138148
}

0 commit comments

Comments
 (0)