|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the bootstrap-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2018 WEBEWEB |
|
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 WBW\Bundle\BootstrapBundle\Twig\Extension\Utility; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
15
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
16
|
|
|
use Twig_SimpleFunction; |
|
17
|
|
|
use WBW\Bundle\BootstrapBundle\Twig\Extension\Component\LabelComponentTwigExtension; |
|
18
|
|
|
use WBW\Library\Core\Utility\Argument\StringUtility; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Role label utility Twig extension. |
|
22
|
|
|
* |
|
23
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
24
|
|
|
* @package WBW\Bundle\BootstrapBundle\Twig\Extension\Utility |
|
25
|
|
|
*/ |
|
26
|
|
|
class RoleLabelUtilityTwigExtension extends AbstractUtilityTwigExtension { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Service name. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.utility.rolelabel"; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Extension. |
|
37
|
|
|
* |
|
38
|
|
|
* @var LabelComponentTwigExtension |
|
39
|
|
|
*/ |
|
40
|
|
|
private $extension; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Translator. |
|
44
|
|
|
* |
|
45
|
|
|
* @var TranslatorInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
private $translator; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param LabelComponentTwigExtension $extension The label component Twig extension. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(TranslatorInterface $translator, LabelComponentTwigExtension $extension) { |
|
55
|
|
|
parent::__construct(); |
|
56
|
|
|
$this->extension = $extension; |
|
57
|
|
|
$this->translator = $translator; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Apply the color. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $label The label. |
|
64
|
|
|
* @param string $content The content. |
|
65
|
|
|
* @param string $color The color. |
|
66
|
|
|
* @return string Returns the label with applied color. |
|
67
|
|
|
*/ |
|
68
|
|
|
private function applyColor($label, $content, $color) { |
|
69
|
|
|
|
|
70
|
|
|
$searches = ">" . $content; |
|
71
|
|
|
$replaces = " style=\"background-color:" . $color . ";\"" . $searches; |
|
72
|
|
|
|
|
73
|
|
|
return StringUtility::replace($label, [$searches], [$replaces]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Display a Bootstrap role label. |
|
78
|
|
|
* |
|
79
|
|
|
* @param UserInterface $user The user. |
|
80
|
|
|
* @param array $roleColors The role colors. |
|
81
|
|
|
* @param array $roleTrans The role translations. |
|
82
|
|
|
* @return string Returns the Bootstrap role label. |
|
83
|
|
|
*/ |
|
84
|
|
|
public function bootstrapRoleLabelFunction(UserInterface $user = null, array $roleColors = [], array $roleTrans = []) { |
|
85
|
|
|
|
|
86
|
|
|
// Check the user. |
|
87
|
|
|
if (null === $user) { |
|
88
|
|
|
return ""; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Initialize the ouptut. |
|
92
|
|
|
$output = []; |
|
93
|
|
|
|
|
94
|
|
|
// Handle each role. |
|
95
|
|
|
foreach ($user->getRoles() as $current) { |
|
96
|
|
|
|
|
97
|
|
|
// Initialize the translation. |
|
98
|
|
|
$trans = $current; |
|
99
|
|
|
if (true === array_key_exists($current, $roleTrans)) { |
|
100
|
|
|
$trans = $this->translator->trans($roleTrans[$current]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Initialize the label. |
|
104
|
|
|
$label = $this->extension->bootstrapLabelDefaultFunction(["content" => $trans]); |
|
105
|
|
|
if (true === array_key_exists($current, $roleColors)) { |
|
106
|
|
|
$label = $this->applyColor($label, $trans, $roleColors[$current]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Add the label. |
|
110
|
|
|
$output[] = $label; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// Return the output. |
|
114
|
|
|
return implode(" ", $output); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get the Twig functions. |
|
119
|
|
|
* |
|
120
|
|
|
* @return array Returns the Twig functions. |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getFunctions() { |
|
123
|
|
|
return [ |
|
124
|
|
|
new Twig_SimpleFunction("bootstrapRoleLabel", [$this, "bootstrapRoleLabelFunction"], ["is_safe" => ["html"]]), |
|
125
|
|
|
]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|