support custom fields in timesheet batch update (#2043)
This commit is contained in:
@@ -270,6 +270,15 @@ abstract class TimesheetAbstractController extends AbstractController
|
||||
$dto->setEntities($form->getData()->getEntities());
|
||||
}
|
||||
|
||||
// using a new timesheet to make sure we ONLY use meta-fields which are registered via events
|
||||
$fake = new Timesheet();
|
||||
$event = new TimesheetMetaDefinitionEvent($fake);
|
||||
$this->dispatcher->dispatch($event);
|
||||
|
||||
foreach ($fake->getMetaFields() as $field) {
|
||||
$dto->setMetaField(clone $field);
|
||||
}
|
||||
|
||||
$form = $this->getMultiUpdateForm($dto);
|
||||
$form->handleRequest($request);
|
||||
|
||||
@@ -342,6 +351,17 @@ abstract class TimesheetAbstractController extends AbstractController
|
||||
$timesheet->setHourlyRate($dto->getHourlyRate());
|
||||
$execute = true;
|
||||
}
|
||||
|
||||
foreach ($dto->getUpdateMeta() as $metaName) {
|
||||
if (null !== ($metaField = $dto->getMetaField($metaName))) {
|
||||
if (null !== ($timesheetMeta = $timesheet->getMetaField($metaName))) {
|
||||
$timesheetMeta->setValue($metaField->getValue());
|
||||
} else {
|
||||
$timesheet->setMetaField(clone $metaField);
|
||||
}
|
||||
$execute = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($execute) {
|
||||
|
||||
@@ -48,10 +48,6 @@ final class EnhancedChoiceTypeExtension extends AbstractTypeExtension
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($view->vars['attr'])) {
|
||||
$view->vars['attr'] = [];
|
||||
}
|
||||
|
||||
$extendedOptions = ['class' => 'selectpicker'];
|
||||
|
||||
if (false !== $options['width']) {
|
||||
|
||||
@@ -16,6 +16,7 @@ use App\Form\Type\InvoiceRendererType;
|
||||
use App\Form\Type\LanguageType;
|
||||
use App\Form\Type\YesNoType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
@@ -62,16 +63,16 @@ class InvoiceTemplateForm extends AbstractType
|
||||
'label' => 'label.invoice_bank_account',
|
||||
'required' => false,
|
||||
])
|
||||
->add('dueDays', TextType::class, [
|
||||
->add('dueDays', IntegerType::class, [
|
||||
'label' => 'label.due_days',
|
||||
])
|
||||
->add('vat', NumberType::class, [
|
||||
'label' => 'label.vat',
|
||||
'scale' => 2,
|
||||
])
|
||||
->add('renderer', InvoiceRendererType::class, [])
|
||||
->add('calculator', InvoiceCalculatorType::class, [])
|
||||
->add('numberGenerator', InvoiceNumberGeneratorType::class, [])
|
||||
->add('renderer', InvoiceRendererType::class)
|
||||
->add('calculator', InvoiceCalculatorType::class)
|
||||
->add('numberGenerator', InvoiceNumberGeneratorType::class)
|
||||
->add('language', LanguageType::class, [
|
||||
'required' => false,
|
||||
])
|
||||
|
||||
@@ -13,6 +13,7 @@ use App\Form\Type\ActivityType;
|
||||
use App\Form\Type\CustomerType;
|
||||
use App\Form\Type\FixedRateType;
|
||||
use App\Form\Type\HourlyRateType;
|
||||
use App\Form\Type\MetaFieldsCollectionType;
|
||||
use App\Form\Type\ProjectType;
|
||||
use App\Form\Type\TagsType;
|
||||
use App\Form\Type\UserType;
|
||||
@@ -60,6 +61,7 @@ class TimesheetMultiUpdate extends AbstractType
|
||||
$project = null;
|
||||
$customer = null;
|
||||
$currency = null;
|
||||
$entry = null;
|
||||
$customerCount = $this->customers->countCustomer(true);
|
||||
|
||||
if (isset($options['data'])) {
|
||||
@@ -119,6 +121,7 @@ class TimesheetMultiUpdate extends AbstractType
|
||||
);
|
||||
|
||||
// replaces the project select after submission, to make sure only projects for the selected customer are displayed
|
||||
// TODO replace me with FormTrait
|
||||
$builder->addEventListener(
|
||||
FormEvents::PRE_SUBMIT,
|
||||
function (FormEvent $event) use ($builder, $project, $customer) {
|
||||
@@ -146,12 +149,14 @@ class TimesheetMultiUpdate extends AbstractType
|
||||
'required' => false,
|
||||
'placeholder' => '',
|
||||
'query_builder' => function (ActivityRepository $repo) use ($activity, $project) {
|
||||
// TODO respect user (team permission)
|
||||
return $repo->getQueryBuilderForFormType(new ActivityFormTypeQuery($activity, $project));
|
||||
},
|
||||
])
|
||||
;
|
||||
|
||||
// replaces the activity select after submission, to make sure only activities for the selected project are displayed
|
||||
// TODO replace me with FormTrait
|
||||
$builder->addEventListener(
|
||||
FormEvents::PRE_SUBMIT,
|
||||
function (FormEvent $event) use ($activity) {
|
||||
@@ -164,6 +169,7 @@ class TimesheetMultiUpdate extends AbstractType
|
||||
'required' => false,
|
||||
'placeholder' => '',
|
||||
'query_builder' => function (ActivityRepository $repo) use ($data, $activity) {
|
||||
// TODO respect user (team permission)
|
||||
return $repo->getQueryBuilderForFormType(new ActivityFormTypeQuery($activity, $data['project']));
|
||||
},
|
||||
]);
|
||||
@@ -215,6 +221,25 @@ class TimesheetMultiUpdate extends AbstractType
|
||||
;
|
||||
}
|
||||
|
||||
// meta fields only if at least one exists
|
||||
if ($entry !== null && $entry->getMetaFields()->count() > 0) {
|
||||
$builder->add('metaFields', MetaFieldsCollectionType::class);
|
||||
|
||||
$choices = [];
|
||||
foreach ($entry->getMetaFields() as $field) {
|
||||
$name = $field->getName();
|
||||
$label = $entry->getMetaField($name)->getLabel();
|
||||
$choices[$label] = $name;
|
||||
}
|
||||
$builder->add('updateMeta', ChoiceType::class, [
|
||||
'choices' => $choices,
|
||||
'multiple' => true,
|
||||
'expanded' => true,
|
||||
'label' => 'label.batch_meta_fields',
|
||||
'help' => 'help.batch_meta_fields',
|
||||
]);
|
||||
}
|
||||
|
||||
$builder->add('entities', HiddenType::class, [
|
||||
'required' => false,
|
||||
]);
|
||||
|
||||
@@ -11,15 +11,19 @@ namespace App\Form\MultiUpdate;
|
||||
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\EntityWithMetaFields;
|
||||
use App\Entity\MetaTableTypeInterface;
|
||||
use App\Entity\Project;
|
||||
use App\Entity\Tag;
|
||||
use App\Entity\TimesheetMeta;
|
||||
use App\Entity\User;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
|
||||
/**
|
||||
* @App\Validator\Constraints\TimesheetMultiUpdate
|
||||
*/
|
||||
class TimesheetMultiUpdateDTO extends MultiUpdateTableDTO
|
||||
class TimesheetMultiUpdateDTO extends MultiUpdateTableDTO implements EntityWithMetaFields
|
||||
{
|
||||
/**
|
||||
* @var Tag[]|ArrayCollection|iterable
|
||||
@@ -61,6 +65,19 @@ class TimesheetMultiUpdateDTO extends MultiUpdateTableDTO
|
||||
* @var float|null
|
||||
*/
|
||||
private $hourlyRate = null;
|
||||
/**
|
||||
* @var TimesheetMeta[]|Collection
|
||||
*/
|
||||
private $meta;
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $updateMeta = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->meta = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getCustomer(): ?Customer
|
||||
{
|
||||
@@ -184,4 +201,52 @@ class TimesheetMultiUpdateDTO extends MultiUpdateTableDTO
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TimesheetMeta[]|Collection
|
||||
*/
|
||||
public function getMetaFields(): Collection
|
||||
{
|
||||
return $this->meta;
|
||||
}
|
||||
|
||||
public function getMetaField(string $name): ?MetaTableTypeInterface
|
||||
{
|
||||
foreach ($this->meta as $field) {
|
||||
if (strtolower($field->getName()) === strtolower($name)) {
|
||||
return $field;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function setMetaField(MetaTableTypeInterface $meta): EntityWithMetaFields
|
||||
{
|
||||
$this->updateMeta[$meta->getName()] = $meta->getName();
|
||||
if (null === ($current = $this->getMetaField($meta->getName()))) {
|
||||
$this->meta->add($meta);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$current->merge($meta);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setUpdateMeta(array $names): EntityWithMetaFields
|
||||
{
|
||||
$this->updateMeta = $names;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getUpdateMeta(): array
|
||||
{
|
||||
return $this->updateMeta;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,12 +146,23 @@ abstract class AbstractToolbarForm extends AbstractType
|
||||
]);
|
||||
}
|
||||
|
||||
protected function addDateRangeChoice(FormBuilderInterface $builder, $allowEmpty = true, $required = false)
|
||||
protected function addDateRange(FormBuilderInterface $builder, array $options, $allowEmpty = true, $required = false)
|
||||
{
|
||||
$builder->add('daterange', DateRangeType::class, [
|
||||
$params = [
|
||||
'required' => $required,
|
||||
'allow_empty' => $allowEmpty,
|
||||
]);
|
||||
];
|
||||
|
||||
if (\array_key_exists('timezone', $options)) {
|
||||
$params['timezone'] = $options['timezone'];
|
||||
}
|
||||
|
||||
$builder->add('daterange', DateRangeType::class, $params);
|
||||
}
|
||||
|
||||
protected function addDateRangeChoice(FormBuilderInterface $builder, $allowEmpty = true, $required = false)
|
||||
{
|
||||
$this->addDateRange($builder, [], $allowEmpty, $required);
|
||||
}
|
||||
|
||||
protected function addProjectChoice(FormBuilderInterface $builder, array $options = [], bool $multiCustomer = false, bool $multiActivity = false)
|
||||
|
||||
@@ -13,7 +13,8 @@ use App\Timesheet\UserDateTimeFactory;
|
||||
use App\Utils\LocaleSettings;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\DateType;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
@@ -21,20 +22,9 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
*/
|
||||
class DatePickerType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* @var LocaleSettings
|
||||
*/
|
||||
protected $localeSettings;
|
||||
private $localeSettings;
|
||||
private $dateTime;
|
||||
|
||||
/**
|
||||
* @var UserDateTimeFactory
|
||||
*/
|
||||
protected $dateTime;
|
||||
|
||||
/**
|
||||
* @param LocaleSettings $localeSettings
|
||||
* @param UserDateTimeFactory $dateTime
|
||||
*/
|
||||
public function __construct(LocaleSettings $localeSettings, UserDateTimeFactory $dateTime)
|
||||
{
|
||||
$this->localeSettings = $localeSettings;
|
||||
@@ -58,14 +48,16 @@ class DatePickerType extends AbstractType
|
||||
'model_timezone' => $timezone,
|
||||
'view_timezone' => $timezone,
|
||||
]);
|
||||
}
|
||||
|
||||
$resolver->setDefault('attr', function (Options $options) {
|
||||
return [
|
||||
'autocomplete' => 'off',
|
||||
'placeholder' => $options['format'],
|
||||
'data-format' => $options['format_picker'],
|
||||
];
|
||||
});
|
||||
public function buildView(FormView $view, FormInterface $form, array $options)
|
||||
{
|
||||
$view->vars['attr'] = array_merge($view->vars['attr'], [
|
||||
'data-datepickerenable' => 'on',
|
||||
'autocomplete' => 'off',
|
||||
'placeholder' => strtoupper($options['format']),
|
||||
'data-format' => $options['format_picker'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,6 +17,8 @@ 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\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
@@ -30,16 +32,12 @@ class DateRangeType extends AbstractType
|
||||
/**
|
||||
* @var LocaleSettings
|
||||
*/
|
||||
protected $localeSettings;
|
||||
private $localeSettings;
|
||||
/**
|
||||
* @var UserDateTimeFactory
|
||||
*/
|
||||
protected $dateFactory;
|
||||
private $dateFactory;
|
||||
|
||||
/**
|
||||
* @param LocaleSettings $localeSettings
|
||||
* @param UserDateTimeFactory $dateTime
|
||||
*/
|
||||
public function __construct(LocaleSettings $localeSettings, UserDateTimeFactory $dateTime)
|
||||
{
|
||||
$this->localeSettings = $localeSettings;
|
||||
@@ -55,23 +53,23 @@ class DateRangeType extends AbstractType
|
||||
$dateFormat = $this->localeSettings->getDateFormat();
|
||||
|
||||
$resolver->setDefaults([
|
||||
'model_timezone' => null,
|
||||
'view_timezone' => null,
|
||||
'timezone' => date_default_timezone_get(),
|
||||
'label' => 'label.daterange',
|
||||
'format' => $dateFormat,
|
||||
'separator' => self::DATE_SPACER,
|
||||
'format_picker' => $pickerFormat,
|
||||
'allow_empty' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
$resolver->setDefault('attr', function (Options $options) {
|
||||
return [
|
||||
'autocomplete' => 'off',
|
||||
'placeholder' => $options['format_picker'] . $options['separator'] . $options['format_picker'],
|
||||
'data-format' => $options['format_picker'],
|
||||
'data-separator' => $options['separator'],
|
||||
];
|
||||
});
|
||||
public function buildView(FormView $view, FormInterface $form, array $options)
|
||||
{
|
||||
$view->vars['attr'] = array_merge($view->vars['attr'], [
|
||||
'autocomplete' => 'off',
|
||||
'placeholder' => strtoupper($options['format_picker']) . $options['separator'] . strtoupper($options['format_picker']),
|
||||
'data-format' => $options['format_picker'],
|
||||
'data-separator' => $options['separator'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,6 +127,8 @@ class DateRangeType extends AbstractType
|
||||
$formatDate = $options['format'];
|
||||
$separator = $options['separator'];
|
||||
$allowEmpty = $options['allow_empty'];
|
||||
//$timezone = new \DateTimeZone($options['timezone']);
|
||||
$timezone = $this->dateFactory->getTimezone();
|
||||
$pattern = $this->formatToPattern($formatDate, $separator);
|
||||
|
||||
$builder->addModelTransformer(new CallbackTransformer(
|
||||
@@ -152,7 +152,7 @@ class DateRangeType extends AbstractType
|
||||
|
||||
return $display;
|
||||
},
|
||||
function ($dates) use ($formatDate, $pattern, $separator, $allowEmpty) {
|
||||
function ($dates) use ($formatDate, $pattern, $separator, $allowEmpty, $timezone) {
|
||||
$range = new DateRange();
|
||||
|
||||
if (empty($dates) && $allowEmpty) {
|
||||
@@ -171,13 +171,13 @@ class DateRangeType extends AbstractType
|
||||
throw new TransformationFailedException('Invalid date range given');
|
||||
}
|
||||
|
||||
$begin = \DateTime::createFromFormat($formatDate, $values[0], $this->dateFactory->getTimezone());
|
||||
$begin = \DateTime::createFromFormat($formatDate, $values[0], $timezone);
|
||||
if ($begin === false) {
|
||||
throw new TransformationFailedException('Invalid begin date given');
|
||||
}
|
||||
$range->setBegin($begin);
|
||||
|
||||
$end = \DateTime::createFromFormat($formatDate, $values[1], $this->dateFactory->getTimezone());
|
||||
$end = \DateTime::createFromFormat($formatDate, $values[1], $timezone);
|
||||
if ($end === false) {
|
||||
throw new TransformationFailedException('Invalid end date given');
|
||||
}
|
||||
|
||||
@@ -14,7 +14,8 @@ use App\Timesheet\UserDateTimeFactory;
|
||||
use App\Utils\LocaleSettings;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
@@ -61,22 +62,17 @@ class DateTimePickerType extends AbstractType
|
||||
'with_seconds' => false,
|
||||
'model_timezone' => $timezone,
|
||||
'view_timezone' => $timezone,
|
||||
'autofocus' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
$resolver->setDefault('attr', function (Options $options) {
|
||||
$values = [
|
||||
'data-datetimepicker' => 'on',
|
||||
'autocomplete' => 'off',
|
||||
'placeholder' => $options['format'],
|
||||
'data-format' => $options['format_picker'],
|
||||
];
|
||||
if ($options['autofocus']) {
|
||||
$values['autofocus'] = 'autofocus';
|
||||
}
|
||||
|
||||
return $values;
|
||||
});
|
||||
public function buildView(FormView $view, FormInterface $form, array $options)
|
||||
{
|
||||
$view->vars['attr'] = array_merge($view->vars['attr'], [
|
||||
'data-datetimepicker' => 'on',
|
||||
'autocomplete' => 'off',
|
||||
'placeholder' => strtoupper($options['format']),
|
||||
'data-format' => $options['format_picker'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,19 +11,15 @@ namespace App\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
|
||||
class DayTimeType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
public function buildView(FormView $view, FormInterface $form, array $options)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'attr' => [
|
||||
'placeholder' => 'hh:mm'
|
||||
],
|
||||
$view->vars['attr'] = array_merge($view->vars['attr'], [
|
||||
'placeholder' => 'hh:mm'
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -42,11 +42,20 @@ class EntityMetaDefinitionType extends AbstractType
|
||||
return;
|
||||
}
|
||||
|
||||
$attr = ['data-name' => $definition->getName()];
|
||||
$options = $definition->getOptions();
|
||||
|
||||
if (\array_key_exists('attr', $options)) {
|
||||
$attr = array_merge($attr, $options['attr']);
|
||||
unset($options['attr']);
|
||||
}
|
||||
|
||||
$event->getForm()->add('value', $definition->getType(), array_merge([
|
||||
'label' => $definition->getLabel(),
|
||||
'constraints' => $definition->getConstraints(),
|
||||
'required' => $definition->isRequired(),
|
||||
], $definition->getOptions()));
|
||||
'attr' => $attr,
|
||||
], $options));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
@@ -32,10 +34,6 @@ class TagsInputType extends AbstractType
|
||||
*/
|
||||
private $router;
|
||||
|
||||
/**
|
||||
* @param TagArrayToStringTransformer $transformer
|
||||
* @param UrlGeneratorInterface $router
|
||||
*/
|
||||
public function __construct(TagArrayToStringTransformer $transformer, UrlGeneratorInterface $router)
|
||||
{
|
||||
$this->transformer = $transformer;
|
||||
@@ -58,17 +56,20 @@ class TagsInputType extends AbstractType
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
// documentation is for NelmioApiDocBundle
|
||||
'documentation' => [
|
||||
'type' => 'string',
|
||||
'description' => 'Comma separated list of tags',
|
||||
],
|
||||
'label' => 'label.tag',
|
||||
'attr' => [
|
||||
'data-autocomplete-url' => $this->router->generate('get_tags'),
|
||||
'class' => 'js-autocomplete',
|
||||
'autocomplete' => 'off',
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function buildView(FormView $view, FormInterface $form, array $options)
|
||||
{
|
||||
$view->vars['attr'] = array_merge($view->vars['attr'], [
|
||||
'data-autocomplete-url' => $this->router->generate('get_tags'),
|
||||
'class' => 'js-autocomplete',
|
||||
'autocomplete' => 'off',
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user