fix changing project for existing entries (#940)

This commit is contained in:
Kevin Papst
2019-07-10 19:17:34 +02:00
committed by GitHub
parent c33a87a07c
commit 8a1d28563d
2 changed files with 24 additions and 16 deletions

View File

@@ -79,7 +79,6 @@ class TimesheetEditForm extends AbstractType
$currency = false;
$begin = null;
$customerCount = $this->customers->countCustomer(true);
$projectCount = $this->projects->countProject(true);
$timezone = $this->dateTime->getTimezone()->getName();
$isNew = true;
@@ -132,7 +131,7 @@ class TimesheetEditForm extends AbstractType
$this->addCustomer($builder, $customer);
}
$this->addProject($builder, $customerCount, $projectCount, $project, $customer);
$this->addProject($builder, $customerCount, $isNew, $project, $customer);
$this->addActivity($builder, $activity, $project);
$this->addDescription($builder);
$this->addTags($builder);
@@ -175,7 +174,7 @@ class TimesheetEditForm extends AbstractType
]);
}
protected function addProject(FormBuilderInterface $builder, int $customerCount, int $projectCount, ?Project $project = null, ?Customer $customer = null)
protected function addProject(FormBuilderInterface $builder, int $customerCount, bool $isNew, ?Project $project = null, ?Customer $customer = null)
{
$projectOptions = [];
@@ -183,10 +182,6 @@ class TimesheetEditForm extends AbstractType
$projectOptions['group_by'] = null;
}
if ($projectCount < 2) {
$projectOptions['group_by'] = null;
}
$builder
->add(
'project',
@@ -203,18 +198,31 @@ class TimesheetEditForm extends AbstractType
// replaces the project select after submission, to make sure only projects for the selected customer are displayed
$builder->addEventListener(
FormEvents::PRE_SUBMIT,
function (FormEvent $event) use ($project) {
function (FormEvent $event) use ($project, $customer, $isNew) {
$data = $event->getData();
if (!isset($data['customer']) || empty($data['customer'])) {
return;
}
$customer = isset($data['customer']) && !empty($data['customer']) ? $data['customer'] : null;
$project = isset($data['project']) && !empty($data['project']) ? $data['project'] : $project;
$event->getForm()->add('project', ProjectType::class, [
'placeholder' => '',
'activity_enabled' => true,
'group_by' => null,
'query_builder' => function (ProjectRepository $repo) use ($data, $project) {
return $repo->getQueryBuilderForFormType(new ProjectFormTypeQuery($project, $data['customer']));
'query_builder' => function (ProjectRepository $repo) use ($project, $customer, $isNew) {
// is there a better wa to prevent starting a record with a hidden project ?
if ($isNew && !is_object($project)) {
/** @var Project $project */
$project = $repo->find($project);
if (null !== $project) {
if (!$project->getCustomer()->getVisible()) {
$customer = null;
$project = null;
} elseif (!$project->getVisible()) {
$project = null;
}
}
}
return $repo->getQueryBuilderForFormType(new ProjectFormTypeQuery($project, $customer));
},
]);
}

View File

@@ -306,12 +306,12 @@ class TimesheetControllerTest extends APIControllerBaseTest
// check for project, as this is a required field. It will not be included in the select, as it is
// already filtered within the repository due to the hidden customer
public function testPostActionWithInvisibleProject()
public function testPostActionWithInvisibleProjectIsAccepted()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$customer = (new Customer())->setName('foo-bar-1')->setVisible(false)->setCountry('DE')->setTimezone('Euopre/Berlin');
$customer = (new Customer())->setName('foo-bar-1')->setVisible(false)->setCountry('DE')->setTimezone('Europe/Berlin');
$em->persist($customer);
$project = (new Project())->setName('foo-bar-2')->setVisible(true)->setCustomer($customer);
$em->persist($project);
@@ -339,7 +339,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$customer = (new Customer())->setName('foo-bar-1')->setVisible(true)->setCountry('DE')->setTimezone('Euopre/Berlin');
$customer = (new Customer())->setName('foo-bar-1')->setVisible(true)->setCountry('DE')->setTimezone('Europe/Berlin');
$em->persist($customer);
$project = (new Project())->setName('foo-bar-2')->setVisible(true)->setCustomer($customer);
$em->persist($project);