Create a PickUserGroupOrUserDynamicType

- add necessary vue component to render usergroup within the component AddPersons;
- add necessary normalization and denormalization process for matching the selected usergroup with entities in database
This commit is contained in:
Julien Fastré 2024-09-26 15:10:34 +02:00
parent 9e69c97250
commit 82cd77678b
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
12 changed files with 340 additions and 8 deletions

View File

@ -14,21 +14,17 @@ namespace Chill\MainBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
#[ORM\Entity]
#[ORM\Table(name: 'chill_main_user_group')]
#[Serializer\DiscriminatorMap(typeProperty: 'type', mapping: ['user_group' => UserGroup::class])]
class UserGroup
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER, nullable: false)]
#[Serializer\Groups(['read'])]
private ?int $id = null;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::JSON, nullable: false, options: ['default' => '[]'])]
#[Serializer\Groups(['read'])]
private array $label = [];
/**
@ -39,11 +35,9 @@ class UserGroup
private Collection $users;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::TEXT, nullable: false, options: ['default' => '#ffffffff'])]
#[Serializer\Groups(['read'])]
private string $backgroundColor = '#ffffffff';
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::TEXT, nullable: false, options: ['default' => '#000000ff'])]
#[Serializer\Groups(['read'])]
private string $foregroundColor = '#000000ff';
/**
@ -53,7 +47,6 @@ class UserGroup
* An empty string means "no exclusion"
*/
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::TEXT, nullable: false, options: ['default' => ''])]
#[Serializer\Groups(['read'])]
private string $excludeKey = '';
public function __construct()

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Form\Type\DataTransformer;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\PersonBundle\Entity\Person;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Symfony\Component\Form\DataTransformerInterface;
@ -74,6 +75,7 @@ class EntityToJsonTransformer implements DataTransformerInterface
'user' => User::class,
'person' => Person::class,
'thirdparty' => ThirdParty::class,
'user_group' => UserGroup::class,
default => throw new \UnexpectedValueException('This type is not supported'),
};

View File

@ -0,0 +1,68 @@
<?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\MainBundle\Form\Type;
use Chill\MainBundle\Form\Type\DataTransformer\EntityToJsonTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;
/**
* Entity which picks a user **or** a user group.
*/
final class PickUserGroupOrUserDynamicType extends AbstractType
{
public function __construct(private readonly DenormalizerInterface $denormalizer, private readonly SerializerInterface $serializer, private readonly NormalizerInterface $normalizer) {}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addViewTransformer(new EntityToJsonTransformer($this->denormalizer, $this->serializer, $options['multiple'], 'user_group'));
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['multiple'] = $options['multiple'];
$view->vars['types'] = ['user-group', 'user'];
$view->vars['uniqid'] = uniqid('pick_usergroup_dyn');
$view->vars['suggested'] = [];
$view->vars['as_id'] = true === $options['as_id'] ? '1' : '0';
$view->vars['submit_on_adding_new_entity'] = true === $options['submit_on_adding_new_entity'] ? '1' : '0';
foreach ($options['suggested'] as $userGroup) {
$view->vars['suggested'][] = $this->normalizer->normalize($userGroup, 'json', ['groups' => 'read']);
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefault('multiple', false)
->setAllowedTypes('multiple', ['bool'])
->setDefault('compound', false)
->setDefault('suggested', [])
// if set to true, only the id will be set inside the content. The denormalization will not work.
->setDefault('as_id', false)
->setAllowedTypes('as_id', ['bool'])
->setDefault('submit_on_adding_new_entity', false)
->setAllowedTypes('submit_on_adding_new_entity', ['bool']);
}
public function getBlockPrefix()
{
return 'pick_entity_dynamic';
}
}

View File

