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
namespace Chill\CustomFieldsBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Doctrine\Persistence\ObjectManager;
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldsGroupToIdTransformer;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
class CustomFieldType extends AbstractType
{
/**
*
* @var CustomFieldProvider
*/
private $customFieldProvider;
/**
*
* @var TranslatableStringHelper
*/
private $translatableStringHelper;
/**
* @var ObjectManager
*/
private $om;
public function __construct(
CustomFieldProvider $compiler,
ObjectManager $om,
TranslatableStringHelper $translatableStringHelper
) {
$this->customFieldProvider = $compiler;
$this->om = $om;
$this->translatableStringHelper = $translatableStringHelper;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$customFieldsList = array();
foreach ($this->customFieldProvider->getAllFields() as $key => $field) {
$customFieldsList[$key] = $field->getName();
}
$builder
->add('name', TranslatableStringFormType::class)
->add('active', CheckboxType::class, array('required' => false));
if ($options['group_widget'] === 'entity') {
$builder->add('customFieldsGroup', EntityType::class, array(
'class' => 'ChillCustomFieldsBundle:CustomFieldsGroup',
'choice_label' => function($g) {
return $this->translatableStringHelper->localize($g->getName());
}
));
} elseif ($options['group_widget'] === 'hidden') {
$builder->add('customFieldsGroup', HiddenType::class);
$builder->get('customFieldsGroup')
->addViewTransformer(new CustomFieldsGroupToIdTransformer($this->om));
} else {
throw new \LogicException('The value of group_widget is not handled');
}
$builder
->add('ordering', NumberType::class)
->add('required', CheckboxType::class, array(
'required' => false,
//'expanded' => TRUE,
'label' => 'Required field'
))
->add('type', HiddenType::class, array('data' => $options['type']))
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event)
{
$customField = $event->getData();
$form = $event->getForm();
// check if the customField object is "new"
// If no data is passed to the form, the data is "null".
// This should be considered a new "customField"
if (!$customField || null === $customField->getId()) {
$form->add('slug', TextType::class);
}
});
$builder->add(
$this->customFieldProvider
->getCustomFieldByType($options['type'])
->buildOptionsForm(
$builder
->create('options', null, array('compound' => true))
->setRequired(false)
)
);
}
/**
* @param OptionsResolverInterface $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Chill\CustomFieldsBundle\Entity\CustomField'
));
$resolver->setRequired(array('type', 'group_widget'))
->addAllowedValues('type', array_keys($this->customFieldProvider->getAllFields()))
->addAllowedValues('group_widget', array('hidden', 'entity'))
->setDefault('group_widget', 'entity');
}
/**
* @return string
*/
public function getBlockPrefix()
{
return 'custom_field_choice';
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace Chill\CustomFieldsBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
class CustomFieldsGroupType extends AbstractType
{
private $customizableEntities; //TODO : add comment about this variable
/**
* @var \Symfony\Component\Translation\TranslatorInterface
*/
private $translator;
public function __construct(array $customizableEntities, TranslatorInterface $translator)
{
$this->customizableEntities = $customizableEntities;
$this->translator = $translator;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
//TODO : details about the function
public function buildForm(FormBuilderInterface $builder, array $options)
{
//prepare translation
$entities = array();
$customizableEntities = array(); //TODO : change name too close than $this->customizableEntities
foreach($this->customizableEntities as $key => $definition) {
$entities[$definition['class']] = $this->translator->trans($definition['name']);
$customizableEntities[$definition['class']] = $definition;
}
$builder
->add('name', TranslatableStringFormType::class)
->add('entity', ChoiceType::class, array(
'choices' => array_combine(array_values($entities),array_keys($entities)),
))
;
$builder->addEventListener(FormEvents::POST_SET_DATA,
function(FormEvent $event) use ($customizableEntities, $builder){
$form = $event->getForm();
$group = $event->getData();
//stop the function if entity is not set
if ($group->getEntity() === NULL) {
return;
}
if (count($customizableEntities[$group->getEntity()]['options']) > 0) {
$optionBuilder = $builder
->getFormFactory()
->createBuilderForProperty(
'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup',
'options'
)
->create('options', null, array(
'compound' => true,
'auto_initialize' => false,
'required' => false)
);
}
foreach($customizableEntities[$group->getEntity()]['options'] as $key => $option) {
$optionBuilder
->add($key, $option['form_type'], $option['form_options'])
;
}
if (isset($optionBuilder) && $optionBuilder->count() > 0) {
$form->add($optionBuilder->getForm());
}
});
}
/**
* @param OptionsResolverInterface $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup'
));
}
/**
* @return string
*/
public function getBlockPrefix()
{
return 'custom_fields_group';
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Chill\CustomFieldsBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
use Chill\CustomFieldsBundle\Entity\CustomField;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class CustomFieldDataTransformer implements DataTransformerInterface
{
private $customFieldDefinition;
/**
*
* @var \Chill\CustomFieldsBundle\Entity\CustomField
*/
private $customField;
public function __construct(CustomFieldInterface $customFieldDefinition,
CustomField $customField)
{
$this->customFieldDefinition = $customFieldDefinition;
$this->customField = $customField;
}
public function reverseTransform($value)
{
return $this->customFieldDefinition->serialize($value,
$this->customField);
}
public function transform($value)
{
return $this->customFieldDefinition->deserialize($value,
$this->customField);
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Chill\CustomFieldsBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Doctrine\Persistence\ObjectManager;
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
{
/**
* @var ObjectManager
*/
private $om;
/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om)
{
$this->om = $om;
}
/**
* Transforms an custom_field_group to a string (id)
*
* @param CustomFieldsGroup|null $customFieldsGroup
* @return string
*/
public function transform($customFieldsGroup)
{
if (null === $customFieldsGroup) {
return "";
}
if (!$customFieldsGroup instanceof CustomFieldsGroup) {
throw new TransformationFailedException(sprintf('Transformation failed: '
. 'the expected type of the transforme function is an '
. 'object of type Chill\CustomFieldsBundle\Entity\CustomFieldsGroup, '
. '%s given (value : %s)', gettype($customFieldsGroup),
$customFieldsGroup));
}
return $customFieldsGroup->getId();
}
/**
* Transforms a string (id) to an object (CustomFieldsGroup).
*
* @param string $id
* @return CustomFieldsGroup|null
* @throws TransformationFailedException if object (report) is not found.
*/
public function reverseTransform($id)
{
if (!$id) {
return null;
}
if ($id instanceof CustomFieldsGroup) {
throw new TransformationFailedException(sprintf(
'The transformation failed: the expected argument on '
. 'reverseTransform is an object of type int,'
. 'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup, '
. 'given', gettype($id)));
}
$customFieldsGroup = $this->om
->getRepository('ChillCustomFieldsBundle:customFieldsGroup')->find($id)
;
if (null === $customFieldsGroup) {
throw new TransformationFailedException(sprintf(
'Le group avec le numéro "%s" ne peut pas être trouvé!',
$id
));
}
return $customFieldsGroup;
}
}

View File

@@ -0,0 +1,145 @@
<?php
namespace Chill\CustomFieldsBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Common\Collections\ArrayCollection;
class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
/**
* @var ObjectManager
*/
private $om;
/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om)
{
$this->om = $om;
$customFields = $this->om
->getRepository('ChillCustomFieldsBundle:CustomField')
->findAll();
$customFieldsLablels = array_map(
function($e) { return $e->getLabel(); },
$customFields);
$customFieldsByLabel = array_combine($customFieldsLablels, $customFields);
$this->customField = $customFieldsByLabel;
}
public function transform($customFieldsJSON)
{
echo $customFieldsJSON;
if($customFieldsJSON === null) { // lors de la creation
$customFieldsArray = array();
} else {
$customFieldsArray = json_decode($customFieldsJSON,true);
}
/*
echo "<br> - 4 - <br>";
var_dump($customFieldsArray);
echo "<br> - 5 - <br>";
*/
$customFieldsArrayRet = array();
foreach ($customFieldsArray as $key => $value) {
$traited = false;
if(array_key_exists($key, $this->customField)) {
$type = $this->customField[$key]->getType();
if(strpos($type,'ManyToOne') === 0) {
if(strpos($type,'ManyToOnePersist') ===0) {
$entityClass = substr($type, 17, -1);
} else {
$entityClass = substr($type, 10, -1);
}
$customFieldsArrayRet[$key] = $this->om
->getRepository('ChillCustomFieldsBundle:' . $entityClass)
->findOneById($value);
$traited = true;
} else if ($type === 'ManyToMany(Adress)') {
$customFieldsArrayRet[$key] = $value;
}
}
if(! $traited) {
$customFieldsArrayRet[$key] = $value;
}
}
var_dump($customFieldsArrayRet);
return $customFieldsArrayRet;
}
public function reverseTransform($customFieldsArray)
{
/*
echo "<br> - - 7 - <br>";
var_dump(array_keys($customFieldsArray));
echo "<br> - - 8 - <br>";
var_dump(array_keys($this->customField));
echo "<br> - - 9 - <br>";
*/
//var_dump($customFieldsArray);
$customFieldsArrayRet = array();
foreach ($customFieldsArray as $key => $value) {
$traited = false;
if(array_key_exists($key, $this->customField)) {
$type = $this->customField[$key]->getType();
if(strpos($type,'ManyToOne') === 0) {
// pour le manytoone() faire
// un update du form en js ? : http://symfony.com/fr/doc/current/cookbook/form/form_collections.html
//
//$entityClass = substr($type, 10, -1);
//echo $entityClasss;
if(strpos($type, 'ManyToOnePersist') === 0) {
// PEUT ETRE A FAIRE SI SEULEMENT $value->getId() ne renvoie rien...
//
//
$this->om->persist($value); // pas bon ici
// LE PERSIST NE SERT QUE LA PREMIERE FOIS
// plutot le mettre dans une var temporaire de adress
// et faire le persist qd fait sur l'obj parent
// regarder : http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html
// ou : http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html
// dans yml :
// lifecycleCallbacks:
// prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersist ]
$this->om->flush(); // sinon l'id pose pbm
}
$customFieldsArrayRet[$key] = $value->getId();
$traited = true;
}
}
if(! $traited) {
$customFieldsArrayRet[$key] = $value;
}
}
//echo json_encode($customFieldsArrayRet);
return json_encode($customFieldsArrayRet);
}
}

View File

@@ -0,0 +1,53 @@
<?php
/*
* Copyright (C) 2015 Julien Fastré <julien.fastre@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\CustomFieldsBundle\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
/**
* This extension create the possibility to add some text
* after the input.
*
* This can be used to print the units of the field, or some text.
*
* This class must be extended by Extension class specifics to each input.
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
abstract class PostTextExtension extends AbstractTypeExtension
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefined(array('post_text'));
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
if (array_key_exists('post_text', $options)) {
//set the post text variable to the view
$view->vars['post_text'] = $options['post_text'];
}
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* Copyright (C) 2015 Julien Fastré <julien.fastre@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\CustomFieldsBundle\Form\Extension;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
/**
* This class add the PostTextExtension to integer fields
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class PostTextIntegerExtension extends PostTextExtension
{
public function getExtendedType()
{
return IntegerType::class;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* Copyright (C) 2015 Julien Fastré <julien.fastre@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\CustomFieldsBundle\Form\Extension;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
/**
* This class add the PostTextExtension to number fields
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class PostTextNumberExtension extends PostTextExtension
{
public function getExtendedType()
{
return NumberType::class;
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace Chill\CustomFieldsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
/**
* Return a choice widget with an "other" option
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*
*/
class ChoiceWithOtherType extends AbstractType
{
private $otherValueLabel = 'Other value';
/* (non-PHPdoc)
* @see \Symfony\Component\Form\AbstractType::buildForm()
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
//add an 'other' entry in choices array
$options['choices'][$this->otherValueLabel] = '_other';
//ChoiceWithOther must always be expanded
$options['expanded'] = true;
// adding a default value for choice
$options['empty_data'] = null;
$builder
->add('_other', TextType::class, array('required' => false))
->add('_choices', ChoiceType::class, $options)
;
}
/* (non-PHPdoc)
* @see \Symfony\Component\Form\AbstractType::configureOptions()
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setRequired(array('choices'))
->setAllowedTypes('choices', array('array'))
->setDefaults(array(
'multiple' => false
))
;
}
public function getBlockPrefix()
{
return 'choice_with_other';
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Chill\CustomFieldsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
class ChoicesListType extends AbstractType
{
/* (non-PHPdoc)
* @see \Symfony\Component\Form\AbstractType::buildForm()
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TranslatableStringFormType::class)
->add('active', CheckboxType::class, array(
'required' => false
))
->add('slug', HiddenType::class)
->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
$formData = $form->getData();
if (NULL === $formData['slug']) {
$slug = uniqid(rand(), true);
$data['slug'] = $slug;
$event->setData($data);
} else {
$data['slug'] = $formData['slug'];
$event->setData($data);
}
})
;
}
/*
*
* @see \Symfony\Component\Form\FormTypeInterface::getName()
*/
public function getBlockPrefix()
{
return 'cf_choices_list';
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Chill\CustomFieldsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
/**
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*
*/
class ChoicesType extends AbstractType
{
public function getBlockPrefix()
{
return 'cf_choices';
}
public function getParent()
{
return CollectionType::class;
}
}

View File

@@ -0,0 +1,71 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Chill\CustomFieldsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Chill\CustomFieldsBundle\Form\DataTransformer\JsonCustomFieldToArrayTransformer;
use Doctrine\Persistence\ObjectManager;
use Chill\CustomFieldsBundle\Form\AdressType;
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Chill\CustomFieldsBundle\CustomFields\CustomFieldTitle;
class CustomFieldType extends AbstractType
{
/**
* @var ObjectManager
*/
private $om;
/**
*
* @var CustomFieldCompiler
*/
private $customFieldCompiler;
/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om, CustomFieldProvider $compiler)
{
$this->om = $om;
$this->customFieldCompiler = $compiler;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach ($options['group']->getActiveCustomFields() as $cf) {
$this->customFieldCompiler
->getCustomFieldByType($cf->getType())
->buildForm($builder, $cf);
$builder->get($cf->getSlug())->setRequired($cf->isRequired());
}
}
public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver)
{
$resolver
->setRequired(array('group'))
->addAllowedTypes('group', array('Chill\CustomFieldsBundle\Entity\CustomFieldsGroup'))
;
}
public function getBlockPrefix()
{
return 'custom_field';
}
}

View File

@@ -0,0 +1,38 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.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\CustomFieldsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class CustomFieldsTitleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
}
public function getBlockPrefix()
{
return 'custom_field_title';
}
}

View File

@@ -0,0 +1,138 @@
<?php
/*
* Copyright (C) 2015 Champs-Libres Cooperative <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\CustomFieldsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
/**
* This type create a Choice field with custom fields as choices.
*
* This type can only be associated with a customFieldsGroup type. The field
* is populated when the data (a customFieldsGroup entity) is associated with
* the form
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class LinkedCustomFieldsType extends AbstractType
{
/**
*
* @var TranslatableStringHelper
*/
private $translatableStringHelper;
/**
* The name for the choice field
*
* Extracted from builder::getName
*
* @var string
*/
private $choiceName = 'choice';
/**
* the option of the form.
*
* @internal options are stored at the class level to be reused by appendChoice, after data are setted
* @var array
*/
private $options = array();
public function __construct(TranslatableStringHelper $helper)
{
$this->translatableStringHelper = $helper;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->choiceName = $builder->getName();
$this->options = $options;
$builder->addEventListener(FormEvents::POST_SET_DATA,
array($this, 'appendChoice'))
;
}
public function getParent()
{
return ChoiceType::class;
}
/**
* append Choice on POST_SET_DATA event
*
* Choices are extracted from custom_field_group (the data associated
* with the root form)
*
* @param FormEvent $event
* @return void
*/
public function appendChoice(FormEvent $event)
{
$rootForm = $this->getRootForm($event->getForm());
$group = $rootForm->getData();
if ($group === NULL) {
return;
}
$choices = array();
foreach($group->getCustomFields() as $customFields) {
$choices[$customFields->getSlug()] =
$this->translatableStringHelper
->localize($customFields->getName());
}
$options = array_merge($this->options, array(
'choices' => $choices,
));
$event->getForm()->getParent()->add($this->choiceName, ChoiceType::class,
$options);
}
/**
* Return the root form (i.e. produced from CustomFieldsGroupType::getForm)
*
* @param FormInterface $form
* @return FormInterface
*/
private function getRootForm(FormInterface $form)
{
if ($form->getParent() === NULL) {
return $form;
} else {
return $this->getRootForm($form->getParent());
}
}
public function getBlockPrefix()
{
return 'custom_fields_group_linked_custom_fields';
}
}