-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathUseDisplayDashboardTop.php
More file actions
406 lines (367 loc) · 16 KB
/
UseDisplayDashboardTop.php
File metadata and controls
406 lines (367 loc) · 16 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?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 Academic Free License version 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/AFL-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.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
declare(strict_types=1);
namespace PrestaShop\Module\Mbo\Traits\Hooks;
use PrestaShop\Module\Mbo\Exception\ExpectedServiceNotFoundException;
use PrestaShop\Module\Mbo\Helpers\ErrorHelper;
use PrestaShop\Module\Mbo\Service\View\ContextBuilder;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;
if (!defined('_PS_VERSION_')) {
exit;
}
trait UseDisplayDashboardTop
{
/**
* @var string
*/
public static string $recommendedButtonType = 'button';
/**
* @var string
*/
public static string $recommendedAfterContentType = 'after_content';
/**
* @var string[]
*/
public static array $tabsWithRecommendedButton = [
'AdminOrders',
'AdminInvoices',
'AdminSlip',
'AdminDeliverySlip',
'AdminProducts',
'AdminFeatures',
'AdminManufacturers',
'AdminCartRules',
'AdminSpecificPriceRule',
'AdminCustomers',
'AdminCustomerThreads',
'AdminStats',
'AdminCmsContent',
'AdminImages',
'AdminShipping',
'AdminStatuses', // Shop Parameters > Order Settings > Statuses
'AdminGroups', // Shop Parameters > Customer Settings > Groups
'AdminContacts', // Shop Parameters > Contact > Contact
'AdminMeta', // Shop Parameters > Traffic & SEO > SEO & URLs
'AdminSearchConf', // Shop Parameters > Search > Search
'AdminAdminPreferences', // Advanced Parameters > Administration
'AdminEmails', // Advanced Parameters > E-mail
];
/**
* @var string[]
*/
public static array $tabsWithRecommendedAfterContent = [
'AdminPayment',
'AdminCarriers',
];
/**
* Module with push content & link to addons on configuration page
*
* @var string[]
*/
protected static array $modulesWithConfigurationPush = [
'contactform',
'blockreassurance',
];
/**
* The hook is sometimes called multiple times in the same execution because it's called directly in tpl files & in
* some configurations, multiple files can call/extend those.
* Try to limit this behavior by adding this tiny boolean attribute and test it on exec.
*
* @var bool
*/
protected bool $alreadyProcessedPage = false;
protected array $controllersWithRecommendedModules = [];
protected static array $routeAfterContent = [
'admin_carriers_index',
'admin_payment_methods',
'admin_legacy_controller_route',
];
/**
* Hook displayDashboardTop.
* Includes content just below the toolbar.
*
* @param $params
* @return string
*
* @throws \Exception
*/
public function hookDisplayDashboardTop($params): string
{
// Check if this page has already been processed by the hook to avoid duplicate content
if ($this->alreadyProcessedPage) {
return '';
}
$this->controllersWithRecommendedModules = [
self::$recommendedButtonType => self::$tabsWithRecommendedButton,
self::$recommendedAfterContentType => self::$tabsWithRecommendedAfterContent,
];
$this->alreadyProcessedPage = true;
if ($this->shouldDisplayModuleManagerMessage($params)) {
return $this->renderModuleManagerMessage();
}
// Check if we are on a configuration page and if the module needs to have a push on this page
$shouldDisplayMessageInConfigPage = isset($params['route'])
&& $params['route'] === 'admin_module_configure_action'
&& isset($params['request'])
&& in_array($params['request']->get('module_name'), self::$modulesWithConfigurationPush);
return $shouldDisplayMessageInConfigPage
? $this->displayPushOnConfigurationPage($params['request']->get('module_name'))
: $this->displayRecommendedModules($this->context->controller->controller_name ?? '', $params);
}
/**
* Insert a block on the top of the configuration with a link to addons
*
* @param string $moduleName
*
* @return string
*/
protected function displayPushOnConfigurationPage(string $moduleName): string
{
$defaultLinkText = $this->trans('PrestaShop Addons Marketplace', [], 'Modules.Mbo.Global');
switch ($moduleName) {
case 'contactform':
$this->smarty->assign([
'catchPhrase' => $this->trans(
'For even more security on your website forms, consult our Security & Access modules category on the',
[],
'Modules.Mbo.Global'
),
'linkTarget' => $this->trans(
'https://addons.prestashop.com/en/429-website-security-access?utm_source=back-office&utm_medium=native-contactform&utm_campaign=back-office-EN&utm_content=security',
[],
'Modules.Mbo.Links'
),
'linkText' => $defaultLinkText,
]);
break;
case 'blockreassurance':
$this->smarty->assign([
'catchPhrase' => $this->trans(
'Discover more modules to improve your shop on',
[],
'Modules.Mbo.Global'
),
'linkTarget' => $this->trans(
'https://addons.prestashop.com/en/517-blocks-tabs-banners?utm_source=back-office&utm_medium=modules&utm_campaign=back-office-EN',
[],
'Modules.Mbo.Links'
),
'linkText' => $defaultLinkText,
]);
break;
default:
$this->smarty->assign([
'catchPhrase' => $this->trans(
'Discover more modules to improve your shop on',
[],
'Modules.Mbo.Global'
),
'linkTarget' => $this->trans(
'https://addons.prestashop.com/?utm_source=back-office&utm_medium=modules&utm_campaign=back-office-EN',
[],
'Modules.Mbo.Links'
),
'linkText' => $defaultLinkText,
]);
}
return $this->fetch('module:ps_mbo/views/templates/hook/push-configuration.tpl');
}
/**
* Compute & include data with recommended modules when needed
*
* @throws \Exception
*/
protected function displayRecommendedModules(string $controller, array $hookParams): string
{
$shouldAttachRecommendedModulesAfterContent = $this->shouldAttachRecommendedModules(self::$recommendedAfterContentType);
$shouldAttachRecommendedModulesButton = $this->shouldAttachRecommendedModules(self::$recommendedButtonType);
// Show only in content if index page
$shouldDisplayAfterContent = isset($hookParams['route']) && in_array($hookParams['route'], self::$routeAfterContent);
if ((!$shouldAttachRecommendedModulesAfterContent && !$shouldAttachRecommendedModulesButton)
|| ($shouldAttachRecommendedModulesAfterContent && !$shouldDisplayAfterContent)) {
return '';
}
try {
/** @var UrlGeneratorInterface|null $router */
$router = $this->get('prestashop.router');
if (null === $router) {
throw new ExpectedServiceNotFoundException('Some services not found in UseDisplayDashboardTop');
}
$recommendedModulesUrl = $router->generate(
'admin_mbo_recommended_modules',
[
'tabClassName' => $this->context->controller->controller_name,
'recommendation_format' => $shouldAttachRecommendedModulesButton ? 'modal' : 'card',
]
);
} catch (\Exception $exception) {
// Avoid fatal errors on ServiceNotFoundException
ErrorHelper::reportError($exception);
return '';
}
$this->context->controller->addJs($this->getPathUri() . 'views/js/recommended-modules.js?v=' . $this->version);
$this->context->controller->addCSS($this->getPathUri() . 'views/css/recommended-modules.css');
$extraParams = self::getCdcMediaUrl();
$moduleUri = __PS_BASE_URI__ . 'modules/ps_mbo/';
$extraParams['recommended_modules_js'] = $moduleUri . 'views/js/recommended-modules.js?v=' . self::VERSION;
$extraParams['recommended_modules_css'] = $moduleUri . 'views/css/recommended-modules.css?v=' . self::VERSION;
$this->smarty->assign([
'shouldAttachRecommendedModulesAfterContent' => $shouldAttachRecommendedModulesAfterContent,
'shouldAttachRecommendedModulesButton' => $shouldAttachRecommendedModulesButton,
'shouldUseLegacyTheme' => $this->isAdminLegacyContext() || $hookParams['route'] === 'admin_legacy_controller_route',
'recommendedModulesCloseTranslated' => $this->trans('Close', [], 'Admin.Actions'),
'recommendedModulesUrl' => $recommendedModulesUrl,
'recommendedModulesTitleTranslated' => $this->getRecommendedModulesButtonTitle($controller),
] + $extraParams);
return $this->fetch('module:ps_mbo/views/templates/hook/recommended-modules.tpl');
}
/**
* Indicates if the recommended modules should be attached
*
* @param string $type If we want `afterContent` or `button` related modules
*
* @return bool
*/
protected function shouldAttachRecommendedModules(string $type): bool
{
if ($type === self::$recommendedButtonType) {
$modules = $this->controllersWithRecommendedModules[self::$recommendedButtonType];
} elseif ($type === self::$recommendedAfterContentType) {
$modules = $this->controllersWithRecommendedModules[self::$recommendedAfterContentType];
} else {
return false;
}
return in_array($this->context->controller->controller_name, $modules, true);
}
/**
* Customize title button recommended modules
*/
private function getRecommendedModulesButtonTitle(string $controller): string
{
switch ($controller) {
case 'AdminEmails':
$title = $this->trans('Automate emails', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminOrders':
$title = $this->trans('Boost sales', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminCartRules':
case 'AdminSpecificPriceRule':
$title = $this->trans('Create a discount strategy', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminCmsContent':
$title = $this->trans('Customize pages', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminContacts':
case 'AdminCustomers':
case 'AdminCustomerThreads':
$title = $this->trans('Improve customer experience', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminGroups':
$title = $this->trans('Improve customer targeting', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminStats':
$title = $this->trans('Improve data strategy', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminMeta':
case 'AdminSearchConf':
$title = $this->trans('Improve SEO', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminShipping':
$title = $this->trans('Improve shipping', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminPayment':
$title = $this->trans(
'Improve the checkout experience',
[],
'Modules.Mbo.Recommendedmodulesandservices'
);
break;
case 'AdminImages':
$title = $this->trans('Improve visuals', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminDeliverySlip':
$title = $this->trans('Make shipping easier', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminCarriers':
$title = $this->trans('Make your deliveries easier', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminStatuses':
$title = $this->trans('Optimize order management', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminProducts':
$title = $this->trans('Optimize product catalog', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminFeatures':
$title = $this->trans('Optimize product creation', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminManufacturers':
$title = $this->trans('Promote brands', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminInvoices':
case 'AdminSlip':
$title = $this->trans('Simplify accounting', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
case 'AdminAdminPreferences':
$title = $this->trans('Simplify store management', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
default:
$title = $this->trans('Recommended modules', [], 'Modules.Mbo.Recommendedmodulesandservices');
break;
}
return $title;
}
private function renderModuleManagerMessage(): string
{
try {
/** @var Environment|null $twig */
$twig = $this->get('twig');
/** @var ContextBuilder|null $contextBuilder */
$contextBuilder = $this->get(ContextBuilder::class);
if (null === $contextBuilder || null === $twig) {
throw new ExpectedServiceNotFoundException('Some services not found in UseDisplayAdminAfterHeader');
}
$extraParams = self::getCdcMediaUrl();
return $twig->render(
'@Modules/ps_mbo/views/templates/hook/twig/module_manager_message.html.twig', [
'shop_context' => $contextBuilder->getViewContext(),
] + $extraParams,
);
} catch (\Exception $e) {
ErrorHelper::reportError($e);
return '';
}
}
private function shouldDisplayModuleManagerMessage($params = []): bool
{
if (!isset($params['route'])) {
return false;
}
return in_array(
$params['route'],
[
'admin_module_manage',
'admin_module_notification',
'admin_module_updates',
]
);
}
}