-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayout.php
executable file
·529 lines (502 loc) · 16.8 KB
/
Layout.php
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<?php
/*
* This file is part of Numbers Framework.
*
* (c) Volodymyr Volynets <volodymyr.volynets@gmail.com>
*
* This source file is subject to the Apache 2.0 license that is bundled
* with this source code in the file LICENSE.
*/
use Helper\Ob;
use Object\ACL\Resources;
#[AllowDynamicProperties]
class Layout extends View
{
/**
* Title override
*
* @var string
*/
public static $title_override;
/**
* Icon override
*
* @var string
*/
public static $icon_override;
/**
* Version to be used when rendering JS/CSS links
*
* @var int
*/
public static $version;
/**
* Onload JavaScript
*
* @var string
*/
public static $onload = '';
public static $onload_first = '';
/**
* JavaScript data would be here
*
* @var array
*/
private static $js_data = [];
/**
* HTML to be added last to the page
*
* @var string
*/
public static $onhtml = '';
/**
* Get application version
*
* @return int
*/
public static function getVersion()
{
if (empty(self::$version)) {
$filename = Application::get(['application', 'path_full']) . (Application::isDeployed() ? '../../../deployed' : '../../deployed');
self::$version = filemtime($filename);
}
return self::$version;
}
/**
* Add css file to layout
*
* @param string $css
* @param int $sort
*/
public static function addCss(string $css, int $sort = 0)
{
Application::set(array('layout', 'css', $css), $sort);
}
/**
* Render css files
*
* @param array $options
* boolean return_list
* @return string
*/
public static function renderCss($options = [])
{
$result = '';
$list = [];
$css = Application::get(array('layout', 'css'));
if (!empty($css)) {
asort($css);
foreach ($css as $k => $v) {
$script = $k . (strpos($k, '?') !== false ? '&' : '?') . self::getVersion();
$list[] = $script;
$result .= '<link href="' . $script . '" rel="stylesheet" type="text/css" />';
}
}
// list is needed for ajax form reloads
if (!empty($options['return_list'])) {
return $list;
}
return $result;
}
/**
* Add JavaScript file to the layout
*
* @param string $js
* @param int $sort
*/
public static function addJs(string $js, int $sort = 0, array $options = [])
{
$js = str_replace('\\', '/', $js);
Application::set(['layout', 'js', $js], $sort);
if (!empty($options)) {
Application::set(['layout', 'js_options', $js], $options);
}
}
/**
* Render javascript files
*
* @param array $options
* boolean return_list
* @return string
*/
public static function renderJs($options = [])
{
$result = '';
$list = [];
$js = Application::get(['layout', 'js']);
if (!empty($js)) {
asort($js);
foreach ($js as $k => $v) {
$script = $k . (strpos($k, '?') !== false ? '&' : '?') . self::getVersion();
$list[] = $script;
$js_options = Application::get(['layout', 'js_options', $k]) ?? [];
if (empty($js_options)) {
$js_options['crossorigin'] = 'anonymous';
}
$js_options['type'] = 'text/javascript';
$js_options['src'] = $script;
$result .= HTML::script($js_options);
}
}
// list is needed for ajax form reloads
if (!empty($options['return_list'])) {
return $list;
}
return $result;
}
/**
* Onload js
*
* @param string $js
* @param boolean $first
*/
public static function onLoad(string $js, bool $first = false)
{
if (!$first) {
self::$onload .= $js;
} else {
self::$onload_first .= $js;
}
}
/**
* OnHTML
*
* @param string $html
*/
public static function onHtml($html)
{
self::$onhtml .= $html;
}
/**
* Render onload js
*
* @param boolean $first
* @return string
*/
public static function renderOnLoad()
{
if (!empty(self::$onload) || !empty(self::$onload_first)) {
return HTML::script(['value' => '$(document).ready(function(){ ' . self::$onload_first . self::$onload . ' });']);
}
}
/**
* Add array to JavaScript data
*
* @param array $data
*/
public static function jsData($data)
{
self::$js_data = array_merge_hard(self::$js_data, $data);
}
/**
* Render JavaScript data
*/
public static function renderJsData()
{
return HTML::script(['value' => '$(document).ready(function(){ var numbers_js_data = ' . json_encode(self::$js_data) . '; $.extend(true, Numbers, numbers_js_data); numbers_js_data = null; });']);
}
/**
* Render title
*
* @return string
*/
public static function renderTitle()
{
$loc = 'NF.System.' . String2::createStatic(Application::$controller->title)->englishOnly(true)->toString();
$title = self::$title_override ?? loc($loc, Application::$controller->title) ?? Application::$controller->title;
if (!empty($title)) {
$icon = self::$icon_override ?? Application::$controller->icon ?? null;
return (!empty($icon) ? (HTML::icon(['type' => $icon]) . ' ') : '') . $title;
}
}
/**
* Render document title
*
* @return string
*/
public static function renderDocumentTitle()
{
$title = trim(strip_tags(self::renderTitle() ?? ''));
return '<title>' . $title . '</title>';
}
/**
* Add messages
*
* @param string|array $msg
* @param string $type
* type one of:
* danger
* warning
* success
* info
* other
* @param boolean $postponed
*/
public static function addMessage($msg, string $type = 'danger', bool $postponed = false)
{
if (!$postponed) {
if (is_array($msg)) {
foreach ($msg as $k => $v) {
Application::set(['messages', $type], $v, ['append' => true]);
}
} else {
Application::set(['messages', $type], $msg, ['append' => true]);
}
} else { // postponed messages go into session
if (is_array($msg)) {
foreach ($msg as $k => $v) {
Session::set(['numbers', 'messages', $type], $v, ['append' => true]);
}
} else {
Session::set(['numbers', 'messages', $type], $msg, ['append' => true]);
}
}
}
/**
* Render messages
*
* @return string
*/
public static function renderMessages(): string
{
$result = '';
// we need to see if we have postponed messages and render them first
$postponed = Session::get(['numbers', 'messages']);
if (!empty($postponed)) {
Session::set(['numbers', 'messages'], []);
foreach ($postponed as $k => $v) {
$result .= HTML::message(['options' => $v, 'type' => $k]);
}
}
// regular messages
$messages = Application::get(array('messages'));
if (!empty($messages)) {
foreach ($messages as $k => $v) {
$result .= HTML::message(['options' => $v, 'type' => $k]);
}
}
return $result;
}
/**
* Add action
*
* @param string $code
* @param array $action
*/
public static function addAction(string $code, array $action)
{
$action['order'] = $action['order'] ?? 0;
Application::set(array('layout', 'actions', $code), $action);
}
/**
* Render actions
*
* @return string
*/
public static function renderActions(): string
{
$result = '';
$data = Application::get(array('layout', 'actions'));
if (!empty($data)) {
// sorting first
array_key_sort($data, ['order' => SORT_ASC], ['order' => SORT_NUMERIC]);
// looping through data and building html
$temp = [];
foreach ($data as $k => $v) {
if (empty($v)) {
continue;
}
$icon = !empty($v['icon']) ? (HTML::icon(['type' => $v['icon']]) . ' ') : '';
$onclick = !empty($v['onclick']) ? $v['onclick'] : '';
$value = !empty($v['value']) ? i18n(null, $v['value']) : '';
$href = $v['href'] ?? 'javascript:void(0);';
$temp[] = HTML::a(['value' => $icon . $value, 'href' => $href, 'onclick' => $onclick, 'title' => $v['title'] ?? '']);
}
$result = implode(' ', $temp);
}
return $result;
}
/**
* Render bread crumbs
*
* @return string
*/
public static function renderBreadcrumbs(): string
{
if (!empty(Application::$controller->breadcrumbs)) {
$temp = array_slice(Application::$controller->breadcrumbs, 1, 2);
$keys = [];
foreach ($temp as $v) {
$keys[] = $v;
$keys[] = 'options';
}
$data = Resources::getStatic('menu', 'primary');
// submenu is available only when we have breadcrumbs and menu is there
$submenu = '';
if (!empty($keys) && !empty($data[200])) {
$data = array_key_get($data[200], $keys);
$submenu = [];
if (is_array($data)) {
array_key_sort($data, ['name' => SORT_ASC], ['name' => SORT_NATURAL]);
foreach ($data as $k => $v) {
$submenu[] = HTML::a([
'href' => Request::fixUrl($v['url'], $v['template']),
'value' => HTML::icon(['type' => $v['icon']]) . ' ' . $v['name'],
]);
if (!empty($v['options'])) {
array_key_sort($v['options'], ['name' => SORT_ASC], ['name' => SORT_NATURAL]);
foreach ($v['options'] as $k2 => $v2) {
$submenu[] = HTML::a([
'href' => Request::fixUrl($v2['url'], $v2['template']),
'value' => ' ' . HTML::icon(['type' => $v2['icon']]) . ' ' . $v2['name']
]);
if (!empty($v2['options'])) {
array_key_sort($v2['options'], ['name' => SORT_ASC], ['name' => SORT_NATURAL]);
foreach ($v2['options'] as $k3 => $v3) {
$submenu[] = HTML::a([
'href' => Request::fixUrl($v3['url'], $v3['template']),
'value' => ' ' . HTML::icon(['type' => $v3['icon']]) . ' ' . $v3['name']
]);
}
}
}
}
}
$submenu = HTML::popover([
'id' => 'breadcrumbs_submenu',
'value' => HTML::icon(['type' => 'fas fa-sticky-note']),
'content' => implode(HTML::br(), $submenu),
'style' => 'overflow-y: scroll;'
]);
}
}
if (!empty($submenu)) {
Application::$controller->breadcrumbs[] = $submenu;
}
return HTML::breadcrumbs(Application::$controller->breadcrumbs);
} else {
return '';
}
}
/**
* Render as content type, non HTML output should go though this function
*
* @param mixed $data
* @param string $content_type
* @param array $options
* output_file_name
*/
public static function renderAs($data, string $content_type, array $options = [])
{
// clena up output buffer
Ob::cleanAll();
Application::set('flag.global.__content_type', $content_type);
Application::set('flag.global.__skip_layout', 1);
Application::set('flag.global.__already_rendered', 1);
header("Content-type: " . $content_type);
if (!empty($options['output_file_name'])) {
header('Content-Disposition: attachment; filename=' . $options['output_file_name']);
}
$options['extension'] = ($options['extension'] ?? '');
Log::add([
'type' => 'Layout',
'only_chanel' => 'default',
'message' => 'Content type generated!',
'other' => 'Content type: ' . $content_type . "\n" . 'Status: ' . ($options['status'] ?? 'Unknown'),
]);
// response code
if (isset($options['status'])) {
http_response_code($options['status']);
}
// main switch
switch ($content_type . $options['extension']) {
case 'application/json':
header('Connection: close');
echo json_encode($data);
break;
case 'application/xml':
if (is_array($data)) {
echo array2xml($data);
} else {
echo $data;
}
break;
case 'text/html':
Ob::start();
require(Application::get(['application', 'path_full']) . 'Layout/blank.html');
echo str_replace([
'<!-- [numbers: document title] -->',
'<!-- [numbers: document body] -->'
], [
Layout::renderDocumentTitle(),
$data
], Ob::clean());
break;
case 'text/htmlemail':
Ob::start();
require(Application::get(['application', 'path_full']) . 'Layout/' . Application::get('application.layout.email') . '.html');
echo str_replace([
'<!-- [numbers: document title] -->',
'<!-- [numbers: document body] -->'
], [
'<title>' . ($options['title'] ?? '') . '</title>',
$data
], Ob::clean());
break;
case 'text/htmlplain':
default:
echo $data;
}
Log::deliver();
exit;
}
/**
* Include all media files for controller
*
* @param string $path
* @param string $controller
* @param string $view
* @param string $class
*/
public static function includeMedia($path, $controller, $view, $class)
{
// generating a list of extensions
$valid_extensions = ['js', 'css'];
if (Application::get('dep.submodule.numbers.frontend.media.scss')) {
$valid_extensions[] = 'scss';
}
// we need to fix path for submodules
$path_fixed = str_replace('/', '_', $path);
$path_js = str_replace('_' . $controller, '', $class) . '_';
if (substr($path, 0, 8) == 'numbers/') {
$path = '../libraries/vendor/' . $path;
}
//$path = Application::get(['application', 'path_full']) . $path;
// build an iterator
$iterator = new FilesystemIterator($path);
$filter = new RegexIterator($iterator, '/' . $controller . '(.' . $view . ')?.(' . implode('|', $valid_extensions) . ')$/');
$file_list = [];
// iterating
foreach ($filter as $v) {
$temp = $v->getFilename();
$extension = pathinfo($temp, PATHINFO_EXTENSION);
// we need to sort in a way that view files are included second
if ($controller . '.' . $extension == $temp) {
$sort = 1000;
} else {
$sort = 2000;
}
$new = '/numbers/media_generated/application_' . $path_js . $temp;
if ($extension == 'js') {
self::add_js($new, $sort);
} elseif ($extension == 'css') {
self::add_css($new, $sort);
} elseif ($extension == 'scss') {
$new .= '.css';
self::add_css($new, $sort);
}
// adding media files to application for further reporting
Application::set(['application', 'loaded_classes', $class, 'media'], ['file' => $temp, 'full' => $new], ['append' => true]);
}
}
}