Skip to content

Commit

Permalink
pre-populate tags for new timesheet records via request params (kimai…
Browse files Browse the repository at this point in the history
  • Loading branch information
tobybatch authored and kevinpapst committed Jul 9, 2019
1 parent 6de1a55 commit b833e1c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
14 changes: 13 additions & 1 deletion src/Controller/TimesheetAbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions src/Controller/TimesheetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/TimesheetTeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,9 @@
"ref": "cda8b550123383d25827705d05a42acf6819fe4e"
}
},
"symfony/security": {
"version": "v4.2.8"
},
"symfony/security-bundle": {
"version": "3.3",
"recipe": {
Expand Down
6 changes: 4 additions & 2 deletions tests/Controller/TimesheetControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit b833e1c

Please sign in to comment.