mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-11-23 04:17:45 +00:00
Remove the label if there is only one scope and no scope picking field is displayed.
This commit is contained in:
@@ -16,6 +16,7 @@ use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Form\DataMapper\ScopePickerDataMapper;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
|
||||
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
@@ -32,65 +33,84 @@ use Symfony\Component\Security\Core\Security;
|
||||
* Allow to pick amongst available scope for the current
|
||||
* user.
|
||||
*
|
||||
* options :
|
||||
*
|
||||
* - `center`: the center of the entity
|
||||
* - `role` : the role of the user
|
||||
* Options:
|
||||
* - `role`: string, the role to check permissions for
|
||||
* - Either `subject`: object, entity to resolve centers from
|
||||
* - Or `center`: Center|array|null, the center(s) to check
|
||||
*/
|
||||
class ScopePickerType extends AbstractType
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TranslatableStringHelperInterface $translatableStringHelper,
|
||||
private readonly AuthorizationHelperInterface $authorizationHelper,
|
||||
private readonly Security $security,
|
||||
private readonly TranslatableStringHelperInterface $translatableStringHelper,
|
||||
private readonly CenterResolverManagerInterface $centerResolverManager,
|
||||
) {}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$items = array_values(
|
||||
// Compute centers from subject
|
||||
$centers = $options['center'] ?? null;
|
||||
if (null === $centers && isset($options['subject'])) {
|
||||
$centers = $this->centerResolverManager->resolveCenters($options['subject']);
|
||||
}
|
||||
|
||||
if (null === $centers) {
|
||||
throw new \RuntimeException('Either "center" or "subject" must be provided');
|
||||
}
|
||||
|
||||
$reachableScopes = array_values(
|
||||
array_filter(
|
||||
$this->authorizationHelper->getReachableScopes(
|
||||
$this->security->getUser(),
|
||||
$options['role'],
|
||||
$options['center']
|
||||
$centers
|
||||
),
|
||||
static fn (Scope $s) => $s->isActive()
|
||||
)
|
||||
);
|
||||
|
||||
if (0 === \count($items)) {
|
||||
throw new \RuntimeException('no scopes are reachable. This form should not be shown to user');
|
||||
$builder->setAttribute('reachable_scopes_count', count($reachableScopes));
|
||||
|
||||
if (0 === count($reachableScopes)) {
|
||||
$builder->setAttribute('has_scopes', false);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (1 !== \count($items)) {
|
||||
$builder->setAttribute('has_scopes', true);
|
||||
|
||||
if (1 !== count($reachableScopes)) {
|
||||
$builder->add('scope', EntityType::class, [
|
||||
'class' => Scope::class,
|
||||
'placeholder' => 'Choose the circle',
|
||||
'choice_label' => fn (Scope $c) => $this->translatableStringHelper->localize($c->getName()),
|
||||
'choices' => $items,
|
||||
'choices' => $reachableScopes,
|
||||
]);
|
||||
$builder->setDataMapper(new ScopePickerDataMapper());
|
||||
} else {
|
||||
$builder->add('scope', HiddenType::class, [
|
||||
'data' => $items[0]->getId(),
|
||||
'data' => $reachableScopes[0]->getId(),
|
||||
]);
|
||||
$builder->setDataMapper(new ScopePickerDataMapper($items[0]));
|
||||
$builder->setDataMapper(new ScopePickerDataMapper($reachableScopes[0]));
|
||||
}
|
||||
}
|
||||
|
||||
public function buildView(FormView $view, FormInterface $form, array $options)
|
||||
{
|
||||
$view->vars['fullWidth'] = true;
|
||||
// display of label is handled by the EntityType
|
||||
$view->vars['label'] = false;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver
|
||||
// create `center` option
|
||||
->setRequired('center')
|
||||
->setAllowedTypes('center', [Center::class, 'array', 'null'])
|
||||
// create ``role` option
|
||||
->setRequired('role')
|
||||
->setAllowedTypes('role', ['string']);
|
||||
->setAllowedTypes('role', ['string'])
|
||||
->setDefined('subject')
|
||||
->setAllowedTypes('subject', ['object'])
|
||||
->setDefined('center')
|
||||
->setAllowedTypes('center', [Center::class, 'array', 'null']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace Form\Type;
|
||||
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Form\Type\ScopePickerType;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
|
||||
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
|
||||
@@ -39,11 +39,11 @@ final class ScopePickerTypeTest extends TypeTestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
public function estBuildOneScopeIsSuccessful()
|
||||
public function testBuildOneScopeIsSuccessful()
|
||||
{
|
||||
$form = $this->factory->create(ScopePickerType::class, null, [
|
||||
'center' => new Center(),
|
||||
'role' => 'ONE_SCOPE',
|
||||
'center' => [],
|
||||
]);
|
||||
|
||||
$view = $form->createView();
|
||||
@@ -54,8 +54,8 @@ final class ScopePickerTypeTest extends TypeTestCase
|
||||
public function testBuildThreeScopesIsSuccessful()
|
||||
{
|
||||
$form = $this->factory->create(ScopePickerType::class, null, [
|
||||
'center' => new Center(),
|
||||
'role' => 'THREE_SCOPE',
|
||||
'center' => [],
|
||||
]);
|
||||
|
||||
$view = $form->createView();
|
||||
@@ -66,8 +66,8 @@ final class ScopePickerTypeTest extends TypeTestCase
|
||||
public function testBuildTwoScopesIsSuccessful()
|
||||
{
|
||||
$form = $this->factory->create(ScopePickerType::class, null, [
|
||||
'center' => new Center(),
|
||||
'role' => 'TWO_SCOPE',
|
||||
'center' => [],
|
||||
]);
|
||||
|
||||
$view = $form->createView();
|
||||
@@ -101,10 +101,13 @@ final class ScopePickerTypeTest extends TypeTestCase
|
||||
static fn ($args) => $args[0]['fr']
|
||||
);
|
||||
|
||||
$centerResolverManager = $this->prophesize(CenterResolverManagerInterface::class);
|
||||
|
||||
$type = new ScopePickerType(
|
||||
$translatableStringHelper->reveal(),
|
||||
$authorizationHelper->reveal(),
|
||||
$security->reveal(),
|
||||
$translatableStringHelper->reveal()
|
||||
$centerResolverManager->reveal()
|
||||
);
|
||||
|
||||
// add the mocks for creating EntityType
|
||||
|
||||
Reference in New Issue
Block a user