mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,28 +1,36 @@
|
||||
<?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\CustomFields;
|
||||
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\EntityRepository\CustomFieldLongChoice\OptionRepository;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option;
|
||||
use Chill\CustomFieldsBundle\EntityRepository\CustomFieldLongChoice\OptionRepository;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
|
||||
use Symfony\Bridge\Twig\TwigEngine;
|
||||
use Chill\MainBundle\Form\Type\Select2ChoiceType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use LogicException;
|
||||
use Symfony\Bridge\Twig\TwigEngine;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
class CustomFieldLongChoice extends AbstractCustomField
|
||||
{
|
||||
private OptionRepository $optionRepository;
|
||||
public const KEY = 'key';
|
||||
|
||||
private TranslatableStringHelper $translatableStringHelper;
|
||||
private OptionRepository $optionRepository;
|
||||
|
||||
private TwigEngine $templating;
|
||||
|
||||
public const KEY = 'key';
|
||||
private TranslatableStringHelper $translatableStringHelper;
|
||||
|
||||
public function __construct(
|
||||
OptionRepository $optionRepository,
|
||||
@@ -37,29 +45,32 @@ class CustomFieldLongChoice extends AbstractCustomField
|
||||
public function buildForm(FormBuilderInterface $builder, CustomField $customField)
|
||||
{
|
||||
$options = $customField->getOptions();
|
||||
$entries = $this->optionRepository->findFilteredByKey($options[self::KEY],
|
||||
false, true);
|
||||
$entries = $this->optionRepository->findFilteredByKey(
|
||||
$options[self::KEY],
|
||||
false,
|
||||
true
|
||||
);
|
||||
//create a local copy of translatable string helper
|
||||
$translatableStringHelper = $this->translatableStringHelper;
|
||||
$builder->add($customField->getSlug(), Select2ChoiceType::class, array(
|
||||
$builder->add($customField->getSlug(), Select2ChoiceType::class, [
|
||||
'choices' => $entries,
|
||||
'choice_label' => function(Option $option) use ($translatableStringHelper) {
|
||||
'choice_label' => function (Option $option) use ($translatableStringHelper) {
|
||||
return $translatableStringHelper->localize($option->getText());
|
||||
},
|
||||
'choice_value' => static fn (Option $key): ?int => $key === null ? null : $key->getId(),
|
||||
'choice_value' => static fn (Option $key): ?int => null === $key ? null : $key->getId(),
|
||||
'multiple' => false,
|
||||
'expanded' => false,
|
||||
'required' => $customField->isRequired(),
|
||||
'placeholder' => 'Choose a value',
|
||||
'group_by' => function(Option $option) use ($translatableStringHelper) {
|
||||
'group_by' => function (Option $option) use ($translatableStringHelper) {
|
||||
if ($option->hasParent()) {
|
||||
return $translatableStringHelper->localize($option->getParent()->getText());
|
||||
}
|
||||
|
||||
return $translatableStringHelper->localize($option->getText());
|
||||
},
|
||||
'label' => $translatableStringHelper->localize($customField->getName())
|
||||
));
|
||||
'label' => $translatableStringHelper->localize($customField->getName()),
|
||||
]);
|
||||
|
||||
$builder
|
||||
->get($customField->getSlug())
|
||||
@@ -70,25 +81,24 @@ class CustomFieldLongChoice extends AbstractCustomField
|
||||
{
|
||||
//create a selector between different keys
|
||||
$keys = $this->optionRepository->getKeys();
|
||||
$choices = array();
|
||||
$choices = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$choices[$key] = $key;
|
||||
}
|
||||
|
||||
return $builder->add(self::KEY, ChoiceType::class, array(
|
||||
'choices' => array_combine(array_values($choices),array_keys($choices)),
|
||||
return $builder->add(self::KEY, ChoiceType::class, [
|
||||
'choices' => array_combine(array_values($choices), array_keys($choices)),
|
||||
'label' => 'Options key',
|
||||
));
|
||||
|
||||
]);
|
||||
}
|
||||
|
||||
public function deserialize($serialized, \Chill\CustomFieldsBundle\Entity\CustomField $customField)
|
||||
public function deserialize($serialized, CustomField $customField)
|
||||
{
|
||||
if ($serialized === NULL) {
|
||||
return NULL;
|
||||
if (null === $serialized) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return $this->optionRepository->find($serialized);
|
||||
}
|
||||
|
||||
@@ -97,32 +107,31 @@ class CustomFieldLongChoice extends AbstractCustomField
|
||||
return 'Long choice field';
|
||||
}
|
||||
|
||||
public function render($value, \Chill\CustomFieldsBundle\Entity\CustomField $customField, $documentType = 'html')
|
||||
public function render($value, CustomField $customField, $documentType = 'html')
|
||||
{
|
||||
$option = $this->deserialize($value, $customField);
|
||||
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice_long.'
|
||||
.$documentType.'.twig';
|
||||
. $documentType . '.twig';
|
||||
|
||||
return $this->templating
|
||||
->render($template, array(
|
||||
'values' => $option === NULL ? array() : array($option)
|
||||
));
|
||||
->render($template, [
|
||||
'values' => null === $option ? [] : [$option],
|
||||
]);
|
||||
}
|
||||
|
||||
public function serialize($value, \Chill\CustomFieldsBundle\Entity\CustomField $customField)
|
||||
public function serialize($value, CustomField $customField)
|
||||
{
|
||||
if ($value === NULL) {
|
||||
return NULL;
|
||||
if (null === $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$value instanceof Option) {
|
||||
throw new \LogicException('the value should be an instance of '
|
||||
throw new LogicException('the value should be an instance of '
|
||||
. 'Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option, '
|
||||
. is_object($value) ? get_class($value) : gettype($value).' given');
|
||||
. is_object($value) ? get_class($value) : gettype($value) . ' given');
|
||||
}
|
||||
|
||||
// we place the id in array, to allow in the future multiple select
|
||||
return $value->getId();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user