Newer
Older
activity-manager / src / Form / Task / TaskAddForm.php
<?php
namespace App\Form\Task;

use App\Entity\ActorEntity;
use App\Entity\TaskEntity;
use App\Enum\StatusEnum;
use App\Form\AbstractFormManager;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class TaskAddForm extends AbstractFormManager
{

    /** @var array List of actors */
    protected $actors = [];

    /**
     * {@inheritdoc}
     */
    public function __construct(FormBuilderInterface $formBuilder, array $data = [], bool $usesToken = true)
    {
        $this->actors = $data['actors'];
        parent::__construct($formBuilder, $data, $usesToken);
    }

    /**
     * Create and return taks entity from data
     *
     * @return TaskEntity
     */
    public function getTask(): TaskEntity
    {
        $data = $this->form->getData();
        $task = new TaskEntity();
        $task->generateId();
        $task->setName($data['name']);
        $task->setStatus($data['status']);
        $task->setActor($data['actor']);
        $task->setPlanifiedDate($data['planifiedDate']);
        $task->setExecutionDate($data['executionDate']);

        return $task;
    }

    /**
     * {@inheritdoc}
     */
    public function getTemplate(): string
    {
        return '_includes/html/form/task/add.html.twig';
    }

    /**
     * {@inheritdoc}
     */
    protected function addFields($formBuilder, $options): void
    {
        $formBuilder->add('name', TextType::class, [
            'required' => true
        ])
            ->add('actor', EntityType::class, [
            'class' => ActorEntity::class,
            'choices' => $this->actors,
            'group_by' => function (ActorEntity $choice) {
                if ($choice->getFollowed()) {
                    return 'Suivi';
                }
                if ($choice->getActive()) {
                    return 'Actif';
                }

                return 'Inactif';
            },
            'choice_label' => 'displayName',
            'required' => false,
        ])
            ->add('status', ChoiceType::class, [
            'choices' => TaskEntity::VALID_STATUS,
            'choice_label' => function ($choice, $key, $value) {
                return StatusEnum::STATUS_NAME[$choice];
            },
        ])
            ->add('planifiedDate', DateType::class, [
            'required' => false,
            'widget' => 'single_text',
        ])
            ->add('executionDate', DateType::class, [
            'required' => false,
            'widget' => 'single_text',
        ])
            ->add('submit', SubmitType::class);
    }
}