improved duration and minute selector (#2264)

* do not close modal if form is dirty
* deprecated TimesheetConfiguration
* inject timezone in form types
* cleanup usage of UserDateTimeFactory
* allow to configure increment steps for minutes
* use 15 minutes step for datetimepicker in project edit form
* use rounding rules for increments in minute select for begin and end
* allow duration in multi user and admin timesheet forms
* make dropdown values configurable
This commit is contained in:
Kevin Papst
2021-01-17 14:04:13 +01:00
committed by GitHub
parent e2324b7d51
commit 8d72d114c7
95 changed files with 1381 additions and 510 deletions

View File

@@ -9,7 +9,7 @@
namespace App\Timesheet;
use App\Configuration\TimesheetConfiguration;
use App\Configuration\SystemConfiguration;
use App\Entity\Timesheet;
use App\Timesheet\Rounding\RoundingInterface;
@@ -24,7 +24,7 @@ final class RoundingService
*/
private $rulesCache;
/**
* @var TimesheetConfiguration
* @var SystemConfiguration
*/
private $configuration;
/**
@@ -33,11 +33,11 @@ final class RoundingService
private $roundingModes;
/**
* @param TimesheetConfiguration $configuration
* @param SystemConfiguration $configuration
* @param RoundingInterface[] $roundingModes
* @param array $rules
*/
public function __construct(TimesheetConfiguration $configuration, iterable $roundingModes, array $rules)
public function __construct(SystemConfiguration $configuration, iterable $roundingModes, array $rules)
{
$this->configuration = $configuration;
$this->roundingModes = $roundingModes;
@@ -49,11 +49,11 @@ final class RoundingService
if (empty($this->rulesCache)) {
$this->rulesCache = $this->rules;
if (empty($this->rulesCache) || \array_key_exists('default', $this->rulesCache)) {
$this->rulesCache['default']['days'] = $this->configuration->getDefaultRoundingDays();
$this->rulesCache['default']['begin'] = $this->configuration->getDefaultRoundingBegin();
$this->rulesCache['default']['end'] = $this->configuration->getDefaultRoundingEnd();
$this->rulesCache['default']['duration'] = $this->configuration->getDefaultRoundingDuration();
$this->rulesCache['default']['mode'] = $this->configuration->getDefaultRoundingMode();
$this->rulesCache['default']['days'] = $this->configuration->getTimesheetDefaultRoundingDays();
$this->rulesCache['default']['begin'] = $this->configuration->getTimesheetDefaultRoundingBegin();
$this->rulesCache['default']['end'] = $this->configuration->getTimesheetDefaultRoundingEnd();
$this->rulesCache['default']['duration'] = $this->configuration->getTimesheetDefaultRoundingDuration();
$this->rulesCache['default']['mode'] = $this->configuration->getTimesheetDefaultRoundingMode();
}
// see AppExtension, conversion from string to array due to system configuration ont allowing to store arrays

View File

@@ -9,7 +9,7 @@
namespace App\Timesheet;
use App\Configuration\TimesheetConfiguration;
use App\Configuration\SystemConfiguration;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Event\TimesheetCreatePostEvent;
@@ -27,6 +27,7 @@ use App\Event\TimesheetUpdatePostEvent;
use App\Event\TimesheetUpdatePreEvent;
use App\Repository\TimesheetRepository;
use App\Security\AccessDeniedException;
use App\Timesheet\TrackingMode\TrackingModeInterface;
use App\Validator\ValidationException;
use App\Validator\ValidationFailedException;
use InvalidArgumentException;
@@ -42,7 +43,7 @@ final class TimesheetService
*/
private $repository;
/**
* @var TimesheetConfiguration
* @var SystemConfiguration
*/
private $configuration;
/**
@@ -63,7 +64,7 @@ final class TimesheetService
private $validator;
public function __construct(
TimesheetConfiguration $configuration,
SystemConfiguration $configuration,
TimesheetRepository $repository,
TrackingModeService $service,
EventDispatcherInterface $dispatcher,
@@ -293,7 +294,7 @@ final class TimesheetService
*/
private function stopActiveEntries(Timesheet $timesheet): int
{
$hardLimit = $this->configuration->getActiveEntriesHardLimit();
$hardLimit = $this->configuration->getTimesheetActiveEntriesHardLimit();
$activeEntries = $this->repository->getActiveEntries($timesheet->getUser());
if (empty($activeEntries)) {
@@ -314,4 +315,9 @@ final class TimesheetService
return $counter;
}
public function getActiveTrackingMode(): TrackingModeInterface
{
return $this->trackingModeService->getActiveMode();
}
}

View File

@@ -9,27 +9,13 @@
namespace App\Timesheet\TrackingMode;
use App\Configuration\TimesheetConfiguration;
use App\Entity\Timesheet;
use App\Timesheet\UserDateTimeFactory;
use DateTime;
use Symfony\Component\HttpFoundation\Request;
abstract class AbstractTrackingMode implements TrackingModeInterface
{
/**
* @var UserDateTimeFactory
*/
protected $dateTime;
/**
* @var TimesheetConfiguration
*/
protected $configuration;
public function __construct(UserDateTimeFactory $dateTime, TimesheetConfiguration $configuration)
{
$this->dateTime = $dateTime;
$this->configuration = $configuration;
}
use TrackingModeTrait;
public function create(Timesheet $timesheet, ?Request $request = null): void
{
@@ -48,7 +34,7 @@ abstract class AbstractTrackingMode implements TrackingModeInterface
return;
}
$start = $this->dateTime->createDateTimeFromFormat('Y-m-d', $start);
$start = DateTime::createFromFormat('Y-m-d', $start, $this->getTimezone($entry));
if (false === $start) {
return;
}
@@ -61,7 +47,7 @@ abstract class AbstractTrackingMode implements TrackingModeInterface
return;
}
$end = $this->dateTime->createDateTimeFromFormat('Y-m-d', $end);
$end = DateTime::createFromFormat('Y-m-d', $end, $this->getTimezone($entry));
if (false === $end) {
return;
}
@@ -81,7 +67,7 @@ abstract class AbstractTrackingMode implements TrackingModeInterface
}
try {
$from = $this->dateTime->createDateTime($from);
$from = new DateTime($from, $this->getTimezone($entry));
} catch (\Exception $ex) {
return;
}
@@ -94,7 +80,7 @@ abstract class AbstractTrackingMode implements TrackingModeInterface
}
try {
$to = $this->dateTime->createDateTime($to);
$to = new DateTime($to, $this->getTimezone($entry));
} catch (\Exception $ex) {
return;
}

View File

@@ -9,10 +9,9 @@
namespace App\Timesheet\TrackingMode;
use App\Configuration\TimesheetConfiguration;
use App\Entity\Timesheet;
use App\Timesheet\RoundingService;
use App\Timesheet\UserDateTimeFactory;
use DateTime;
use Symfony\Component\HttpFoundation\Request;
final class DefaultMode extends AbstractTrackingMode
@@ -22,9 +21,8 @@ final class DefaultMode extends AbstractTrackingMode
*/
private $rounding;
public function __construct(UserDateTimeFactory $dateTime, TimesheetConfiguration $configuration, RoundingService $rounding)
public function __construct(RoundingService $rounding)
{
parent::__construct($dateTime, $configuration);
$this->rounding = $rounding;
}
@@ -40,7 +38,7 @@ final class DefaultMode extends AbstractTrackingMode
public function canEditDuration(): bool
{
return false;
return true;
}
public function canUpdateTimesWithAPI(): bool
@@ -63,7 +61,7 @@ final class DefaultMode extends AbstractTrackingMode
parent::create($timesheet, $request);
if (null === $timesheet->getBegin()) {
$timesheet->setBegin($this->dateTime->createDateTime());
$timesheet->setBegin(new DateTime('now', $this->getTimezone($timesheet)));
}
$this->rounding->roundBegin($timesheet);

View File

@@ -9,25 +9,22 @@
namespace App\Timesheet\TrackingMode;
use App\Configuration\TimesheetConfiguration;
use App\Configuration\SystemConfiguration;
use App\Entity\Timesheet;
use App\Timesheet\UserDateTimeFactory;
use DateTime;
use Symfony\Component\HttpFoundation\Request;
final class DurationFixedBeginMode implements TrackingModeInterface
{
use TrackingModeTrait;
/**
* @var UserDateTimeFactory
*/
private $dateTime;
/**
* @var TimesheetConfiguration
* @var SystemConfiguration
*/
private $configuration;
public function __construct(UserDateTimeFactory $dateTime, TimesheetConfiguration $configuration)
public function __construct(SystemConfiguration $configuration)
{
$this->dateTime = $dateTime;
$this->configuration = $configuration;
}
@@ -54,11 +51,11 @@ final class DurationFixedBeginMode implements TrackingModeInterface
public function create(Timesheet $timesheet, ?Request $request = null): void
{
if (null === $timesheet->getBegin()) {
$timesheet->setBegin($this->dateTime->createDateTime());
$timesheet->setBegin(new DateTime('now', $this->getTimezone($timesheet)));
}
$newBegin = clone $timesheet->getBegin();
$newBegin->modify($this->configuration->getDefaultBeginTime());
$newBegin->modify($this->configuration->getTimesheetDefaultBeginTime());
$timesheet->setBegin($newBegin);
}

View File

@@ -9,11 +9,23 @@
namespace App\Timesheet\TrackingMode;
use App\Configuration\SystemConfiguration;
use App\Entity\Timesheet;
use DateTime;
use Symfony\Component\HttpFoundation\Request;
final class DurationOnlyMode extends AbstractTrackingMode
{
/**
* @var SystemConfiguration
*/
private $configuration;
public function __construct(SystemConfiguration $configuration)
{
$this->configuration = $configuration;
}
public function canEditBegin(): bool
{
return true;
@@ -47,11 +59,11 @@ final class DurationOnlyMode extends AbstractTrackingMode
public function create(Timesheet $timesheet, ?Request $request = null): void
{
if (null === $timesheet->getBegin()) {
$timesheet->setBegin($this->dateTime->createDateTime());
$timesheet->setBegin(new DateTime('now', $this->getTimezone($timesheet)));
}
$newBegin = clone $timesheet->getBegin();
$newBegin->modify($this->configuration->getDefaultBeginTime());
$newBegin->modify($this->configuration->getTimesheetDefaultBeginTime());
$timesheet->setBegin($newBegin);
parent::create($timesheet, $request);

View File

@@ -10,20 +10,12 @@
namespace App\Timesheet\TrackingMode;
use App\Entity\Timesheet;
use App\Timesheet\UserDateTimeFactory;
use DateTime;
use Symfony\Component\HttpFoundation\Request;
final class PunchInOutMode implements TrackingModeInterface
{
/**
* @var UserDateTimeFactory
*/
private $dateTime;
public function __construct(UserDateTimeFactory $dateTime)
{
$this->dateTime = $dateTime;
}
use TrackingModeTrait;
public function canEditBegin(): bool
{
@@ -48,7 +40,7 @@ final class PunchInOutMode implements TrackingModeInterface
public function create(Timesheet $timesheet, ?Request $request = null): void
{
if (null === $timesheet->getBegin()) {
$timesheet->setBegin($this->dateTime->createDateTime());
$timesheet->setBegin(new DateTime('now', $this->getTimezone($timesheet)));
}
}

View File

@@ -0,0 +1,31 @@
<?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\Timesheet\TrackingMode;
use App\Entity\Timesheet;
use DateTimeZone;
trait TrackingModeTrait
{
protected function getTimezone(Timesheet $timesheet): DateTimeZone
{
if ($timesheet->getBegin() !== null) {
return $timesheet->getBegin()->getTimezone();
}
$timezone = date_default_timezone_get();
if ($timesheet->getUser() !== null) {
$timezone = $timesheet->getUser()->getTimezone();
}
return new DateTimeZone($timezone);
}
}

View File

@@ -9,7 +9,7 @@
namespace App\Timesheet;
use App\Configuration\TimesheetConfiguration;
use App\Configuration\SystemConfiguration;
use App\Timesheet\TrackingMode\TrackingModeInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
@@ -20,15 +20,15 @@ final class TrackingModeService
*/
private $modes = [];
/**
* @var TimesheetConfiguration
* @var SystemConfiguration
*/
private $configuration;
/**
* @param TimesheetConfiguration $configuration
* @param SystemConfiguration $configuration
* @param TrackingModeInterface[] $modes
*/
public function __construct(TimesheetConfiguration $configuration, iterable $modes)
public function __construct(SystemConfiguration $configuration, iterable $modes)
{
$this->configuration = $configuration;
$this->modes = $modes;
@@ -44,7 +44,7 @@ final class TrackingModeService
public function getActiveMode(): TrackingModeInterface
{
$trackingMode = $this->configuration->getTrackingMode();
$trackingMode = $this->configuration->getTimesheetTrackingMode();
foreach ($this->getModes() as $mode) {
if ($mode->getId() === $trackingMode) {