|
14 | 14 | use App\Entity\Timesheet; |
15 | 15 | use App\Timesheet\TimesheetService; |
16 | 16 | use App\Utils\PageSetup; |
| 17 | +use App\Validator\ValidationFailedException; |
17 | 18 | use KimaiPlugin\PeriodInsertBundle\Entity\PeriodInsert; |
18 | | -use KimaiPlugin\PeriodInsertBundle\Form\PeriodInsertType; |
| 19 | +use KimaiPlugin\PeriodInsertBundle\Form\PeriodInsertForm; |
19 | 20 | use KimaiPlugin\PeriodInsertBundle\Repository\PeriodInsertRepository; |
20 | 21 | use Symfony\Component\Form\FormInterface; |
21 | 22 | use Symfony\Component\HttpFoundation\Request; |
|
25 | 26 |
|
26 | 27 | #[Route(path: '/period_insert')] |
27 | 28 | #[IsGranted('period_insert')] |
28 | | -class PeriodInsertController extends AbstractController |
| 29 | +final class PeriodInsertController extends AbstractController |
29 | 30 | { |
30 | 31 | public function __construct( |
31 | 32 | protected PeriodInsertRepository $repository, |
32 | | - protected TimesheetService $service, |
| 33 | + protected TimesheetService $timesheetService, |
33 | 34 | protected SystemConfiguration $configuration |
34 | | - ) { |
| 35 | + ) |
| 36 | + { |
35 | 37 | } |
36 | 38 |
|
| 39 | + /** |
| 40 | + * @param Request $request |
| 41 | + * @return Response |
| 42 | + */ |
37 | 43 | #[Route(path: '', name: 'period_insert', methods: ['GET', 'POST'])] |
38 | 44 | public function indexAction(Request $request): Response |
39 | 45 | { |
40 | | - $entry = $this->service->createNewTimesheet($this->getUser(), $request); |
| 46 | + $timesheet = $this->timesheetService->createNewTimesheet($this->getUser(), $request); |
41 | 47 |
|
42 | | - $entity = $this->repository->getTimesheet(); |
43 | | - $entity->setUser($this->getUser()); |
| 48 | + $periodInsert = new PeriodInsert(); |
| 49 | + $periodInsert->setUser($this->getUser()); |
| 50 | + if (!$this->timesheetService->getActiveTrackingMode()->canEditBegin()) { |
| 51 | + $periodInsert->setBeginTime($timesheet->getBegin()); |
| 52 | + } |
44 | 53 |
|
45 | | - $form = $this->getInsertForm($entity, $entry); |
| 54 | + $form = $this->getInsertForm($periodInsert, $timesheet); |
46 | 55 | $form->handleRequest($request); |
47 | 56 |
|
48 | 57 | if ($form->isSubmitted() && $form->isValid()) { |
49 | | - /** @var PeriodInsert $entity */ |
50 | | - $entity = $form->getData(); |
51 | | - if ($this->service->getActiveTrackingMode()->getId() === 'duration_fixed_begin') { |
52 | | - $entity->setBeginTime($entry->getBegin()); |
53 | | - } |
54 | | - $entity->setFields(); |
55 | | - |
56 | | - if (($dayToInsert = $this->repository->findDayToInsert($entity)) === '') { |
57 | | - $this->flashError('Could not find a day to insert in the given time range.'); |
58 | | - } |
59 | | - else if ($this->repository->checkFutureTime($entity, $dayToInsert)) { |
60 | | - $this->flashError('The time range cannot be in the future.'); |
61 | | - } |
62 | | - else if ($this->repository->checkZeroDuration($entity)) { |
63 | | - $this->flashError('Duration cannot be zero.'); |
64 | | - } |
65 | | - else if (($overlap = $this->repository->checkOverlappingTimeEntries($entity)) !== '') { |
66 | | - $this->flashError('You already have an entry on ' . $overlap . '.'); |
67 | | - } |
68 | | - else if (($message = $this->repository->checkBudgetOverbooked($entity)) !== '') { |
69 | | - $this->flashError($message); |
70 | | - } |
71 | | - else { |
72 | | - try { |
73 | | - $this->repository->saveTimesheet($entity); |
74 | | - $this->flashSuccess('action.update.success'); |
75 | | - |
76 | | - return $this->redirectToRoute('period_insert'); |
77 | | - } catch (\Exception $ex) { |
78 | | - $this->flashUpdateException($ex); |
79 | | - } |
| 58 | + try { |
| 59 | + $this->repository->savePeriodInsert($periodInsert); |
| 60 | + $this->flashSuccess('action.update.success'); |
| 61 | + |
| 62 | + return $this->redirectToRoute('period_insert'); |
| 63 | + } catch (ValidationFailedException $ex) { |
| 64 | + $this->handleFormUpdateException($ex, $form); |
80 | 65 | } |
81 | 66 | } |
82 | 67 |
|
83 | 68 | return $this->render('@PeriodInsert/index.html.twig', [ |
84 | 69 | 'page_setup' => $this->createPageSetup(), |
85 | 70 | 'route_back' => 'timesheet', |
86 | | - 'entity' => $entity, |
| 71 | + 'period_insert' => $periodInsert, |
87 | 72 | 'form' => $form->createView(), |
88 | 73 | ]); |
89 | 74 | } |
90 | 75 |
|
91 | 76 | /** |
92 | | - * @param PeriodInsert $entity |
93 | | - * @param Timesheet $entry |
| 77 | + * @param PeriodInsert $periodInsert |
| 78 | + * @param Timesheet $timesheet |
94 | 79 | * @return FormInterface |
95 | 80 | */ |
96 | | - protected function getInsertForm(PeriodInsert $entity, Timesheet $entry): FormInterface |
| 81 | + private function getInsertForm(PeriodInsert $periodInsert, Timesheet $timesheet): FormInterface |
97 | 82 | { |
98 | | - return $this->createForm(PeriodInsertType::class, $entity, [ |
| 83 | + return $this->createForm(PeriodInsertForm::class, $periodInsert, [ |
99 | 84 | 'action' => $this->generateUrl('period_insert'), |
100 | | - 'include_user' => $this->isGranted('ROLE_SUPER_ADMIN'), |
101 | | - 'include_rate' => $this->isGranted('edit_rate', $entry), |
102 | | - 'include_billable' => $this->isGranted('edit_billable', $entry), |
103 | | - 'include_exported' => $this->isGranted('edit_export', $entry), |
104 | | - 'allow_begin_datetime' => $this->service->getActiveTrackingMode()->canEditBegin(), |
| 85 | + 'include_user' => $this->isGranted('create_other_timesheet'), |
| 86 | + 'include_rate' => $this->isGranted('edit_rate', $timesheet), |
| 87 | + 'include_billable' => $this->isGranted('edit_billable', $timesheet), |
| 88 | + 'include_exported' => $this->isGranted('edit_export', $timesheet), |
| 89 | + 'allow_begin_datetime' => $this->timesheetService->getActiveTrackingMode()->canEditBegin(), |
105 | 90 | 'duration_minutes' => $this->configuration->getTimesheetIncrementDuration(), |
106 | 91 | 'timezone' => $this->getDateTimeFactory()->getTimezone()->getName(), |
107 | | - 'customer' => true, |
108 | 92 | ]); |
109 | 93 | } |
110 | 94 |
|
111 | 95 | /** |
112 | 96 | * @return PageSetup |
113 | 97 | */ |
114 | | - protected function createPageSetup(): PageSetup |
| 98 | + private function createPageSetup(): PageSetup |
115 | 99 | { |
116 | 100 | $page = new PageSetup('periodinsert.title'); |
117 | 101 | $page->setHelp('https://www.kimai.org/store/lnngyn-period-insert-bundle.html'); |
|
0 commit comments