open create form for activity/project/customer in modals (#2025)

This commit is contained in:
Kevin Papst
2020-10-08 01:12:34 +02:00
committed by GitHub
parent 46bf01d93e
commit fb31319fcf
17 changed files with 60 additions and 169 deletions

View File

@@ -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)
]);

View File

@@ -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)
]);
}

View File

@@ -34,7 +34,6 @@ class ActivityApiEditForm extends ActivityEditForm
$resolver->setDefaults([
'csrf_protection' => false,
'create_more' => false,
]);
}
}

View File

@@ -34,7 +34,6 @@ class ProjectApiEditForm extends ProjectEditForm
$resolver->setDefaults([
'csrf_protection' => false,
'create_more' => false,
]);
}
}

View File

@@ -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,

View File

@@ -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'
],

View File

@@ -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'}}) %}

View File

@@ -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 %}
<div class="hidden">
{{ form_row(form.create_more) }}
</div>
{% endif %}
{{ form_widget(form) }}
{% endblock %}
{% endembed %}

View File

@@ -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'}}) %}

View File

@@ -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')
} %}

View File

@@ -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'}}) %}

View File

@@ -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 %}
<div class="hidden">
{{ form_row(form.create_more) }}
</div>
{% endif %}
{{ form_widget(form) }}
{% endblock %}
{% endembed %}

View File

@@ -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' %}

View File

@@ -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 %}

View File

@@ -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()

View File

@@ -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' => [

View File

@@ -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']