Skip to content

Commit 42ef713

Browse files
authored
Merge pull request #6595 from WoltLab/63-sort-labels-alphabetical
Add option to sort labels automatically in alphabetical order
2 parents a606758 + 44e8b0b commit 42ef713

13 files changed

Lines changed: 83 additions & 7 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* Updates the database layout during the update from 6.2 to 6.3.
5+
*
6+
* @author Marcel Werk
7+
* @copyright 2001-2026 WoltLab GmbH
8+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
9+
*/
10+
11+
use wcf\system\database\table\column\DefaultFalseBooleanDatabaseTableColumn;
12+
use wcf\system\database\table\PartialDatabaseTable;
13+
14+
return [
15+
PartialDatabaseTable::create('wcf1_label_group')
16+
->columns([
17+
DefaultFalseBooleanDatabaseTableColumn::create('sortAlphabetically')
18+
]),
19+
];

wcfsetup/install/files/acp/templates/labelGroupAdd.tpl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
<nav class="contentHeaderNavigation">
2424
<ul>
2525
{if $action == 'edit'}
26-
<li><button type="button" class="button jsChangeShowOrder">{icon name='up-down'} <span>{lang}wcf.global.changeShowOrder{/lang}</span></button></li>
26+
{if !$labelGroup->sortAlphabetically}
27+
<li><button type="button" class="button jsChangeShowOrder">{icon name='up-down'} <span>{lang}wcf.global.changeShowOrder{/lang}</span></button></li>
28+
{/if}
2729
<li>
2830
{unsafe:$interactionContextMenu->render()}
2931
</li>
@@ -84,7 +86,12 @@
8486
<dt></dt>
8587
<dd><label><input type="checkbox" name="forceSelection" id="forceSelection" value="1"{if $labelForceSelection} checked{/if}> {lang}wcf.acp.label.group.forceSelection{/lang}</label></dd>
8688
</dl>
87-
89+
90+
<dl>
91+
<dt></dt>
92+
<dd><label><input type="checkbox" name="sortAlphabetically" id="sortAlphabetically" value="1"{if $sortAlphabetically} checked{/if}> {lang}wcf.acp.label.group.sortAlphabetically{/lang}</label></dd>
93+
</dl>
94+
8895
<dl id="groupPermissions">
8996
<dt>{lang}wcf.acl.permissions{/lang}</dt>
9097
<dd></dd>
@@ -125,7 +132,7 @@
125132
</div>
126133
</form>
127134

128-
{if $action == 'edit'}
135+
{if $action == 'edit' && !$labelGroup->sortAlphabetically}
129136
<script data-relocate="true">
130137
require(["WoltLabSuite/Core/Component/ChangeShowOrder"], ({ setup }) => {
131138
{jsphrase name='wcf.global.changeShowOrder'}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class LabelGroupAddForm extends AbstractForm
4040
*/
4141
public $forceSelection = false;
4242

43+
public bool $sortAlphabetically = false;
44+
4345
/**
4446
* group name
4547
* @var string
@@ -113,6 +115,9 @@ public function readFormParameters()
113115
if (isset($_POST['forceSelection'])) {
114116
$this->forceSelection = true;
115117
}
118+
if (isset($_POST['sortAlphabetically'])) {
119+
$this->sortAlphabetically = true;
120+
}
116121
if (isset($_POST['objectTypes']) && \is_array($_POST['objectTypes'])) {
117122
$this->objectTypes = $_POST['objectTypes'];
118123
}
@@ -179,6 +184,7 @@ public function save()
179184
$this->objectAction = new LabelGroupAction([], 'create', [
180185
'data' => \array_merge($this->additionalFields, [
181186
'forceSelection' => $this->forceSelection ? 1 : 0,
187+
'sortAlphabetically' => $this->sortAlphabetically ? 1 : 0,
182188
'groupName' => $this->groupName,
183189
'groupDescription' => $this->groupDescription,
184190
'showOrder' => $this->showOrder,
@@ -216,6 +222,7 @@ public function save()
216222

217223
// reset values
218224
$this->forceSelection = false;
225+
$this->sortAlphabetically = false;
219226
$this->groupName = $this->groupDescription = '';
220227
$this->objectTypes = [];
221228
$this->showOrder = 0;
@@ -246,6 +253,7 @@ public function assignVariables()
246253
WCF::getTPL()->assign([
247254
'action' => 'add',
248255
'forceSelection' => $this->forceSelection,
256+
'sortAlphabetically' => $this->sortAlphabetically,
249257
'groupName' => $this->groupName,
250258
'groupDescription' => $this->groupDescription,
251259
'labelObjectTypeContainers' => $this->labelObjectTypeContainers,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function save()
8383
[
8484
'data' => \array_merge($this->additionalFields, [
8585
'forceSelection' => $this->forceSelection ? 1 : 0,
86+
'sortAlphabetically' => $this->sortAlphabetically ? 1 : 0,
8687
'groupName' => $this->groupName,
8788
'groupDescription' => $this->groupDescription,
8889
'showOrder' => $this->showOrder,
@@ -119,6 +120,7 @@ public function readData()
119120
I18nHandler::getInstance()->setOptions('groupName', 1, $this->group->groupName, 'wcf.acp.label.group\d+');
120121

121122
$this->forceSelection = ($this->group->forceSelection ? true : false);
123+
$this->sortAlphabetically = ($this->group->sortAlphabetically ? true : false);
122124
$this->groupName = $this->group->groupName;
123125
$this->groupDescription = $this->group->groupDescription;
124126
$this->showOrder = $this->group->showOrder;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* @property-read string $groupName name of the label group or name of language item which contains the label text
1818
* @property-read string $groupDescription description of the label group (only shown in ACP)
1919
* @property-read 1|0 $forceSelection is `1` if a label in the label group has to be selected when creating an object for which the label group is available, otherwise `0`
20+
* @property-read 1|0 $sortAlphabetically is `1` if labels in the label group are sorted alphabetically by their translated name, otherwise `0`
2021
* @property-read int $showOrder position of the label group in relation to the other label groups
2122
*/
2223
class LabelGroup extends DatabaseObject implements IRouteController

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,21 @@ public function search($objectID)
263263
}
264264
}
265265

