Release 2.18 (#4878)

This commit is contained in:
Kevin Papst
2024-06-16 13:15:49 +02:00
committed by GitHub
parent 8792a1df09
commit 987b46bf8f
46 changed files with 1768 additions and 814 deletions

View File

@@ -37,7 +37,22 @@ final class TagArrayToStringTransformer implements DataTransformerInterface
return '';
}
return implode(', ', $value);
if (!\is_array($value)) {
return '';
}
$result = [];
foreach ($value as $item) {
if ($item instanceof Tag) {
$result[] = $item->getName();
} elseif (\is_string($item)) {
$result[] = $item;
} else {
throw new TransformationFailedException('Tags must only contain a Tag or a string.');
}
}
return implode(',', $result);
}
/**
@@ -55,6 +70,7 @@ final class TagArrayToStringTransformer implements DataTransformerInterface
if ('' === $value || null === $value) {
return [];
}
if (!\is_array($value)) {
$names = array_filter(array_unique(array_map('trim', explode(',', $value))));
} else {
@@ -68,18 +84,12 @@ final class TagArrayToStringTransformer implements DataTransformerInterface
}
$tagName = trim($tagName);
$tag = null;
if (is_numeric($tagName)) {
$tag = $this->tagRepository->find($tagName);
}
if ($tag === null) {
$tag = $this->tagRepository->findTagByName($tagName);
}
// do not check for numeric values as ID, this form type only submits tag names
$tag = $this->tagRepository->findTagByName($tagName);
// get the current tags and find the new ones that should be created
if ($this->create && $tag === null) {
if ($tag === null && $this->create) {
$tag = new Tag();
$tag->setName(mb_substr($tagName, 0, 100));
$this->tagRepository->saveTag($tag);

View File

@@ -10,7 +10,6 @@
namespace App\Form\Type;
use App\Configuration\SystemConfiguration;
use App\Constants;
use App\Utils\Color;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
@@ -22,7 +21,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
final class ColorChoiceType extends AbstractType implements DataTransformerInterface
{
public function __construct(private SystemConfiguration $systemConfiguration)
public function __construct(private readonly SystemConfiguration $systemConfiguration)
{
}
@@ -36,7 +35,7 @@ final class ColorChoiceType extends AbstractType implements DataTransformerInter
$options = [
'documentation' => [
'type' => 'string',
'description' => sprintf('The hexadecimal color code (default: %s)', Constants::DEFAULT_COLOR),
'description' => 'The hexadecimal color code (default: auto-calculated by name)',
],
'label' => 'color',
'empty_data' => null,
@@ -50,7 +49,7 @@ final class ColorChoiceType extends AbstractType implements DataTransformerInter
];
$choices = [];
$colors = $this->convertStringToColorArray($this->systemConfiguration->getThemeColorChoices());
$colors = $this->systemConfiguration->getThemeColors();
foreach ($colors as $name => $color) {
$choices[$name] = $color;
@@ -69,44 +68,9 @@ final class ColorChoiceType extends AbstractType implements DataTransformerInter
]);
}
/**
* @param string $config
* @return array<string, string>
*/
private function convertStringToColorArray(string $config): array
public function transform(mixed $value): mixed
{
$config = explode(',', $config);
$colors = [];
foreach ($config as $item) {
if (empty($item)) {
continue;
}
$item = explode('|', $item);
$key = $item[0];
$value = $key;
if (\count($item) > 1) {
$value = $item[1];
}
if (empty($key)) {
$key = $value;
}
if ($value === Constants::DEFAULT_COLOR) {
continue;
}
$colors[$key] = $value;
}
return array_unique($colors);
}
public function transform(mixed $data): mixed
{
return $data;
return $value;
}
public function reverseTransform(mixed $value): mixed

View File

@@ -54,11 +54,12 @@ final class TagsInputType extends AbstractType
public function buildView(FormView $view, FormInterface $form, array $options): void
{
$view->vars['attr'] = array_merge($view->vars['attr'], [
'data-autocomplete-url' => $this->router->generate('get_tags'),
'data-autocomplete-url' => $this->router->generate('get_tags_full'),
'data-minimum-character' => 3,
'class' => 'form-select',
'autocomplete' => 'off',
'data-form-widget' => 'autocomplete'
'data-form-widget' => 'tags',
'data-renderer' => 'color',
]);
if ($options['allow_create']) {

View File

@@ -12,7 +12,6 @@ namespace App\Form\Type;
use App\Entity\Tag;
use App\Repository\Query\TagFormTypeQuery;
use App\Repository\TagRepository;
use App\Utils\Color;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
@@ -87,17 +86,11 @@ final class TagsSelectType extends AbstractType
return $tag->getId();
},
'choice_attr' => function (Tag $tag) {
$color = $tag->getColor();
if ($color === null) {
$color = (new Color())->getRandom($tag->getName());
}
return ['data-color' => $color];
return ['data-color' => $tag->getColorSafe()];
},
'choice_label' => function (Tag $tag) {
return $tag->getName();
},
'attr' => ['data-renderer' => 'color'],
]);
$resolver->setDefault('query_builder', function (Options $options) {
@@ -119,6 +112,10 @@ final class TagsSelectType extends AbstractType
'data-create' => 'post_tag',
]);
}
$view->vars['attr'] = array_merge($view->vars['attr'], [
'data-renderer' => 'color',
]);
}
public function getParent(): string

View File

@@ -16,6 +16,11 @@ use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
final class TagsType extends AbstractType
{
/**
* See KimaiFormSelect.js (maxOptions) as well.
*/
public const MAX_AMOUNT_SELECT = 500;
private ?int $count = null;
public function __construct(
@@ -37,7 +42,7 @@ final class TagsType extends AbstractType
$this->count = $this->repository->count([]);
}
if ($this->count > TagRepository::MAX_AMOUNT_SELECT) {
if ($this->count > self::MAX_AMOUNT_SELECT) {
return TagsInputType::class;
}