|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Zikula package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Zikula\Bundle\CoreBundle\Twig\Runtime; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
17
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
18
|
|
|
use Translation\Extractor\Annotation\Ignore; |
|
19
|
|
|
use Twig\Extension\RuntimeExtensionInterface; |
|
20
|
|
|
|
|
21
|
|
|
class SessionRuntime implements RuntimeExtensionInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var SessionInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $session; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var TranslatorInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $translator; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct( |
|
34
|
|
|
SessionInterface $session, |
|
35
|
|
|
TranslatorInterface $translator |
|
36
|
|
|
) { |
|
37
|
|
|
$this->session = $session; |
|
38
|
|
|
$this->translator = $translator; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Display flash messages in twig template. Defaults to Bootstrap alert classes. |
|
43
|
|
|
* |
|
44
|
|
|
* <pre> |
|
45
|
|
|
* {{ showflashes() }} |
|
46
|
|
|
* {{ showflashes({'class': 'custom-class', 'tag': 'span'}) }} |
|
47
|
|
|
* </pre> |
|
48
|
|
|
*/ |
|
49
|
|
|
public function showFlashes(array $params = []): string |
|
50
|
|
|
{ |
|
51
|
|
|
$result = ''; |
|
52
|
|
|
$totalMessages = []; |
|
53
|
|
|
$messageTypeMap = [ |
|
54
|
|
|
'error' => 'danger', |
|
55
|
|
|
'warning' => 'warning', |
|
56
|
|
|
'status' => 'success', |
|
57
|
|
|
'danger' => 'danger', |
|
58
|
|
|
'success' => 'success', |
|
59
|
|
|
'info' => 'info' |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
foreach ($messageTypeMap as $messageType => $bootstrapClass) { |
|
63
|
|
|
$messages = $this->session->getFlashBag()->get($messageType); |
|
64
|
|
|
if (1 > count($messages)) { |
|
65
|
|
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$translatedMessages = []; |
|
69
|
|
|
foreach ($messages as $message) { |
|
70
|
|
|
$translatedMessages[] = $this->translator->trans(/** @Ignore */$message); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// set class for the messages |
|
74
|
|
|
$class = !empty($params['class']) ? $params['class'] : "alert alert-${bootstrapClass}"; |
|
75
|
|
|
$totalMessages += $messages; |
|
76
|
|
|
// build output of the messages |
|
77
|
|
|
if (empty($params['tag']) || ('span' !== $params['tag'])) { |
|
78
|
|
|
$params['tag'] = 'div'; |
|
79
|
|
|
} |
|
80
|
|
|
$result .= '<' . $params['tag'] . ' class="' . $class . '"'; |
|
81
|
|
|
if (!empty($params['style'])) { |
|
82
|
|
|
$result .= ' style="' . $params['style'] . '"'; |
|
83
|
|
|
} |
|
84
|
|
|
$result .= '>'; |
|
85
|
|
|
$result .= implode('<hr />', $translatedMessages); |
|
86
|
|
|
$result .= '</' . $params['tag'] . '>'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (empty($totalMessages)) { |
|
90
|
|
|
return ''; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|