added hourly and money budgets to activity, project and customer (#843)

This commit is contained in:
Kevin Papst
2019-06-11 13:18:57 +02:00
committed by GitHub
parent 833968a87d
commit 5702d7afa8
118 changed files with 1743 additions and 1077 deletions

View File

@@ -10,16 +10,12 @@
namespace App\Form;
use App\Entity\Activity;
use App\Form\Type\ColorPickerType;
use App\Entity\Customer;
use App\Form\Type\CustomerType;
use App\Form\Type\FixedRateType;
use App\Form\Type\HourlyRateType;
use App\Form\Type\ProjectType;
use App\Form\Type\YesNoType;
use App\Repository\CustomerRepository;
use App\Repository\ProjectRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
@@ -27,11 +23,10 @@ use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Defines the form used to manipulate Activities.
*/
class ActivityEditForm extends AbstractType
{
use EntityFormTrait;
/**
* {@inheritdoc}
*/
@@ -39,7 +34,6 @@ class ActivityEditForm extends AbstractType
{
$project = null;
$customer = null;
$currency = false;
$id = null;
if (isset($options['data'])) {
@@ -49,21 +43,19 @@ class ActivityEditForm extends AbstractType
if (null !== $entry->getProject()) {
$project = $entry->getProject();
$customer = $project->getCustomer();
$currency = $customer->getCurrency();
$options['currency'] = $customer->getCurrency();
}
$id = $entry->getId();
}
$builder
// string - length 255
->add('name', TextType::class, [
'label' => 'label.name',
'attr' => [
'autofocus' => 'autofocus'
],
])
// text
->add('comment', TextareaType::class, [
'label' => 'label.comment',
'required' => false,
@@ -109,26 +101,10 @@ class ActivityEditForm extends AbstractType
}
);
$builder
->add('color', ColorPickerType::class)
->add('fixedRate', FixedRateType::class, [
'currency' => $currency,
])
->add('hourlyRate', HourlyRateType::class, [
'currency' => $currency,
])
// boolean
->add('visible', YesNoType::class, [
'label' => 'label.visible',
])
;
$this->addCommonFields($builder, $options);
if (null === $id && $options['create_more']) {
$builder->add('create_more', CheckboxType::class, [
'label' => 'label.create_more',
'required' => false,
'mapped' => false,
]);
$this->addCreateMore($builder);
}
}
@@ -144,6 +120,8 @@ class ActivityEditForm extends AbstractType
'csrf_token_id' => 'admin_activity_edit',
'create_more' => false,
'customer' => false,
'currency' => Customer::DEFAULT_CURRENCY,
'include_budget' => false,
'attr' => [
'data-form-event' => 'kimai.activityUpdate'
],

View File

@@ -10,10 +10,6 @@
namespace App\Form;
use App\Entity\Customer;
use App\Form\Type\ColorPickerType;
use App\Form\Type\FixedRateType;
use App\Form\Type\HourlyRateType;
use App\Form\Type\YesNoType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
@@ -26,22 +22,19 @@ use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Defines the form used to edit Customer entities.
*/
class CustomerEditForm extends AbstractType
{
use EntityFormTrait;
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$currency = false;
if (isset($options['data'])) {
/** @var Customer $customer */
$customer = $options['data'];
$currency = $customer->getCurrency();
$options['currency'] = $customer->getCurrency();
}
$builder
@@ -101,18 +94,9 @@ class CustomerEditForm extends AbstractType
])
->add('timezone', TimezoneType::class, [
'label' => 'label.timezone',
])
->add('color', ColorPickerType::class)
->add('fixedRate', FixedRateType::class, [
'currency' => $currency ?? false,
])
->add('hourlyRate', HourlyRateType::class, [
'currency' => $currency ?? false,
])
->add('visible', YesNoType::class, [
'label' => 'label.visible',
])
;
]);
$this->addCommonFields($builder, $options);
}
/**
@@ -125,6 +109,8 @@ class CustomerEditForm extends AbstractType
'csrf_protection' => true,
'csrf_field_name' => '_token',
'csrf_token_id' => 'admin_customer_edit',
'currency' => Customer::DEFAULT_CURRENCY,
'include_budget' => false,
'attr' => [
'data-form-event' => 'kimai.customerUpdate'
],

View File

@@ -0,0 +1,73 @@
<?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\DataTransformer;
use App\Utils\Duration;
use App\Validator\Constraints\Duration as DurationConstraint;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class DurationStringToSecondsTransformer implements DataTransformerInterface
{
/**
* @var Duration
*/
protected $formatter;
/**
* @var string
*/
private $pattern;
public function __construct()
{
$this->formatter = new Duration();
$constraint = new DurationConstraint();
$this->pattern = $constraint->pattern;
}
/**
* @param int $intToFormat
* @return string|null
*/
public function transform($intToFormat)
{
try {
return $this->formatter->format($intToFormat);
} catch (\Exception $e) {
throw new TransformationFailedException($e->getMessage());
}
}
/**
* @param string $formatToInt
* @return int|null
*/
public function reverseTransform($formatToInt)
{
if (null === $formatToInt) {
return null;
}
if (empty($formatToInt)) {
return 0;
}
// we need this one here, because the data transformer is executed BEFORE the constraint is called
if (!preg_match($this->pattern, $formatToInt)) {
throw new TransformationFailedException('Invalid duration format given');
}
try {
return $this->formatter->parseDurationString($formatToInt);
} catch (\Exception $e) {
throw new TransformationFailedException($e->getMessage());
}
}
}

