Skip to content

Commit

Permalink
feat: Migrate Node::status to NodeStatus enum, reduced int to smallin…
Browse files Browse the repository at this point in the history
…t when possible
  • Loading branch information
roadiz-ci committed Dec 4, 2024
1 parent c87489f commit 75b346e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/AjaxControllers/AjaxNodesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ protected function changeNodeStatus(Node $node, string $transition): JsonRespons
$this->em()->flush();
$msg = $this->getTranslator()->trans('node.%name%.status_changed_to.%status%', [
'%name%' => $node->getNodeName(),
'%status%' => $this->getTranslator()->trans(Node::getStatusLabel($node->getStatus())),
'%status%' => $node->getStatus()->trans($this->getTranslator()),
]);
$this->publishConfirmMessage($request, $msg, $node->getNodeSources()->first() ?: $node);

Expand Down
3 changes: 2 additions & 1 deletion src/AjaxControllers/AjaxNodesExplorerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\Tag;
use RZ\Roadiz\CoreBundle\EntityApi\NodeTypeApi;
use RZ\Roadiz\CoreBundle\Enum\NodeStatus;
use RZ\Roadiz\CoreBundle\Explorer\AbstractExplorerItem;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemFactoryInterface;
use RZ\Roadiz\CoreBundle\SearchEngine\ClientRegistry;
Expand Down Expand Up @@ -72,7 +73,7 @@ public function indexAction(Request $request): Response
protected function parseFilterFromRequest(Request $request): array
{
$arrayFilter = [
'status' => ['<=', Node::ARCHIVED],
'status' => ['<=', NodeStatus::ARCHIVED],
];

if ($request->query->has('tagId') && $request->get('tagId') > 0) {
Expand Down
11 changes: 6 additions & 5 deletions src/Controllers/Nodes/NodesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\CoreBundle\Entity\User;
use RZ\Roadiz\CoreBundle\EntityHandler\NodeHandler;
use RZ\Roadiz\CoreBundle\Enum\NodeStatus;
use RZ\Roadiz\CoreBundle\Event\Node\NodeCreatedEvent;
use RZ\Roadiz\CoreBundle\Event\Node\NodeDeletedEvent;
use RZ\Roadiz\CoreBundle\Event\Node\NodePathChangedEvent;
Expand Down Expand Up @@ -78,25 +79,25 @@ public function indexAction(Request $request, ?string $filter = null): Response
case 'draft':
$this->assignation['mainFilter'] = $filter;
$arrayFilter = [
'status' => Node::DRAFT,
'status' => NodeStatus::DRAFT,
];
break;
case 'pending':
$this->assignation['mainFilter'] = $filter;
$arrayFilter = [
'status' => Node::PENDING,
'status' => NodeStatus::PENDING,
];
break;
case 'archived':
$this->assignation['mainFilter'] = $filter;
$arrayFilter = [
'status' => Node::ARCHIVED,
'status' => NodeStatus::ARCHIVED,
];
break;
case 'deleted':
$this->assignation['mainFilter'] = $filter;
$arrayFilter = [
'status' => Node::DELETED,
'status' => NodeStatus::DELETED,
];
break;
default:
Expand Down Expand Up @@ -526,7 +527,7 @@ public function emptyTrashAction(Request $request): Response
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$criteria = ['status' => Node::DELETED];
$criteria = ['status' => NodeStatus::DELETED];
/** @var Node|null $chroot */
$chroot = $this->nodeChrootResolver->getChroot($this->getUser());
if (null !== $chroot) {
Expand Down
9 changes: 5 additions & 4 deletions src/Controllers/Nodes/NodesTreesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use RZ\Roadiz\CoreBundle\Entity\Tag;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\CoreBundle\EntityHandler\NodeHandler;
use RZ\Roadiz\CoreBundle\Enum\NodeStatus;
use RZ\Roadiz\CoreBundle\Security\Authorization\Chroot\NodeChrootResolver;
use RZ\Roadiz\CoreBundle\Security\Authorization\Voter\NodeVoter;
use Symfony\Component\Form\ClickableInterface;
Expand Down Expand Up @@ -500,10 +501,10 @@ private function buildBulkStatusForm(
'label' => false,
'data' => $status,
'choices' => [
Node::getStatusLabel(Node::DRAFT) => 'reject',
Node::getStatusLabel(Node::PENDING) => 'review',
Node::getStatusLabel(Node::PUBLISHED) => 'publish',
Node::getStatusLabel(Node::ARCHIVED) => 'archive',
NodeStatus::DRAFT->getLabel() => 'reject',
NodeStatus::PENDING->getLabel() => 'review',
NodeStatus::PUBLISHED->getLabel() => 'publish',
NodeStatus::ARCHIVED->getLabel() => 'archive',
],
'constraints' => [
new NotNull(),
Expand Down
10 changes: 2 additions & 8 deletions src/Forms/Node/AddNodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use Doctrine\Persistence\ManagerRegistry;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Form\DataTransformer\NodeTypeTransformer;
use RZ\Roadiz\CoreBundle\Form\NodeStatesType;
use RZ\Roadiz\CoreBundle\Form\NodeTypesType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Event\SubmitEvent;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
Expand Down Expand Up @@ -74,15 +74,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'hiding-children',
'required' => false,
])
->add('status', ChoiceType::class, [
->add('status', NodeStatesType::class, [
'label' => 'node.status',
'required' => true,
'choices' => [
Node::getStatusLabel(Node::DRAFT) => Node::DRAFT,
Node::getStatusLabel(Node::PENDING) => Node::PENDING,
Node::getStatusLabel(Node::PUBLISHED) => Node::PUBLISHED,
Node::getStatusLabel(Node::ARCHIVED) => Node::ARCHIVED,
],
]);

$builder->addEventListener(FormEvents::SUBMIT, function (SubmitEvent $event) {
Expand Down
9 changes: 2 additions & 7 deletions src/Forms/NodeTreeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\NodeType;
use RZ\Roadiz\CoreBundle\Entity\NodeTypeField;
use RZ\Roadiz\CoreBundle\Enum\NodeStatus;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormInterface;
Expand Down Expand Up @@ -89,13 +90,7 @@ public function finishView(FormView $view, FormInterface $form, array $options):
}

$view->vars['nodeTree'] = $nodeTree;
$view->vars['nodeStatuses'] = [
Node::getStatusLabel(Node::DRAFT) => Node::DRAFT,
Node::getStatusLabel(Node::PENDING) => Node::PENDING,
Node::getStatusLabel(Node::PUBLISHED) => Node::PUBLISHED,
Node::getStatusLabel(Node::ARCHIVED) => Node::ARCHIVED,
Node::getStatusLabel(Node::DELETED) => Node::DELETED,
];
$view->vars['nodeStatuses'] = NodeStatus::allLabelsAndValues();
}

public function getParent(): ?string
Expand Down

0 comments on commit 75b346e

Please sign in to comment.