From b84ee916528fa837ea68b66f267fa59aac0d142a Mon Sep 17 00:00:00 2001 From: nobohan Date: Wed, 4 Apr 2018 15:55:45 +0200 Subject: [PATCH] fix deprecations: use fqcn instead of 'choices' --- Controller/CustomFieldsGroupController.php | 3 +- CustomFields/CustomFieldChoice.php | 85 +++++++++++----------- CustomFields/CustomFieldLongChoice.php | 3 +- CustomFields/CustomFieldText.php | 31 ++++---- CustomFields/CustomFieldTitle.php | 21 +++--- Form/CustomFieldsGroupType.php | 31 ++++---- Form/Type/ChoiceWithOtherType.php | 18 +++-- 7 files changed, 100 insertions(+), 92 deletions(-) diff --git a/Controller/CustomFieldsGroupController.php b/Controller/CustomFieldsGroupController.php index 2d4487159..69e6586b6 100644 --- a/Controller/CustomFieldsGroupController.php +++ b/Controller/CustomFieldsGroupController.php @@ -6,6 +6,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Doctrine\ORM\Query; use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup; @@ -252,7 +253,7 @@ class CustomFieldsGroupController extends Controller 'action' => $this->generateUrl('customfield_new'), 'csrf_protection' => false )) - ->add('type', 'choice', array( + ->add('type', ChoiceType::class, array( 'choices' => $fieldChoices )) ->add('customFieldsGroup', HiddenType::class) diff --git a/CustomFields/CustomFieldChoice.php b/CustomFields/CustomFieldChoice.php index d979e84b4..d04706c38 100644 --- a/CustomFields/CustomFieldChoice.php +++ b/CustomFields/CustomFieldChoice.php @@ -3,17 +3,17 @@ /* * Chill is a software for social workers * Copyright (C) 2014, Champs Libres Cooperative SCRLFS, - * + * * 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 . */ @@ -31,9 +31,10 @@ use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer; use Symfony\Bridge\Twig\TwigEngine; use Chill\MainBundle\Templating\TranslatableStringHelper; use Symfony\Component\Translation\Translator; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; /** - * + * * * @author Julien Fastré * @author Marc Ducobu @@ -49,7 +50,7 @@ class CustomFieldChoice extends AbstractCustomField private $defaultLocales; /** - * + * * @var TwigEngine */ private $templating; @@ -58,9 +59,9 @@ class CustomFieldChoice extends AbstractCustomField * @var TranslatableStringHelper Helper that find the string in current locale from an array of translation */ private $translatableStringHelper; - + public function __construct( - Translator $translator, + Translator $translator, TwigEngine $templating, TranslatableStringHelper $translatableStringHelper) { @@ -68,7 +69,7 @@ class CustomFieldChoice extends AbstractCustomField $this->templating = $templating; $this->translatableStringHelper = $translatableStringHelper; } - + public function buildForm(FormBuilderInterface $builder, CustomField $customField) { //prepare choices @@ -80,7 +81,7 @@ class CustomFieldChoice extends AbstractCustomField $choices[$persistedChoices['slug']] = $this->translatableStringHelper->localize($persistedChoices['name']); } } - + //prepare $options $options = array( 'multiple' => $customFieldOptions[self::MULTIPLE], @@ -104,13 +105,13 @@ class CustomFieldChoice extends AbstractCustomField new ChoiceWithOtherType($otherValueLabel), $options) ->addModelTransformer(new CustomFieldDataTransformer($this, $customField))); - + } else { //if allow_other = false //we add the 'expanded' to options $options['expanded'] = $customFieldOptions[self::EXPANDED]; - + $builder->add( - $builder->create($customField->getSlug(), 'choice', $options) + $builder->create($customField->getSlug(), ChoiceType::class, $options) ->addModelTransformer(new CustomFieldDataTransformer($this, $customField)) ); } @@ -119,7 +120,7 @@ class CustomFieldChoice extends AbstractCustomField public function buildOptionsForm(FormBuilderInterface $builder) { $builder - ->add(self::MULTIPLE, 'choice', array( + ->add(self::MULTIPLE, ChoiceType::class, array( 'expanded' => true, 'multiple' => false, 'choices' => array( @@ -128,7 +129,7 @@ class CustomFieldChoice extends AbstractCustomField 'empty_data' => '0', 'label' => 'Multiplicity' )) - ->add(self::EXPANDED, 'choice', array( + ->add(self::EXPANDED, ChoiceType::class, array( 'expanded' => true, 'multiple' => false, 'choices' => array( @@ -137,7 +138,7 @@ class CustomFieldChoice extends AbstractCustomField 'empty_data' => '0', 'label' => 'Choice display' )) - ->add(self::ALLOW_OTHER, 'choice', array( + ->add(self::ALLOW_OTHER, ChoiceType::class, array( 'label' => 'Allow other', 'choices' => array( '0' => 'No', @@ -152,7 +153,7 @@ class CustomFieldChoice extends AbstractCustomField 'type' => new ChoicesListType($this->defaultLocales), 'allow_add' => true )); - + return $builder; } @@ -160,7 +161,7 @@ class CustomFieldChoice extends AbstractCustomField { // we always have to adapt to what the current data should be $options = $customField->getOptions(); - + if ($options[self::MULTIPLE]) { return $this->deserializeToMultiple($serialized, $options[self::ALLOW_OTHER]); } else { @@ -168,62 +169,62 @@ class CustomFieldChoice extends AbstractCustomField } return $serialized; } - + private function deserializeToUnique($serialized, $allowOther) { $value = $this->guessValue($serialized); - + // set in a single value. We must have a single string - $fixedValue = is_array($value) ? + $fixedValue = is_array($value) ? // check if the array has an element, if not replace by empty string - count($value) > 0 ? end($value) : '' - : + count($value) > 0 ? end($value) : '' + : $value; - + if ($allowOther) { return $this->deserializeWithAllowOther($serialized, $fixedValue); } else { return $fixedValue; } } - + /** - * deserialized the data from the database to a multiple + * deserialized the data from the database to a multiple * field - * + * * @param mixed $serialized * @param boolean $allowOther */ private function deserializeToMultiple($serialized, $allowOther) { $value = $this->guessValue($serialized); - + // set in an array : we want a multiple $fixedValue = is_array($value) ? $value : array($value); - + if ($allowOther) { return $this->deserializeWithAllowOther($serialized, $fixedValue); } else { return $fixedValue; } } - + private function deserializeWithAllowOther($serialized, $value) { $existingOther = isset($serialized['_other']) ? $serialized['_other'] : ''; - + return array( '_other' => $existingOther, '_choices' => $value ); } - + /** * Guess the value from the representation of it. - * + * * If the value had an 'allow_other' = true option, the returned value * **is not** the content of the _other field, but the `_other` string. - * + * * @param array|string $value * @return mixed * @throws \LogicException if the case is not covered by this @@ -233,7 +234,7 @@ class CustomFieldChoice extends AbstractCustomField if ($value === NULL) { return NULL; } - + if (!is_array($value)) { return $value; } else { @@ -245,7 +246,7 @@ class CustomFieldChoice extends AbstractCustomField return $value; } } - + throw \LogicException("This case is not expected."); } @@ -253,13 +254,13 @@ class CustomFieldChoice extends AbstractCustomField { return 'Choices'; } - + public function isEmptyValue($value, CustomField $customField) { if ($value === NULL) { return true; } - + // if multiple choice OR multiple/single choice with other if (is_array($value)) { @@ -284,7 +285,7 @@ class CustomFieldChoice extends AbstractCustomField } /** - * + * * @internal this function is able to receive data whichever is the value of "other", "multiple" * @param mixed $value * @param CustomField $customField @@ -294,13 +295,13 @@ class CustomFieldChoice extends AbstractCustomField { //extract the data. They are under a _choice key if they are stored with allow_other $data = (isset($value['_choices'])) ? $value['_choices'] : $value; - $selected = (is_array($data)) ? $data : array($data); + $selected = (is_array($data)) ? $data : array($data); $choices = $customField->getOptions()[self::CHOICES]; - + if (in_array('_other', $selected)){ $choices[] = array('name' => $value['_other'], 'slug' => '_other'); } - + $template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice.html.twig'; if($documentType == 'csv') { $template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice.csv.twig'; @@ -309,7 +310,7 @@ class CustomFieldChoice extends AbstractCustomField return $this->templating ->render($template, array( - 'choices' => $choices, + 'choices' => $choices, 'selected' => $selected, 'multiple' => $customField->getOptions()[self::MULTIPLE], 'expanded' => $customField->getOptions()[self::EXPANDED], diff --git a/CustomFields/CustomFieldLongChoice.php b/CustomFields/CustomFieldLongChoice.php index ac37bf2be..cd6159666 100644 --- a/CustomFields/CustomFieldLongChoice.php +++ b/CustomFields/CustomFieldLongChoice.php @@ -27,6 +27,7 @@ use Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option; use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer; use Symfony\Bridge\Twig\TwigEngine; use Chill\MainBundle\Form\Type\Select2ChoiceType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; /** * @@ -109,7 +110,7 @@ class CustomFieldLongChoice extends AbstractCustomField $choices[$key] = $key; } - return $builder->add(self::KEY, 'choice', array( + return $builder->add(self::KEY, ChoiceType::class, array( 'choices' => $choices, 'label' => 'Options key' )); diff --git a/CustomFields/CustomFieldText.php b/CustomFields/CustomFieldText.php index 1205d2c93..663168ffc 100644 --- a/CustomFields/CustomFieldText.php +++ b/CustomFields/CustomFieldText.php @@ -3,7 +3,7 @@ /* * Chill is a software for social workers * - * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, + * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, * , * * This program is free software: you can redistribute it and/or modify @@ -28,6 +28,7 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Bundle\TwigBundle\TwigEngine; use Chill\MainBundle\Templating\TranslatableStringHelper; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; /** * @author Julien Fastré @@ -35,21 +36,21 @@ use Chill\MainBundle\Templating\TranslatableStringHelper; */ class CustomFieldText extends AbstractCustomField { - + private $requestStack; - + /** - * + * * @var TwigEngine */ private $templating; - + /** * @var TranslatableStringHelper Helper that find the string in current locale from an array of translation */ private $translatableStringHelper; - + public function __construct(RequestStack $requestStack, TwigEngine $templating, TranslatableStringHelper $translatableStringHelper) { @@ -57,16 +58,16 @@ class CustomFieldText extends AbstractCustomField $this->templating = $templating; $this->translatableStringHelper = $translatableStringHelper; } - + const MAX_LENGTH = 'maxLength'; const MULTIPLE_CF_INLINE ='multipleCFInline'; - + /** * Create a form according to the maxLength option - * + * * if maxLength < 256 THEN the form type is 'text' * if not, THEN the form type is textarea - * + * * @param FormBuilderInterface $builder * @param CustomField $customField */ @@ -74,16 +75,16 @@ class CustomFieldText extends AbstractCustomField { $options = $customField->getOptions(); - $type = ($options[self::MAX_LENGTH] < 256) ? 'text' + $type = ($options[self::MAX_LENGTH] < 256) ? 'text' : 'textarea'; $attrArray = array(); if(array_key_exists(self::MULTIPLE_CF_INLINE, $options) and $options[self::MULTIPLE_CF_INLINE]) { - $attrArray['class'] = 'multiple-cf-inline'; + $attrArray['class'] = 'multiple-cf-inline'; } - + $builder->add($customField->getSlug(), $type, array( 'label' => $this->translatableStringHelper->localize($customField->getName()), 'required' => false, @@ -121,9 +122,9 @@ class CustomFieldText extends AbstractCustomField { return $builder ->add(self::MAX_LENGTH, 'integer', array('empty_data' => 256)) - ->add(self::MULTIPLE_CF_INLINE, 'choice', array( + ->add(self::MULTIPLE_CF_INLINE, ChoiceType::class, array( 'choices' => array( - '1' => 'Multiple boxes on the line', + '1' => 'Multiple boxes on the line', '0' => 'One box on the line' ), 'label' => 'Box appearance', diff --git a/CustomFields/CustomFieldTitle.php b/CustomFields/CustomFieldTitle.php index 11470df28..85c50fdf2 100644 --- a/CustomFields/CustomFieldTitle.php +++ b/CustomFields/CustomFieldTitle.php @@ -1,20 +1,20 @@ - * + * * 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 . */ @@ -27,6 +27,7 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Bundle\TwigBundle\TwigEngine; use Chill\MainBundle\Templating\TranslatableStringHelper; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; class CustomFieldTitle extends AbstractCustomField { @@ -37,7 +38,7 @@ class CustomFieldTitle extends AbstractCustomField private $requestStack; /** - * + * * @var TwigEngine */ private $templating; @@ -54,7 +55,7 @@ class CustomFieldTitle extends AbstractCustomField $this->templating = $templating; $this->translatableStringHelper = $translatableStringHelper; } - + public function buildForm(FormBuilderInterface $builder, CustomField $customField) { $builder->add($customField->getSlug(), 'custom_field_title', array( @@ -68,9 +69,9 @@ class CustomFieldTitle extends AbstractCustomField } public function render($value, CustomField $customField, $documentType = 'html') - { + { return $this->templating - ->render('ChillCustomFieldsBundle:CustomFieldsRendering:title.html.twig', + ->render('ChillCustomFieldsBundle:CustomFieldsRendering:title.html.twig', array( 'title' => $customField->getName(), 'type' => $customField->getOptions()[self::TYPE] @@ -92,7 +93,7 @@ class CustomFieldTitle extends AbstractCustomField { return 'Title'; } - + public function isEmptyValue($value, CustomField $customField) { return false; @@ -100,7 +101,7 @@ class CustomFieldTitle extends AbstractCustomField public function buildOptionsForm(FormBuilderInterface $builder) { - return $builder->add(self::TYPE, 'choice', + return $builder->add(self::TYPE, ChoiceType::class, array( 'choices' => array( self::TYPE_TITLE => 'Main title', diff --git a/Form/CustomFieldsGroupType.php b/Form/CustomFieldsGroupType.php index 55cd23156..eba1804e1 100644 --- a/Form/CustomFieldsGroupType.php +++ b/Form/CustomFieldsGroupType.php @@ -8,25 +8,26 @@ 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; class CustomFieldsGroupType extends AbstractType { - + private $customizableEntities; //TOOD : 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 @@ -37,43 +38,43 @@ class CustomFieldsGroupType extends AbstractType //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', 'translatable_string') - ->add('entity', 'choice', array( + ->add('entity', ChoiceType::class, array( 'choices' => $entities )) ; - $builder->addEventListener(FormEvents::POST_SET_DATA, + $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', + 'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup', 'options' ) ->create('options', null, array( - 'compound' => true, + 'compound' => true, 'auto_initialize' => false, 'required' => false) ); } - + foreach($customizableEntities[$group->getEntity()]['options'] as $key => $option) { $optionBuilder ->add($key, $option['form_type'], $option['form_options']) @@ -82,10 +83,10 @@ class CustomFieldsGroupType extends AbstractType if (isset($optionBuilder) && $optionBuilder->count() > 0) { $form->add($optionBuilder->getForm()); } - + }); } - + /** * @param OptionsResolverInterface $resolver */ diff --git a/Form/Type/ChoiceWithOtherType.php b/Form/Type/ChoiceWithOtherType.php index 5520981d2..8f4b4b02c 100644 --- a/Form/Type/ChoiceWithOtherType.php +++ b/Form/Type/ChoiceWithOtherType.php @@ -4,10 +4,12 @@ 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é * */ @@ -24,7 +26,7 @@ class ChoiceWithOtherType extends AbstractType /* (non-PHPdoc) * @see \Symfony\Component\Form\AbstractType::buildForm() */ - public function buildForm(FormBuilderInterface $builder, array $options) + public function buildForm(FormBuilderInterface $builder, array $options) { //add an 'other' entry in choices array $options['choices']['_other'] = $this->otherValueLabel; @@ -32,17 +34,17 @@ class ChoiceWithOtherType extends AbstractType $options['expanded'] = true; // adding a default value for choice $options['empty_data'] = null; - + $builder - ->add('_other', 'text', array('required' => false)) - ->add('_choices', 'choice', $options) - ; + ->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) + public function configureOptions(OptionsResolver $resolver) { $resolver ->setRequired(array('choices')) @@ -57,4 +59,4 @@ class ChoiceWithOtherType extends AbstractType { return 'choice_with_other'; } -} \ No newline at end of file +}