Next major version 2 with PHP 8.1, Symfony 6, Tabler UI, 2FA ... (#2902)
This commit is contained in:
@@ -9,14 +9,16 @@
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Configuration\SystemConfiguration;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Form\Type\DateTimePickerType;
|
||||
use App\Form\Type\DatePickerType;
|
||||
use App\Form\Type\DescriptionType;
|
||||
use App\Form\Type\DurationType;
|
||||
use App\Form\Type\FixedRateType;
|
||||
use App\Form\Type\HourlyRateType;
|
||||
use App\Form\Type\MetaFieldsCollectionType;
|
||||
use App\Form\Type\TagsType;
|
||||
use App\Form\Type\TimePickerType;
|
||||
use App\Form\Type\TimesheetBillableType;
|
||||
use App\Form\Type\UserType;
|
||||
use App\Form\Type\YesNoType;
|
||||
@@ -28,6 +30,7 @@ use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
|
||||
/**
|
||||
* Defines the form used to manipulate Timesheet entries.
|
||||
@@ -36,20 +39,11 @@ class TimesheetEditForm extends AbstractType
|
||||
{
|
||||
use FormTrait;
|
||||
|
||||
/**
|
||||
* @var CustomerRepository
|
||||
*/
|
||||
private $customers;
|
||||
|
||||
public function __construct(CustomerRepository $customer)
|
||||
public function __construct(private CustomerRepository $customers, private SystemConfiguration $systemConfiguration)
|
||||
{
|
||||
$this->customers = $customer;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$activity = null;
|
||||
$project = null;
|
||||
@@ -65,7 +59,7 @@ class TimesheetEditForm extends AbstractType
|
||||
|
||||
$activity = $entry->getActivity();
|
||||
$project = $entry->getProject();
|
||||
$customer = null === $project ? null : $project->getCustomer();
|
||||
$customer = $project?->getCustomer();
|
||||
|
||||
if (null !== $entry->getId()) {
|
||||
$isNew = false;
|
||||
@@ -110,8 +104,12 @@ class TimesheetEditForm extends AbstractType
|
||||
$this->addCustomer($builder, $customer);
|
||||
}
|
||||
|
||||
$allowCreate = (bool) $this->systemConfiguration->find('activity.allow_inline_create');
|
||||
|
||||
$this->addProject($builder, $isNew, $project, $customer);
|
||||
$this->addActivity($builder, $activity, $project);
|
||||
$this->addActivity($builder, $activity, $project, [
|
||||
'allow_create' => $allowCreate && $options['create_activity'],
|
||||
]);
|
||||
|
||||
$descriptionOptions = ['required' => false];
|
||||
if (!$isNew) {
|
||||
@@ -144,34 +142,129 @@ class TimesheetEditForm extends AbstractType
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function addBegin(FormBuilderInterface $builder, array $dateTimeOptions, array $options = [])
|
||||
protected function addBegin(FormBuilderInterface $builder, array $dateTimeOptions, array $options = []): void
|
||||
{
|
||||
if ($options['begin_minutes'] >= 1 && $options['begin_minutes'] <= 60) {
|
||||
$dateTimeOptions['time_increment'] = $options['begin_minutes'];
|
||||
}
|
||||
|
||||
$builder->add('begin', DateTimePickerType::class, array_merge($dateTimeOptions, [
|
||||
'label' => 'label.begin',
|
||||
$dateOptions = $dateTimeOptions;
|
||||
$builder->add('begin_date', DatePickerType::class, array_merge($dateOptions, [
|
||||
'label' => 'date',
|
||||
'mapped' => false,
|
||||
'constraints' => [
|
||||
new NotBlank()
|
||||
]
|
||||
]));
|
||||
|
||||
$timeOptions = $dateTimeOptions;
|
||||
|
||||
$builder->add('begin_time', TimePickerType::class, array_merge($timeOptions, [
|
||||
'label' => 'starttime',
|
||||
'mapped' => false,
|
||||
'constraints' => [
|
||||
new NotBlank()
|
||||
]
|
||||
]));
|
||||
|
||||
$builder->addEventListener(
|
||||
FormEvents::POST_SET_DATA,
|
||||
function (FormEvent $event) {
|
||||
/** @var Timesheet $timesheet */
|
||||
$timesheet = $event->getData();
|
||||
$begin = $timesheet->getBegin();
|
||||
|
||||
if (null !== $begin) {
|
||||
$event->getForm()->get('begin_date')->setData($begin);
|
||||
$event->getForm()->get('begin_time')->setData($begin);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// map single fields to original datetime object
|
||||
$builder->addEventListener(
|
||||
FormEvents::SUBMIT,
|
||||
function (FormEvent $event) {
|
||||
/** @var Timesheet $data */
|
||||
$data = $event->getData();
|
||||
|
||||
/** @var \DateTime|null $date */
|
||||
$date = $event->getForm()->get('begin_date')->getData();
|
||||
$time = $event->getForm()->get('begin_time')->getData();
|
||||
|
||||
if ($date === null || $time === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// mutable datetime are a problem for doctrine
|
||||
$newDate = clone $date;
|
||||
$newDate->setTime($time->format('H'), $time->format('i'));
|
||||
|
||||
if ($data->getBegin() === null || $data->getBegin()->getTimestamp() !== $newDate->getTimestamp()) {
|
||||
$data->setBegin($newDate);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function addEnd(FormBuilderInterface $builder, array $dateTimeOptions, array $options = [])
|
||||
protected function addEnd(FormBuilderInterface $builder, array $dateTimeOptions, array $options = []): void
|
||||
{
|
||||
if ($options['end_minutes'] >= 1 && $options['end_minutes'] <= 60) {
|
||||
$dateTimeOptions['time_increment'] = (int) $options['end_minutes'];
|
||||
}
|
||||
|
||||
$builder->add('end', DateTimePickerType::class, array_merge($dateTimeOptions, [
|
||||
'label' => 'label.end',
|
||||
$builder->add('end_time', TimePickerType::class, array_merge($dateTimeOptions, [
|
||||
'required' => false,
|
||||
'label' => 'endtime',
|
||||
'mapped' => false
|
||||
]));
|
||||
|
||||
$builder->addEventListener(
|
||||
FormEvents::POST_SET_DATA,
|
||||
function (FormEvent $event) {
|
||||
/** @var Timesheet|null $data */
|
||||
$data = $event->getData();
|
||||
if (null !== $data->getEnd()) {
|
||||
$event->getForm()->get('end_time')->setData($data->getEnd());
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// make sure that date & time fields are mapped back to begin & end fields
|
||||
$builder->addEventListener(
|
||||
FormEvents::SUBMIT,
|
||||
function (FormEvent $event) {
|
||||
/** @var Timesheet $timesheet */
|
||||
$timesheet = $event->getData();
|
||||
$oldEnd = $timesheet->getEnd();
|
||||
|
||||
$end = $event->getForm()->get('end_time')->getData();
|
||||
if ($end === null || $end === false) {
|
||||
$timesheet->setEnd(null);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// mutable datetime are a problem for doctrine
|
||||
$end = clone $end;
|
||||
|
||||
// end is assumed to be the same day then start, if not we raise the day by one
|
||||
//$time = $event->getForm()->get('begin_time')->getData();
|
||||
$time = $timesheet->getBegin();
|
||||
if ($time === null) {
|
||||
throw new \Exception('Cannot work with timesheets without start time');
|
||||
}
|
||||
$newEnd = clone $time;
|
||||
$newEnd->setTime($end->format('H'), $end->format('i'));
|
||||
|
||||
if ($newEnd < $time) {
|
||||
$newEnd->modify('+ 1 day');
|
||||
}
|
||||
|
||||
if ($oldEnd === null || $oldEnd->getTimestamp() !== $newEnd->getTimestamp()) {
|
||||
$timesheet->setEnd($newEnd);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function addDuration(FormBuilderInterface $builder, array $options, bool $forceApply = false, bool $autofocus = false)
|
||||
protected function addDuration(FormBuilderInterface $builder, array $options, bool $forceApply = false, bool $autofocus = false): void
|
||||
{
|
||||
$durationOptions = [
|
||||
'required' => false,
|
||||
'docu_chapter' => 'duration-format.html',
|
||||
//'toggle' => true,
|
||||
'attr' => [
|
||||
'placeholder' => '0:00',
|
||||
],
|
||||
@@ -200,9 +293,9 @@ class TimesheetEditForm extends AbstractType
|
||||
$builder->addEventListener(
|
||||
FormEvents::POST_SET_DATA,
|
||||
function (FormEvent $event) {
|
||||
/** @var Timesheet|null $data */
|
||||
$data = $event->getData();
|
||||
if (null === $data || null === $data->getEnd()) {
|
||||
/** @var Timesheet|null $timesheet */
|
||||
$timesheet = $event->getData();
|
||||
if (null === $timesheet || null === $timesheet->getEnd()) {
|
||||
$event->getForm()->get('duration')->setData(null);
|
||||
}
|
||||
}
|
||||
@@ -212,21 +305,31 @@ class TimesheetEditForm extends AbstractType
|
||||
$builder->addEventListener(
|
||||
FormEvents::SUBMIT,
|
||||
function (FormEvent $event) use ($forceApply) {
|
||||
/** @var Timesheet $data */
|
||||
$data = $event->getData();
|
||||
$duration = $data->getDuration();
|
||||
/** @var Timesheet $timesheet */
|
||||
$timesheet = $event->getData();
|
||||
|
||||
$newDuration = $event->getForm()->get('duration')->getData();
|
||||
if ($newDuration !== null && $newDuration > 0 && $newDuration !== $timesheet->getDuration()) {
|
||||
// TODO allow to use a duration that differs from end-start by adding a system configuration check here
|
||||
if ($timesheet->getEnd() === null) {
|
||||
$timesheet->setDuration($newDuration);
|
||||
}
|
||||
}
|
||||
|
||||
$duration = $timesheet->getDuration() ?? 0;
|
||||
|
||||
// only apply the duration, if the end is not yet set
|
||||
// without that check, the end would be overwritten and the real end time would be lost
|
||||
if (($forceApply && null !== $duration) || (null !== $duration && null === $data->getEnd())) {
|
||||
$end = clone $data->getBegin();
|
||||
if (($forceApply && $duration > 0) || ($duration > 0 && null === $timesheet->getEnd())) {
|
||||
$end = clone $timesheet->getBegin();
|
||||
$end->modify('+ ' . $duration . 'seconds');
|
||||
$data->setEnd($end);
|
||||
$timesheet->setEnd($end);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function addRates(FormBuilderInterface $builder, $currency, array $options)
|
||||
protected function addRates(FormBuilderInterface $builder, $currency, array $options): void
|
||||
{
|
||||
if (!$options['include_rate']) {
|
||||
return;
|
||||
@@ -241,7 +344,7 @@ class TimesheetEditForm extends AbstractType
|
||||
]);
|
||||
}
|
||||
|
||||
protected function addUser(FormBuilderInterface $builder, array $options)
|
||||
protected function addUser(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
if (!$options['include_user']) {
|
||||
return;
|
||||
@@ -250,18 +353,18 @@ class TimesheetEditForm extends AbstractType
|
||||
$builder->add('user', UserType::class);
|
||||
}
|
||||
|
||||
protected function addExported(FormBuilderInterface $builder, array $options)
|
||||
protected function addExported(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
if (!$options['include_exported']) {
|
||||
return;
|
||||
}
|
||||
|
||||
$builder->add('exported', YesNoType::class, [
|
||||
'label' => 'label.exported'
|
||||
'label' => 'exported'
|
||||
]);
|
||||
}
|
||||
|
||||
protected function addBillable(FormBuilderInterface $builder, array $options)
|
||||
protected function addBillable(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
if ($options['include_billable']) {
|
||||
$builder->add('billableMode', TimesheetBillableType::class, []);
|
||||
@@ -281,18 +384,21 @@ class TimesheetEditForm extends AbstractType
|
||||
},
|
||||
function (Timesheet $record) {
|
||||
$billable = new BillableCalculator();
|
||||
$billable->calculate($record);
|
||||
$billable->calculate($record, []);
|
||||
|
||||
return $record;
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$maxMinutes = $this->systemConfiguration->getTimesheetLongRunningDuration();
|
||||
$maxHours = 8;
|
||||
if ($maxMinutes > 0) {
|
||||
$maxHours = (int) ($maxMinutes / 60);
|
||||
}
|
||||
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Timesheet::class,
|
||||
'csrf_protection' => true,
|
||||
@@ -302,6 +408,7 @@ class TimesheetEditForm extends AbstractType
|
||||
'include_exported' => false,
|
||||
'include_billable' => true,
|
||||
'include_rate' => true,
|
||||
'create_activity' => false,
|
||||
'docu_chapter' => 'timesheet.html',
|
||||
'method' => 'POST',
|
||||
'date_format' => null,
|
||||
@@ -311,9 +418,7 @@ class TimesheetEditForm extends AbstractType
|
||||
'allow_end_datetime' => true,
|
||||
'allow_duration' => false,
|
||||
'duration_minutes' => null,
|
||||
'duration_hours' => 10,
|
||||
'begin_minutes' => 1,
|
||||
'end_minutes' => 1,
|
||||
'duration_hours' => $maxHours,
|
||||
'attr' => [
|
||||
'data-form-event' => 'kimai.timesheetUpdate',
|
||||
'data-msg-success' => 'action.update.success',
|
||||
|
||||
Reference in New Issue
Block a user