mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,29 +1,23 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2018 Champs-Libres <info@champs-libres.coop>
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\EventBundle\Form\ChoiceLoader;
|
||||
|
||||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Chill\EventBundle\Entity\Event;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
||||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
|
||||
use function call_user_func;
|
||||
use function in_array;
|
||||
|
||||
/***
|
||||
/*
|
||||
* Class EventChoiceLoader
|
||||
*
|
||||
* @package Chill\EventBundle\Form\ChoiceLoader
|
||||
@@ -31,38 +25,99 @@ use Chill\EventBundle\Entity\Event;
|
||||
*/
|
||||
class EventChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityRepository
|
||||
*/
|
||||
protected $eventRepository;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $lazyLoadedEvents = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $centers = [];
|
||||
|
||||
|
||||
/**
|
||||
* @var EntityRepository
|
||||
*/
|
||||
protected $eventRepository;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $lazyLoadedEvents = [];
|
||||
|
||||
/**
|
||||
* EventChoiceLoader constructor.
|
||||
*
|
||||
* @param EntityRepository $eventRepository
|
||||
* @param array|null $centers
|
||||
*/
|
||||
public function __construct(
|
||||
EntityRepository $eventRepository,
|
||||
array $centers = null
|
||||
?array $centers = null
|
||||
) {
|
||||
$this->eventRepository = $eventRepository;
|
||||
if (NULL !== $centers) {
|
||||
|
||||
if (null !== $centers) {
|
||||
$this->centers = $centers;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param null $value
|
||||
*/
|
||||
public function loadChoiceList($value = null): ChoiceListInterface
|
||||
{
|
||||
return new \Symfony\Component\Form\ChoiceList\ArrayChoiceList(
|
||||
$this->lazyLoadedEvents,
|
||||
function (Event $p) use ($value) {
|
||||
return call_user_func($value, $p);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $value
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function loadChoicesForValues(array $values, $value = null)
|
||||
{
|
||||
$choices = [];
|
||||
|
||||
foreach ($values as $value) {
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$event = $this->eventRepository->find($value);
|
||||
|
||||
if ($this->hasCenterFilter()
|
||||
&& !in_array($event->getCenter(), $this->centers)) {
|
||||
throw new RuntimeException('chosen an event not in correct center');
|
||||
}
|
||||
|
||||
$choices[] = $event;
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $value
|
||||
*
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function loadValuesForChoices(array $choices, $value = null)
|
||||
{
|
||||
$values = [];
|
||||
|
||||
foreach ($choices as $choice) {
|
||||
if (null === $choice) {
|
||||
$values[] = null;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = call_user_func($value, $choice);
|
||||
$values[] = $id;
|
||||
$this->lazyLoadedEvents[$id] = $choice;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
@@ -70,69 +125,4 @@ class EventChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
return count($this->centers) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $value
|
||||
* @return ChoiceListInterface
|
||||
*/
|
||||
public function loadChoiceList($value = null): ChoiceListInterface
|
||||
{
|
||||
$list = new \Symfony\Component\Form\ChoiceList\ArrayChoiceList(
|
||||
$this->lazyLoadedEvents,
|
||||
function(Event $p) use ($value) {
|
||||
return \call_user_func($value, $p);
|
||||
});
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $values
|
||||
* @param null $value
|
||||
* @return array
|
||||
*/
|
||||
public function loadChoicesForValues(array $values, $value = null)
|
||||
{
|
||||
$choices = [];
|
||||
|
||||
foreach($values as $value) {
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$event = $this->eventRepository->find($value);
|
||||
|
||||
if ($this->hasCenterFilter() &&
|
||||
!\in_array($event->getCenter(), $this->centers)) {
|
||||
throw new \RuntimeException("chosen an event not in correct center");
|
||||
}
|
||||
|
||||
$choices[] = $event;
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $choices
|
||||
* @param null $value
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function loadValuesForChoices(array $choices, $value = null)
|
||||
{
|
||||
$values = [];
|
||||
|
||||
foreach ($choices as $choice) {
|
||||
if (NULL === $choice) {
|
||||
$values[] = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = \call_user_func($value, $choice);
|
||||
$values[] = $id;
|
||||
$this->lazyLoadedEvents[$id] = $choice;
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,91 +1,63 @@
|
||||
<?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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
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;
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\MainBundle\Form\Type\ChillDateTimeType;
|
||||
use Chill\MainBundle\Form\Type\ScopePickerType;
|
||||
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
|
||||
{
|
||||
/**
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param array $options
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('name')
|
||||
->add('date', ChillDateTimeType::class, array(
|
||||
'required' => true
|
||||
))
|
||||
->add('circle', ScopePickerType::class, [
|
||||
'center' => $options['center'],
|
||||
'role' => $options['role']
|
||||
->add('date', ChillDateTimeType::class, [
|
||||
'required' => true,
|
||||
])
|
||||
->add('type', PickEventTypeType::class, array(
|
||||
'placeholder' => 'Pick a type of event',
|
||||
'attr' => array(
|
||||
'class' => ''
|
||||
)
|
||||
))
|
||||
->add('moderator', UserPickerType::class, array(
|
||||
->add('circle', ScopePickerType::class, [
|
||||
'center' => $options['center'],
|
||||
'role' => $options['role'],
|
||||
'role' => $options['role'],
|
||||
])
|
||||
->add('type', PickEventTypeType::class, [
|
||||
'placeholder' => 'Pick a type of event',
|
||||
'attr' => [
|
||||
'class' => '',
|
||||
],
|
||||
])
|
||||
->add('moderator', UserPickerType::class, [
|
||||
'center' => $options['center'],
|
||||
'role' => $options['role'],
|
||||
'placeholder' => 'Pick a moderator',
|
||||
'attr' => array(
|
||||
'class' => ''
|
||||
),
|
||||
'required' => false
|
||||
))
|
||||
;
|
||||
'attr' => [
|
||||
'class' => '',
|
||||
],
|
||||
'required' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsResolver $resolver
|
||||
*/
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => 'Chill\EventBundle\Entity\Event'
|
||||
));
|
||||
$resolver->setDefaults([
|
||||
'data_class' => 'Chill\EventBundle\Entity\Event',
|
||||
]);
|
||||
$resolver
|
||||
->setRequired(array('center', 'role'))
|
||||
->setRequired(['center', 'role'])
|
||||
->setAllowedTypes('center', Center::class)
|
||||
->setAllowedTypes('role', Role::class)
|
||||
;
|
||||
->setAllowedTypes('role', Role::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,34 +1,26 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\EventBundle\Form;
|
||||
|
||||
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
|
||||
|
||||
class EventTypeType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param array $options
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('name', TranslatableStringFormType::class)
|
||||
->add('active')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsResolverInterface $resolver
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => 'Chill\EventBundle\Entity\EventType'
|
||||
));
|
||||
->add('active');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,4 +30,11 @@ class EventTypeType extends AbstractType
|
||||
{
|
||||
return 'chill_eventbundle_eventtype';
|
||||
}
|
||||
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => 'Chill\EventBundle\Entity\EventType',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -1,76 +1,60 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\EventBundle\Form;
|
||||
|
||||
use Chill\EventBundle\Entity\EventType;
|
||||
use Chill\EventBundle\Entity\Status;
|
||||
use Chill\EventBundle\Form\Type\PickRoleType;
|
||||
use Chill\EventBundle\Form\Type\PickStatusType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Chill\EventBundle\Entity\EventType;
|
||||
use Chill\EventBundle\Entity\Status;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\EventBundle\Form\Type\PickRoleType;
|
||||
use Chill\EventBundle\Form\Type\PickStatusType;
|
||||
|
||||
/**
|
||||
* A type to create a participation
|
||||
*
|
||||
* If the `event` option is defined, the role will be restricted
|
||||
* A type to create a participation.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* If the `event` option is defined, the role will be restricted
|
||||
*/
|
||||
class ParticipationType extends AbstractType
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
|
||||
public function __construct(TranslatableStringHelper $translatableStringHelper)
|
||||
{
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
// local copy of variable for Closure
|
||||
$translatableStringHelper = $this->translatableStringHelper;
|
||||
|
||||
|
||||
// add role
|
||||
$builder->add('role', PickRoleType::class, array(
|
||||
'event_type' => $options['event_type']
|
||||
));
|
||||
|
||||
$builder->add('role', PickRoleType::class, [
|
||||
'event_type' => $options['event_type'],
|
||||
]);
|
||||
|
||||
// add a status
|
||||
$builder->add('status', PickStatusType::class, array(
|
||||
'event_type' => $options['event_type']
|
||||
));
|
||||
|
||||
$builder->add('status', PickStatusType::class, [
|
||||
'event_type' => $options['event_type'],
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefined('event_type')
|
||||
->setAllowedTypes('event_type', array('null', EventType::class))
|
||||
->setDefault('event_type', 'null');
|
||||
->setAllowedTypes('event_type', ['null', EventType::class])
|
||||
->setDefault('event_type', 'null');
|
||||
}
|
||||
}
|
||||
|
@@ -1,53 +1,45 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\EventBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
use Chill\EventBundle\Entity\EventType;
|
||||
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Chill\EventBundle\Entity\EventType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
|
||||
class RoleType extends AbstractType
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
public function __construct(TranslatableStringHelper $translatableStringHelper) {
|
||||
|
||||
public function __construct(TranslatableStringHelper $translatableStringHelper)
|
||||
{
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param array $options
|
||||
*/
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('name', TranslatableStringFormType::class)
|
||||
->add('active')
|
||||
->add('type', EntityType::class, array(
|
||||
->add('type', EntityType::class, [
|
||||
'class' => EventType::class,
|
||||
'choice_label' => function (EventType $e) {
|
||||
'choice_label' => function (EventType $e) {
|
||||
return $this->translatableStringHelper->localize($e->getName());
|
||||
}
|
||||
))
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsResolverInterface $resolver
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => 'Chill\EventBundle\Entity\Role'
|
||||
));
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,4 +49,11 @@ class RoleType extends AbstractType
|
||||
{
|
||||
return 'chill_eventbundle_role';
|
||||
}
|
||||
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => 'Chill\EventBundle\Entity\Role',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -1,36 +1,28 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\EventBundle\Form;
|
||||
|
||||
use Chill\EventBundle\Form\Type\PickEventTypeType;
|
||||
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
|
||||
use Chill\EventBundle\Form\Type\PickEventTypeType;
|
||||
|
||||
class StatusType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param array $options
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('name', TranslatableStringFormType::class)
|
||||
->add('active')
|
||||
->add('type', PickEventTypeType::class)
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsResolverInterface $resolver
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => 'Chill\EventBundle\Entity\Status'
|
||||
));
|
||||
->add('type', PickEventTypeType::class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,4 +32,11 @@ class StatusType extends AbstractType
|
||||
{
|
||||
return 'chill_eventbundle_status';
|
||||
}
|
||||
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => 'Chill\EventBundle\Entity\Status',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -1,23 +1,10 @@
|
||||
<?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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\EventBundle\Form\Type;
|
||||
@@ -30,6 +17,7 @@ use Chill\MainBundle\Entity\Center;
|
||||
use Chill\MainBundle\Entity\GroupCenter;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use RuntimeException;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Finder\Exception\AccessDeniedException;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
@@ -41,47 +29,37 @@ use Symfony\Component\Security\Core\Role\Role;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* Class PickEventType
|
||||
*
|
||||
* @package Chill\EventBundle\Form\Type
|
||||
* @author Mathieu Jaumotte jaum_mathieu@collectifs.net
|
||||
* Class PickEventType.
|
||||
*/
|
||||
class PickEventType extends AbstractType
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EventRepository
|
||||
*/
|
||||
protected $eventRepository;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* @var AuthorizationHelper
|
||||
*/
|
||||
protected $authorizationHelper;
|
||||
|
||||
|
||||
/**
|
||||
* @var UrlGeneratorInterface
|
||||
* @var EventRepository
|
||||
*/
|
||||
protected $urlGenerator;
|
||||
|
||||
protected $eventRepository;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
|
||||
/**
|
||||
* @var UrlGeneratorInterface
|
||||
*/
|
||||
protected $urlGenerator;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* PickEventType constructor.
|
||||
*
|
||||
* @param EventRepository $eventRepository
|
||||
* @param TokenStorageInterface $tokenStorage
|
||||
* @param AuthorizationHelper $authorizationHelper
|
||||
* @param UrlGeneratorInterface $urlGenerator
|
||||
* @param TranslatorInterface $translator
|
||||
*/
|
||||
public function __construct(
|
||||
EventRepository $eventRepository,
|
||||
@@ -96,80 +74,69 @@ class PickEventType extends AbstractType
|
||||
$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, H:i') . ' → ' .
|
||||
// $e->getType()->getName()['fr'] . ': ' . // display the type of event
|
||||
$e->getName();
|
||||
},
|
||||
'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' ]);
|
||||
->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');
|
||||
}
|
||||
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
parent::configureOptions($resolver);
|
||||
|
||||
// add the possibles options for this type
|
||||
$resolver
|
||||
->setDefined('centers')
|
||||
->addAllowedTypes('centers', ['array', Center::class, 'null'])
|
||||
->setDefault('centers', null);
|
||||
$resolver
|
||||
->setDefined('role')
|
||||
->addAllowedTypes('role', [Role::class, 'null'])
|
||||
->setDefault('role', null);
|
||||
|
||||
// add the default options
|
||||
$resolver->setDefaults([
|
||||
'class' => Event::class,
|
||||
'choice_label' => function (Event $e) {
|
||||
return $e->getDate()->format('d/m/Y, H:i') . ' → ' .
|
||||
// $e->getType()->getName()['fr'] . ': ' . // display the type of event
|
||||
$e->getName();
|
||||
},
|
||||
'placeholder' => 'Pick an event',
|
||||
'attr' => ['class' => 'select2 '],
|
||||
'choice_attr' => function (Event $e) {
|
||||
return ['data-center' => $e->getCenter()->getId()];
|
||||
},
|
||||
'choiceloader' => function (Options $options) {
|
||||
$centers = $this->filterCenters($options);
|
||||
|
||||
return new EventChoiceLoader($this->eventRepository, $centers);
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
return EntityType::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Options $options
|
||||
* @return array
|
||||
*/
|
||||
protected function filterCenters(Options $options)
|
||||
{
|
||||
// option role
|
||||
if ($options['role'] === NULL) {
|
||||
if (null === $options['role']) {
|
||||
$centers = array_map(
|
||||
function (GroupCenter $g) {
|
||||
return $g->getCenter();
|
||||
@@ -184,31 +151,31 @@ class PickEventType extends AbstractType
|
||||
}
|
||||
|
||||
// option center
|
||||
if ($options['centers'] === NULL)
|
||||
{
|
||||
if (null === $options['centers']) {
|
||||
// we select all selected centers
|
||||
$selectedCenters = $centers;
|
||||
|
||||
} else {
|
||||
$selectedCenters = array();
|
||||
$selectedCenters = [];
|
||||
$optionsCenters = is_array($options['centers']) ?
|
||||
$options['centers'] : array($options['centers']);
|
||||
|
||||
$options['centers'] : [$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);
|
||||
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))) {
|
||||
function (Center $c) { return $c->getId(); },
|
||||
$centers
|
||||
))) {
|
||||
throw new AccessDeniedException('The given center is not reachable');
|
||||
}
|
||||
$selectedCenters[] = $c;
|
||||
}
|
||||
}
|
||||
|
||||
return $selectedCenters;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,38 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2016, 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/>.
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\EventBundle\Form\Type;
|
||||
|
||||
use Chill\EventBundle\Entity\EventType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Chill\EventBundle\Entity\EventType;
|
||||
|
||||
/**
|
||||
* Description of TranslatableEventType
|
||||
*
|
||||
* @author Champs-Libres Coop
|
||||
* Description of TranslatableEventType.
|
||||
*/
|
||||
class PickEventTypeType extends AbstractType
|
||||
{
|
||||
@@ -40,34 +25,34 @@ class PickEventTypeType extends AbstractType
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
|
||||
public function __construct(TranslatableStringHelper $helper)
|
||||
{
|
||||
$this->translatableStringHelper = $helper;
|
||||
}
|
||||
|
||||
public function getParent()
|
||||
{
|
||||
return EntityType::class;
|
||||
}
|
||||
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$helper = $this->translatableStringHelper;
|
||||
$resolver->setDefaults(
|
||||
array(
|
||||
[
|
||||
'class' => EventType::class,
|
||||
'query_builder' => function (EntityRepository $er) {
|
||||
return $er->createQueryBuilder('et')
|
||||
->where('et.active = true');
|
||||
->where('et.active = true');
|
||||
},
|
||||
'choice_label' => function (EventType $t) use ($helper) {
|
||||
return $helper->localize($t->getName());
|
||||
},
|
||||
'choice_attrs' => function (EventType $t) {
|
||||
return array('data-link-category' => $t->getId());
|
||||
}
|
||||
)
|
||||
return ['data-link-category' => $t->getId()];
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function getParent()
|
||||
{
|
||||
return EntityType::class;
|
||||
}
|
||||
}
|
||||
|
@@ -1,149 +1,128 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2016, 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/>.
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\EventBundle\Form\Type;
|
||||
|
||||
use Chill\EventBundle\Entity\EventType;
|
||||
use Chill\EventBundle\Entity\Role;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Chill\EventBundle\Entity\Role;
|
||||
use Chill\EventBundle\Entity\EventType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* Allow to pick a choice amongst different choices
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @author Champs Libres <info@champs-libres.coop>
|
||||
* Allow to pick a choice amongst different choices.
|
||||
*/
|
||||
class PickRoleType extends AbstractType
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var EntityRepository
|
||||
*/
|
||||
protected $roleRepository;
|
||||
|
||||
|
||||
/**
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
public function __construct(
|
||||
TranslatableStringHelper $translatableStringHelper,
|
||||
TranslatorInterface $translator,
|
||||
EntityRepository $roleRepository
|
||||
TranslatableStringHelper $translatableStringHelper,
|
||||
TranslatorInterface $translator,
|
||||
EntityRepository $roleRepository
|
||||
) {
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
$this->translator = $translator;
|
||||
$this->roleRepository = $roleRepository;
|
||||
}
|
||||
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
// create copy for easier management
|
||||
$qb = $options['query_builder'];
|
||||
|
||||
|
||||
if ($options['event_type'] instanceof EventType) {
|
||||
$options['query_builder']->where($qb->expr()->eq('r.type', ':event_type'))
|
||||
->setParameter('event_type', $options['event_type']);
|
||||
}
|
||||
|
||||
if ($options['active_only'] === true) {
|
||||
$options['query_builder']->andWhere($qb->expr()->eq('r.active', ':active'))
|
||||
->setParameter('active', true);
|
||||
->setParameter('event_type', $options['event_type']);
|
||||
}
|
||||
|
||||
if ($options['group_by'] === null) {
|
||||
$builder->addEventListener(
|
||||
FormEvents::PRE_SET_DATA,
|
||||
function(FormEvent $event) use ($options) {
|
||||
if ($options['event_type'] === null) {
|
||||
$form = $event->getForm();
|
||||
$name = $form->getName();
|
||||
$config = $form->getConfig();
|
||||
$type = $config->getType()->getName();
|
||||
$options = $config->getOptions();
|
||||
|
||||
$form->getParent()->add($name, $type, array_replace($options, array(
|
||||
'group_by' => function(Role $r)
|
||||
{ return $this->translatableStringHelper->localize($r->getType()->getName()); }
|
||||
)));
|
||||
}
|
||||
if (true === $options['active_only']) {
|
||||
$options['query_builder']->andWhere($qb->expr()->eq('r.active', ':active'))
|
||||
->setParameter('active', true);
|
||||
}
|
||||
|
||||
if (null === $options['group_by']) {
|
||||
$builder->addEventListener(
|
||||
FormEvents::PRE_SET_DATA,
|
||||
function (FormEvent $event) use ($options) {
|
||||
if (null === $options['event_type']) {
|
||||
$form = $event->getForm();
|
||||
$name = $form->getName();
|
||||
$config = $form->getConfig();
|
||||
$type = $config->getType()->getName();
|
||||
$options = $config->getOptions();
|
||||
|
||||
$form->getParent()->add($name, $type, array_replace($options, [
|
||||
'group_by' => function (Role $r) {
|
||||
return $this->translatableStringHelper->localize($r->getType()->getName());
|
||||
},
|
||||
]));
|
||||
}
|
||||
}
|
||||
);
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
// create copy for use in Closure
|
||||
$translatableStringHelper = $this->translatableStringHelper;
|
||||
$translator = $this->translator;
|
||||
|
||||
|
||||
$resolver
|
||||
// add option "event_type"
|
||||
->setDefined('event_type')
|
||||
->setAllowedTypes('event_type', array('null', EventType::class))
|
||||
->setAllowedTypes('event_type', ['null', EventType::class])
|
||||
->setDefault('event_type', null)
|
||||
// add option allow unactive
|
||||
->setDefault('active_only', true)
|
||||
->setAllowedTypes('active_only', array('boolean'))
|
||||
;
|
||||
|
||||
->setAllowedTypes('active_only', ['boolean']);
|
||||
|
||||
$qb = $this->roleRepository->createQueryBuilder('r');
|
||||
|
||||
$resolver->setDefaults(array(
|
||||
'class' => Role::class,
|
||||
'query_builder' => $qb,
|
||||
'group_by' => null,
|
||||
'choice_attr' => function(Role $r) {
|
||||
return array(
|
||||
'data-event-type' => $r->getType()->getId(),
|
||||
'data-link-category' => $r->getType()->getId()
|
||||
);
|
||||
},
|
||||
'choice_label' => function(Role $r)
|
||||
use ($translatableStringHelper, $translator) {
|
||||
return $translatableStringHelper->localize($r->getName()).
|
||||
|
||||
$resolver->setDefaults([
|
||||
'class' => Role::class,
|
||||
'query_builder' => $qb,
|
||||
'group_by' => null,
|
||||
'choice_attr' => function (Role $r) {
|
||||
return [
|
||||
'data-event-type' => $r->getType()->getId(),
|
||||
'data-link-category' => $r->getType()->getId(),
|
||||
];
|
||||
},
|
||||
'choice_label' => function (Role $r) use ($translatableStringHelper, $translator) {
|
||||
return $translatableStringHelper->localize($r->getName()) .
|
||||
($r->getActive() === true ? '' :
|
||||
' ('.$translator->trans('unactive').')');
|
||||
}
|
||||
));
|
||||
' (' . $translator->trans('unactive') . ')');
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function getParent()
|
||||
{
|
||||
return EntityType::class;
|
||||
|
@@ -1,152 +1,129 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2016, 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/>.
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\EventBundle\Form\Type;
|
||||
|
||||
use Chill\EventBundle\Entity\EventType;
|
||||
use Chill\EventBundle\Entity\Status;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Chill\EventBundle\Entity\Status;
|
||||
use Chill\EventBundle\Entity\EventType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* Allow to pick amongst type
|
||||
*
|
||||
* parameters :
|
||||
*
|
||||
* Allow to pick amongst type.
|
||||
*
|
||||
* parameters :
|
||||
*
|
||||
* - event_type : restricts to a certain event type. Default null (= all event types)
|
||||
* - active_only: restricts to active type only. Default true
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @author Champs Libres <info@champs-libres.coop>
|
||||
*/
|
||||
class PickStatusType extends AbstractType
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var EntityRepository
|
||||
*/
|
||||
protected $statusRepository;
|
||||
|
||||
|
||||
/**
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
public function __construct(
|
||||
TranslatableStringHelper $translatableStringHelper,
|
||||
TranslatorInterface $translator,
|
||||
EntityRepository $statusRepository
|
||||
TranslatableStringHelper $translatableStringHelper,
|
||||
TranslatorInterface $translator,
|
||||
EntityRepository $statusRepository
|
||||
) {
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
$this->translator = $translator;
|
||||
$this->statusRepository = $statusRepository;
|
||||
}
|
||||
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$qb = $options['query_builder'];
|
||||
|
||||
|
||||
if ($options['event_type'] instanceof EventType) {
|
||||
$options['query_builder']->where($qb->expr()->eq('r.type', ':event_type'))
|
||||
->setParameter('event_type', $options['event_type']);
|
||||
|
||||
->setParameter('event_type', $options['event_type']);
|
||||
}
|
||||
|
||||
if ($options['active_only'] === true) {
|
||||
if (true === $options['active_only']) {
|
||||
$options['query_builder']->andWhere($qb->expr()->eq('r.active', ':active'))
|
||||
->setParameter('active', true);
|
||||
->setParameter('active', true);
|
||||
}
|
||||
|
||||
if ($options['group_by'] === null && $options['event_type'] === null) {
|
||||
|
||||
if (null === $options['group_by'] && null === $options['event_type']) {
|
||||
$builder->addEventListener(
|
||||
FormEvents::PRE_SET_DATA,
|
||||
function(FormEvent $event) {
|
||||
$form = $event->getForm();
|
||||
$name = $form->getName();
|
||||
$config = $form->getConfig();
|
||||
$type = $config->getType()->getName();
|
||||
$options = $config->getOptions();
|
||||
$form->getParent()->add($name, $type, array_replace($options, array(
|
||||
'group_by' => function(Status $s)
|
||||
{ return $this->translatableStringHelper->localize($s->getType()->getName()); }
|
||||
)));
|
||||
}
|
||||
FormEvents::PRE_SET_DATA,
|
||||
function (FormEvent $event) {
|
||||
$form = $event->getForm();
|
||||
$name = $form->getName();
|
||||
$config = $form->getConfig();
|
||||
$type = $config->getType()->getName();
|
||||
$options = $config->getOptions();
|
||||
$form->getParent()->add($name, $type, array_replace($options, [
|
||||
'group_by' => function (Status $s) {
|
||||
return $this->translatableStringHelper->localize($s->getType()->getName());
|
||||
},
|
||||
]));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
// create copy for use in Closure
|
||||
$translatableStringHelper = $this->translatableStringHelper;
|
||||
$translator = $this->translator;
|
||||
|
||||
|
||||
$resolver
|
||||
// add option "event_type"
|
||||
->setDefined('event_type')
|
||||
->setAllowedTypes('event_type', array('null', EventType::class))
|
||||
->setAllowedTypes('event_type', ['null', EventType::class])
|
||||
->setDefault('event_type', null)
|
||||
// add option allow unactive
|
||||
->setDefault('active_only', true)
|
||||
->setAllowedTypes('active_only', array('boolean'))
|
||||
;
|
||||
|
||||
->setAllowedTypes('active_only', ['boolean']);
|
||||
|
||||
$qb = $this->statusRepository->createQueryBuilder('r');
|
||||
|
||||
$resolver->setDefaults(array(
|
||||
'class' => Status::class,
|
||||
'query_builder' => $qb,
|
||||
'group_by' => null,
|
||||
'choice_attr' => function(Status $s) {
|
||||
return array(
|
||||
'data-event-type' => $s->getType()->getId(),
|
||||
'data-link-category' => $s->getType()->getId()
|
||||
);
|
||||
},
|
||||
'choice_label' => function(Status $s)
|
||||
use ($translatableStringHelper, $translator) {
|
||||
return $translatableStringHelper->localize($s->getName()).
|
||||
|
||||
$resolver->setDefaults([
|
||||
'class' => Status::class,
|
||||
'query_builder' => $qb,
|
||||
'group_by' => null,
|
||||
'choice_attr' => function (Status $s) {
|
||||
return [
|
||||
'data-event-type' => $s->getType()->getId(),
|
||||
'data-link-category' => $s->getType()->getId(),
|
||||
];
|
||||
},
|
||||
'choice_label' => function (Status $s) use ($translatableStringHelper, $translator) {
|
||||
return $translatableStringHelper->localize($s->getName()) .
|
||||
($s->getActive() === true ? '' :
|
||||
' ('.$translator->trans('unactive').')');
|
||||
}
|
||||
));
|
||||
' (' . $translator->trans('unactive') . ')');
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function getParent()
|
||||
{
|
||||
return EntityType::class;
|
||||
|
Reference in New Issue
Block a user