1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jaxon\Dialogs\Dialog; |
4
|
|
|
|
5
|
|
|
use Jaxon\App\Config\ConfigManager; |
6
|
|
|
use Jaxon\App\Dialog\Library\LibraryInterface; |
7
|
|
|
use Jaxon\Utils\Template\TemplateEngine; |
8
|
|
|
|
9
|
|
|
use function is_bool; |
10
|
|
|
use function is_numeric; |
11
|
|
|
use function is_string; |
12
|
|
|
use function json_encode; |
13
|
|
|
use function rtrim; |
14
|
|
|
use function str_repeat; |
15
|
|
|
use function trim; |
16
|
|
|
|
17
|
|
|
class LibraryHelper |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The name of the library |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $sName = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The URI where to get the library files from |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $sUri = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The constructor |
35
|
|
|
* |
36
|
|
|
* @param LibraryInterface $xDialogLibrary |
37
|
|
|
* @param ConfigManager $xConfigManager |
38
|
|
|
* @param TemplateEngine $xTemplateEngine |
39
|
|
|
*/ |
40
|
|
|
public function __construct(LibraryInterface $xDialogLibrary, |
41
|
|
|
private ConfigManager $xConfigManager, private TemplateEngine $xTemplateEngine) |
42
|
|
|
{ |
43
|
|
|
// Set the library name |
44
|
|
|
$this->sName = $xDialogLibrary->getName(); |
45
|
|
|
// Set the default URI. |
46
|
|
|
$sDefaultUri = $xConfigManager->getAppOption('dialogs.lib.uri', $xDialogLibrary->getUri()); |
47
|
|
|
// Set the library URI. |
48
|
|
|
$this->sUri = rtrim($this->getOption('uri', $sDefaultUri), '/'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the value of a config option |
53
|
|
|
* |
54
|
|
|
* @param string $sOptionName The option name |
55
|
|
|
* @param mixed $xDefault The default value, to be returned if the option is not defined |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public function getOption(string $sOptionName, $xDefault = null) |
60
|
|
|
{ |
61
|
|
|
$sOptionName = 'dialogs.' . $this->sName . '.' . $sOptionName; |
62
|
|
|
return $this->xConfigManager->getAppOption($sOptionName, $xDefault); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check the presence of a config option |
67
|
|
|
* |
68
|
|
|
* @param string $sOptionName The option name |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function hasOption(string $sOptionName): bool |
73
|
|
|
{ |
74
|
|
|
$sOptionName = 'dialogs.' . $this->sName . '.' . $sOptionName; |
75
|
|
|
return $this->xConfigManager->hasAppOption($sOptionName); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the names of the options matching a given prefix |
80
|
|
|
* |
81
|
|
|
* @param string $sPrefix The prefix to match |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function getOptionNames(string $sPrefix): array |
86
|
|
|
{ |
87
|
|
|
// The options names are relative to the plugin in Dialogs configuration |
88
|
|
|
return $this->xConfigManager->getOptionNames('dialogs.' . $this->sName . '.' . $sPrefix); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the names of the options matching a given prefix |
93
|
|
|
* |
94
|
|
|
* @param string $sVarPrefix |
95
|
|
|
* @param string $sKeyPrefix |
96
|
|
|
* @param int $nSpaces |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getOptionScript(string $sVarPrefix, string $sKeyPrefix, int $nSpaces = 4): string |
101
|
|
|
{ |
102
|
|
|
$aOptions = $this->getOptionNames($sKeyPrefix); |
103
|
|
|
$sSpaces = str_repeat(' ', $nSpaces); |
104
|
|
|
$sScript = ''; |
105
|
|
|
foreach($aOptions as $sShortName => $sFullName) |
106
|
|
|
{ |
107
|
|
|
$value = $this->xConfigManager->getAppOption($sFullName); |
108
|
|
|
if(is_string($value)) |
109
|
|
|
{ |
110
|
|
|
$value = "'$value'"; |
111
|
|
|
} |
112
|
|
|
elseif(is_bool($value)) |
113
|
|
|
{ |
114
|
|
|
$value = ($value ? 'true' : 'false'); |
115
|
|
|
} |
116
|
|
|
elseif(!is_numeric($value)) |
117
|
|
|
{ |
118
|
|
|
$value = json_encode($value); |
119
|
|
|
} |
120
|
|
|
$sScript .= "\n" . $sSpaces . $sVarPrefix . $sShortName . ' = ' . $value . ';'; |
121
|
|
|
} |
122
|
|
|
return $sScript; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $sOption The assets option name |
127
|
|
|
* @param string $sFile The javascript file name |
128
|
|
|
* |
129
|
|
|
* @return string|null |
130
|
|
|
*/ |
131
|
|
|
private function getAssetUri(string $sOption, string $sFile): ?string |
132
|
|
|
{ |
133
|
|
|
return !$this->hasOption($sOption) ? "{$this->sUri}/$sFile" : |
134
|
|
|
(trim($this->getOption($sOption)) ?: null); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the javascript HTML header code |
139
|
|
|
* |
140
|
|
|
* @param string $sFile The javascript file name |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getJsCode(string $sFile): string |
145
|
|
|
{ |
146
|
|
|
// If this 'assets.js' option is defined and evaluates to false, then the asset is not displayed. |
147
|
|
|
$sUri = $this->getAssetUri('assets.js', $sFile); |
148
|
|
|
return !$sUri ? '' : '<script type="text/javascript" src="' . $sUri . '"></script>'; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the CSS HTML header code |
153
|
|
|
* |
154
|
|
|
* @param string $sFile The CSS file name |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getCssCode(string $sFile): string |
159
|
|
|
{ |
160
|
|
|
// If this 'assets.css' option is defined and evaluates to false, then the asset is not displayed. |
161
|
|
|
$sUri = $this->getAssetUri('assets.css', $sFile); |
162
|
|
|
return !$sUri ? '' : '<link rel="stylesheet" href="' . $sUri . '" />'; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Render a template |
167
|
|
|
* |
168
|
|
|
* @param string $sTemplate The name of template to be rendered |
169
|
|
|
* @param array $aVars The template vars |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function render(string $sTemplate, array $aVars = []): string |
174
|
|
|
{ |
175
|
|
|
return $this->xTemplateEngine->render('jaxon::dialogs::' . $sTemplate, $aVars); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|