266+
/**
267+
* Sorts the labels alphabetically by their translated name using locale-aware comparison.
268+
*
269+
* @since 6.3
270+
*/
271+
public function sortLabelsAlphabetically(): void
272+
{
273+
$collator = new \Collator(WCF::getLanguage()->getLocale());
274+
\uasort(
275+
$this->labels,
276+
static fn(Label $a, Label $b) => $collator->compare($a->getTitle(), $b->getTitle())
277+
);
278+
$this->indexToObject = \array_values(\array_keys($this->labels));
279+
}
280+
266281
/**
267282
* Returns true if any permissions have been set for this label group.
268283
*

wcfsetup/install/files/lib/system/cache/builder/LabelCacheBuilder.class.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ protected function rebuild(array $parameters)
4747
$data['options'] = $permissions['options']->getObjects();
4848

4949
// assign permissions for each label group
50-
/** @var ViewableLabelGroup $group */
5150
foreach ($data['groups'] as $groupID => $group) {
51+
\assert($group instanceof ViewableLabelGroup);
52+
5253
// group permissions
5354
if (isset($permissions['group'][$groupID])) {
5455
$group->setGroupPermissions($permissions['group'][$groupID]);

wcfsetup/install/files/lib/system/endpoint/controller/core/labels/groups/ChangeLabelShowOrder.class.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use wcf\http\Helper;
1212
use wcf\system\endpoint\IController;
1313
use wcf\system\endpoint\PostRequest;
14+
use wcf\system\exception\IllegalLinkException;
1415
use wcf\system\label\LabelHandler;
1516
use wcf\system\showOrder\ShowOrderHandler;
1617
use wcf\system\showOrder\ShowOrderItem;
@@ -32,6 +33,10 @@ public function __invoke(ServerRequestInterface $request, array $variables): Res
3233
WCF::getSession()->checkPermissions(['admin.content.label.canManageLabel']);
3334

3435
$labelGroup = Helper::fetchObjectFromRequestParameter($variables['id'], LabelGroup::class);
36+
if ($labelGroup->sortAlphabetically) {
37+
throw new IllegalLinkException();
38+
}
39+
3540
$items = \array_map(
3641
static fn(Label $label) => new ShowOrderItem($label->labelID, $label->getTitle()),
3742
LabelHandler::getInstance()->getLabelGroup($labelGroup->groupID)->getLabels()

wcfsetup/install/files/lib/system/endpoint/controller/core/labels/groups/GetLabelShowOrder.class.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use wcf\http\Helper;
1010
use wcf\system\endpoint\GetRequest;
1111
use wcf\system\endpoint\IController;
12+
use wcf\system\exception\IllegalLinkException;
1213
use wcf\system\label\LabelHandler;
1314
use wcf\system\showOrder\ShowOrderHandler;
1415
use wcf\system\showOrder\ShowOrderItem;
@@ -30,6 +31,10 @@ public function __invoke(ServerRequestInterface $request, array $variables): Res
3031
WCF::getSession()->checkPermissions(['admin.content.label.canManageLabel']);
3132

3233
$labelGroup = Helper::fetchObjectFromRequestParameter($variables['id'], LabelGroup::class);
34+
if ($labelGroup->sortAlphabetically) {
35+
throw new IllegalLinkException();
36+
}
37+
3338
$items = \array_map(
3439
static fn(Label $label) => new ShowOrderItem($label->labelID, $label->getTitle()),
3540
LabelHandler::getInstance()->getLabelGroup($labelGroup->groupID)->getLabels()

wcfsetup/install/files/lib/system/label/LabelHandler.class.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public function getLabelGroups(array $groupIDs = [], $validatePermissions = true
381381
}
382382
}
383383

384-
$data[$groupID] = $this->labelGroups['groups'][$groupID];
384+
$data[$groupID] = $this->getLabelGroup($groupID);
385385
}
386386

387387
// @phpstan-ignore argument.type
@@ -413,9 +413,19 @@ public function getAccessibleLabelIDs()
413413
* @param int $groupID
414414
* @return ViewableLabelGroup|null
415415
*/
416-
public function getLabelGroup($groupID)
416+
public function getLabelGroup($groupID): ?ViewableLabelGroup
417417
{
418-
return $this->labelGroups['groups'][$groupID] ?? null;
418+
if (!isset($this->labelGroups['groups'][$groupID])) {
419+
return null;
420+
}
421+
422+
$labelGroup = $this->labelGroups['groups'][$groupID];
423+
424+
if ($labelGroup->sortAlphabetically) {
425+
$labelGroup->sortLabelsAlphabetically();
426+
}
427+
428+
return $labelGroup;
419429
}
420430

421431
/**

0 commit comments

Comments
 (0)