0
0
Fork 0
mirror of https://github.com/kevinpapst/kimai2.git synced 2025-05-12 12:42:00 +00:00

additional form types for simple usage in SystemConfiguration and UserPreferences

This commit is contained in:
Kevin Papst 2025-02-13 15:22:30 +01:00
parent 56c14b9935
commit c78b392668
6 changed files with 198 additions and 0 deletions

View file

@ -0,0 +1,38 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form\DataTransformer;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\DataTransformerInterface;
final class EntityByIdTransformer implements DataTransformerInterface // @phpstan-ignore missingType.generics
{
public function __construct(private readonly EntityRepository $repository) // @phpstan-ignore missingType.generics
{
}
public function transform(mixed $value): mixed
{
if (is_numeric($value)) {
return $this->repository->find($value);
}
return $value;
}
public function reverseTransform(mixed $value): mixed
{
if (\is_object($value) && method_exists($value, 'getId') && $value->getId() !== null) {
return (string) $value->getId();
}
return $value;
}
}

View file

@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form\Type;
use App\Form\DataTransformer\EntityByIdTransformer;
use App\Repository\ActivityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
final class ActivityByIdType extends AbstractType
{
public function __construct(private readonly ActivityRepository $repository)
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addModelTransformer(new EntityByIdTransformer($this->repository));
}
public function getParent(): string
{
return ActivityType::class;
}
}

View file

@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form\Type;
use App\Form\DataTransformer\EntityByIdTransformer;
use App\Repository\CustomerRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
final class CustomerByIdType extends AbstractType
{
public function __construct(private readonly CustomerRepository $repository)
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addModelTransformer(new EntityByIdTransformer($this->repository));
}
public function getParent(): string
{
return CustomerType::class;
}
}

View file

@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form\Type;
use App\Form\DataTransformer\EntityByIdTransformer;
use App\Repository\ProjectRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
final class ProjectByIdType extends AbstractType
{
public function __construct(private readonly ProjectRepository $repository)
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addModelTransformer(new EntityByIdTransformer($this->repository));
}
public function getParent(): string
{
return ProjectType::class;
}
}

View file

@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form\Type;
use App\Form\DataTransformer\EntityByIdTransformer;
use App\Repository\TeamRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
final class TeamByIdType extends AbstractType
{
public function __construct(private readonly TeamRepository $repository)
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addModelTransformer(new EntityByIdTransformer($this->repository));
}
public function getParent(): string
{
return TeamType::class;
}
}

View file

@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form\Type;
use App\Form\DataTransformer\EntityByIdTransformer;
use App\Repository\UserRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
final class UserByIdType extends AbstractType
{
public function __construct(private readonly UserRepository $repository)
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addModelTransformer(new EntityByIdTransformer($this->repository));
}
public function getParent(): string
{
return UserType::class;
}
}