Skip to content

Commit

Permalink
assign existing tag on timesheet import (kimai#1877) (kimai#1883)
Browse files Browse the repository at this point in the history
  • Loading branch information
synaestic authored and Sergii committed Sep 29, 2020
1 parent ad6a7f7 commit 1675729
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
19 changes: 15 additions & 4 deletions src/Command/ImportTimesheetCommand.php
Expand Up @@ -21,6 +21,7 @@
use App\Repository\ActivityRepository;
use App\Repository\CustomerRepository;
use App\Repository\ProjectRepository;
use App\Repository\TagRepository;
use App\Repository\TimesheetRepository;
use App\Repository\UserRepository;
use App\Utils\Duration;
Expand Down Expand Up @@ -79,6 +80,10 @@ class ImportTimesheetCommand extends Command
* @var UserRepository
*/
private $users;
/**
* @var TagRepository
*/
private $tagRepository;
/**
* @var TimesheetRepository
*/
Expand Down Expand Up @@ -116,13 +121,14 @@ class ImportTimesheetCommand extends Command
*/
private $begin = self::DEFAULT_BEGIN;

public function __construct(CustomerRepository $customers, ProjectRepository $projects, ActivityRepository $activities, UserRepository $users, TimesheetRepository $timesheets, FormConfiguration $configuration)
public function __construct(CustomerRepository $customers, ProjectRepository $projects, ActivityRepository $activities, UserRepository $users, TagRepository $tagRepository, TimesheetRepository $timesheets, FormConfiguration $configuration)
{
parent::__construct();
$this->customers = $customers;
$this->projects = $projects;
$this->activities = $activities;
$this->users = $users;
$this->tagRepository = $tagRepository;
$this->timesheets = $timesheets;
$this->configuration = $configuration;
}
Expand Down Expand Up @@ -309,11 +315,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
$timesheet->setExported((bool) $record['Exported']);

if (!empty($record['Tags'])) {
foreach (explode(',', $record['Tags']) as $tag) {
if (empty($tag)) {
foreach (explode(',', $record['Tags']) as $tagName) {
if (empty($tagName)) {
continue;
}
$timesheet->addTag((new Tag())->setName($tag));

if (null === ($tag = $this->tagRepository->findTagByName($tagName))) {
$tag = (new Tag())->setName($tagName);
}

$timesheet->addTag($tag);
}
}

Expand Down
4 changes: 3 additions & 1 deletion tests/Command/ImportTimesheetCommandTest.php
Expand Up @@ -14,6 +14,7 @@
use App\Repository\ActivityRepository;
use App\Repository\CustomerRepository;
use App\Repository\ProjectRepository;
use App\Repository\TagRepository;
use App\Repository\TimesheetRepository;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Console\Application;
Expand All @@ -39,10 +40,11 @@ protected function setUp(): void
$projects = $this->createMock(ProjectRepository::class);
$activities = $this->createMock(ActivityRepository::class);
$users = $this->createMock(UserRepository::class);
$tagRepository = $this->createMock(TagRepository::class);
$timesheets = $this->createMock(TimesheetRepository::class);
$configuration = $this->createMock(FormConfiguration::class);

$this->application->add(new ImportTimesheetCommand($customers, $projects, $activities, $users, $timesheets, $configuration));
$this->application->add(new ImportTimesheetCommand($customers, $projects, $activities, $users, $tagRepository, $timesheets, $configuration));
}

public function testCommandName()
Expand Down

0 comments on commit 1675729

Please sign in to comment.