fix changing project for existing entries (#940)
This commit is contained in:
@@ -79,7 +79,6 @@ class TimesheetEditForm extends AbstractType
|
|||||||
$currency = false;
|
$currency = false;
|
||||||
$begin = null;
|
$begin = null;
|
||||||
$customerCount = $this->customers->countCustomer(true);
|
$customerCount = $this->customers->countCustomer(true);
|
||||||
$projectCount = $this->projects->countProject(true);
|
|
||||||
$timezone = $this->dateTime->getTimezone()->getName();
|
$timezone = $this->dateTime->getTimezone()->getName();
|
||||||
$isNew = true;
|
$isNew = true;
|
||||||
|
|
||||||
@@ -132,7 +131,7 @@ class TimesheetEditForm extends AbstractType
|
|||||||
$this->addCustomer($builder, $customer);
|
$this->addCustomer($builder, $customer);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->addProject($builder, $customerCount, $projectCount, $project, $customer);
|
$this->addProject($builder, $customerCount, $isNew, $project, $customer);
|
||||||
$this->addActivity($builder, $activity, $project);
|
$this->addActivity($builder, $activity, $project);
|
||||||
$this->addDescription($builder);
|
$this->addDescription($builder);
|
||||||
$this->addTags($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 = [];
|
$projectOptions = [];
|
||||||
|
|
||||||
@@ -183,10 +182,6 @@ class TimesheetEditForm extends AbstractType
|
|||||||
$projectOptions['group_by'] = null;
|
$projectOptions['group_by'] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($projectCount < 2) {
|
|
||||||
$projectOptions['group_by'] = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$builder
|
$builder
|
||||||
->add(
|
->add(
|
||||||
'project',
|
'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
|
// replaces the project select after submission, to make sure only projects for the selected customer are displayed
|
||||||
$builder->addEventListener(
|
$builder->addEventListener(
|
||||||
FormEvents::PRE_SUBMIT,
|
FormEvents::PRE_SUBMIT,
|
||||||
function (FormEvent $event) use ($project) {
|
function (FormEvent $event) use ($project, $customer, $isNew) {
|
||||||
$data = $event->getData();
|
$data = $event->getData();
|
||||||
if (!isset($data['customer']) || empty($data['customer'])) {
|
$customer = isset($data['customer']) && !empty($data['customer']) ? $data['customer'] : null;
|
||||||
return;
|
$project = isset($data['project']) && !empty($data['project']) ? $data['project'] : $project;
|
||||||
}
|
|
||||||
|
|
||||||
$event->getForm()->add('project', ProjectType::class, [
|
$event->getForm()->add('project', ProjectType::class, [
|
||||||
'placeholder' => '',
|
'placeholder' => '',
|
||||||
'activity_enabled' => true,
|
'activity_enabled' => true,
|
||||||
'group_by' => null,
|
'group_by' => null,
|
||||||
'query_builder' => function (ProjectRepository $repo) use ($data, $project) {
|
'query_builder' => function (ProjectRepository $repo) use ($project, $customer, $isNew) {
|
||||||
return $repo->getQueryBuilderForFormType(new ProjectFormTypeQuery($project, $data['customer']));
|
// 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));
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
// 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
|
// already filtered within the repository due to the hidden customer
|
||||||
public function testPostActionWithInvisibleProject()
|
public function testPostActionWithInvisibleProjectIsAccepted()
|
||||||
{
|
{
|
||||||
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
|
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
|
||||||
|
|
||||||
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
|
$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);
|
$em->persist($customer);
|
||||||
$project = (new Project())->setName('foo-bar-2')->setVisible(true)->setCustomer($customer);
|
$project = (new Project())->setName('foo-bar-2')->setVisible(true)->setCustomer($customer);
|
||||||
$em->persist($project);
|
$em->persist($project);
|
||||||
@@ -339,7 +339,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
|
|||||||
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
|
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
|
||||||
|
|
||||||
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
|
$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);
|
$em->persist($customer);
|
||||||
$project = (new Project())->setName('foo-bar-2')->setVisible(true)->setCustomer($customer);
|
$project = (new Project())->setName('foo-bar-2')->setVisible(true)->setCustomer($customer);
|
||||||
$em->persist($project);
|
$em->persist($project);
|
||||||
|
|||||||
Reference in New Issue
Block a user