cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -1,43 +1,47 @@
<?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\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\CustomFieldsBundle\Service\CustomFieldProvider;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\Persistence\ObjectManager;
use LogicException;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CustomFieldType extends AbstractType
{
/**
*
* @var CustomFieldProvider
*/
private $customFieldProvider;
/**
*
* @var TranslatableStringHelper
*/
private $translatableStringHelper;
/**
* @var ObjectManager
*/
private $om;
/**
* @var TranslatableStringHelper
*/
private $translatableStringHelper;
public function __construct(
CustomFieldProvider $compiler,
@@ -48,14 +52,10 @@ class CustomFieldType extends AbstractType
$this->om = $om;
$this->translatableStringHelper = $translatableStringHelper;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$customFieldsList = array();
$customFieldsList = [];
foreach ($this->customFieldProvider->getAllFields() as $key => $field) {
$customFieldsList[$key] = $field->getName();
@@ -63,33 +63,32 @@ class CustomFieldType extends AbstractType
$builder
->add('name', TranslatableStringFormType::class)
->add('active', CheckboxType::class, array('required' => false));
->add('active', CheckboxType::class, ['required' => false]);
if ($options['group_widget'] === 'entity') {
$builder->add('customFieldsGroup', EntityType::class, array(
'class' => 'ChillCustomFieldsBundle:CustomFieldsGroup',
'choice_label' => function($g) {
if ('entity' === $options['group_widget']) {
$builder->add('customFieldsGroup', EntityType::class, [
'class' => 'ChillCustomFieldsBundle:CustomFieldsGroup',
'choice_label' => function ($g) {
return $this->translatableStringHelper->localize($g->getName());
}
));
} elseif ($options['group_widget'] === 'hidden') {
},
]);
} elseif ('hidden' === $options['group_widget']) {
$builder->add('customFieldsGroup', HiddenType::class);
$builder->get('customFieldsGroup')
->addViewTransformer(new CustomFieldsGroupToIdTransformer($this->om));
->addViewTransformer(new CustomFieldsGroupToIdTransformer($this->om));
} else {
throw new \LogicException('The value of group_widget is not handled');
throw new LogicException('The value of group_widget is not handled');
}
$builder
->add('ordering', NumberType::class)
->add('required', CheckboxType::class, array(
->add('required', CheckboxType::class, [
'required' => false,
//'expanded' => TRUE,
'label' => 'Required field'
))
->add('type', HiddenType::class, array('data' => $options['type']))
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event)
{
'label' => 'Required field',
])
->add('type', HiddenType::class, ['data' => $options['type']])
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$customField = $event->getData();
$form = $event->getForm();
@@ -101,16 +100,15 @@ class CustomFieldType extends AbstractType
}
});
$builder->add(
$this->customFieldProvider
->getCustomFieldByType($options['type'])
->buildOptionsForm(
$builder
->create('options', null, array('compound' => true))
->setRequired(false)
)
);
->create('options', null, ['compound' => true])
->setRequired(false)
)
);
}
/**
@@ -118,14 +116,14 @@ class CustomFieldType extends AbstractType
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Chill\CustomFieldsBundle\Entity\CustomField'
));
$resolver->setDefaults([
'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');
$resolver->setRequired(['type', 'group_widget'])
->addAllowedValues('type', array_keys($this->customFieldProvider->getAllFields()))
->addAllowedValues('group_widget', ['hidden', 'entity'])
->setDefault('group_widget', 'entity');
}
/**

View File

@@ -1,22 +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\CustomFieldsBundle\Form;
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
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
/**
@@ -30,41 +34,37 @@ class CustomFieldsGroupType extends AbstractType
$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
$entities = [];
$customizableEntities = []; //TODO : change name too close than $this->customizableEntities
foreach($this->customizableEntities as $key => $definition) {
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)),
))
;
->add('entity', ChoiceType::class, [
'choices' => array_combine(array_values($entities), array_keys($entities)),
]);
$builder->addEventListener(
FormEvents::POST_SET_DATA,
function(FormEvent $event) use ($customizableEntities, $builder) {
function (FormEvent $event) use ($customizableEntities, $builder) {
$form = $event->getForm();
$group = $event->getData();
//stop the function if entity is not set
if ($group->getEntity() === NULL) {
if ($group->getEntity() === null) {
return;
}
$optionBuilder = null;
if (count($customizableEntities[$group->getEntity()]['options']) > 0) {
$optionBuilder = $builder
->getFormFactory()
@@ -75,11 +75,11 @@ class CustomFieldsGroupType extends AbstractType
[
'compound' => true,
'auto_initialize' => false,
'required' => false
'required' => false,
]
);
foreach($customizableEntities[$group->getEntity()]['options'] as $key => $option) {
foreach ($customizableEntities[$group->getEntity()]['options'] as $key => $option) {
$optionBuilder->add($key, $option['form_type'], $option['form_options']);
}
}
@@ -87,7 +87,8 @@ class CustomFieldsGroupType extends AbstractType
if ((null !== $optionBuilder) && $optionBuilder->count() > 0) {
$form->add($optionBuilder->getForm());
}
});
}
);
}
public function configureOptions(OptionsResolver $resolver)

View File

@@ -1,43 +1,49 @@
<?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\CustomFieldsBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
use Chill\CustomFieldsBundle\Entity\CustomField;
use Symfony\Component\Form\DataTransformerInterface;
/**
*
*
* @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)
private $customFieldDefinition;
public function __construct(
CustomFieldInterface $customFieldDefinition,
CustomField $customField
)
{
$this->customFieldDefinition = $customFieldDefinition;
$this->customField = $customField;
}
public function reverseTransform($value)
{
return $this->customFieldDefinition->serialize($value,
$this->customField);
return $this->customFieldDefinition->serialize(
$value,
$this->customField
);
}
public function transform($value)
{
return $this->customFieldDefinition->deserialize($value,
$this->customField);
return $this->customFieldDefinition->deserialize(
$value,
$this->customField
);
}
}

View File

@@ -1,11 +1,18 @@
<?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\CustomFieldsBundle\Form\DataTransformer;
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Doctrine\Persistence\ObjectManager;
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
{
@@ -14,43 +21,19 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
*/
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.
*
* @return CustomFieldsGroup|null
*/
public function reverseTransform($id)
{
@@ -61,7 +44,7 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
if ($id instanceof CustomFieldsGroup) {
throw new TransformationFailedException(
sprintf(
'The transformation failed: the expected argument on '
'The transformation failed: the expected argument on '
. 'reverseTransform is an object of type int,'
. 'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup, '
. 'given'
@@ -70,8 +53,7 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
}
$customFieldsGroup = $this->om
->getRepository(CustomFieldsGroup::class)->find($id)
;
->getRepository(CustomFieldsGroup::class)->find($id);
if (null === $customFieldsGroup) {
throw new TransformationFailedException(
@@ -84,4 +66,31 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
return $customFieldsGroup;
}
/**
* 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();
}
}

View File

@@ -1,18 +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.
*/
declare(strict_types=1);
namespace Chill\CustomFieldsBundle\Form\DataTransformer;
use Chill\CustomFieldsBundle\Entity\CustomField;
use Symfony\Component\Form\DataTransformerInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Form\DataTransformerInterface;
class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
private ObjectManager $om;
class JsonCustomFieldToArrayTransformer implements DataTransformerInterface
{
private array $customField;
private ObjectManager $om;
public function __construct(ObjectManager $om)
{
$this->om = $om;
@@ -23,56 +31,15 @@ class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
// @TODO: in the array_map callback, CustomField::getLabel() does not exist. What do we do here?
$customFieldsLablels = array_map(
function($e) { return $e->getLabel(); },
$customFields);
function ($e) { return $e->getLabel(); },
$customFields
);
$customFieldsByLabel = array_combine($customFieldsLablels, $customFields);
$this->customField = $customFieldsByLabel;
}
public function transform($customFieldsJSON)
{
echo $customFieldsJSON;
if($customFieldsJSON === null) {
$customFieldsArray = [];
} else {
$customFieldsArray = json_decode($customFieldsJSON, true, 512, JSON_THROW_ON_ERROR);
}
$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)
{
/*
@@ -86,23 +53,25 @@ class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
var_dump(array_keys($this->customField));
echo "<br> - - 9 - <br>";
*/
*/
//var_dump($customFieldsArray);
$customFieldsArrayRet = array();
$customFieldsArrayRet = [];
foreach ($customFieldsArray as $key => $value) {
$traited = false;
if(array_key_exists($key, $this->customField)) {
if (array_key_exists($key, $this->customField)) {
$type = $this->customField[$key]->getType();
if(strpos($type,'ManyToOne') === 0) {
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) {
if (strpos($type, 'ManyToOnePersist') === 0) {
// PEUT ETRE A FAIRE SI SEULEMENT $value->getId() ne renvoie rien...
//
//
@@ -123,14 +92,57 @@ class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
}
}
if(! $traited) {
if (!$traited) {
$customFieldsArrayRet[$key] = $value;
}
}
//echo json_encode($customFieldsArrayRet);
return json_encode($customFieldsArrayRet);
}
public function transform($customFieldsJSON)
{
echo $customFieldsJSON;
if (null === $customFieldsJSON) {
$customFieldsArray = [];
} else {
$customFieldsArray = json_decode($customFieldsJSON, true, 512, JSON_THROW_ON_ERROR);
}
$customFieldsArrayRet = [];
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;
} elseif ('ManyToMany(Adress)' === $type) {
$customFieldsArrayRet[$key] = $value;
}
}
if (!$traited) {
$customFieldsArrayRet[$key] = $value;
}
}
var_dump($customFieldsArrayRet);
return $customFieldsArrayRet;
}
}

View File

@@ -1,47 +1,29 @@
<?php
/*
* Copyright (C) 2015 Julien Fastré <julien.fastre@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\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;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* 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.
* after the input.
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* 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.
*/
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)) {
@@ -50,4 +32,8 @@ abstract class PostTextExtension extends AbstractTypeExtension
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefined(['post_text']);
}
}

