mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
146 lines
5.3 KiB
PHP
146 lines
5.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
|
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace Chill\EventBundle\Form;
|
|
|
|
use Chill\EventBundle\Form\Type\PickEventType;
|
|
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
|
use Chill\MainBundle\Entity\User;
|
|
use Chill\MainBundle\Entity\Center;
|
|
use Chill\MainBundle\Entity\Scope;
|
|
use Chill\MainBundle\Form\Type\ChillDateTimeType;
|
|
use Chill\MainBundle\Form\Type\UserPickerType;
|
|
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
|
use Symfony\Component\Form\AbstractType;
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
|
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('type', PickEventType::class)
|
|
->add('moderator', UserPickerType::class, array(
|
|
'center' => $options['center'],
|
|
'role' => $options['role'],
|
|
'placeholder' => '',
|
|
'required' => false
|
|
))
|
|
;
|
|
}
|
|
|
|
/**
|
|
* @param OptionsResolver $resolver
|
|
*/
|
|
public function configureOptions(OptionsResolver $resolver)
|
|
{
|
|
$resolver->setDefaults(array(
|
|
'data_class' => 'Chill\EventBundle\Entity\Event'
|
|
));
|
|
$resolver
|
|
->setRequired(array('center', 'role'))
|
|
->setAllowedTypes('center', Center::class)
|
|
->setAllowedTypes('role', Role::class)
|
|
;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getName()
|
|
{
|
|
return 'chill_eventbundle_event';
|
|
}
|
|
}
|