add "duration + fixed start time" tracking mode (#859)

This commit is contained in:
Kevin Papst
2019-06-15 23:04:53 +02:00
committed by GitHub
parent 92f3f4ee57
commit 59d2946b91
36 changed files with 1094 additions and 265 deletions

View File

@@ -18,6 +18,8 @@ use App\Form\TimesheetEditForm;
use App\Repository\Query\TimesheetQuery;
use App\Repository\TagRepository;
use App\Repository\TimesheetRepository;
use App\Timesheet\TrackingMode\TrackingModeInterface;
use App\Timesheet\TrackingModeService;
use App\Timesheet\UserDateTimeFactory;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\RestBundle\Controller\Annotations as Rest;
@@ -62,21 +64,30 @@ class TimesheetController extends BaseApiController
* @var TagRepository
*/
protected $tagRepository;
/**
* @param ViewHandlerInterface $viewHandler
* @param TimesheetRepository $repository
* @param UserDateTimeFactory $dateTime
* @param TimesheetConfiguration $configuration
* @param TagRepository $tagRepository
* @var TrackingModeService
*/
public function __construct(ViewHandlerInterface $viewHandler, TimesheetRepository $repository, UserDateTimeFactory $dateTime, TimesheetConfiguration $configuration, TagRepository $tagRepository)
{
protected $trackingModeService;
public function __construct(
ViewHandlerInterface $viewHandler,
TimesheetRepository $repository,
UserDateTimeFactory $dateTime,
TimesheetConfiguration $configuration,
TagRepository $tagRepository,
TrackingModeService $trackingModeService
) {
$this->viewHandler = $viewHandler;
$this->repository = $repository;
$this->configuration = $configuration;
$this->dateTime = $dateTime;
$this->tagRepository = $tagRepository;
$this->trackingModeService = $trackingModeService;
}
protected function getTrackingMode(): TrackingModeInterface
{
return $this->trackingModeService->getActiveMode();
}
/**
@@ -271,11 +282,15 @@ class TimesheetController extends BaseApiController
$timesheet->setUser($this->getUser());
$timesheet->setBegin($this->dateTime->createDateTime());
$mode = $this->getTrackingMode();
$form = $this->createForm(TimesheetEditForm::class, $timesheet, [
'csrf_protection' => false,
'include_rate' => $this->isGranted('edit_rate', $timesheet),
'include_exported' => $this->isGranted('edit_export', $timesheet),
'include_datetime' => !$this->configuration->isPunchInOut(),
'allow_begin_datetime' => $mode->canUpdateTimesWithAPI(),
'allow_end_datetime' => $mode->canUpdateTimesWithAPI(),
'allow_duration' => false,
'date_format' => self::DATE_FORMAT,
]);
@@ -349,11 +364,15 @@ class TimesheetController extends BaseApiController
throw new AccessDeniedHttpException('You are not allowed to update this timesheet');
}
$mode = $this->getTrackingMode();
$form = $this->createForm(TimesheetEditForm::class, $timesheet, [
'csrf_protection' => false,
'include_rate' => $this->isGranted('edit_rate', $timesheet),
'include_exported' => $this->isGranted('edit_export', $timesheet),
'include_datetime' => !$this->configuration->isPunchInOut(),
'allow_begin_datetime' => $mode->canUpdateTimesWithAPI(),
'allow_end_datetime' => $mode->canUpdateTimesWithAPI(),
'allow_duration' => false,
'date_format' => self::DATE_FORMAT,
]);

View File

@@ -11,10 +11,6 @@ namespace App\Configuration;
class TimesheetConfiguration implements SystemBundleConfiguration
{
public const MODE_DURATION_ONLY = 'duration_only';
public const MODE_DEFAULT = 'default';
public const MODE_PUNCH_IN_OUT = 'punch';
use StringAccessibleConfigTrait;
public function getPrefix(): string
@@ -27,14 +23,14 @@ class TimesheetConfiguration implements SystemBundleConfiguration
return (bool) $this->find('rules.allow_future_times');
}
public function isDurationOnly(): bool
public function getTrackingMode(): string
{
return $this->find('mode') === self::MODE_DURATION_ONLY;
return (string) $this->find('mode');
}
public function isPunchInOut(): bool
public function getDefaultBeginTime(): string
{
return $this->find('mode') === self::MODE_PUNCH_IN_OUT;
return (string) $this->find('default_begin');
}
public function isMarkdownEnabled(): bool

View File

@@ -12,6 +12,7 @@ namespace App\Controller;
use App\Calendar\Google;
use App\Calendar\Source;
use App\Configuration\CalendarConfiguration;
use App\Timesheet\TrackingModeService;
use App\Timesheet\UserDateTimeFactory;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Routing\Annotation\Route;
@@ -27,12 +28,15 @@ class CalendarController extends AbstractController
/**
* @Route(path="/", name="calendar", methods={"GET"})
*/
public function userCalendar(CalendarConfiguration $configuration, UserDateTimeFactory $dateTime)
public function userCalendar(CalendarConfiguration $configuration, UserDateTimeFactory $dateTime, TrackingModeService $service)
{
$mode = $service->getActiveMode();
return $this->render('calendar/user.html.twig', [
'config' => $configuration,
'google' => $this->getGoogleSources($configuration),
'now' => $dateTime->createDateTime(),
'is_punch_mode' => !$mode->canEditDuration() && !$mode->canEditBegin() && !$mode->canEditEnd()
]);
}

View File

@@ -15,7 +15,7 @@ use App\Form\Model\Configuration;
use App\Form\Model\SystemConfiguration as SystemConfigurationModel;
use App\Form\SystemConfigurationForm;
use App\Form\Type\EnhancedSelectboxType;
use App\Form\Type\TimesheetModeType;
use App\Form\Type\TrackingModeType;
use App\Repository\ConfigurationRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -189,7 +189,7 @@ class SystemConfigurationController extends AbstractController
->setConfiguration([
(new Configuration())
->setName('timesheet.mode')
->setType(TimesheetModeType::class)
->setType(TrackingModeType::class)
->setTranslationDomain('system-configuration'),
(new Configuration())
->setName('timesheet.rules.allow_future_times')

View File

@@ -19,6 +19,8 @@ use App\Repository\ProjectRepository;
use App\Repository\Query\TimesheetQuery;
use App\Repository\TagRepository;
use App\Repository\TimesheetRepository;
use App\Timesheet\TrackingMode\TrackingModeInterface;
use App\Timesheet\TrackingModeService;
use App\Timesheet\UserDateTimeFactory;
use Doctrine\Common\Collections\ArrayCollection;
use Pagerfanta\Pagerfanta;
@@ -40,26 +42,34 @@ abstract class TimesheetAbstractController extends AbstractController
* @var TimesheetRepository
*/
protected $repository;
/**
* @var TrackingModeService
*/
protected $trackingModeService;
public function __construct(UserDateTimeFactory $dateTime, TimesheetConfiguration $configuration, TimesheetRepository $repository)
{
public function __construct(
UserDateTimeFactory $dateTime,
TimesheetConfiguration $configuration,
TimesheetRepository $repository,
TrackingModeService $service
) {
$this->dateTime = $dateTime;
$this->configuration = $configuration;
$this->repository = $repository;
$this->trackingModeService = $service;
}
/**
* @return int
*/
protected function getSoftLimit()
protected function getTrackingMode(): TrackingModeInterface
{
return $this->trackingModeService->getActiveMode();
}
protected function getSoftLimit(): int
{
return $this->configuration->getActiveEntriesSoftLimit();
}
/**
* @return TimesheetRepository
*/
protected function getRepository()
protected function getRepository(): TimesheetRepository
{
return $this->repository;
}
@@ -107,6 +117,7 @@ abstract class TimesheetAbstractController extends AbstractController
'showFilter' => $form->isSubmitted(),
'toolbarForm' => $form->createView(),
'showSummary' => $this->includeSummary(),
'showStartEndTime' => $this->canSeeStartEndTime()
]);
}
@@ -150,8 +161,6 @@ abstract class TimesheetAbstractController extends AbstractController
$entry->setUser($this->getUser());
$entry->setBegin($this->dateTime->createDateTime());
$this->setBeginEndFromRequest($request, $entry);
if ($request->query->get('project')) {
$project = $projectRepository->find($request->query->get('project'));
$entry->setProject($project);
@@ -162,7 +171,10 @@ abstract class TimesheetAbstractController extends AbstractController
$entry->setActivity($activity);
}
$createForm = $this->getCreateForm($entry);
$mode = $this->getTrackingMode();
$mode->create($entry, $request);
$createForm = $this->getCreateForm($entry, $mode);
$createForm->handleRequest($request);
if ($createForm->isSubmitted() && $createForm->isValid()) {
@@ -192,52 +204,6 @@ abstract class TimesheetAbstractController extends AbstractController
]);
}
protected function setBeginEndFromRequest(Request $request, Timesheet $entry)
{
if ($this->configuration->isPunchInOut()) {
return;
}
$start = $request->get('begin');
if ($start !== null) {
$start = $this->dateTime->createDateTimeFromFormat('Y-m-d', $start);
if ($start !== false) {
$entry->setBegin($start);
// only check for an end date if a begin date was given
$end = $request->get('end');
if ($end !== null) {
$end = $this->dateTime->createDateTimeFromFormat('Y-m-d', $end);
if ($end !== false) {
$start->setTime(10, 0, 0);
$end->setTime(18, 0, 0);
$entry->setEnd($end);
$entry->setDuration($end->getTimestamp() - $start->getTimestamp());
}
}
}
}
$from = $request->get('from');
if ($from !== null) {
$from = $this->dateTime->createDateTime($from);
if ($from !== false) {
$entry->setBegin($from);
// only check for an end datetime if a begin datetime was given
$to = $request->get('to');
if ($to !== null) {
$to = $this->dateTime->createDateTime($to);
if ($to !== false) {
$entry->setEnd($to);
$entry->setDuration($to->getTimestamp() - $from->getTimestamp());
}
}
}
}
}
/**
* @param Request $request
* @param string $renderTemplate
@@ -281,19 +247,16 @@ abstract class TimesheetAbstractController extends AbstractController
]);
}
/**
* @param Timesheet $entry
* @return \Symfony\Component\Form\FormInterface
*/
protected function getCreateForm(Timesheet $entry)
protected function getCreateForm(Timesheet $entry, TrackingModeInterface $mode): FormInterface
{
return $this->createForm($this->getCreateFormClassName(), $entry, [
'action' => $this->generateUrl($this->getCreateRoute()),
'include_rate' => $this->isGranted('edit_rate', $entry),
'include_exported' => $this->isGranted('edit_export', $entry),
'include_user' => $this->includeUserInForms(),
'use_duration' => $this->configuration->isDurationOnly(),
'include_datetime' => !$this->configuration->isPunchInOut(),
'allow_begin_datetime' => $mode->canEditBegin(),
'allow_end_datetime' => $mode->canEditEnd(),
'allow_duration' => $mode->canEditDuration(),
'customer' => true,
]);
}
@@ -305,6 +268,8 @@ abstract class TimesheetAbstractController extends AbstractController
*/
protected function getEditForm(Timesheet $entry, $page)
{
$mode = $this->getTrackingMode();
return $this->createForm($this->getEditFormClassName(), $entry, [
'action' => $this->generateUrl($this->getEditRoute(), [
'id' => $entry->getId(),
@@ -313,8 +278,9 @@ abstract class TimesheetAbstractController extends AbstractController
'include_rate' => $this->isGranted('edit_rate', $entry),
'include_exported' => $this->isGranted('edit_export', $entry),
'include_user' => $this->includeUserInForms(),
'include_datetime' => !$this->configuration->isPunchInOut(),
'use_duration' => $this->configuration->isDurationOnly(),
'allow_begin_datetime' => $mode->canEditBegin(),
'allow_end_datetime' => $mode->canEditEnd(),
'allow_duration' => $mode->canEditDuration(),
'customer' => true,
]);
}
@@ -334,12 +300,12 @@ abstract class TimesheetAbstractController extends AbstractController
]);
}
protected function getCreateFormClassName()
protected function getCreateFormClassName(): string
{
return TimesheetEditForm::class;
}
protected function getEditFormClassName()
protected function getEditFormClassName(): string
{
return TimesheetEditForm::class;
}
@@ -368,4 +334,9 @@ abstract class TimesheetAbstractController extends AbstractController
{
return 'timesheet_create';
}
protected function canSeeStartEndTime(): bool
{
return $this->getTrackingMode()->canSeeBeginAndEndTimes();
}
}

