1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CMS Kernel library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LIN3S\CMSKernel\Infrastructure\LIN3SAdminBundle\Action\Type; |
13
|
|
|
|
14
|
|
|
use LIN3S\AdminBundle\Action\ActionType; |
15
|
|
|
use LIN3S\AdminBundle\Action\Type\EntityId; |
16
|
|
|
use LIN3S\AdminBundle\Action\Type\OptionResolver; |
17
|
|
|
use LIN3S\AdminBundle\Action\Type\Redirect; |
18
|
|
|
use LIN3S\AdminBundle\Configuration\EntityConfiguration; |
19
|
|
|
use LIN3S\AdminBundle\Form\FormHandler; |
20
|
|
|
use LIN3S\SharedKernel\Application\CommandBus; |
21
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
24
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
25
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Beñat Espiña <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class NewTranslatableActionType implements ActionType |
31
|
|
|
{ |
32
|
|
|
use EntityId; |
33
|
|
|
use OptionResolver; |
34
|
|
|
use Redirect; |
35
|
|
|
|
36
|
|
|
private $flashBag; |
37
|
|
|
private $twig; |
38
|
|
|
private $formHandler; |
39
|
|
|
private $commandBus; |
40
|
|
|
private $urlGenerator; |
41
|
|
|
|
42
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
43
|
|
|
FormHandler $formHandler, |
44
|
|
|
CommandBus $commandBus, |
45
|
|
|
\Twig_Environment $twig, |
46
|
|
|
FlashBagInterface $flashBag, |
47
|
|
|
UrlGeneratorInterface $urlGenerator |
48
|
|
|
) { |
49
|
|
|
$this->twig = $twig; |
50
|
|
|
$this->flashBag = $flashBag; |
51
|
|
|
$this->formHandler = $formHandler; |
52
|
|
|
$this->commandBus = $commandBus; |
53
|
|
|
$this->urlGenerator = $urlGenerator; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function execute($entity, EntityConfiguration $config, Request $request, $options = null) |
57
|
|
|
{ |
58
|
|
|
$this->checkRequired($options, 'form'); |
59
|
|
|
|
60
|
|
|
$locale = $request->query->get('locale'); |
61
|
|
|
|
62
|
|
|
// TODO: Check if locale is defined in the bundle configuration |
63
|
|
|
// if (whatever) { |
64
|
|
|
// throw new NotFoundHttpException( |
65
|
|
|
// sprintf('%s locale is not supported from the admin', $locale) |
66
|
|
|
// ); |
67
|
|
|
// } |
68
|
|
|
|
69
|
|
|
$form = $this->formHandler->createForm($options['form'], null, ['locale' => $locale]); |
70
|
|
|
if ($request->isMethod('POST') || $request->isMethod('PUT') || $request->isMethod('PATCH')) { |
71
|
|
|
$form->handleRequest($request); |
72
|
|
|
if ($form->isValid() && $form->isSubmitted()) { |
73
|
|
|
$this->commandBus->handle( |
74
|
|
|
$form->getData() |
75
|
|
|
); |
76
|
|
|
$this->flashBag->add( |
77
|
|
|
'lin3s_admin_success', |
78
|
|
|
sprintf( |
79
|
|
|
'The %s translation is successfully saved', |
80
|
|
|
$config->name() |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
return $this->redirect($this->urlGenerator, $options, $config->name(), $form->getData()); |
85
|
|
|
} else { |
86
|
|
|
$this->flashBag->add( |
87
|
|
|
'lin3s_admin_error', |
88
|
|
|
sprintf( |
89
|
|
|
'Errors while saving %s translation. Please check all fields and try again', |
90
|
|
|
$config->name() |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return new Response( |
97
|
|
|
$this->twig->render('@LIN3SAdmin/Admin/new.html.twig', [ |
98
|
|
|
'entity' => $entity, |
99
|
|
|
'entityConfig' => $config, |
100
|
|
|
'locale' => $locale, |
101
|
|
|
'form' => $form->createView(), |
102
|
|
|
]) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.