Improve ACP forum management UI
This commit is contained in:
47
api/src/State/ForumPositionProcessor.php
Normal file
47
api/src/State/ForumPositionProcessor.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Entity\Forum;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
|
||||
class ForumPositionProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $entityManager,
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
||||
private ProcessorInterface $persistProcessor
|
||||
) {
|
||||
}
|
||||
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
||||
{
|
||||
if (!$data instanceof Forum) {
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
$previous = $context['previous_data'] ?? null;
|
||||
$parentChanged = $previous instanceof Forum && $previous->getParent()?->getId() !== $data->getParent()?->getId();
|
||||
|
||||
if ($data->getPosition() === 0 || $parentChanged) {
|
||||
$qb = $this->entityManager->createQueryBuilder();
|
||||
$qb->select('COALESCE(MAX(f.position), 0)')
|
||||
->from(Forum::class, 'f');
|
||||
|
||||
if ($data->getParent()) {
|
||||
$qb->andWhere('f.parent = :parent')
|
||||
->setParameter('parent', $data->getParent());
|
||||
} else {
|
||||
$qb->andWhere('f.parent IS NULL');
|
||||
}
|
||||
|
||||
$maxPosition = (int) $qb->getQuery()->getSingleScalarResult();
|
||||
$data->setPosition($maxPosition + 1);
|
||||
}
|
||||
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user