-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAdmin.php
More file actions
executable file
·342 lines (294 loc) · 11.9 KB
/
Admin.php
File metadata and controls
executable file
·342 lines (294 loc) · 11.9 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
<?php
namespace Foolz\FoolFrame\Controller;
use Foolz\FoolFrame\Model\Config;
use Foolz\FoolFrame\Model\Notices;
use Foolz\FoolFrame\Model\Plugins;
use Foolz\FoolFrame\Model\Preferences;
use Foolz\FoolFrame\Model\Security;
use Foolz\FoolFrame\Model\Uri;
use Foolz\Plugin\Hook;
use Foolz\Theme\Loader;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Admin extends Common
{
protected $_views = null;
/**
* @var \Foolz\Theme\Theme
*/
protected $theme;
/**
* @var \Foolz\Theme\ParamManager
*/
protected $param_manager;
/**
* @var \Foolz\Theme\Builder
*/
protected $builder;
/**
* @var Uri
*/
protected $uri;
/**
* @var Mailer
*/
protected $mailer;
/**
* @var Notices
*/
protected $notices;
/**
* @var Preferences
*/
protected $preferences;
/**
* @var Config
*/
protected $config;
/**
* @var Security
*/
protected $security;
public function before()
{
$request = $this->getRequest();
$this->uri = $this->getContext()->getService('uri');
$this->mailer = $this->getContext()->getService('mailer');
$this->notices = $this->getContext()->getService('notices');
$this->preferences = $this->getContext()->getService('preferences');
$this->config = $this->getContext()->getService('config');
$this->security = $this->getContext()->getService('security');
$theme_instance = \Foolz\Theme\Loader::forge('foolframe_admin');
$theme_instance->addDir(VENDPATH.'foolz/foolframe/assets/themes-admin/');
$theme_instance->addDir(VAPPPATH.'foolz/foolframe/themes-admin/');
$theme_instance->setBaseUrl($this->uri->base().'foolframe/');
$theme_instance->setPublicDir(DOCROOT.'foolframe/');
// make it possible to override the theme so other framework components can extend with their own
$this->setupTheme($theme_instance);
$this->builder = $this->theme->createBuilder();
$this->param_manager = $this->builder->getParamManager();
$this->builder->createLayout('base');
$this->builder->getProps()->addTitle(_i('Control Panel').' - '.$this->preferences->get('foolframe.gen.website_title'));
$this->param_manager->setParams([
'context' => $this->getContext(),
'notices' => $this->notices,
'uri' => $this->uri,
'request' => $request
]);
// returns the hardcoded sidebar array (can't use functions when declaring a class variable)
$sidebar = $this->getSidebarValues();
$sidebar_dynamic = Hook::forge('Foolz\FoolFrame\Controller\Admin::before#var.sidebar')
->setObject($this)
->setParam('sidebar', [])
->execute()
->getParam('sidebar');
// merge if there were sidebar elements added dynamically
if (!empty($sidebar_dynamic)) {
$sidebar = $this->mergeSidebars($sidebar, $sidebar_dynamic);
}
$this->builder->createPartial('navbar', 'navbar');
$this->builder->createPartial('sidebar', 'sidebar')
->getParamManager()
->setParams(['sidebar' => $this->getSidebar($request, $sidebar)]);
}
public function redirect($url, $status = 302)
{
return new RedirectResponse($this->uri->create($url, $status));
}
public function redirectToLogin()
{
return $this->redirect('admin/account/login');
}
public function redirectToAdmin()
{
return $this->redirect('admin/account/profile');
}
/**
* Selects the theme. Can be overridden so other controllers can use their own admin components
*
* @param Loader $theme_instance
*/
public function setupTheme(Loader $theme_instance)
{
$this->theme = $theme_instance->get('foolz/foolframe-theme-admin');
}
public function action_index()
{
return $this->redirectToAdmin();
}
/**
* Non-dynamic sidebar array.
* Permissions are set inside
*
* @return array sidebar array
*/
private function getSidebarValues()
{
$sidebar = [];
// load sidebars from modules and leave FoolFrame sidebar on bottom
foreach($this->config->get('foolz/foolframe', 'config', 'modules.installed') as $module) {
$module_sidebar = $this->config->get($module['namespace'], 'sidebar');
if(is_array($module_sidebar)) {
$sidebar = array_merge($module_sidebar['sidebar'], $sidebar);
}
}
return $sidebar;
}
/**
* Merges two sidebars, where $array2 overwrites values of
* $array1.
*
* @param array $array1 sidebar array to be merged into
* @param array $array2 sidebar array with elements to merge
* @return array resulting sidebar
*/
public function mergeSidebars($array1, $array2)
{
// there's a numbered index on the outside!
foreach ($array2 as $key_top => $item_top) {
foreach($item_top as $key => $item) {
// are we inserting in an already existing method?
if (isset($array1[$key])) {
// overriding the name
if (isset($item['name'])) {
$array1[$key]['name'] = $item['name'];
}
// overriding the permission level
if (isset($item['level'])) {
$array1[$key]['level'] = $item['level'];
}
// overriding the default url to reach
if (isset($item['default'])) {
$array1[$key]['default'] = $item['default'];
}
// overriding the default url to reach
if (isset($item['icon'])) {
$array1[$key]['icon'] = $item['icon'];
}
// adding or overriding the inner elements
if (isset($item['content'])) {
if (isset($array1[$key]['content'])) {
$array1[$key]['content'] = $this->mergeSidebars($array1[$key]['content'], $item);
} else {
$array1[$key]['content'] = $this->mergeSidebars([], $item);
}
}
} else {
// the element doesn't exist at all yet
// let's trust the plugin creator in understanding the structure
// extra control: allow him to put the plugin after or before any function
if (isset($item['position']) && is_array($item['position'])) {
$before = $item['position']['beforeafter'] == 'before' ? true : false;
$element = $item['position']['element'];
$array_temp = $array1;
$array1 = [];
foreach ($array_temp as $subkey => $temp) {
if ($subkey == $element) {
if ($before) {
$array1[$key] = $item;
$array1[$subkey] = $temp;
} else {
$array1[$subkey] = $temp;
$array1[$key] = $item;
}
unset($array_temp[$subkey]);
// flush the rest
foreach ($array_temp as $k => $t) {
$array1[$k] = $t;
}
break;
} else {
$array1[$subkey] = $temp;
unset($array_temp[$subkey]);
}
}
} else {
$array1[$key] = $item;
}
}
}
}
return $array1;
}
/**
* Returns the sidebar array
*/
public function getSidebar(Request $request, $array)
{
$segments = explode('/', $request->getPathInfo());
// not logged in users don't need the sidebar
if (!$this->getAuth()->hasAccess('maccess.user')) {
return [];
}
$result = [];
foreach ($array as $key => $item) {
if ($this->getAuth()->hasAccess('maccess.' . $item['level']) && !empty($item)) {
$subresult = $item;
// segment 2 contains what's currently active so we can set it lighted up
if (isset($segments[2]) && $segments[2] == $key) {
$subresult['active'] = true;
} else {
$subresult['active'] = false;
}
// we'll cherry-pick the content next
unset($subresult['content']);
// recognize plain URLs
if ((substr($item['default'], 0, 7) == 'http://') ||
(substr($item['default'], 0, 8) == 'https://'))
{
// nothing to do here, just copy the URL
$subresult['href'] = $item['default'];
} else {
// else these are internal URIs
// what if it uses more segments or is even an array?
if (!is_array($item['default'])) {
$default_uri = explode('/', $item['default']);
} else {
$default_uri = $item['default'];
}
array_unshift($default_uri, 'admin', $key);
$subresult['href'] = $this->uri->create(implode('/', $default_uri));
}
$subresult['content'] = [];
// cherry-picking subfunctions
foreach ($item['content'] as $subkey => $subitem) {
$subsubresult = $subitem;
if ($this->getAuth()->hasAccess('maccess.' . $subitem['level'])) {
if ($subresult['active'] && (isset($segments[2]) && $segments[3] == $subkey ||
(
isset($subitem['alt_highlight']) &&
in_array($segments[3], $subitem['alt_highlight'])
)
))
{
$subsubresult['active'] = true;
} else {
$subsubresult['active'] = false;
}
// recognize plain URLs
if ((substr($subkey, 0, 7) == 'http://') ||
(substr($subkey, 0, 8) == 'https://'))
{
// nothing to do here, just copy the URL
$subsubresult['href'] = $subkey;
} else {
// else these are internal URIs
// what if it uses more segments or is even an array?
if (!is_array($subkey)) {
$default_uri = explode('/', $subkey);
} else {
$default_uri = $subkey;
}
array_unshift($default_uri, 'admin', $key);
$subsubresult['href'] = $this->uri->create(implode('/', $default_uri));
}
$subresult['content'][] = $subsubresult;
}
}
$result[] = $subresult;
}
}
return $result;
}
}