View File

@@ -75,12 +75,12 @@ class TimesheetTeamController extends TimesheetAbstractController
return $this->create($request, 'timesheet-team/edit.html.twig', $projectRepository, $activityRepository);
}
protected function getCreateFormClassName()
protected function getCreateFormClassName(): string
{
return TimesheetAdminEditForm::class;
}
protected function getEditFormClassName()
protected function getEditFormClassName(): string
{
return TimesheetAdminEditForm::class;
}
@@ -104,4 +104,9 @@ class TimesheetTeamController extends TimesheetAbstractController
{
return 'admin_timesheet_create';
}
protected function canSeeStartEndTime(): bool
{
return true;
}
}

View File

@@ -75,6 +75,9 @@ class Configuration implements ConfigurationInterface
$node
->children()
->scalarNode('default_begin')
->defaultValue('now')
->end()
->booleanNode('duration_only')
->setDeprecated()
->end()

View File

@@ -9,11 +9,17 @@
namespace App\Form;
use Symfony\Component\Form\FormBuilderInterface;
class TimesheetAdminEditForm extends TimesheetEditForm
{
protected function showTimeFields(array $options): bool
public function buildForm(FormBuilderInterface $builder, array $options)
{
return true;
$options['allow_begin_datetime'] = true;
$options['allow_end_datetime'] = true;
$options['allow_duration'] = false;
parent::buildForm($builder, $options);
}
protected function showCustomer(array $options, bool $isNew, int $customerCount): bool

View File

@@ -114,14 +114,14 @@ class TimesheetEditForm extends AbstractType
$dateTimeOptions['format'] = $options['date_format'];
}
if ($this->showTimeFields($options)) {
if ($options['allow_begin_datetime']) {
$this->addBegin($builder, $dateTimeOptions);
}
if ($options['use_duration']) {
$this->addDuration($builder);
} else {
$this->addEnd($builder, $dateTimeOptions);
}
if ($options['allow_duration']) {
$this->addDuration($builder);
} elseif ($options['allow_end_datetime']) {
$this->addEnd($builder, $dateTimeOptions);
}
if ($this->showCustomer($options, $isNew, $customerCount)) {
@@ -154,11 +154,6 @@ class TimesheetEditForm extends AbstractType
return true;
}
protected function showTimeFields(array $options): bool
{
return $options['include_datetime'];
}
protected function addCustomer(FormBuilderInterface $builder, ?Customer $customer = null)
{
$builder
@@ -377,8 +372,9 @@ class TimesheetEditForm extends AbstractType
'method' => 'POST',
'date_format' => null,
'customer' => false, // for API usage
'use_duration' => false, // duration instead of end (for duration_only mode)
'include_datetime' => true,
'allow_begin_datetime' => true,
'allow_end_datetime' => true,
'allow_duration' => false,
'attr' => [
'data-form-event' => 'kimai.timesheetUpdate',
'data-msg-success' => 'action.update.success',

View File

@@ -9,7 +9,7 @@
namespace App\Form\Type;
use App\Configuration\TimesheetConfiguration;
use App\Timesheet\TrackingModeService;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -17,20 +17,30 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Custom form field type to select the timesheet mode.
*/
class TimesheetModeType extends AbstractType
class TrackingModeType extends AbstractType
{
protected $service;
public function __construct(TrackingModeService $service)
{
$this->service = $service;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$choices = [];
foreach ($this->service->getModes() as $mode) {
$id = $mode->getId();
$choices['label.timesheet.mode_' . $id] = $id;
}
$resolver->setDefaults([
'label' => 'label.timesheet.mode',
'choices' => [
'label.timesheet.mode_default' => TimesheetConfiguration::MODE_DEFAULT,
'label.timesheet.mode_punch' => TimesheetConfiguration::MODE_PUNCH_IN_OUT,
'label.timesheet.mode_duration_only' => TimesheetConfiguration::MODE_DURATION_ONLY,
],
'choices' => $choices,
]);
}

View File

@@ -0,0 +1,101 @@
<?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\Configuration\TimesheetConfiguration;
use App\Entity\Timesheet;
use App\Timesheet\UserDateTimeFactory;
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;
}
public function create(Timesheet $timesheet, Request $request): void
{
$this->setBeginEndFromRequest($timesheet, $request);
$this->setFromToFromRequest($timesheet, $request);
}
protected function setBeginEndFromRequest(Timesheet $entry, Request $request)
{
$start = $request->get('begin');
if (null === $start) {
return;
}
$start = $this->dateTime->createDateTimeFromFormat('Y-m-d', $start);
if (false === $start) {
return;
}
$entry->setBegin($start);
// only check for an end date if a begin date was given
$end = $request->get('end');
if (null === $end) {
return;
}
$end = $this->dateTime->createDateTimeFromFormat('Y-m-d', $end);
if (false === $end) {
return;
}
$start->setTime(10, 0, 0);
$end->setTime(18, 0, 0);
$entry->setEnd($end);
$entry->setDuration($end->getTimestamp() - $start->getTimestamp());
}
protected function setFromToFromRequest(Timesheet $entry, Request $request)
{
$from = $request->get('from');
if (null === $from) {
return;
}
try {
$from = $this->dateTime->createDateTime($from);
} catch (\Exception $ex) {
return;
}
$entry->setBegin($from);
$to = $request->get('to');
if (null === $to) {
return;
}
try {
$to = $this->dateTime->createDateTime($to);
} catch (\Exception $ex) {
return;
}
$entry->setEnd($to);
$entry->setDuration($to->getTimestamp() - $from->getTimestamp());
}
}

View File

@@ -0,0 +1,43 @@
<?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;
class DefaultMode extends AbstractTrackingMode
{
public function canEditBegin(): bool
{
return true;
}
public function canEditEnd(): bool
{
return true;
}
public function canEditDuration(): bool
{
return false;
}
public function canUpdateTimesWithAPI(): bool
{
return true;
}
public function getId(): string
{
return 'default';
}
public function canSeeBeginAndEndTimes(): bool
{
return true;
}
}

View File

@@ -0,0 +1,72 @@
<?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\Configuration\TimesheetConfiguration;
use App\Entity\Timesheet;
use App\Timesheet\UserDateTimeFactory;
use Symfony\Component\HttpFoundation\Request;
class DurationFixedStartMode implements TrackingModeInterface
{
/**
* @var UserDateTimeFactory
*/
protected $dateTime;
/**
* @var TimesheetConfiguration
*/
protected $configuration;
public function __construct(UserDateTimeFactory $dateTime, TimesheetConfiguration $configuration)
{
$this->dateTime = $dateTime;
$this->configuration = $configuration;
}
public function canEditBegin(): bool
{
return false;
}
public function canEditEnd(): bool
{
return false;
}
public function canEditDuration(): bool
{
return true;
}
public function canUpdateTimesWithAPI(): bool
{
return false;
}
public function create(Timesheet $timesheet, Request $request): void
{
if (null === $timesheet->getBegin()) {
$timesheet->setBegin($this->dateTime->createDateTime());
}
$timesheet->getBegin()->modify($this->configuration->getDefaultBeginTime());
}
public function getId(): string
{
return 'duration_fixed_start';
}
public function canSeeBeginAndEndTimes(): bool
{
return false;
}
}

View File

@@ -0,0 +1,43 @@
<?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;
class DurationOnlyMode extends AbstractTrackingMode
{
public function canEditBegin(): bool
{
return true;
}
public function canEditEnd(): bool
{
return false;
}
public function canEditDuration(): bool
{
return true;
}
public function canUpdateTimesWithAPI(): bool
{
return true;
}
public function getId(): string
{
return 'duration_only';
}
public function canSeeBeginAndEndTimes(): bool
{
return false;
}
}

View File

@@ -0,0 +1,50 @@
<?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 Symfony\Component\HttpFoundation\Request;
class PunchInOutMode implements TrackingModeInterface
{
public function canEditBegin(): bool
{
return false;
}
public function canEditEnd(): bool
{
return false;
}
public function canEditDuration(): bool
{
return false;
}
public function canUpdateTimesWithAPI(): bool
{
return false;
}
public function create(Timesheet $timesheet, Request $request): void
{
}
public function getId(): string
{
return 'punch';
}
public function canSeeBeginAndEndTimes(): bool
{
return true;
}
}

View File

@@ -0,0 +1,72 @@
<?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 Symfony\Component\HttpFoundation\Request;
/**
* A tracking-mode defines the behaviour of the user timesheet.
* It is NOT used for the timesheet administration.
*/
interface TrackingModeInterface
{
/**
* Set default values on this new timesheet entity,
* before form data is rendered/processed.
*
* @param Timesheet $timesheet
* @param Request $request
*/
public function create(Timesheet $timesheet, Request $request): void;
/**
* Whether the user can edit the begin datetime.
*
* @return bool
*/
public function canEditBegin(): bool;
/**
* Whether the user can edit the end datetime.
*
* @return bool
*/
public function canEditEnd(): bool;
/**
* Whether the user can edit the duration.
* If this is true, the result of canEditEnd() will be ignored.
*
* @return bool
*/
public function canEditDuration(): bool;
/**
* Whether the API can be used to manipulate the start and end times.
*
* @return bool
*/
public function canUpdateTimesWithAPI(): bool;
/**
* Whether the real begin and end times are shown in the user timesheet.
*
* @return bool
*/
public function canSeeBeginAndEndTimes(): bool;
/**
* Returns a unique identifier for this tracking mode.
*
* @return string
*/
public function getId(): string;
}

View File

@@ -0,0 +1,62 @@
<?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;
use App\Configuration\TimesheetConfiguration;
use App\Timesheet\TrackingMode\DefaultMode;
use App\Timesheet\TrackingMode\DurationFixedStartMode;
use App\Timesheet\TrackingMode\DurationOnlyMode;
use App\Timesheet\TrackingMode\PunchInOutMode;
use App\Timesheet\TrackingMode\TrackingModeInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
class TrackingModeService
{
/**
* @var UserDateTimeFactory
*/
protected $dateTime;
/**
* @var TimesheetConfiguration
*/
protected $configuration;
public function __construct(UserDateTimeFactory $dateTime, TimesheetConfiguration $configuration)
{
$this->dateTime = $dateTime;
$this->configuration = $configuration;
}
/**
* @return TrackingModeInterface[]
*/
public function getModes(): iterable
{
return [
new DefaultMode($this->dateTime, $this->configuration),
new PunchInOutMode(),
new DurationOnlyMode($this->dateTime, $this->configuration),
new DurationFixedStartMode($this->dateTime, $this->configuration),
];
}
public function getActiveMode(): TrackingModeInterface
{
$trackingMode = $this->configuration->getTrackingMode();
foreach ($this->getModes() as $mode) {
if ($mode->getId() === $trackingMode) {
return $mode;
}
}
throw new ServiceNotFoundException($trackingMode);
}
}

View File

@@ -19,9 +19,6 @@ class UserDateTimeFactory
*/
protected $timezone;
/**
* @param CurrentUser $user
*/
public function __construct(CurrentUser $user)
{
$timezone = date_default_timezone_get();
@@ -34,18 +31,12 @@ class UserDateTimeFactory
$this->timezone = new \DateTimeZone($timezone);
}
/**
* @return \DateTimeZone
*/
public function getTimezone()
public function getTimezone(): \DateTimeZone
{
return $this->timezone;
}
/**
* @return \DateTime
*/
public function getStartOfMonth()
public function getStartOfMonth(): \DateTime
{
$date = $this->createDateTime('first day of this month');
$date->setTime(0, 0, 0);
@@ -53,10 +44,7 @@ class UserDateTimeFactory
return $date;
}
/**
* @return \DateTime
*/
public function getEndOfMonth()
public function getEndOfMonth(): \DateTime
{
$date = $this->createDateTime('last day of this month');
$date->setTime(23, 59, 59);
@@ -64,11 +52,7 @@ class UserDateTimeFactory
return $date;
}
/**
* @param string $datetime
* @return \DateTime
*/
public function createDateTime(string $datetime = 'now')
public function createDateTime(string $datetime = 'now'): \DateTime
{
$date = new \DateTime($datetime, $this->timezone);

View File

@@ -1,48 +0,0 @@
<?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\Twig;
use App\Configuration\TimesheetConfiguration;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class TimesheetConfigExtension extends AbstractExtension
{
/**
* @var TimesheetConfiguration
*/
protected $configuration;
public function __construct(TimesheetConfiguration $configuration)
{
$this->configuration = $configuration;
}
/**
* @return TwigFunction[]
*/
public function getFunctions()
{
return [
new TwigFunction('is_duration_only', [$this, 'isDurationOnly']),
new TwigFunction('is_punch_mode', [$this, 'isPunchInOut']),
];
}
public function isDurationOnly(): bool
{
return $this->configuration->isDurationOnly();
}
public function isPunchInOut(): bool
{
return $this->configuration->isPunchInOut();
}
}

View File

@@ -11,6 +11,7 @@ namespace App\Validator\Constraints;
use App\Configuration\TimesheetConfiguration;
use App\Entity\Timesheet as TimesheetEntity;
use App\Timesheet\TrackingModeService;
use App\Validator\Constraints\Timesheet as TimesheetConstraint;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Validator\Constraint;
@@ -28,15 +29,20 @@ class TimesheetValidator extends ConstraintValidator
* @var TimesheetConfiguration
*/
protected $configuration;
/**
* @var TrackingModeService
*/
protected $trackingModeService;
/**
* @param AuthorizationCheckerInterface $auth
* @param TimesheetConfiguration $configuration
*/
public function __construct(AuthorizationCheckerInterface $auth, TimesheetConfiguration $configuration)
public function __construct(AuthorizationCheckerInterface $auth, TimesheetConfiguration $configuration, TrackingModeService $service)
{
$this->auth = $auth;
$this->configuration = $configuration;
$this->trackingModeService = $service;
}
/**
@@ -68,9 +74,16 @@ class TimesheetValidator extends ConstraintValidator
// an entry is edited and the end date is removed (or duration deleted) would restart the record,
// which might be disallowed for the current user
if ($context->getViolations()->count() == 0 && null === $timesheet->getEnd()) {
$mode = $this->trackingModeService->getActiveMode();
$path = 'start';
if ($mode->canEditEnd()) {
$path = 'end';
} elseif ($mode->canEditDuration()) {
$path = 'duration';
}
if (!$this->auth->isGranted('start', $timesheet)) {
$context->buildViolation('You are not allowed to start this timesheet record.')
->atPath($this->configuration->isDurationOnly() ? 'duration' : 'end')
->atPath($path)
->setTranslationDomain('validators')
->setCode(TimesheetConstraint::START_DISALLOWED)
->addViolation();