View File

@@ -36,7 +36,7 @@ class TagArrayToStringTransformer implements DataTransformerInterface
*
* @return string
*/
public function transform($tags): string
public function transform($tags)
{
if (empty($tags)) {
return '';
@@ -53,7 +53,7 @@ class TagArrayToStringTransformer implements DataTransformerInterface
* @return Tag[]
* @throws TransformationFailedException if object (issue) is not found
*/
public function reverseTransform($stringOfTags): array
public function reverseTransform($stringOfTags)
{
// check for empty tag list
if (empty($stringOfTags)) {

View File

@@ -0,0 +1,64 @@
<?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;
use App\Form\Type\ColorPickerType;
use App\Form\Type\DurationType;
use App\Form\Type\FixedRateType;
use App\Form\Type\HourlyRateType;
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;
trait EntityFormTrait
{
public function addCommonFields(FormBuilderInterface $builder, array $options): void
{
$currency = $options['currency'];
$builder
->add('color', ColorPickerType::class)
->add('fixedRate', FixedRateType::class, [
'currency' => $currency,
])
->add('hourlyRate', HourlyRateType::class, [
'currency' => $currency,
])
;
if ($options['include_budget']) {
$builder
->add('budget', MoneyType::class, [
'label' => 'label.budget',
'required' => false,
'currency' => $currency,
])
->add('timeBudget', DurationType::class, [
'label' => 'label.timeBudget',
'required' => false,
])
;
}
$builder
->add('visible', YesNoType::class, [
'label' => 'label.visible',
]);
}
public function addCreateMore(FormBuilderInterface $builder): void
{
$builder->add('create_more', CheckboxType::class, [
'label' => 'label.create_more',
'required' => false,
'mapped' => false,
]);
}
}

View File

@@ -11,32 +11,24 @@ namespace App\Form;
use App\Entity\Customer;
use App\Entity\Project;
use App\Form\Type\ColorPickerType;
use App\Form\Type\CustomerType;
use App\Form\Type\FixedRateType;
use App\Form\Type\HourlyRateType;
use App\Form\Type\YesNoType;
use App\Repository\CustomerRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Defines the form used to edit Projects.
*/
class ProjectEditForm extends AbstractType
{
use EntityFormTrait;
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$customer = null;
$currency = false;
$id = null;
if (isset($options['data'])) {
@@ -46,7 +38,7 @@ class ProjectEditForm extends AbstractType
if ($id !== null) {
$customer = $entry->getCustomer();
$currency = $customer->getCurrency();
$options['currency'] = $customer->getCurrency();
}
}
@@ -69,30 +61,12 @@ class ProjectEditForm extends AbstractType
'query_builder' => function (CustomerRepository $repo) use ($customer) {
return $repo->builderForEntityType($customer);
},
])
->add('color', ColorPickerType::class)
->add('fixedRate', FixedRateType::class, [
'currency' => $currency,
])
->add('hourlyRate', HourlyRateType::class, [
'currency' => $currency,
])
->add('budget', MoneyType::class, [
'label' => 'label.budget',
'required' => false,
'currency' => $currency,
])
->add('visible', YesNoType::class, [
'label' => 'label.visible',
])
;
]);
$this->addCommonFields($builder, $options);
if (null === $id && $options['create_more']) {
$builder->add('create_more', CheckboxType::class, [
'label' => 'label.create_more',
'required' => false,
'mapped' => false,
]);
$this->addCreateMore($builder);
}
}
@@ -107,6 +81,7 @@ class ProjectEditForm extends AbstractType
'csrf_field_name' => '_token',
'csrf_token_id' => 'admin_project_edit',
'currency' => Customer::DEFAULT_CURRENCY,
'include_budget' => false,
'create_more' => false,
'attr' => [
'data-form-event' => 'kimai.projectUpdate'

View File

@@ -9,42 +9,18 @@
namespace App\Form\Type;
use App\Utils\Duration;
use App\Form\DataTransformer\DurationStringToSecondsTransformer;
use App\Validator\Constraints\Duration as DurationConstraint;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Regex;
/**
* Custom form field type to handle a timesheet duration.
* Custom form field type to handle duration strings.
*/
class DurationType extends AbstractType
{
/**
* @var string
*/
protected $pattern;
/**
* DurationType constructor.
*/
public function __construct()
{
$patterns = [
'[0-9]{1,}',
'[0-9]{1,}:[0-9]{1,2}:[0-9]{1,2}',
'[0-9]{1,2}:[0-9]{1,2}',
'[0-9]{1,}[hmsHMS]{1}',
'[0-9]{1,}[hmsHMS]{1}[0-9]{1,}[hmsHMS]{1}',
'[0-9]{1,}[hmsHMS]{1}[0-9]{1,}[hmsHMS]{1}[0-9]{1,}[hmsHMS]{1}',
];
$this->pattern = '/^' . implode('$|^', $patterns) . '$/';
}
/**
* {@inheritdoc}
*/
@@ -52,7 +28,7 @@ class DurationType extends AbstractType
{
$resolver->setDefaults([
'label' => 'label.duration',
'constraints' => [new Regex(['pattern' => $this->pattern])],
'constraints' => [new DurationConstraint()],
]);
}
@@ -61,37 +37,7 @@ class DurationType extends AbstractType
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$formatter = new Duration();
$pattern = $this->pattern;
$builder->addModelTransformer(new CallbackTransformer(
function ($intToFormat) use ($formatter) {
try {
return $formatter->format($intToFormat);
} catch (\Exception $e) {
throw new TransformationFailedException($e->getMessage());
}
},
function ($formatToInt) use ($formatter, $pattern) {
if (null === $formatToInt) {
return null;
}
if (empty($formatToInt)) {
return 0;
}
if (!preg_match($pattern, $formatToInt)) {
throw new TransformationFailedException('Invalid duration format given');
}
try {
return $formatter->parseDurationString($formatToInt);
} catch (\Exception $e) {
throw new TransformationFailedException($e->getMessage());
}
}
));
$builder->addModelTransformer(new DurationStringToSecondsTransformer());
}
/**