-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBackOfficeConfigurationForm.php
More file actions
264 lines (225 loc) · 9.45 KB
/
BackOfficeConfigurationForm.php
File metadata and controls
264 lines (225 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
declare(strict_types=1);
namespace PrestaShop\Module\PsOnePageCheckout\Form;
use Twig\Environment;
class BackOfficeConfigurationForm
{
private const FORM_SUBMIT_ACTION = 'submitPsOnePageCheckoutConfiguration';
private const SUCCESS_FLASH_COOKIE_KEY = 'psopc_bo_configuration_saved';
private \Module $module;
private string $configurationKey;
public function __construct(\Module $module, string $configurationKey)
{
$this->module = $module;
$this->configurationKey = $configurationKey;
}
public function renderBackOfficeConfiguration(): string
{
$output = '';
if (\Tools::isSubmit(self::FORM_SUBMIT_ACTION)) {
$isEnabled = (int) \Tools::getValue($this->configurationKey, 0) === 1;
$this->persistConfigurationValue((int) $isEnabled);
$this->storeSuccessFlash();
$this->redirectToConfigurationForm();
return '';
}
if ($this->consumeSuccessFlash()) {
$output .= $this->module->displayConfirmation(
$this->trans('Settings updated.', 'Admin.Notifications.Success')
);
}
return $output . $this->renderConfigurationForm();
}
private function renderConfigurationForm(): string
{
$this->registerBackOfficeAssets();
$templateVariables = $this->buildTemplateVariables();
$twig = $this->resolveTwigEnvironment();
if (!$twig instanceof Environment) {
throw new \RuntimeException('Twig environment is required to render ps_onepagecheckout back office configuration.');
}
return $twig->render(
'@Modules/ps_onepagecheckout/views/templates/admin/checkout_layout_configuration.html.twig',
$templateVariables
);
}
/**
* @return array<string, mixed>
*/
private function buildTemplateVariables(): array
{
$isEnabled = $this->getCurrentConfigurationValue() === 1;
$onePageLabel = $this->trans('One-page checkout', 'Admin.Design.Feature');
$fourPageLabel = $this->trans('Four-page checkout', 'Admin.Design.Feature');
return [
'configuration_key' => $this->configurationKey,
'configuration_form_action' => $this->getConfigurationFormAction(),
'form_submit_action' => self::FORM_SUBMIT_ACTION,
'checkout_layout_css_url' => $this->module->getPathUri() . 'views/css/checkout_layout_choice.css',
'is_single_shop_context' => $this->isSingleShopContext(),
'single_shop_context_warning' => $this->trans(
'Note that this page is available in a single shop context only. Switch context to work on it.',
'Admin.Notifications.Info'
),
'checkout_layout_title' => $this->trans('Checkout layout', 'Admin.Design.Feature'),
'checkout_layout_description' => $this->trans(
'Allow faster checkout for higher conversion and spontaneous purchases.',
'Admin.Design.Feature'
),
'save_button_label' => $this->trans('Save', 'Admin.Actions'),
'choices' => [
[
'id' => $this->configurationKey . '_one_page',
'value' => 1,
'checked' => $isEnabled,
'label' => $onePageLabel,
'badge' => $this->trans('Recommended', 'Admin.Design.Feature'),
'description' => $this->trans(
'Propose the best checkout experience to your clients with the one-page layout. All the payment steps will be displayed in one page and let you benefit from:',
'Admin.Design.Feature'
),
'features' => [
$this->trans('Reduced time spent, less clicks & no extra-step', 'Admin.Design.Feature'),
$this->trans(
'Increased conversion with a seamless and frictionless experience',
'Admin.Design.Feature'
),
$this->trans('Mobile-friendly checkout', 'Admin.Design.Feature'),
],
'illustration' => $this->module->getPathUri() . 'views/img/checkout/one-page-checkout.svg',
],
[
'id' => $this->configurationKey . '_four_page',
'value' => 0,
'checked' => !$isEnabled,
'label' => $fourPageLabel,
'badge' => '',
'description' => $this->trans(
'Propose a step-by-step checkout experience. All the usual payment steps will be displayed on different screens. Choose this method if:',
'Admin.Design.Feature'
),
'features' => [
$this->trans(
'You need flexibility for complex or specific information',
'Admin.Design.Feature'
),
$this->trans('You\'re under regulatory compliance', 'Admin.Design.Feature'),
$this->trans('You want to integrate upsell & cross-sell strategies', 'Admin.Design.Feature'),
],
'illustration' => $this->module->getPathUri() . 'views/img/checkout/four-page-checkout.svg',
],
],
];
}
private function getConfigurationFormAction(): string
{
$link = \Context::getContext()->link;
if (null === $link) {
return '';
}
if ($this->isAdminPsOnePageCheckoutContext()) {
return $link->getAdminLink('AdminPsOnePageCheckout');
}
return $link->getAdminLink('AdminModules', true, [], ['configure' => $this->module->name]);
}
private function registerBackOfficeAssets(): void
{
$context = \Context::getContext();
if (!isset($context->controller)) {
return;
}
$context->controller->addCSS(
$this->module->getPathUri() . 'views/css/checkout_layout_choice.css'
);
}
private function trans(string $message, string $domain = 'Modules.Psonepagecheckout.Admin'): string
{
$translator = \Context::getContext()->getTranslator();
return $translator->trans($message, [], $domain);
}
private function isAdminPsOnePageCheckoutContext(): bool
{
$controllerName = (string) \Tools::getValue('controller');
if ($controllerName === 'AdminPsOnePageCheckout') {
return true;
}
$context = \Context::getContext();
return isset($context->controller)
&& in_array(
get_class($context->controller),
['AdminPsOnePageCheckoutController'],
true
);
}
private function resolveTwigEnvironment(): ?Environment
{
return $this->module->getTwig();
}
private function isSingleShopContext(): bool
{
return \Shop::getContext() === \Shop::CONTEXT_SHOP;
}
protected function persistConfigurationValue(int $value): void
{
$this->updateConfigurationValue($value);
}
protected function getCurrentConfigurationValue(): int
{
return $this->readConfigurationValue();
}
protected function updateConfigurationValue(int $value): void
{
\Configuration::updateValue($this->configurationKey, $value, false);
}
protected function readConfigurationValue(): int
{
return (int) \Configuration::get($this->configurationKey);
}
protected function redirectToConfigurationForm(): void
{
\Tools::redirectAdmin($this->getConfigurationFormAction());
}
protected function storeSuccessFlash(): void
{
$context = \Context::getContext();
if (!isset($context->cookie)) {
return;
}
$context->cookie->{self::SUCCESS_FLASH_COOKIE_KEY} = '1';
$context->cookie->write();
}
protected function consumeSuccessFlash(): bool
{
$context = \Context::getContext();
if (!isset($context->cookie) || !isset($context->cookie->{self::SUCCESS_FLASH_COOKIE_KEY})) {
return false;
}
unset($context->cookie->{self::SUCCESS_FLASH_COOKIE_KEY});
$context->cookie->write();
return true;
}
}