mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-03 21:34:59 +00:00
subscribe a person since listByPerson context (issue #19)
This commit is contained in:
211
Form/Type/PickSubscriptionType.php
Normal file
211
Form/Type/PickSubscriptionType.php
Normal file
@@ -0,0 +1,211 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2019, 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\Type;
|
||||
|
||||
use Chill\EventBundle\Entity\Event;
|
||||
use Chill\EventBundle\Form\ChoiceLoader\EventChoiceLoader;
|
||||
use Chill\EventBundle\Repository\EventRepository;
|
||||
use Chill\EventBundle\Search\EventSearch;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\MainBundle\Entity\GroupCenter;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Finder\Exception\AccessDeniedException;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* Class PickSubscriptionType
|
||||
*
|
||||
* @package Chill\EventBundle\Form\Type
|
||||
* @author Mathieu Jaumotte jaum_mathieu@collectifs.net
|
||||
*/
|
||||
class PickSubscriptionType extends AbstractType
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EventRepository
|
||||
*/
|
||||
protected $eventRepository;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* @var AuthorizationHelper
|
||||
*/
|
||||
protected $authorizationHelper;
|
||||
|
||||
/**
|
||||
* @var UrlGeneratorInterface
|
||||
*/
|
||||
protected $urlGenerator;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* PickSubscriptionType constructor.
|
||||
*
|
||||
* @param EventRepository $eventRepository
|
||||
* @param TokenStorageInterface $tokenStorage
|
||||
* @param AuthorizationHelper $authorizationHelper
|
||||
* @param UrlGeneratorInterface $urlGenerator
|
||||
* @param TranslatorInterface $translator
|
||||
*/
|
||||
public function __construct(
|
||||
EventRepository $eventRepository,
|
||||
TokenStorageInterface $tokenStorage,
|
||||
AuthorizationHelper $authorizationHelper,
|
||||
UrlGeneratorInterface $urlGenerator,
|
||||
TranslatorInterface $translator
|
||||
) {
|
||||
$this->eventRepository = $eventRepository;
|
||||
$this->user = $tokenStorage->getToken()->getUser();
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsResolver $resolver
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
parent::configureOptions($resolver);
|
||||
|
||||
// add the possibles options for this type
|
||||
$resolver
|
||||
->setDefined('centers')
|
||||
->addAllowedTypes('centers', array('array', Center::class, 'null'))
|
||||
->setDefault('centers', null)
|
||||
;
|
||||
$resolver
|
||||
->setDefined('role')
|
||||
->addAllowedTypes('role', array(Role::class, 'null'))
|
||||
->setDefault('role', null)
|
||||
;
|
||||
|
||||
// add the default options
|
||||
$resolver->setDefaults(array(
|
||||
'class' => Event::class,
|
||||
'choice_label' => function(Event $e) {
|
||||
return $e->getDate()->format('d-m-Y'). ' ' .$e->getName();
|
||||
// TODO ajouter si possible le type de l'événement !
|
||||
},
|
||||
'placeholder' => 'Pick an event',
|
||||
'attr' => array('class' => 'select2 '),
|
||||
'choice_attr' => function(Event $e) {
|
||||
return array('data-center' => $e->getCenter()->getId());
|
||||
},
|
||||
'choiceloader' => function(Options $options) {
|
||||
$centers = $this->filterCenters($options);
|
||||
return new EventChoiceLoader($this->eventRepository, $centers);
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|string
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
return EntityType::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Symfony\Component\Form\FormView $view
|
||||
* @param \Symfony\Component\Form\FormInterface $form
|
||||
* @param array $options
|
||||
*/
|
||||
public function buildView(\Symfony\Component\Form\FormView $view, \Symfony\Component\Form\FormInterface $form, array $options)
|
||||
{
|
||||
$view->vars['attr']['data-person-picker'] = true;
|
||||
$view->vars['attr']['data-select-interactive-loading'] = true;
|
||||
$view->vars['attr']['data-search-url'] = $this->urlGenerator
|
||||
->generate('chill_main_search', [ 'name' => EventSearch::NAME, '_format' => 'json' ]);
|
||||
$view->vars['attr']['data-placeholder'] = $this->translator->trans($options['placeholder']);
|
||||
$view->vars['attr']['data-no-results-label'] = $this->translator->trans('select2.no_results');
|
||||
$view->vars['attr']['data-error-load-label'] = $this->translator->trans('select2.error_loading');
|
||||
$view->vars['attr']['data-searching-label'] = $this->translator->trans('select2.searching');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Options $options
|
||||
* @return array
|
||||
*/
|
||||
protected function filterCenters(Options $options)
|
||||
{
|
||||
|
||||
if ($options['role'] === NULL) {
|
||||
$centers = array_map(
|
||||
function (GroupCenter $g) {
|
||||
return $g->getCenter();
|
||||
},
|
||||
$this->user->getGroupCenters()->toArray()
|
||||
);
|
||||
} else {
|
||||
$centers = $this->authorizationHelper->getReachableCenters(
|
||||
$this->user,
|
||||
$options['role']
|
||||
);
|
||||
}
|
||||
|
||||
if ($options['centers'] === NULL)
|
||||
{
|
||||
// we select all selected centers
|
||||
$selectedCenters = $centers;
|
||||
|
||||
} else {
|
||||
$selectedCenters = array();
|
||||
$optionsCenters = is_array($options['centers']) ?
|
||||
$options['centers'] : array($options['centers']);
|
||||
|
||||
foreach ($optionsCenters as $c) {
|
||||
// check that every member of the array is a center
|
||||
if (!$c instanceof Center) {
|
||||
throw new \RuntimeException('Every member of the "centers" '
|
||||
. 'option must be an instance of '.Center::class);
|
||||
}
|
||||
if (!in_array($c->getId(), array_map(
|
||||
function(Center $c) { return $c->getId();},
|
||||
$centers))) {
|
||||
throw new AccessDeniedException('The given center is not reachable');
|
||||
}
|
||||
$selectedCenters[] = $c;
|
||||
}
|
||||
}
|
||||
return $selectedCenters;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user