Use ScopePickerType in EventType for circle field

This commit is contained in:
Jean-Francois Monfort 2021-01-19 17:52:28 +01:00
parent ce28ad18b1
commit 20af1b180d
2 changed files with 6 additions and 70 deletions

View File

@ -23,6 +23,7 @@
namespace Chill\EventBundle\Form;
use Chill\EventBundle\Form\Type\PickEventTypeType;
use Chill\MainBundle\Form\Type\ScopePickerType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Center;
@ -39,77 +40,21 @@ use Symfony\Component\Security\Core\Role\Role;
class EventType extends AbstractType
{
/** @var User */
protected $user;
/** @var AuthorizationHelper */
protected $authorizationHelper;
/** @var TranslatableStringHelper */
protected $translatableStringHelper;
public function __construct(
TokenStorageInterface $tokenStorage,
AuthorizationHelper $authorizationHelper,
TranslatableStringHelper $translatableStringHelper
) {
if (!$tokenStorage->getToken()->getUser() instanceof User) {
throw new \RuntimeException("you should have a valid user");
}
$this->user = $tokenStorage->getToken()->getUser();
$this->authorizationHelper = $authorizationHelper;
$this->translatableStringHelper = $translatableStringHelper;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$userReachableCenters = $this->authorizationHelper
->getReachableCenters($this->user, new Role('CHILL_EVENT_CREATE'));
$userReachableCirclesByCircleId = array();
$userReachableCentersByCircleId = array();
foreach ($userReachableCenters as $center) {
foreach ($this->authorizationHelper
->getReachableCircles($this->user, new Role('CHILL_EVENT_CREATE'), $center) as $circle) {
if (array_key_exists($circle->getId(), $userReachableCirclesByCircleId)) {
array_push($userReachableCentersByCircleId[$circle->getId()], $center);
} else {
$userReachableCirclesByCircleId[$circle->getId()] = $circle;
$userReachableCentersByCircleId[$circle->getId()] = array($center);
}
}
}
$builder
->add('name')
->add('date', ChillDateTimeType::class, array(
'required' => true
))
->add('circle', EntityType::class, array(
'class' => Scope::class,
'choices' => array_values($userReachableCirclesByCircleId),
'choice_label' => function ($circle) {
$helper = $this->translatableStringHelper;
return $helper->localize($circle->getName());
},
'choice_attr' => function ($circle) use ($userReachableCentersByCircleId) {
$centersId = "";
foreach ($userReachableCentersByCircleId[$circle->getId()] as $center) {
$centersId = $centersId.($center->getId()).',';
}
$centersId = trim($centersId, ',');
return array(
'data-link-categories' => $centersId,
);
},
))
->add('circle', ScopePickerType::class, [
'center' => $options['center'],
'role' => $options['role']
])
->add('type', PickEventTypeType::class, array(
'placeholder' => 'Pick a type of event',
'attr' => array(

View File

@ -5,16 +5,7 @@ services:
- "@chill.main.helper.translatable_string"
tags:
- { name: form.type }
chill.event.form.event_type_test:
class: Chill\EventBundle\Form\EventType
arguments:
- "@security.token_storage"
- "@chill.main.security.authorization.helper"
- "@chill.main.helper.translatable_string"
tags:
- { name: form.type }
chill.event.form.participation_type:
class: Chill\EventBundle\Form\ParticipationType
arguments: