From b833e1ce27ef9edbc77d1f94fc70ab608f4cdbb7 Mon Sep 17 00:00:00 2001 From: Toby Batch Date: Tue, 9 Jul 2019 15:32:40 +0100 Subject: [PATCH] pre-populate tags for new timesheet records via request params (#930) --- src/Controller/TimesheetAbstractController.php | 14 +++++++++++++- src/Controller/TimesheetController.php | 5 +++-- src/Controller/TimesheetTeamController.php | 5 +++-- symfony.lock | 3 +++ tests/Controller/TimesheetControllerTest.php | 6 ++++-- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/Controller/TimesheetAbstractController.php b/src/Controller/TimesheetAbstractController.php index 653ab3810c..a77121fd1b 100644 --- a/src/Controller/TimesheetAbstractController.php +++ b/src/Controller/TimesheetAbstractController.php @@ -165,7 +165,7 @@ protected function edit(Timesheet $entry, Request $request, string $renderTempla * @param ActivityRepository $activityRepository * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response */ - protected function create(Request $request, string $renderTemplate, ProjectRepository $projectRepository, ActivityRepository $activityRepository) + protected function create(Request $request, string $renderTemplate, ProjectRepository $projectRepository, ActivityRepository $activityRepository, TagRepository $tagRepository) { $entry = new Timesheet(); $entry->setUser($this->getUser()); @@ -181,6 +181,18 @@ protected function create(Request $request, string $renderTemplate, ProjectRepos $entry->setActivity($activity); } + if ($request->query->get('tags')) { + $tagNames = explode(',', $request->query->get('tags')); + foreach ($tagNames as $tagName) { + $tag = $tagRepository->findOneByName($tagName); + if (!$tag) { + $tag = new Tag(); + $tag->setName($tagName); + } + $entry->addTag($tag); + } + } + $event = new TimesheetMetaDefinitionEvent($entry); $this->dispatcher->dispatch(TimesheetMetaDefinitionEvent::class, $event); diff --git a/src/Controller/TimesheetController.php b/src/Controller/TimesheetController.php index 3c0d793403..009011b895 100644 --- a/src/Controller/TimesheetController.php +++ b/src/Controller/TimesheetController.php @@ -12,6 +12,7 @@ use App\Entity\Timesheet; use App\Repository\ActivityRepository; use App\Repository\ProjectRepository; +use App\Repository\TagRepository; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; @@ -70,9 +71,9 @@ public function editAction(Timesheet $entry, Request $request) * @param ActivityRepository $activityRepository * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response */ - public function createAction(Request $request, ProjectRepository $projectRepository, ActivityRepository $activityRepository) + public function createAction(Request $request, ProjectRepository $projectRepository, ActivityRepository $activityRepository, TagRepository $tagRepository) { - return $this->create($request, 'timesheet/edit.html.twig', $projectRepository, $activityRepository); + return $this->create($request, 'timesheet/edit.html.twig', $projectRepository, $activityRepository, $tagRepository); } /** diff --git a/src/Controller/TimesheetTeamController.php b/src/Controller/TimesheetTeamController.php index 71d3e37384..d385f88fb1 100644 --- a/src/Controller/TimesheetTeamController.php +++ b/src/Controller/TimesheetTeamController.php @@ -13,6 +13,7 @@ use App\Form\TimesheetAdminEditForm; use App\Repository\ActivityRepository; use App\Repository\ProjectRepository; +use App\Repository\TagRepository; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; @@ -70,9 +71,9 @@ public function editAction(Timesheet $entry, Request $request) * @param ActivityRepository $activityRepository * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response */ - public function createAction(Request $request, ProjectRepository $projectRepository, ActivityRepository $activityRepository) + public function createAction(Request $request, ProjectRepository $projectRepository, ActivityRepository $activityRepository, TagRepository $tagRepository) { - return $this->create($request, 'timesheet-team/edit.html.twig', $projectRepository, $activityRepository); + return $this->create($request, 'timesheet-team/edit.html.twig', $projectRepository, $activityRepository, $tagRepository); } protected function getCreateFormClassName(): string diff --git a/symfony.lock b/symfony.lock index cc0f81b46b..0fc0bfeb1d 100644 --- a/symfony.lock +++ b/symfony.lock @@ -591,6 +591,9 @@ "ref": "cda8b550123383d25827705d05a42acf6819fe4e" } }, + "symfony/security": { + "version": "v4.2.8" + }, "symfony/security-bundle": { "version": "3.3", "recipe": { diff --git a/tests/Controller/TimesheetControllerTest.php b/tests/Controller/TimesheetControllerTest.php index 16286c149a..da99e604d6 100644 --- a/tests/Controller/TimesheetControllerTest.php +++ b/tests/Controller/TimesheetControllerTest.php @@ -202,10 +202,10 @@ public function testCreateActionWithFromAndToValues() $this->assertEquals($expected->format(\DateTime::ATOM), $timesheet->getEnd()->format(\DateTime::ATOM)); } - public function testCreateActionWithBeginAndEndValues() + public function testCreateActionWithBeginAndEndAndTagValues() { $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); - $this->request($client, '/timesheet/create?begin=2018-08-02&end=2018-08-02'); + $this->request($client, '/timesheet/create?begin=2018-08-02&end=2018-08-02&tags=one,two,three'); $this->assertTrue($client->getResponse()->isSuccessful()); $form = $client->getCrawler()->filter('form[name=timesheet_edit_form]')->form(); @@ -234,6 +234,8 @@ public function testCreateActionWithBeginAndEndValues() $expected = new \DateTime('2018-08-02T18:00:00'); $this->assertEquals($expected->format(\DateTime::ATOM), $timesheet->getEnd()->format(\DateTime::ATOM)); + + $this->assertEquals(['one', 'two', 'three'], $timesheet->getTagsAsArray()); } public function testEditAction()