fix folder name

This commit is contained in:
2021-03-18 13:37:13 +01:00
parent a2f6773f5a
commit eaa0ad925f
1578 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,138 @@
<?php
/*
* Copyright (C) 2018 Champs-Libres <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\ChoiceLoader;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Doctrine\ORM\EntityRepository;
use Chill\EventBundle\Entity\Event;
/***
* Class EventChoiceLoader
*
* @package Chill\EventBundle\Form\ChoiceLoader
* @author Mathieu Jaumotte jaum_mathieu@collectifs.net
*/
class EventChoiceLoader implements ChoiceLoaderInterface
{
/**
* @var EntityRepository
*/
protected $eventRepository;
/**
* @var array
*/
protected $lazyLoadedEvents = [];
/**
* @var array
*/
protected $centers = [];
/**
* EventChoiceLoader constructor.
*
* @param EntityRepository $eventRepository
* @param array|null $centers
*/
public function __construct(
EntityRepository $eventRepository,
array $centers = null
) {
$this->eventRepository = $eventRepository;
if (NULL !== $centers) {
$this->centers = $centers;
}
}
/**
* @return bool
*/
protected function hasCenterFilter()
{
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;
}
}

View File

@@ -0,0 +1,98 @@
<?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\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\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('type', PickEventTypeType::class, array(
'placeholder' => 'Pick a type of event',
'attr' => array(
'class' => ''
)
))
->add('moderator', UserPickerType::class, array(
'center' => $options['center'],
'role' => $options['role'],
'placeholder' => 'Pick a moderator',
'attr' => array(
'class' => ''
),
'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';
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Chill\EventBundle\Form;
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'
));
}
/**
* @return string
*/
public function getName()
{
return 'chill_eventbundle_eventtype';
}
}

View File

@@ -0,0 +1,76 @@
<?php
/*
* Copyright (C) 2016 Champs-Libres <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 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
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
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']
));
// add a status
$builder->add('status', PickStatusType::class, array(
'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');
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace Chill\EventBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Chill\EventBundle\Entity\EventType;
class RoleType extends AbstractType
{
/**
*
* @var TranslatableStringHelper
*/
protected $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(
'class' => EventType::class,
'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'
));
}
/**
* @return string
*/
public function getName()
{
return 'chill_eventbundle_role';
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Chill\EventBundle\Form;
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'
));
}
/**
* @return string
*/
public function getName()
{
return 'chill_eventbundle_status';
}
}

View File

@@ -0,0 +1,214 @@
<?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 PickEventType
*
* @package Chill\EventBundle\Form\Type
* @author Mathieu Jaumotte jaum_mathieu@collectifs.net
*/
class PickEventType extends AbstractType
{
/**
* @var EventRepository
*/
protected $eventRepository;
/**
* @var User
*/
protected $user;
/**
* @var AuthorizationHelper
*/
protected $authorizationHelper;
/**
* @var UrlGeneratorInterface
*/
protected $urlGenerator;
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* PickEventType 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, 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' ]);
$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)
{
// option role
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']
);
}
// option center
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;
}
}

View File

@@ -0,0 +1,73 @@
<?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/>.
*/
namespace Chill\EventBundle\Form\Type;
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
*/
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');
},
'choice_label' => function (EventType $t) use ($helper) {
return $helper->localize($t->getName());
},
'choice_attrs' => function (EventType $t) {
return array('data-link-category' => $t->getId());
}
)
);
}
}

View File

@@ -0,0 +1,151 @@
<?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/>.
*/
namespace Chill\EventBundle\Form\Type;
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;
/**
* Allow to pick a choice amongst different choices
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @author Champs Libres <info@champs-libres.coop>
*/
class PickRoleType extends AbstractType
{
/**
*
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
/**
*
* @var TranslatorInterface
*/
protected $translator;
/**
*
* @var EntityRepository
*/
protected $roleRepository;
public function __construct(
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);
}
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()); }
)));
}
}
);
}
}
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))
->setDefault('event_type', null)
// add option allow unactive
->setDefault('active_only', true)
->setAllowedTypes('active_only', array('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()).
($r->getActive() === true ? '' :
' ('.$translator->trans('unactive').')');
}
));
}
public function getParent()
{
return EntityType::class;
}
}

View File

@@ -0,0 +1,154 @@
<?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/>.
*/
namespace Chill\EventBundle\Form\Type;
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;
/**
* 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;
public function __construct(
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']);
}
if ($options['active_only'] === true) {
$options['query_builder']->andWhere($qb->expr()->eq('r.active', ':active'))
->setParameter('active', true);
}
if ($options['group_by'] === null && $options['event_type'] === null) {
$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()); }
)));
}
);
}
}
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))
->setDefault('event_type', null)
// add option allow unactive
->setDefault('active_only', true)
->setAllowedTypes('active_only', array('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()).
($s->getActive() === true ? '' :
' ('.$translator->trans('unactive').')');
}
));
}
public function getParent()
{
return EntityType::class;
}
}