allow to pre-define colors to choose from (#2481)
This commit is contained in:
@@ -240,4 +240,40 @@ class SystemConfiguration implements SystemBundleConfiguration
|
||||
{
|
||||
return $this->getIncrement('timesheet.time_increment', $this->getTimesheetDefaultRoundingEnd(), 0);
|
||||
}
|
||||
|
||||
// ========== Theme configurations ==========
|
||||
|
||||
public function isThemeColorsLimited(): bool
|
||||
{
|
||||
return (bool) $this->find('theme.colors_limited');
|
||||
}
|
||||
|
||||
public function getThemeColorChoices(): ?array
|
||||
{
|
||||
$config = $this->find('theme.color_choices');
|
||||
if (empty($config)) {
|
||||
return null;
|
||||
}
|
||||
$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;
|
||||
}
|
||||
$colors[$key] = $value;
|
||||
}
|
||||
|
||||
return array_unique($colors);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,11 +25,11 @@ class Constants
|
||||
/**
|
||||
* The current release status, either "stable" or "dev"
|
||||
*/
|
||||
public const STATUS = 'dev';
|
||||
public const STATUS = 'stable';
|
||||
/**
|
||||
* The software name
|
||||
*/
|
||||
public const SOFTWARE = 'Kimai 2';
|
||||
public const SOFTWARE = 'Kimai';
|
||||
/**
|
||||
* The release name, will only change for new major version
|
||||
*/
|
||||
|
||||
@@ -14,6 +14,7 @@ use App\Event\SystemConfigurationEvent;
|
||||
use App\Form\Model\Configuration;
|
||||
use App\Form\Model\SystemConfiguration as SystemConfigurationModel;
|
||||
use App\Form\SystemConfigurationForm;
|
||||
use App\Form\Type\ArrayToCommaStringType;
|
||||
use App\Form\Type\DateTimeTextType;
|
||||
use App\Form\Type\DayTimeType;
|
||||
use App\Form\Type\LanguageType;
|
||||
@@ -25,6 +26,7 @@ use App\Form\Type\WeekDaysType;
|
||||
use App\Form\Type\YesNoType;
|
||||
use App\Repository\ConfigurationRepository;
|
||||
use App\Validator\Constraints\AllowedHtmlTags;
|
||||
use App\Validator\Constraints\ColorChoices;
|
||||
use App\Validator\Constraints\DateTimeFormat;
|
||||
use App\Validator\Constraints\TimeFormat;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
|
||||
@@ -429,6 +431,19 @@ final class SystemConfigurationController extends AbstractController
|
||||
->setLabel('theme.tags_create')
|
||||
->setType(CheckboxType::class)
|
||||
->setTranslationDomain('system-configuration'),
|
||||
(new Configuration())
|
||||
->setName('theme.colors_limited')
|
||||
->setLabel('theme.colors_limited')
|
||||
->setType(CheckboxType::class)
|
||||
->setTranslationDomain('system-configuration'),
|
||||
(new Configuration())
|
||||
->setName('theme.color_choices')
|
||||
->setRequired(false)
|
||||
->setLabel('theme.color_choices')
|
||||
->setType(ArrayToCommaStringType::class)
|
||||
->setOptions(['help' => 'help.theme.color_choices'])
|
||||
->setConstraints([new ColorChoices()])
|
||||
->setTranslationDomain('system-configuration'),
|
||||
]),
|
||||
(new SystemConfigurationModel())
|
||||
->setSection(SystemConfigurationModel::SECTION_CALENDAR)
|
||||
|
||||
@@ -410,6 +410,19 @@ class Configuration implements ConfigurationInterface
|
||||
->booleanNode('show_about')
|
||||
->defaultTrue()
|
||||
->end()
|
||||
->booleanNode('colors_limited')
|
||||
->defaultTrue()
|
||||
->end()
|
||||
->scalarNode('color_choices')
|
||||
->defaultValue(implode(',', [
|
||||
Constants::SOFTWARE . '|' . Constants::DEFAULT_COLOR, 'Silver|#c0c0c0', 'Gray|#808080', 'Black|#000000',
|
||||
'Maroon|#800000', 'Brown|#a52a2a', 'Red|#ff0000', 'Orange|#ffa500',
|
||||
'Gold|#ffd700', 'Yellow|#ffff00', 'Peach|#ffdab9', 'Khaki|#f0e68c',
|
||||
'Olive|#808000', 'Lime|#00ff00', 'Jelly|#9acd32', 'Green|#008000', 'Teal|#008080',
|
||||
'Aqua|#00ffff', 'LightBlue|#add8e6', 'DeepSky|#00bfff', 'Dodger|#1e90ff', 'Blue|#0000ff', 'Navy|#000080',
|
||||
'Purple|#800080', 'Fuchsia|#ff00ff', 'Violet|#ee82ee', 'Rose|#ffe4e1', 'Lavender|#E6E6FA'
|
||||
]))
|
||||
->end()
|
||||
->arrayNode('chart')
|
||||
->addDefaultsIfNotSet()
|
||||
->children()
|
||||
|
||||
@@ -45,7 +45,8 @@ class Configuration
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @ORM\Column(name="value", type="string", length=255, nullable=true)
|
||||
* @ORM\Column(name="value", type="string", length=1024, nullable=true)
|
||||
* @Assert\Length(max=1024, allowEmptyString=true)
|
||||
*/
|
||||
private $value;
|
||||
|
||||
|
||||
@@ -9,23 +9,21 @@
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Form\Type\ColorPickerType;
|
||||
use App\Form\Type\ColorChoiceType;
|
||||
use App\Form\Type\DurationType;
|
||||
use App\Form\Type\MetaFieldsCollectionType;
|
||||
use App\Form\Type\YesNoType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
|
||||
trait EntityFormTrait
|
||||
{
|
||||
public function addCommonFields(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('color', ColorPickerType::class, [
|
||||
'required' => false,
|
||||
])
|
||||
;
|
||||
$this->addColor($builder);
|
||||
|
||||
if ($options['include_budget']) {
|
||||
$builder
|
||||
@@ -52,6 +50,39 @@ trait EntityFormTrait
|
||||
]);
|
||||
}
|
||||
|
||||
public function addColor(FormBuilderInterface $builder): void
|
||||
{
|
||||
$builder
|
||||
->add('color', ColorChoiceType::class, [
|
||||
'required' => false,
|
||||
])
|
||||
;
|
||||
|
||||
// this code exists only for backward compatibility
|
||||
$builder->addEventListener(
|
||||
FormEvents::PRE_SET_DATA,
|
||||
function (FormEvent $event) use ($builder) {
|
||||
if (!$builder->get('color')->hasOption('choices')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$data = $event->getData();
|
||||
$choices = $builder->get('color')->getOption('choices');
|
||||
if (\is_object($data) && method_exists($data, 'getColor')) {
|
||||
$color = $data->getColor();
|
||||
if (!empty($color) && array_search($color, $choices) === false) {
|
||||
$choices[$color] = $color;
|
||||
}
|
||||
}
|
||||
|
||||
$event->getForm()->add('color', ColorChoiceType::class, [
|
||||
'required' => false,
|
||||
'choices' => $choices,
|
||||
]);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function addCreateMore(FormBuilderInterface $builder): void
|
||||
{
|
||||
$builder->add('create_more', CheckboxType::class, [
|
||||
|
||||
42
src/Form/Type/ArrayToCommaStringType.php
Normal file
42
src/Form/Type/ArrayToCommaStringType.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\CallbackTransformer;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
class ArrayToCommaStringType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->addModelTransformer(new CallbackTransformer(
|
||||
function ($value) {
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
},
|
||||
function ($value) {
|
||||
return $value;
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
return TextType::class;
|
||||
}
|
||||
}
|
||||
115
src/Form/Type/ColorChoiceType.php
Normal file
115
src/Form/Type/ColorChoiceType.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Form\Type;
|
||||
|
||||
use App\Configuration\SystemConfiguration;
|
||||
use App\Constants;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ColorType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class ColorChoiceType extends AbstractType implements DataTransformerInterface
|
||||
{
|
||||
public const DEFAULT_COLOR = Constants::DEFAULT_COLOR;
|
||||
|
||||
private $systemConfiguration;
|
||||
/**
|
||||
* @var bool|null
|
||||
*/
|
||||
private $limitedColors;
|
||||
|
||||
public function __construct(SystemConfiguration $systemConfiguration)
|
||||
{
|
||||
$this->systemConfiguration = $systemConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->addViewTransformer($this);
|
||||
}
|
||||
|
||||
private function isLimitedColors(): bool
|
||||
{
|
||||
if (null === $this->limitedColors) {
|
||||
$this->limitedColors = $this->systemConfiguration->isThemeColorsLimited();
|
||||
}
|
||||
|
||||
return $this->limitedColors;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$options = [
|
||||
'documentation' => [
|
||||
'type' => 'string',
|
||||
'description' => sprintf('The hexadecimal color code (default: %s)', self::DEFAULT_COLOR),
|
||||
],
|
||||
'label' => 'label.color',
|
||||
'empty_data' => null,
|
||||
];
|
||||
|
||||
if ($this->isLimitedColors()) {
|
||||
$choices = [];
|
||||
foreach ($this->systemConfiguration->getThemeColorChoices() as $name => $color) {
|
||||
$choices[$name] = $color;
|
||||
}
|
||||
$options['choices'] = $choices;
|
||||
$options['search'] = false;
|
||||
$options['attr']['data-renderer'] = 'color';
|
||||
}
|
||||
|
||||
$resolver->setDefaults($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transform($data)
|
||||
{
|
||||
if (empty($data) && !$this->isLimitedColors()) {
|
||||
return self::DEFAULT_COLOR;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function reverseTransform($data)
|
||||
{
|
||||
if (null === $data && !$this->isLimitedColors()) {
|
||||
return self::DEFAULT_COLOR;
|
||||
}
|
||||
|
||||
return null === $data ? null : $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
if ($this->isLimitedColors()) {
|
||||
return ChoiceType::class;
|
||||
}
|
||||
|
||||
return ColorType::class;
|
||||
}
|
||||
}
|
||||
40
src/Migrations/Version20210405105611.php
Normal file
40
src/Migrations/Version20210405105611.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use App\Doctrine\AbstractMigration;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
|
||||
/**
|
||||
* Increase the configuration table column length
|
||||
*
|
||||
* @version 1.14
|
||||
*/
|
||||
final class Version20210405105611 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Increase the configuration table column length';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$invoices = $schema->getTable('kimai2_configuration');
|
||||
$invoices->getColumn('value')->setLength(1024);
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$invoices = $schema->getTable('kimai2_configuration');
|
||||
$invoices->getColumn('value')->setLength(255);
|
||||
}
|
||||
}
|
||||
26
src/Validator/Constraints/ColorChoices.php
Normal file
26
src/Validator/Constraints/ColorChoices.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
class ColorChoices extends Constraint
|
||||
{
|
||||
public const COLOR_CHOICES_ERROR = 'ui5hffg-dsfef3-1234-5678-2g8jkfr56d84';
|
||||
public const COLOR_CHOICES_NAME_ERROR = 'ui5hffg-dsfef3-1234-5679-2g8jkfr56d84';
|
||||
|
||||
protected static $errorNames = [
|
||||
self::COLOR_CHOICES_ERROR => 'COLOR_CHOICES_ERROR',
|
||||
self::COLOR_CHOICES_NAME_ERROR => 'COLOR_CHOICES_NAME_ERROR',
|
||||
];
|
||||
|
||||
public $message = 'The given value {{ value }} is not a valid hexadecimal color.';
|
||||
public $invalidNameMessage = 'The given value {{ name }} is not a valid color name for {{ color }}. Allowed are {{ max }} characters, given {{ count }}.';
|
||||
}
|
||||
71
src/Validator/Constraints/ColorChoicesValidator.php
Normal file
71
src/Validator/Constraints/ColorChoicesValidator.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
class ColorChoicesValidator extends ConstraintValidator
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (!$constraint instanceof ColorChoices) {
|
||||
throw new UnexpectedTypeException($constraint, ColorChoices::class);
|
||||
}
|
||||
|
||||
$color = $value;
|
||||
|
||||
if ($color === null || (\is_string($color) && empty(trim($color)))) {
|
||||
return;
|
||||
}
|
||||
|
||||
$colors = explode(',', $color);
|
||||
|
||||
foreach ($colors as $color) {
|
||||
$color = explode('|', $color);
|
||||
$name = $color[0];
|
||||
$code = $color[0];
|
||||
if (\count($color) > 1) {
|
||||
$code = $color[1];
|
||||
}
|
||||
|
||||
if (empty($name)) {
|
||||
$name = $code;
|
||||
}
|
||||
|
||||
if (!\is_string($code) || 1 !== preg_match('/^#[0-9a-fA-F]{6}$/i', $code)) {
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->setParameter('{{ value }}', $this->formatValue($code))
|
||||
->setCode(ColorChoices::COLOR_CHOICES_ERROR)
|
||||
->addViolation();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($name === $code) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!\is_string($name) || 1 !== preg_match('/^[0-9a-zA-Z]{1,10}$/i', $name)) {
|
||||
$this->context->buildViolation($constraint->invalidNameMessage)
|
||||
->setParameter('{{ name }}', $this->formatValue($name))
|
||||
->setParameter('{{ color }}', $this->formatValue($code))
|
||||
->setParameter('{{ max }}', $this->formatValue(10))
|
||||
->setParameter('{{ count }}', $this->formatValue(\strlen($name)))
|
||||
->setCode(ColorChoices::COLOR_CHOICES_NAME_ERROR)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user