View File

@@ -1,20 +1,10 @@
<?php
/*
* Copyright (C) 2015 Julien Fastré <julien.fastre@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\CustomFieldsBundle\Form\Extension;
@@ -22,15 +12,12 @@ 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>
* This class add the PostTextExtension to integer fields.
*/
class PostTextIntegerExtension extends PostTextExtension
{
public function getExtendedType()
{
return IntegerType::class;
return IntegerType::class;
}
}

View File

@@ -1,20 +1,10 @@
<?php
/*
* Copyright (C) 2015 Julien Fastré <julien.fastre@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\CustomFieldsBundle\Form\Extension;
@@ -22,9 +12,7 @@ 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>
* This class add the PostTextExtension to number fields.
*/
class PostTextNumberExtension extends PostTextExtension
{
@@ -32,5 +20,4 @@ class PostTextNumberExtension extends PostTextExtension
{
return NumberType::class;
}
}

View File

@@ -1,17 +1,22 @@
<?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\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;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Return a choice widget with an "other" option
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*
* Return a choice widget with an "other" option.
*/
class ChoiceWithOtherType extends AbstractType
{
@@ -22,7 +27,6 @@ class ChoiceWithOtherType extends AbstractType
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
//add an 'other' entry in choices array
$options['choices'][$this->otherValueLabel] = '_other';
//ChoiceWithOther must always be expanded
@@ -31,9 +35,8 @@ class ChoiceWithOtherType extends AbstractType
$options['empty_data'] = null;
$builder
->add('_other', TextType::class, array('required' => false))
->add('_choices', ChoiceType::class, $options)
;
->add('_other', TextType::class, ['required' => false])
->add('_choices', ChoiceType::class, $options);
}
/* (non-PHPdoc)
@@ -42,12 +45,11 @@ class ChoiceWithOtherType extends AbstractType
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setRequired(array('choices'))
->setAllowedTypes('choices', array('array'))
->setDefaults(array(
'multiple' => false
))
;
->setRequired(['choices'])
->setAllowedTypes('choices', ['array'])
->setDefaults([
'multiple' => false,
]);
}
public function getBlockPrefix()

View File

@@ -1,35 +1,42 @@
<?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\CustomFieldsBundle\Form\Type;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
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 Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Symfony\Component\Form\FormEvents;
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('active', CheckboxType::class, [
'required' => false,
])
->add('slug', HiddenType::class)
->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
$formData = $form->getData();
if (NULL === $formData['slug']) {
if (null === $formData['slug']) {
$slug = uniqid(rand(), true);
$data['slug'] = $slug;
@@ -38,11 +45,9 @@ class ChoicesListType extends AbstractType
$data['slug'] = $formData['slug'];
$event->setData($data);
}
})
;
});
}
/*
*
* @see \Symfony\Component\Form\FormTypeInterface::getName()
@@ -51,5 +56,4 @@ class ChoicesListType extends AbstractType
{
return 'cf_choices_list';
}
}

View File

@@ -1,15 +1,17 @@
<?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\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()

View File

@@ -1,44 +1,32 @@
<?php
/*
* This file is part of the Symfony package.
/**
* Chill is a software for social workers
*
* (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.
* 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 Chill\CustomFieldsBundle\Service\CustomFieldProvider;
use Doctrine\Persistence\ObjectManager;
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 CustomFieldCompiler
*/
private $customFieldCompiler;
/**
* @var ObjectManager
*/
private $om;
/**
*
* @var CustomFieldCompiler
*/
private $customFieldCompiler;
/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om, CustomFieldProvider $compiler)
{
$this->om = $om;
@@ -55,17 +43,15 @@ class CustomFieldType extends AbstractType
}
}
public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setRequired(array('group'))
->addAllowedTypes('group', array('Chill\CustomFieldsBundle\Entity\CustomFieldsGroup'))
;
->setRequired(['group'])
->addAllowedTypes('group', ['Chill\CustomFieldsBundle\Entity\CustomFieldsGroup']);
}
public function getBlockPrefix()
{
return 'custom_field';
}
}

