|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* Copyright (C) 2024 Rafael San José <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
8
|
|
|
* any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Alxarafe\Base\Controller\Trait; |
|
20
|
|
|
|
|
21
|
|
|
use Jenssegers\Blade\Blade; |
|
22
|
|
|
|
|
23
|
|
|
trait ViewTrait |
|
24
|
|
|
{ |
|
25
|
|
|
public static $messages = []; |
|
26
|
|
|
/** |
|
27
|
|
|
* Theme name. TODO: Has to be updated according to the configuration. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
public $theme; |
|
32
|
|
|
/** |
|
33
|
|
|
* Code lang for <html lang> tag |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
public $lang = 'en'; |
|
38
|
|
|
public $body_class; |
|
39
|
|
|
public $templatesPath; |
|
40
|
|
|
public $template; |
|
41
|
|
|
public $title; |
|
42
|
|
|
public $alerts; |
|
43
|
|
|
|
|
44
|
|
|
public static function addMessage($message) |
|
45
|
|
|
{ |
|
46
|
|
|
self::$messages[]['success'] = $message; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function addAdvice($message) |
|
50
|
|
|
{ |
|
51
|
|
|
self::$messages[]['warning'] = $message; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function addError($message) |
|
55
|
|
|
{ |
|
56
|
|
|
self::$messages[]['danger'] = $message; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function __destruct() |
|
60
|
|
|
{ |
|
61
|
|
|
if (!isset($this->template)) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (!isset($this->theme)) { |
|
66
|
|
|
$this->theme = 'alixar'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!isset($this->title)) { |
|
70
|
|
|
$this->title = 'Alixar'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->alerts = static::getMessages(); |
|
74
|
|
|
|
|
75
|
|
|
$vars = ['me' => $this]; |
|
76
|
|
|
$viewPaths = [ |
|
77
|
|
|
$this->templatesPath, |
|
78
|
|
|
BASE_PATH . '/Templates', |
|
79
|
|
|
BASE_PATH . '/Templates/theme/' . $this->theme, |
|
80
|
|
|
BASE_PATH . '/Templates/common', |
|
81
|
|
|
]; |
|
82
|
|
|
$cachePaths = realpath(BASE_PATH . '/..') . '/tmp/blade'; |
|
83
|
|
|
if (!is_dir($cachePaths) && !mkdir($cachePaths, 0777, true) && !is_dir($cachePaths)) { |
|
84
|
|
|
die('Could not create cache directory for templates: ' . $cachePaths); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
$blade = new Blade($viewPaths, $cachePaths); |
|
87
|
|
|
echo $blade->render($this->template, $vars); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public static function getMessages() |
|
91
|
|
|
{ |
|
92
|
|
|
$alerts = []; |
|
93
|
|
|
foreach (self::$messages as $message) { |
|
94
|
|
|
foreach ($message as $type => $text) { |
|
95
|
|
|
$alerts[] = [ |
|
96
|
|
|
'type' => $type, |
|
97
|
|
|
'text' => $text |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
self::$messages = []; |
|
102
|
|
|
return $alerts; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getTemplatesPath(): string |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->templatesPath; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function setTemplatesPath(string $path) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->templatesPath = $path; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.