From 8a1d28563d5b4b86b4ec0829560ec4de17e8632b Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Wed, 10 Jul 2019 19:17:34 +0200 Subject: [PATCH] fix changing project for existing entries (#940) --- src/Form/TimesheetEditForm.php | 34 +++++++++++++++++---------- tests/API/TimesheetControllerTest.php | 6 ++--- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/Form/TimesheetEditForm.php b/src/Form/TimesheetEditForm.php index 6024e91d..b57c3a0d 100644 --- a/src/Form/TimesheetEditForm.php +++ b/src/Form/TimesheetEditForm.php @@ -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)); }, ]); } diff --git a/tests/API/TimesheetControllerTest.php b/tests/API/TimesheetControllerTest.php index 9e4cc85a..4514699f 100644 --- a/tests/API/TimesheetControllerTest.php +++ b/tests/API/TimesheetControllerTest.php @@ -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);