create timesheet for multiple users (#1716)

This commit is contained in:
Kevin Papst
2020-05-18 23:35:43 +02:00
committed by GitHub
parent 9da46bbdcd
commit e61ffe4db4
20 changed files with 548 additions and 16 deletions

View File

@@ -0,0 +1,86 @@
<?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\Form\Model;
use App\Entity\Team;
use App\Entity\Timesheet;
use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* @App\Validator\Constraints\TimesheetMultiUser
*/
final class MultiUserTimesheet extends Timesheet
{
/**
* @var Collection<User>
*/
private $users;
/**
* @var Collection<Team>
*/
private $teams;
public function __construct()
{
parent::__construct();
$this->users = new ArrayCollection();
$this->teams = new ArrayCollection();
}
/**
* @return Collection<User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user)
{
$this->users->add($user);
return $this;
}
public function removeUser(User $user)
{
if ($this->users->contains($user)) {
$this->users->remove($user);
}
return $this;
}
/**
* @return Collection<Team>
*/
public function getTeams(): Collection
{
return $this->teams;
}
public function addTeam(Team $team)
{
$this->teams->add($team);
return $this;
}
public function removeTeam(Team $team)
{
if ($this->teams->contains($team)) {
$this->teams->remove($team);
}
return $this;
}
}

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\Form;
use App\Form\Type\TeamMemberType;
use App\Form\Type\TeamType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TimesheetMultiUserEditForm extends TimesheetAdminEditForm
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$options['allow_begin_datetime'] = true;
$options['allow_end_datetime'] = true;
$options['allow_duration'] = false;
$options['include_user'] = false;
parent::buildForm($builder, $options);
$builder->add('users', TeamMemberType::class, [
'multiple' => true,
'required' => false,
]);
$builder->add('teams', TeamType::class, [
'multiple' => true,
'required' => false,
]);
}
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
}
}

View File

@@ -37,6 +37,7 @@ class PageSizeType extends AbstractType
500 => 500
],
'placeholder' => null,
'choice_translation_domain' => false,
]);
}

View File

@@ -0,0 +1,68 @@
<?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\Form\Type;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Select a user that
*/
class TeamMemberType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'class' => User::class,
'label' => 'label.user',
'choice_label' => function (User $user) {
return $user->getDisplayName();
},
]);
$resolver->setDefault('query_builder', function (Options $options) {
return function (UserRepository $repo) use ($options) {
$qb = $repo->createQueryBuilder('u');
$qb
->andWhere($qb->expr()->eq('u.enabled', ':enabled'))
->setParameter('enabled', true, \PDO::PARAM_BOOL)
->orderBy('u.username', 'ASC');
/** @var User $user */
$user = $options['user'];
if (null !== $user && !$user->getTeams()->isEmpty() && !$user->isSuperAdmin() && !$user->isAdmin()) {
$qb
->leftJoin('u.teams', 'teams')
->leftJoin('teams.users', 'users')
->andWhere($qb->expr()->isMemberOf(':teams', 'u.teams'))
->setParameter('teams', $user->getTeams());
}
return $qb;
};
});
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return EntityType::class;
}
}

View File

@@ -33,6 +33,7 @@ class UserType extends AbstractType
'choice_label' => function (User $user) {
return $user->getDisplayName();
},
'choice_translation_domain' => false,
]);
$resolver->setDefault('query_builder', function (Options $options) {