simplify timesheet edit form if only one customer is existing (#443)

This commit is contained in:
Kevin Papst
2018-11-28 15:05:12 +01:00
committed by GitHub
parent 8fddf627bf
commit 2d6109d587
4 changed files with 69 additions and 25 deletions

2
.github/lock.yml vendored
View File

@@ -14,6 +14,8 @@ lockComment: >
This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.
If you use Kimai on a daily basis, please [consider donating](https://www.kimai.org/donate/) to
support further development of Kimai.
# Assign `resolved` as the reason for locking. Set to `false` to disable
setLockReason: false

View File

@@ -32,6 +32,26 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
*/
class TimesheetEditForm extends AbstractType
{
/**
* @var CustomerRepository
*/
private $customers;
/**
* @var ProjectRepository
*/
private $projects;
/**
* @param CustomerRepository $customer
* @param ProjectRepository $project
*/
public function __construct(CustomerRepository $customer, ProjectRepository $project)
{
$this->customers = $customer;
$this->projects = $project;
}
/**
* {@inheritdoc}
*/
@@ -82,34 +102,45 @@ class TimesheetEditForm extends AbstractType
]);
}
$projectOptions = [];
if ($this->customers->countCustomer(true) > 1) {
$builder
->add('customer', CustomerType::class, [
// documentation is for NelmioApiDocBundle
'documentation' => [
'type' => 'integer',
'description' => 'Customer ID',
],
'query_builder' => function (CustomerRepository $repo) use ($customer) {
return $repo->builderForEntityType($customer);
},
'data' => $customer ? $customer : '',
'required' => false,
'mapped' => false,
'attr' => [
'data-related-select' => $this->getBlockPrefix() . '_project',
'data-api-url' => ['get_projects', ['customer' => '-s-']],
],
]);
} else {
$projectOptions['group_by'] = null;
}
if ($this->projects->countProject(true) > 1) {
$projectOptions['placeholder'] = null;
} else {
$projectOptions['group_by'] = null;
}
$builder
->add('customer', CustomerType::class, [
// documentation is for NelmioApiDocBundle
'documentation' => [
'type' => 'integer',
'description' => 'Customer ID',
],
'label' => 'label.customer',
'query_builder' => function (CustomerRepository $repo) use ($customer) {
return $repo->builderForEntityType($customer);
},
'data' => $customer ? $customer : '',
'required' => false,
'mapped' => false,
'attr' => [
'data-related-select' => $this->getBlockPrefix() . '_project',
'data-api-url' => ['get_projects', ['customer' => '-s-']],
],
])
->add('project', ProjectType::class, [
->add('project', ProjectType::class, array_merge($projectOptions, [
// documentation is for NelmioApiDocBundle
'documentation' => [
'type' => 'integer',
'description' => 'Project ID',
],
'required' => true,
'placeholder' => '',
'label' => 'label.project',
'query_builder' => function (ProjectRepository $repo) use ($project) {
return $repo->builderForEntityType($project);
},
@@ -117,14 +148,15 @@ class TimesheetEditForm extends AbstractType
'data-related-select' => $this->getBlockPrefix() . '_activity',
'data-api-url' => ['get_activities', ['project' => '-s-']],
],
])
]));
$builder
->add('activity', ActivityType::class, [
// documentation is for NelmioApiDocBundle
'documentation' => [
'type' => 'integer',
'description' => 'Activity ID',
],
'label' => 'label.activity',
'query_builder' => function (ActivityRepository $repo) use ($activity) {
return $repo->builderForEntityType($activity);
},

View File

@@ -35,10 +35,15 @@ class CustomerRepository extends AbstractRepository
}
/**
* @param null|bool $visible
* @return int
*/
public function countCustomer()
public function countCustomer($visible = null)
{
if (null !== $visible) {
return $this->count(['visible' => (int) $visible]);
}
return $this->count([]);
}

View File

@@ -35,10 +35,15 @@ class ProjectRepository extends AbstractRepository
}
/**
* @param null|bool $visible
* @return int
*/
public function countProject()
public function countProject($visible = null)
{
if (null !== $visible) {
return $this->count(['visible' => (int) $visible]);
}
return $this->count([]);
}