View File

@@ -1,23 +1,12 @@
<?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/>.
* 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;
@@ -27,12 +16,10 @@ class CustomFieldsTitleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
}
public function getBlockPrefix()
{
return 'custom_field_title';
}
}

View File

@@ -1,31 +1,21 @@
<?php
/*
* Copyright (C) 2015 Champs-Libres Cooperative <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\CustomFieldsBundle\Form\Type;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
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.
@@ -33,19 +23,11 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
* 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
* The name for the choice field.
*
* Extracted from builder::getName
*
@@ -57,24 +39,71 @@ class LinkedCustomFieldsType extends AbstractType
* 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();
private $options = [];
/**
* @var TranslatableStringHelper
*/
private $translatableStringHelper;
public function __construct(TranslatableStringHelper $helper)
{
$this->translatableStringHelper = $helper;
}
/**
* append Choice on POST_SET_DATA event.
*
* Choices are extracted from custom_field_group (the data associated
* with the root form)
*
* @return void
*/
public function appendChoice(FormEvent $event)
{
$rootForm = $this->getRootForm($event->getForm());
$group = $rootForm->getData();
if (null === $group) {
return;
}
$choices = [];
foreach ($group->getCustomFields() as $customFields) {
$choices[$customFields->getSlug()] =
$this->translatableStringHelper
->localize($customFields->getName());
}
$options = array_merge($this->options, [
'choices' => $choices,
]);
$event->getForm()->getParent()->add(
$this->choiceName,
ChoiceType::class,
$options
);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->choiceName = $builder->getName();
$this->options = $options;
$builder->addEventListener(FormEvents::POST_SET_DATA,
array($this, 'appendChoice'))
;
$builder->addEventListener(
FormEvents::POST_SET_DATA,
[$this, 'appendChoice']
);
}
public function getBlockPrefix()
{
return 'custom_fields_group_linked_custom_fields';
}
public function getParent()
@@ -83,56 +112,16 @@ class LinkedCustomFieldsType extends AbstractType
}
/**
* append Choice on POST_SET_DATA event
* Return the root form (i.e. produced from CustomFieldsGroupType::getForm).
*
* 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) {
if ($form->getParent() === null) {
return $form;
} else {
return $this->getRootForm($form->getParent());
}
}
public function getBlockPrefix()
{
return 'custom_fields_group_linked_custom_fields';
return $this->getRootForm($form->getParent());
}
}