Use a DTO to create or edit a Motive

This commit is contained in:
2025-10-22 11:16:26 +02:00
parent e8450a519d
commit 19785795ad
7 changed files with 107 additions and 16 deletions

View File

@@ -0,0 +1,6 @@
kind: Feature
body: Add an admin interface for Motive entity
time: 2025-10-22T11:15:52.13937955+02:00
custom:
Issue: ""
SchemaChange: Add columns or tables

View File

@@ -13,9 +13,11 @@ namespace Chill\TicketBundle\Controller\Admin;
use Chill\MainBundle\CRUD\Controller\CRUDController;
use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\TicketBundle\Entity\Motive;
use Chill\TicketBundle\MotiveDTO;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class MotiveController extends CRUDController
{
@@ -26,4 +28,27 @@ class MotiveController extends CRUDController
return parent::orderQuery($action, $query, $request, $paginator);
}
protected function createFormFor(string $action, mixed $entity, ?string $formClass = null, array $formOptions = []): FormInterface
{
if (in_array($action, ['new', 'edit'], true) && $entity instanceof Motive) {
$dto = MotiveDTO::fromMotive($entity);
return parent::createFormFor($action, $dto, $formClass, $formOptions);
}
return parent::createFormFor($action, $entity, $formClass, $formOptions);
}
protected function onFormValid(string $action, object $entity, FormInterface $form, Request $request): void
{
if (in_array($action, ['new', 'edit'], true) && $entity instanceof Motive) {
$dto = $form->getData();
if ($dto instanceof MotiveDTO) {
$dto->applyToMotive($entity);
}
}
parent::onFormValid($action, $entity, $form, $request);
}
}

View File

@@ -220,6 +220,7 @@ class Motive
return $collection;
}
public function getOrdering(): float
{
return $this->ordering;

View File

@@ -12,13 +12,11 @@ declare(strict_types=1);
namespace Chill\TicketBundle\Form;
use Chill\MainBundle\Form\Type\ChillCollectionType;
use Chill\MainBundle\Form\Type\CommentType;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Chill\TicketBundle\Entity\EmergencyStatusEnum;
use Chill\TicketBundle\Entity\Motive;
use Chill\TicketBundle\MotiveDTO;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
@@ -56,7 +54,7 @@ class MotiveType extends AbstractType
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Motive::class,
'data_class' => MotiveDTO::class,
]);
}

View File

@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\TicketBundle;
use Chill\TicketBundle\Entity\EmergencyStatusEnum;
use Chill\TicketBundle\Entity\Motive;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
class MotiveDTO
{
public function __construct(
#[Assert\NotBlank()]
public array $label = [],
public bool $active = true,
public Collection $supplementaryComments = new ArrayCollection(),
public ?EmergencyStatusEnum $makeTicketEmergency = null,
) {
if ($this->supplementaryComments->isEmpty()) {
$this->supplementaryComments = new ArrayCollection();
}
}
public static function fromMotive(Motive $motive): self
{
$supplementaryCommentsCollection = new ArrayCollection();
foreach ($motive->getSupplementaryComments() as $comment) {
$supplementaryCommentsCollection->add($comment['label']);
}
return new self(
label: $motive->getLabel(),
active: $motive->isActive(),
supplementaryComments: $supplementaryCommentsCollection,
makeTicketEmergency: $motive->getMakeTicketEmergency(),
);
}
public function applyToMotive(Motive $motive): void
{
$motive->setLabel($this->label);
$motive->setActive($this->active);
$motive->setMakeTicketEmergency($this->makeTicketEmergency);
$supplementaryCommentsArray = [];
foreach ($this->supplementaryComments as $supplementaryComment) {
$supplementaryCommentsArray[] = ['label' => $supplementaryComment];
}
$motive->setSupplementaryComment($supplementaryCommentsArray);
}
}

View File

@@ -46,18 +46,8 @@
<div class="card mb-3">
<div class="card-body">
<div class="comment-content">
{{ comment.comment|raw }}
{{ comment.label|raw }}
</div>
{% if comment.date %}
<div class="comment-meta text-muted mt-2">
<small>
{{ 'Date'|trans }}: {{ comment.date|date('d/m/Y H:i') }}
{% if comment.userId %}
- {{ 'User'|trans }}: {{ comment.userId }}
{% endif %}
</small>
</div>
{% endif %}
</div>
</div>
{% endfor %}

View File

@@ -2,6 +2,13 @@
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\Migrations\Ticket;
use Doctrine\DBAL\Schema\Schema;