|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace wcf\system\endpoint\controller\core\attachments; |
| 4 | + |
| 5 | +use Laminas\Diactoros\Response\JsonResponse; |
| 6 | +use Psr\Http\Message\ResponseInterface; |
| 7 | +use Psr\Http\Message\ServerRequestInterface; |
| 8 | +use wcf\http\Helper; |
| 9 | +use wcf\system\attachment\AttachmentHandler; |
| 10 | +use wcf\system\endpoint\IController; |
| 11 | +use wcf\system\endpoint\PostRequest; |
| 12 | +use wcf\system\exception\PermissionDeniedException; |
| 13 | +use wcf\system\WCF; |
| 14 | + |
| 15 | +/** |
| 16 | + * API endpoint for changing the show order of attachments. |
| 17 | + * |
| 18 | + * @author Olaf Braun |
| 19 | + * @copyright 2001-2025 WoltLab GmbH |
| 20 | + * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 21 | + * @since 6.2 |
| 22 | + */ |
| 23 | +#[PostRequest('/core/attachments/show-order')] |
| 24 | +final class ChangeShowOrder implements IController |
| 25 | +{ |
| 26 | + #[\Override] |
| 27 | + public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface |
| 28 | + { |
| 29 | + $parameters = Helper::mapApiParameters($request, ChangeShowOrderParameters::class); |
| 30 | + |
| 31 | + $attachmentHandler = new AttachmentHandler( |
| 32 | + $parameters->objectType, |
| 33 | + $parameters->objectID, |
| 34 | + $parameters->tmpHash, |
| 35 | + $parameters->parentObjectID, |
| 36 | + ); |
| 37 | + |
| 38 | + $this->assertAttachmentsCanBeSorted($attachmentHandler, $parameters->attachmentIDs); |
| 39 | + |
| 40 | + $this->saveShowOrder($parameters->attachmentIDs); |
| 41 | + |
| 42 | + return new JsonResponse([]); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param list<int> $attachmentIDs |
| 47 | + */ |
| 48 | + private function assertAttachmentsCanBeSorted(AttachmentHandler $attachmentHandler, array $attachmentIDs): void |
| 49 | + { |
| 50 | + if (!$attachmentHandler->canUpload()) { |
| 51 | + throw new PermissionDeniedException(); |
| 52 | + } |
| 53 | + |
| 54 | + $attachmentList = $attachmentHandler->getAttachmentList(); |
| 55 | + foreach ($attachmentIDs as $attachmentID) { |
| 56 | + if (!\in_array($attachmentID, $attachmentList->getObjectIDs(), true)) { |
| 57 | + throw new PermissionDeniedException(); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param list<int> $attachmentIDs |
| 64 | + */ |
| 65 | + private function saveShowOrder(array $attachmentIDs): void |
| 66 | + { |
| 67 | + WCF::getDB()->beginTransaction(); |
| 68 | + |
| 69 | + $sql = "UPDATE wcf1_attachment |
| 70 | + SET showOrder = ? |
| 71 | + WHERE attachmentID = ?"; |
| 72 | + $statement = WCF::getDB()->prepare($sql); |
| 73 | + |
| 74 | + foreach ($attachmentIDs as $showOrder => $attachmentID) { |
| 75 | + $statement->execute([ |
| 76 | + $showOrder + 1, |
| 77 | + $attachmentID, |
| 78 | + ]); |
| 79 | + } |
| 80 | + |
| 81 | + WCF::getDB()->commitTransaction(); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** @internal */ |
| 86 | +final class ChangeShowOrderParameters |
| 87 | +{ |
| 88 | + public function __construct( |
| 89 | + /** @var non-empty-string */ |
| 90 | + public readonly string $objectType, |
| 91 | + /** @var non-negative-int */ |
| 92 | + public readonly int $objectID, |
| 93 | + /** @var non-negative-int */ |
| 94 | + public readonly int $parentObjectID, |
| 95 | + public readonly string $tmpHash, |
| 96 | + /** @var list<positive-int> */ |
| 97 | + public readonly array $attachmentIDs, |
| 98 | + ) { |
| 99 | + } |
| 100 | +} |
0 commit comments