Skip to content

Commit 19ad003

Browse files
authored
Merge pull request #561 from WoltLab/6.2-show-order
Document show order API
2 parents dcd4121 + 93fe63f commit 19ad003

2 files changed

Lines changed: 129 additions & 0 deletions

File tree

docs/php/api/show_order.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
2+
# Show Order
3+
4+
Based on the generic show order implementation, users can be given the option of reordering entries using drag & drop.
5+
6+
## Backend Implementation
7+
8+
The backend must provide two RPC endpoints.
9+
10+
The first endpoint returns the list of entries according to the current show order:
11+
12+
```php
13+
<?php
14+
15+
namespace wcf\system\endpoint\controller\foo;
16+
17+
use Psr\Http\Message\ResponseInterface;
18+
use Psr\Http\Message\ServerRequestInterface;
19+
use wcf\system\endpoint\GetRequest;
20+
use wcf\system\endpoint\IController;
21+
use wcf\system\exception\IllegalLinkException;
22+
use wcf\system\showOrder\ShowOrderHandler;
23+
use wcf\system\showOrder\ShowOrderItem;
24+
use wcf\system\WCF;
25+
26+
#[GetRequest('/your-endpoint/show-order')]
27+
final class GetShowOrder implements IController
28+
{
29+
public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
30+
{
31+
WCF::getSession()->checkPermissions(["admin.foo.canManageFoo"]);
32+
33+
$list = new FooList();
34+
$list->sqlOrderBy = 'showOrder ASC';
35+
$list->readObjects();
36+
37+
$items = \array_map(
38+
static fn($object) => new ShowOrderItem(
39+
$object->getObjectID(),
40+
$object->getName()
41+
),
42+
$list->getObjects()
43+
);
44+
45+
return (new ShowOrderHandler($items))->toJsonResponse();
46+
}
47+
}
48+
```
49+
50+
The second endpoint accepts the new show order and saves it.
51+
52+
```php
53+
<?php
54+
55+
namespace wcf\system\endpoint\controller\foo;
56+
57+
use Laminas\Diactoros\Response\JsonResponse;
58+
use Psr\Http\Message\ResponseInterface;
59+
use Psr\Http\Message\ServerRequestInterface;
60+
use wcf\system\endpoint\IController;
61+
use wcf\system\endpoint\PostRequest;
62+
use wcf\system\exception\IllegalLinkException;
63+
use wcf\system\showOrder\ShowOrderHandler;
64+
use wcf\system\showOrder\ShowOrderItem;
65+
use wcf\system\WCF;
66+
67+
#[PostRequest('/your-endpoint/show-order')]
68+
final class ChangeShowOrder implements IController
69+
{
70+
public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
71+
{
72+
WCF::getSession()->checkPermissions(["admin.foo.canManageFoo"]);
73+
74+
$list = new FooList();
75+
$list->sqlOrderBy = 'showOrder ASC';
76+
$list->readObjects();
77+
78+
$items = \array_map(
79+
static fn($object) => new ShowOrderItem(
80+
$object->getObjectID(),
81+
$object->getName()
82+
),
83+
$list->getObjects()
84+
);
85+
86+
$sortedItems = (new ShowOrderHandler($items))->getSortedItemsFromRequest($request);
87+
$this->saveShowOrder($sortedItems);
88+
89+
return new JsonResponse([]);
90+
}
91+
92+
/**
93+
* @param list<ShowOrderItem> $items
94+
*/
95+
private function saveShowOrder(array $items): void
96+
{
97+
WCF::getDB()->beginTransaction();
98+
$sql = "UPDATE wcf1_foo
99+
SET showOrder = ?
100+
WHERE id = ?";
101+
$statement = WCF::getDB()->prepare($sql);
102+
for ($i = 0, $length = \count($items); $i < $length; $i++) {
103+
$statement->execute([
104+
$i + 1,
105+
$items[$i]->id,
106+
]);
107+
}
108+
WCF::getDB()->commitTransaction();
109+
}
110+
}
111+
```
112+
113+
## Frontend Implementation
114+
115+
```smarty
116+
<button type="button" class="button jsChangeShowOrder">{icon name='up-down'} <span>{lang}wcf.global.changeShowOrder{/lang}</span></button>
117+
118+
<script data-relocate="true">
119+
require(["WoltLabSuite/Core/Component/ChangeShowOrder"], ({ setup }) => {
120+
{jsphrase name='wcf.global.changeShowOrder'}
121+
122+
setup(
123+
document.querySelector('.jsChangeShowOrder'),
124+
'your-endpoint/show-order',
125+
);
126+
});
127+
</script>
128+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ nav:
6161
- 'Interactions': 'php/api/interactions.md'
6262
- 'List Views': 'php/api/list_views.md'
6363
- 'Package Installation Plugins': 'php/api/package_installation_plugins.md'
64+
- 'Show Order': 'php/api/show_order.md'
6465
- 'RPC API':
6566
- 'Overview': 'php/api/rpc_api.md'
6667
- 'Endpoints': 'php/api/rpc_api_endpoints.md'

0 commit comments

Comments
 (0)