<?php
namespace App\Form\Forms\Security\Profile;
use App\Form\Type\Select2EntityType;
use Cms\ContainerBundle\Doctrine\ContainerRepository;
use Cms\ContainerBundle\Entity\Container;
use Cms\ContainerBundle\Entity\Containers\GenericContainer;
use Cms\ContainerBundle\Entity\Containers\IntranetContainer;
use Common\Util\Strings;
use Platform\SecurityBundle\Entity\Access\Role;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ContainerRoleType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('container', Select2EntityType::class, [
'required' => false,
'label_tooltip' => true,
'placeholder' => '[Global]',
'class' => Container::class,
'empty_data' => null,
'choice_label' => function (Container $choice) {
return str_repeat(Strings::nbsp(), ($choice->getLevel() * 4)) . $choice->getName();
},
'group_by' => function ($val) {
if ($val instanceof Container) {
switch (true) {
case $val instanceof GenericContainer:
return 'Websites';
case $val instanceof IntranetContainer:
return 'Intranets';
default:
}
}
throw new \LogicException();
},
'query_builder' => function (ContainerRepository $repo) {
return $repo->findAllHierarchyQb(null, [GenericContainer::class, IntranetContainer::class]);
},
]);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'class' => Role\CmsRole::class,
]);
}
/**
* {@inheritdoc}
*/
public function getParent(): string
{
return RoleType::class;
}
}