|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Framework |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
|
7
|
|
|
* PHP Web framework |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine Framework |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
15
|
|
|
* in the Software without restriction, including without limitation the rights |
|
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
18
|
|
|
* furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
21
|
|
|
* copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
29
|
|
|
* SOFTWARE. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
declare(strict_types=1); |
|
33
|
|
|
|
|
34
|
|
|
namespace Platine\Framework\Helper; |
|
35
|
|
|
|
|
36
|
|
|
use Platine\Config\Config; |
|
37
|
|
|
use Platine\Framework\Auth\AuthorizationInterface; |
|
38
|
|
|
use Platine\Framework\Security\Csrf\CsrfManager; |
|
39
|
|
|
use Platine\Lang\Lang; |
|
40
|
|
|
use Platine\Route\RouteCollectionInterface; |
|
41
|
|
|
use Platine\Route\Router; |
|
42
|
|
|
use Platine\Stdlib\Helper\Arr; |
|
43
|
|
|
use Platine\Stdlib\Helper\Str; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @class Sidebar |
|
47
|
|
|
* @package Platine\Framework\Helper |
|
48
|
|
|
* @template T |
|
49
|
|
|
*/ |
|
50
|
|
|
class Sidebar |
|
51
|
|
|
{ |
|
52
|
|
|
/** |
|
53
|
|
|
* The sidebar data |
|
54
|
|
|
* @var array<string, array<int, array<string, mixed>>> |
|
55
|
|
|
*/ |
|
56
|
|
|
protected array $data = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Create new instance |
|
60
|
|
|
* @param Router $router |
|
61
|
|
|
* @param Lang $lang |
|
62
|
|
|
* @param CsrfManager $csrfManager |
|
63
|
|
|
* @param Config<T> $config |
|
64
|
|
|
* @param AuthorizationInterface $authorization |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct( |
|
67
|
|
|
protected Router $router, |
|
68
|
|
|
protected Lang $lang, |
|
69
|
|
|
protected CsrfManager $csrfManager, |
|
70
|
|
|
protected Config $config, |
|
71
|
|
|
protected AuthorizationInterface $authorization |
|
72
|
|
|
) { |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Add new sidebar |
|
77
|
|
|
* @param string $group |
|
78
|
|
|
* @param string $title |
|
79
|
|
|
* @param string $name |
|
80
|
|
|
* @param array<string, mixed> $params |
|
81
|
|
|
* @param array<string, mixed> $extras |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
|
|
public function add( |
|
85
|
|
|
string $group, |
|
86
|
|
|
string $title, |
|
87
|
|
|
string $name, |
|
88
|
|
|
array $params = [], |
|
89
|
|
|
array $extras = [] |
|
90
|
|
|
): self { |
|
91
|
|
|
if (empty($group)) { |
|
92
|
|
|
$group = 'Actions'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (!isset($this->data[$group])) { |
|
96
|
|
|
$this->data[$group] = []; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->data[$group][] = [ |
|
100
|
|
|
'title' => $title, |
|
101
|
|
|
'name' => $name, |
|
102
|
|
|
'params' => $params, |
|
103
|
|
|
'extras' => $extras, |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
/** |
|
109
|
|
|
* Render the sidebar |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function render(): string |
|
113
|
|
|
{ |
|
114
|
|
|
$str = ''; |
|
115
|
|
|
|
|
116
|
|
|
/** @var RouteCollectionInterface $routes */ |
|
117
|
|
|
$routes = $this->router->routes(); |
|
118
|
|
|
$actionCount = 0; |
|
119
|
|
|
$color = $this->config->get('platform.sidebar_color_scheme', 'primary'); |
|
120
|
|
|
|
|
121
|
|
|
foreach ($this->data as $group => $sidebar) { |
|
122
|
|
|
$str .= sprintf( |
|
123
|
|
|
'<div class="list-group page-sidebar">' |
|
124
|
|
|
. '<a href="#" class="sidebar-action list-group-item list-group-item-%s"><b>%s</b></a>', |
|
125
|
|
|
$color, |
|
126
|
|
|
$group |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
foreach ($sidebar as $data) { |
|
130
|
|
|
$name = $data['name']; |
|
131
|
|
|
$route = $routes->get($name); |
|
132
|
|
|
$permission = $route->getAttribute('permission'); |
|
133
|
|
|
if ($permission !== null && $this->authorization->isGranted($permission) === false) { |
|
134
|
|
|
continue; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$actionCount++; |
|
138
|
|
|
|
|
139
|
|
|
$attributes = ''; |
|
140
|
|
|
$query = ''; |
|
141
|
|
|
$title = Str::ucfirst($data['title']); |
|
142
|
|
|
$csrf = (bool) $route->getAttribute('csrf'); |
|
143
|
|
|
if ($csrf) { |
|
144
|
|
|
if (!isset($data['extras']['query'])) { |
|
145
|
|
|
$data['extras']['query'] = []; |
|
146
|
|
|
} |
|
147
|
|
|
$data['extras']['query'] = array_merge( |
|
148
|
|
|
$data['extras']['query'], |
|
149
|
|
|
$this->csrfManager->getTokenQuery() |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if (count($data['extras']) > 0) { |
|
154
|
|
|
$extras = $data['extras']; |
|
155
|
|
|
if (isset($extras['confirm'])) { |
|
156
|
|
|
$confirmMessage = $extras['confirm_message'] ?? |
|
157
|
|
|
$this->lang->tr('Do you want to proceed with this operation? [%s] ?', $title); |
|
158
|
|
|
unset($extras['confirm'], $extras['confirm_message']); |
|
159
|
|
|
$extras['data-text-confirm'] = $confirmMessage; |
|
160
|
|
|
} |
|
161
|
|
|
if (isset($extras['query'])) { |
|
162
|
|
|
$query = Arr::query($extras['query']); |
|
163
|
|
|
unset($extras['query']); |
|
164
|
|
|
} |
|
165
|
|
|
$attributes = Str::toAttribute($extras); |
|
166
|
|
|
} |
|
167
|
|
|
$url = sprintf( |
|
168
|
|
|
'%s%s', |
|
169
|
|
|
$this->router->getUri( |
|
170
|
|
|
$data['name'], |
|
171
|
|
|
$data['params'] |
|
172
|
|
|
), |
|
173
|
|
|
(!empty($query) ? '?' . $query : '') |
|
174
|
|
|
); |
|
175
|
|
|
|
|
176
|
|
|
$str .= sprintf( |
|
177
|
|
|
'<a %s class="list-group-item list-group-item-action" href="%s">%s</a>', |
|
178
|
|
|
$attributes, |
|
179
|
|
|
$url, |
|
180
|
|
|
$title |
|
181
|
|
|
); |
|
182
|
|
|
} |
|
183
|
|
|
$str .= '</div>'; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
if ($actionCount === 0) { |
|
187
|
|
|
return ''; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $str; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|