@ -30,6 +30,11 @@ export interface Scope {
};
}
export interface ResultItem<T> {
result: T;
relevance: number;
}
export interface User {
type: "user";
id: number;
@ -43,12 +48,13 @@ export interface User {
}
export interface UserGroup {
type: "chill_main_user_group" | "user_group";
type: "user_group";
id: number;
label: TranslatableString;
backgroundColor: string;
foregroundColor: string;
excludeKey: string;
text: string;
}
export type UserGroupOrUser = User | UserGroup;

View File

@ -0,0 +1,26 @@
<script setup lang="ts">
import {UserGroup} from "../../../types";
import {computed} from "vue";
interface UserGroupRenderBoxProps {
userGroup: UserGroup;
}
const props = defineProps<UserGroupRenderBoxProps>();
const styles = computed<{color: string, "background-color": string}>(() => {
return {
color: props.userGroup.foregroundColor,
"background-color": props.userGroup.backgroundColor,
}
});
</script>
<template>
<span class="badge-user-group" :style="styles">{{ userGroup.label.fr }}</span>
</template>
<style scoped lang="scss">
</style>

View File

@ -0,0 +1,37 @@
<?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\MainBundle\Serializer\Normalizer;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Repository\UserGroupRepositoryInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class UserGroupDenormalizer implements DenormalizerInterface
{
public function __construct(private readonly UserGroupRepositoryInterface $userGroupRepository) {}
public function denormalize($data, string $type, ?string $format = null, array $context = []): ?UserGroup
{
return $this->userGroupRepository->find($data['id']);
}
public function supportsDenormalization($data, string $type, ?string $format = null): bool
{
return UserGroup::class === $type
&& 'json' === $format
&& is_array($data)
&& array_key_exists('id', $data)
&& 'user_group' === ($data['type'] ?? false)
&& 2 === count(array_keys($data))
;
}
}

View File

@ -0,0 +1,41 @@
<?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\MainBundle\Serializer\Normalizer;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Templating\Entity\UserGroupRenderInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class UserGroupNormalizer implements NormalizerInterface
{
public function __construct(private readonly UserGroupRenderInterface $userGroupRender) {}
public function normalize($object, ?string $format = null, array $context = [])
{
/* @var UserGroup $object */
return [
'type' => 'user_group',
'id' => $object->getId(),
'label' => $object->getLabel(),
'backgroundColor' => $object->getBackgroundColor(),
'foregroundColor' => $object->getForegroundColor(),
'excludeKey' => $object->getExcludeKey(),
'text' => $this->userGroupRender->renderString($object, []),
];
}
public function supportsNormalization($data, ?string $format = null)
{
return $data instanceof UserGroup;
}
}

View File

@ -0,0 +1,62 @@
<?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 Serializer\Normalizer;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Repository\UserGroupRepositoryInterface;
use Chill\MainBundle\Serializer\Normalizer\UserGroupDenormalizer;
use PHPUnit\Framework\TestCase;
/**
* @internal
*
* @coversNothing
*/
class UserGroupDenormalizerTest extends TestCase
{
/**
* @throws \PHPUnit\Framework\MockObject\Exception
*
* @dataProvider provideSupportsDenormalization
*/
public function testSupportsDenormalization($data, string $type, bool $expected): void
{
$repository = $this->createMock(UserGroupRepositoryInterface::class);
$denormalizer = new UserGroupDenormalizer($repository);
$actual = $denormalizer->supportsDenormalization($data, $type, 'json');
self::assertSame($expected, $actual);
}
public static function provideSupportsDenormalization(): iterable
{
yield [['type' => 'user_group', 'id' => 10], UserGroup::class, true];
yield [['type' => 'person', 'id' => 10], UserGroup::class, false];
yield [['type' => 'user_group', 'id' => 10], \stdClass::class, false];
}
public function testDenormalize(): void
{
$repository = $this->createMock(UserGroupRepositoryInterface::class);
$repository->expects($this->once())
->method('find')
->with(10)
->willReturn($userGroup = new UserGroup());
$denormalizer = new UserGroupDenormalizer($repository);
$actual = $denormalizer->denormalize(['type' => 'user_group', 'id' => 10], UserGroup::class, 'json');
self::assertSame($userGroup, $actual);
}
}

View File

@ -0,0 +1,56 @@
<?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\MainBundle\Tests\Serializer\Normalizer;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Serializer\Normalizer\UserGroupNormalizer;
use Chill\MainBundle\Templating\Entity\UserGroupRenderInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
/**
* @internal
*
* @coversNothing
*/
class UserGroupNormalizerTest extends TestCase
{
public function testNormalize()
{
$userGroup = new UserGroup();
$userGroup
->setLabel(['fr' => 'test'])
->setExcludeKey('top')
->setForegroundColor('#123456')
->setBackgroundColor('#456789');
$entityRender = $this->createMock(UserGroupRenderInterface::class);
$entityRender->expects($this->once())
->method('renderString')
->with($userGroup, [])
->willReturn('text');
$normalizer = new UserGroupNormalizer($entityRender);
$actual = $normalizer->normalize($userGroup, 'json', [AbstractNormalizer::GROUPS => ['read']]);
self::assertEqualsCanonicalizing([
'type' => 'user_group',
'text' => 'text',
'label' => ['fr' => 'test'],
'excludeKey' => 'top',
'foregroundColor' => '#123456',
'backgroundColor' => '#456789',
'id' => null,
], $actual);
}
}

View File

@ -238,6 +238,10 @@ div[class*='budget-'] {
background-color: $chill-ll-gray;
color: $chill-blue;
}
&.bg-user-group {
background-color: $chill-l-gray;
color: $chill-blue;
}
&.bg-confidential {
background-color: $chill-ll-gray;
color: $chill-red;

View File

@ -27,6 +27,11 @@
v-bind:item="item">
</suggestion-user>
<suggestion-user-group
v-if="item.result.type === 'user_group'"
v-bind:item="item">
></suggestion-user-group>
<suggestion-household
v-if="item.result.type === 'household'"
v-bind:item="item">
@ -41,6 +46,7 @@ import SuggestionPerson from './TypePerson';
import SuggestionThirdParty from './TypeThirdParty';
import SuggestionUser from './TypeUser';
import SuggestionHousehold from './TypeHousehold';
import SuggestionUserGroup from './TypeUserGroup';
export default {
name: 'PersonSuggestion',
@ -49,6 +55,7 @@ export default {
SuggestionThirdParty,
SuggestionUser,
SuggestionHousehold,
SuggestionUserGroup,
},
props: [
'item',

View File

@ -0,0 +1,30 @@
<script setup lang="ts">
import {ResultItem, UserGroup} from "../../../../../../ChillMainBundle/Resources/public/types";
import BadgeEntity from "ChillMainAssets/vuejs/_components/BadgeEntity.vue";
import UserRenderBoxBadge from "ChillMainAssets/vuejs/_components/Entity/UserRenderBoxBadge.vue";
import UserGroupRenderBox from "ChillMainAssets/vuejs/_components/Entity/UserGroupRenderBox.vue";
interface TypeUserGroupProps {
item: ResultItem<UserGroup>;
}
const props = defineProps<TypeUserGroupProps>();
</script>
<template>
<div class="container user-group-container">
<div class="user-group-identification">
<user-group-render-box :user-group="props.item.result"></user-group-render-box>
</div>
</div>
<div class="right_actions">
<span class="badge rounded-pill bg-user-group">
Groupe d'utilisateur
</span>
</div>
</template>
<style scoped lang="scss">
</style>