diff --git a/src/Controller/ActivityController.php b/src/Controller/ActivityController.php index af60cc86..e3ae80c3 100644 --- a/src/Controller/ActivityController.php +++ b/src/Controller/ActivityController.php @@ -35,9 +35,7 @@ use Pagerfanta\Pagerfanta; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\FormInterface; -use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; /** @@ -192,7 +190,27 @@ final class ActivityController extends AbstractController $activity->setProject($project); } - return $this->renderActivityForm($activity, $request); + $event = new ActivityMetaDefinitionEvent($activity); + $this->dispatcher->dispatch($event); + + $editForm = $this->createEditForm($activity); + $editForm->handleRequest($request); + + if ($editForm->isSubmitted() && $editForm->isValid()) { + try { + $this->repository->saveActivity($activity); + $this->flashSuccess('action.update.success'); + + return $this->redirectToRoute('admin_activity'); + } catch (Exception $ex) { + $this->flashUpdateException($ex); + } + } + + return $this->render('activity/edit.html.twig', [ + 'activity' => $activity, + 'form' => $editForm->createView() + ]); } /** @@ -258,7 +276,27 @@ final class ActivityController extends AbstractController */ public function editAction(Activity $activity, Request $request) { - return $this->renderActivityForm($activity, $request); + $event = new ActivityMetaDefinitionEvent($activity); + $this->dispatcher->dispatch($event); + + $editForm = $this->createEditForm($activity); + $editForm->handleRequest($request); + + if ($editForm->isSubmitted() && $editForm->isValid()) { + try { + $this->repository->saveActivity($activity); + $this->flashSuccess('action.update.success'); + + return $this->redirectToRoute('activity_details', ['id' => $activity->getId()]); + } catch (Exception $ex) { + $this->flashUpdateException($ex); + } + } + + return $this->render('activity/edit.html.twig', [ + 'activity' => $activity, + 'form' => $editForm->createView() + ]); } /** @@ -342,47 +380,6 @@ final class ActivityController extends AbstractController return $writer->getFileResponse($spreadsheet); } - /** - * @param Activity $activity - * @param Request $request - * @return RedirectResponse|Response - */ - protected function renderActivityForm(Activity $activity, Request $request) - { - $event = new ActivityMetaDefinitionEvent($activity); - $this->dispatcher->dispatch($event); - - $editForm = $this->createEditForm($activity); - $editForm->handleRequest($request); - - if ($editForm->isSubmitted() && $editForm->isValid()) { - try { - $this->repository->saveActivity($activity); - $this->flashSuccess('action.update.success'); - - if ($editForm->has('create_more') && $editForm->get('create_more')->getData() === true) { - $newActivity = new Activity(); - $newActivity->setProject($activity->getProject()); - $editForm = $this->createEditForm($newActivity); - $editForm->get('create_more')->setData(true); - $activity = $newActivity; - } else { - return $this->redirectToRoute('admin_activity'); - } - } catch (Exception $ex) { - $this->flashUpdateException($ex); - } - } - - return $this->render( - 'activity/edit.html.twig', - [ - 'activity' => $activity, - 'form' => $editForm->createView() - ] - ); - } - /** * @param ActivityQuery $query * @return FormInterface @@ -417,7 +414,6 @@ final class ActivityController extends AbstractController 'action' => $url, 'method' => 'POST', 'currency' => $currency, - 'create_more' => true, 'customer' => true, 'include_budget' => $this->isGranted('budget', $activity) ]); diff --git a/src/Controller/ProjectController.php b/src/Controller/ProjectController.php index e5f7aa78..c92980fd 100644 --- a/src/Controller/ProjectController.php +++ b/src/Controller/ProjectController.php @@ -165,14 +165,7 @@ final class ProjectController extends AbstractController $this->projectService->saveNewProject($project); $this->flashSuccess('action.update.success'); - if ($editForm->has('create_more') && $editForm->get('create_more')->getData() === true) { - $newProject = $this->projectService->createNewProject($project->getCustomer()); - $editForm = $this->createEditForm($newProject); - $editForm->get('create_more')->setData(true); - $project = $newProject; - } else { - return $this->redirectToRoute('project_details', ['id' => $project->getId()]); - } + return $this->redirectToRoute('project_details', ['id' => $project->getId()]); } catch (\Exception $ex) { $this->flashUpdateException($ex); } @@ -530,7 +523,6 @@ final class ProjectController extends AbstractController 'action' => $url, 'method' => 'POST', 'currency' => $currency, - 'create_more' => true, 'include_budget' => $this->isGranted('budget', $project) ]); } diff --git a/src/Form/API/ActivityApiEditForm.php b/src/Form/API/ActivityApiEditForm.php index 1662b2f2..5eb937a2 100644 --- a/src/Form/API/ActivityApiEditForm.php +++ b/src/Form/API/ActivityApiEditForm.php @@ -34,7 +34,6 @@ class ActivityApiEditForm extends ActivityEditForm $resolver->setDefaults([ 'csrf_protection' => false, - 'create_more' => false, ]); } } diff --git a/src/Form/API/ProjectApiEditForm.php b/src/Form/API/ProjectApiEditForm.php index 5b90eb88..d3a941f7 100644 --- a/src/Form/API/ProjectApiEditForm.php +++ b/src/Form/API/ProjectApiEditForm.php @@ -34,7 +34,6 @@ class ProjectApiEditForm extends ProjectEditForm $resolver->setDefaults([ 'csrf_protection' => false, - 'create_more' => false, ]); } } diff --git a/src/Form/ActivityEditForm.php b/src/Form/ActivityEditForm.php index ed242618..2a6dc987 100644 --- a/src/Form/ActivityEditForm.php +++ b/src/Form/ActivityEditForm.php @@ -115,10 +115,6 @@ class ActivityEditForm extends AbstractType ); $this->addCommonFields($builder, $options); - - if (null === $id && $options['create_more']) { - $this->addCreateMore($builder); - } } /** @@ -131,7 +127,6 @@ class ActivityEditForm extends AbstractType 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_token_id' => 'admin_activity_edit', - 'create_more' => false, 'customer' => false, 'currency' => Customer::DEFAULT_CURRENCY, 'include_budget' => false, diff --git a/src/Form/ProjectEditForm.php b/src/Form/ProjectEditForm.php index b89fa9d0..3fe6ac95 100644 --- a/src/Form/ProjectEditForm.php +++ b/src/Form/ProjectEditForm.php @@ -88,10 +88,6 @@ class ProjectEditForm extends AbstractType ]); $this->addCommonFields($builder, $options); - - if (null === $id && $options['create_more']) { - $this->addCreateMore($builder); - } } /** @@ -107,7 +103,6 @@ class ProjectEditForm extends AbstractType 'currency' => Customer::DEFAULT_CURRENCY, 'date_format' => null, 'include_budget' => false, - 'create_more' => false, 'attr' => [ 'data-form-event' => 'kimai.projectUpdate' ], diff --git a/templates/activity/actions.html.twig b/templates/activity/actions.html.twig index 1b6c6323..dc7eedec 100644 --- a/templates/activity/actions.html.twig +++ b/templates/activity/actions.html.twig @@ -6,7 +6,7 @@ {% set actions = actions|merge({'download': {'url': path('activity_export'), 'class': 'toolbar-action'}}) %} {% if is_granted('create_activity') %} - {% set actions = actions|merge({'create': path('admin_activity_create')}) %} + {% set actions = actions|merge({'create': {'url': path('admin_activity_create'), 'class': 'modal-ajax-form'}}) %} {% endif %} {% set actions = actions|merge({'help': {'url': 'activity.html'|docu_link, 'target': '_blank'}}) %} diff --git a/templates/activity/edit.html.twig b/templates/activity/edit.html.twig index 05191580..06469f17 100644 --- a/templates/activity/edit.html.twig +++ b/templates/activity/edit.html.twig @@ -7,7 +7,7 @@ {% block main %} {% set formEditTemplate = app.request.xmlHttpRequest ? 'default/_form_modal.html.twig' : 'default/_form.html.twig' %} {% set formOptions = { - 'title': activity.name|default('create-activity'|trans({}, 'actions')), + 'title': (activity.id is null ? 'create'|trans : 'edit'|trans({}, 'actions')), 'form': form, 'back': path('admin_activity') } %} @@ -40,11 +40,6 @@ {{ form_row(meta) }} {% endfor %} {% endif %} - {% if form.create_more is defined and app.request.xmlHttpRequest %} - - {% endif %} {{ form_widget(form) }} {% endblock %} {% endembed %} diff --git a/templates/customer/actions.html.twig b/templates/customer/actions.html.twig index a0d9d4b9..257e9f06 100644 --- a/templates/customer/actions.html.twig +++ b/templates/customer/actions.html.twig @@ -6,7 +6,7 @@ {% set actions = actions|merge({'download': {'url': path('customer_export'), 'class': 'toolbar-action'}}) %} {% if is_granted('create_customer') %} - {% set actions = actions|merge({'create': path('admin_customer_create')}) %} + {% set actions = actions|merge({'create': {'url': path('admin_customer_create'), 'class': 'modal-ajax-form'}}) %} {% endif %} {% set actions = actions|merge({'help': {'url': 'customer.html'|docu_link, 'target': '_blank'}}) %} diff --git a/templates/customer/edit.html.twig b/templates/customer/edit.html.twig index 81457a9e..241bc09b 100644 --- a/templates/customer/edit.html.twig +++ b/templates/customer/edit.html.twig @@ -7,7 +7,7 @@ {% block main %} {% set formEditTemplate = app.request.xmlHttpRequest ? 'default/_form_modal.html.twig' : 'default/_form.html.twig' %} {% set formOptions = { - 'title': customer.name|default('create'|trans), + 'title': (customer.id is null ? 'create'|trans : 'edit'|trans({}, 'actions')), 'form': form, 'back': path('admin_customer') } %} diff --git a/templates/project/actions.html.twig b/templates/project/actions.html.twig index ede7ea09..ff4f38c1 100644 --- a/templates/project/actions.html.twig +++ b/templates/project/actions.html.twig @@ -6,7 +6,7 @@ {% set actions = actions|merge({'download': {'url': path('project_export'), 'class': 'toolbar-action'}}) %} {% if is_granted('create_project') %} - {% set actions = actions|merge({'create': path('admin_project_create')}) %} + {% set actions = actions|merge({'create': {'url': path('admin_project_create'), 'class': 'modal-ajax-form'}}) %} {% endif %} {% set actions = actions|merge({'help': {'url': 'project.html'|docu_link, 'target': '_blank'}}) %} diff --git a/templates/project/edit.html.twig b/templates/project/edit.html.twig index 3952ca36..aaed2a25 100644 --- a/templates/project/edit.html.twig +++ b/templates/project/edit.html.twig @@ -7,7 +7,7 @@ {% block main %} {% set formEditTemplate = app.request.xmlHttpRequest ? 'default/_form_modal.html.twig' : 'default/_form.html.twig' %} {% set formOptions = { - 'title': project.name|default('create-project'|trans({}, 'actions')), + 'title': (project.id is null ? 'create'|trans : 'edit'|trans({}, 'actions')), 'form': form, 'back': path('admin_project') } %} @@ -55,11 +55,6 @@ {{ form_row(meta) }} {% endfor %} {% endif %} - {% if form.create_more is defined and app.request.xmlHttpRequest %} - - {% endif %} {{ form_widget(form) }} {% endblock %} {% endembed %} diff --git a/templates/user/actions.html.twig b/templates/user/actions.html.twig index d339369d..d7c5017a 100644 --- a/templates/user/actions.html.twig +++ b/templates/user/actions.html.twig @@ -16,7 +16,7 @@ {% endif %} {% if is_granted('create_user') %} - {% set actions = actions|merge({'create': path('admin_user_create')}) %} + {% set actions = actions|merge({'create': {'url': path('admin_user_create')}}) %} {% endif %} {% if view == 'index' %} diff --git a/templates/user/edit.html.twig b/templates/user/edit.html.twig index fa1dd883..57c970c7 100644 --- a/templates/user/edit.html.twig +++ b/templates/user/edit.html.twig @@ -3,9 +3,15 @@ {% block page_title %}{{ 'admin_user.title'|trans }}{% endblock %} {% block main %} - {{ include('default/_form.html.twig', { + {% set formEditTemplate = app.request.xmlHttpRequest ? 'default/_form_modal.html.twig' : 'default/_form.html.twig' %} + {% set formOptions = { 'title': 'create'|trans, 'form': form, 'back': path('admin_user') - }) }} + } %} + {% embed formEditTemplate with formOptions %} + {% block form_body %} + {{ form_widget(form) }} + {% endblock %} + {% endembed %} {% endblock %} diff --git a/tests/Controller/ActivityControllerTest.php b/tests/Controller/ActivityControllerTest.php index 7e65a3d3..2e86a8e2 100644 --- a/tests/Controller/ActivityControllerTest.php +++ b/tests/Controller/ActivityControllerTest.php @@ -15,7 +15,6 @@ use App\Entity\Project; use App\Entity\Timesheet; use App\Entity\User; use App\Tests\DataFixtures\ActivityFixtures; -use App\Tests\DataFixtures\ProjectFixtures; use App\Tests\DataFixtures\TeamFixtures; use App\Tests\DataFixtures\TimesheetFixtures; use App\Tests\Mocks\ActivityTestMetaFieldSubscriberMock; @@ -171,8 +170,6 @@ class ActivityControllerTest extends ControllerBaseTest $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); $this->assertAccessIsGranted($client, '/admin/activity/create'); $form = $client->getCrawler()->filter('form[name=activity_edit_form]')->form(); - $this->assertTrue($form->has('activity_edit_form[create_more]')); - $this->assertFalse($form->get('activity_edit_form[create_more]')->hasValue()); $client->submit($form, [ 'activity_edit_form' => [ 'name' => 'An AcTiVitY Name', @@ -203,52 +200,17 @@ class ActivityControllerTest extends ControllerBaseTest $this->assertFalse($form->has('activity_edit_form[metaFields][1][value]')); } - public function testCreateActionWithCreateMore() - { - $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); - - $fixture = new ProjectFixtures(); - $fixture->setAmount(10); - $this->importFixture($fixture); - - $this->assertAccessIsGranted($client, '/admin/activity/create'); - $form = $client->getCrawler()->filter('form[name=activity_edit_form]')->form(); - $this->assertTrue($form->has('activity_edit_form[create_more]')); - - /** @var \Symfony\Component\DomCrawler\Field\ChoiceFormField $project */ - $project = $form->get('activity_edit_form[project]'); - $options = $project->availableOptionValues(); - $selectedProject = $options[array_rand($options)]; - - $client->submit($form, [ - 'activity_edit_form' => [ - 'name' => 'Test create more', - 'create_more' => true, - 'project' => $selectedProject, - ] - ]); - $this->assertFalse($client->getResponse()->isRedirect()); - $this->assertTrue($client->getResponse()->isSuccessful()); - $form = $client->getCrawler()->filter('form[name=activity_edit_form]')->form(); - $this->assertTrue($form->has('activity_edit_form[create_more]')); - $this->assertTrue($form->get('activity_edit_form[create_more]')->hasValue()); - $this->assertEquals(1, $form->get('activity_edit_form[create_more]')->getValue()); - $this->assertEquals($selectedProject, $form->get('activity_edit_form[project]')->getValue()); - } - public function testEditAction() { $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); $this->assertAccessIsGranted($client, '/admin/activity/1/edit'); $form = $client->getCrawler()->filter('form[name=activity_edit_form]')->form(); - $this->assertFalse($form->has('activity_edit_form[create_more]')); $this->assertEquals('Test', $form->get('activity_edit_form[name]')->getValue()); $client->submit($form, [ 'activity_edit_form' => ['name' => 'Test 2', 'customer' => 1, 'project' => '1'] ]); - $this->assertIsRedirect($client, $this->createUrl('/admin/activity/')); + $this->assertIsRedirect($client, $this->createUrl('/admin/activity/1/details')); $client->followRedirect(); - $this->assertHasDataTable($client); $this->request($client, '/admin/activity/1/edit'); $editForm = $client->getCrawler()->filter('form[name=activity_edit_form]')->form(); $this->assertEquals('Test 2', $editForm->get('activity_edit_form[name]')->getValue()); @@ -261,20 +223,15 @@ class ActivityControllerTest extends ControllerBaseTest $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); $this->assertAccessIsGranted($client, '/admin/activity/1/edit'); $form = $client->getCrawler()->filter('form[name=activity_edit_form]')->form(); - $this->assertFalse($form->has('activity_edit_form[create_more]')); $this->assertEquals('Test', $form->get('activity_edit_form[name]')->getValue()); $client->submit($form, [ 'activity_edit_form' => ['name' => 'Test 2'] ]); - $this->assertIsRedirect($client, $this->createUrl('/admin/activity/')); + $this->assertIsRedirect($client, $this->createUrl('/admin/activity/1/details')); $client->followRedirect(); - $this->assertHasDataTable($client); $this->request($client, '/admin/activity/1/edit'); $editForm = $client->getCrawler()->filter('form[name=activity_edit_form]')->form(); $this->assertEquals('Test 2', $editForm->get('activity_edit_form[name]')->getValue()); - // make sure no customer or project is pre-selected for global activities - $this->assertEquals('', $editForm->get('activity_edit_form[customer]')->getValue()); - $this->assertEquals('', $editForm->get('activity_edit_form[project]')->getValue()); } public function testTeamPermissionAction() diff --git a/tests/Controller/CustomerControllerTest.php b/tests/Controller/CustomerControllerTest.php index f4d90130..162f3649 100644 --- a/tests/Controller/CustomerControllerTest.php +++ b/tests/Controller/CustomerControllerTest.php @@ -300,7 +300,6 @@ class CustomerControllerTest extends ControllerBaseTest $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); $this->assertAccessIsGranted($client, '/admin/customer/1/edit'); $form = $client->getCrawler()->filter('form[name=customer_edit_form]')->form(); - $this->assertFalse($form->has('customer_edit_form[create_more]')); $this->assertEquals('Test', $form->get('customer_edit_form[name]')->getValue()); $client->submit($form, [ 'customer_edit_form' => [ diff --git a/tests/Controller/ProjectControllerTest.php b/tests/Controller/ProjectControllerTest.php index 585387ad..870f819b 100644 --- a/tests/Controller/ProjectControllerTest.php +++ b/tests/Controller/ProjectControllerTest.php @@ -19,7 +19,6 @@ use App\Entity\Team; use App\Entity\Timesheet; use App\Entity\User; use App\Tests\DataFixtures\ActivityFixtures; -use App\Tests\DataFixtures\CustomerFixtures; use App\Tests\DataFixtures\ProjectFixtures; use App\Tests\DataFixtures\TeamFixtures; use App\Tests\DataFixtures\TimesheetFixtures; @@ -341,8 +340,6 @@ class ProjectControllerTest extends ControllerBaseTest $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); $this->assertAccessIsGranted($client, '/admin/project/create'); $form = $client->getCrawler()->filter('form[name=project_edit_form]')->form(); - $this->assertTrue($form->has('project_edit_form[create_more]')); - $this->assertFalse($form->get('project_edit_form[create_more]')->hasValue()); $client->submit($form, [ 'project_edit_form' => [ 'name' => 'Test 2', @@ -366,45 +363,11 @@ class ProjectControllerTest extends ControllerBaseTest $this->assertFalse($form->has('project_edit_form[metaFields][1][value]')); } - public function testCreateActionWithCreateMore() - { - $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); - - $fixture = new CustomerFixtures(); - $fixture->setAmount(10); - $this->importFixture($fixture); - - $this->assertAccessIsGranted($client, '/admin/project/create'); - $form = $client->getCrawler()->filter('form[name=project_edit_form]')->form(); - $this->assertTrue($form->has('project_edit_form[create_more]')); - - /** @var \Symfony\Component\DomCrawler\Field\ChoiceFormField $customer */ - $customer = $form->get('project_edit_form[customer]'); - $options = $customer->availableOptionValues(); - $selectedCustomer = $options[array_rand($options)]; - - $client->submit($form, [ - 'project_edit_form' => [ - 'name' => 'Test create more', - 'create_more' => true, - 'customer' => $selectedCustomer - ] - ]); - $this->assertFalse($client->getResponse()->isRedirect()); - $this->assertTrue($client->getResponse()->isSuccessful()); - $form = $client->getCrawler()->filter('form[name=project_edit_form]')->form(); - $this->assertTrue($form->has('project_edit_form[create_more]')); - $this->assertTrue($form->get('project_edit_form[create_more]')->hasValue()); - $this->assertEquals(1, $form->get('project_edit_form[create_more]')->getValue()); - $this->assertEquals($selectedCustomer, $form->get('project_edit_form[customer]')->getValue()); - } - public function testEditAction() { $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); $this->assertAccessIsGranted($client, '/admin/project/1/edit'); $form = $client->getCrawler()->filter('form[name=project_edit_form]')->form(); - $this->assertFalse($form->has('project_edit_form[create_more]')); $this->assertEquals('Test', $form->get('project_edit_form[name]')->getValue()); $client->submit($form, [ 'project_edit_form' => ['name' => 'Test 2']