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 array_filter; |
10
|
|
|
use function array_map; |
11
|
|
|
use function implode; |
12
|
|
|
use function is_array; |
13
|
|
|
use function rtrim; |
14
|
|
|
use function trim; |
15
|
|
|
|
16
|
|
|
class LibraryHelper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Default library URL |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/jaxon-dialogs@main/js'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The name of the library |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $sName = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The URI where to get the library files from |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $sUri = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The constructor |
41
|
|
|
* |
42
|
|
|
* @param LibraryInterface $xDialogLibrary |
43
|
|
|
* @param ConfigManager $xConfigManager |
44
|
|
|
* @param TemplateEngine $xTemplateEngine |
45
|
|
|
*/ |
46
|
|
|
public function __construct(LibraryInterface $xDialogLibrary, |
47
|
|
|
private ConfigManager $xConfigManager, private TemplateEngine $xTemplateEngine) |
48
|
|
|
{ |
49
|
|
|
// Set the library name |
50
|
|
|
$this->sName = $xDialogLibrary->getName(); |
51
|
|
|
// Set the default URI. |
52
|
|
|
$sDefaultUri = $xConfigManager->getAppOption('dialogs.lib.uri', $xDialogLibrary->getUri()); |
53
|
|
|
// Set the library URI. |
54
|
|
|
$this->sUri = rtrim($this->getOption('uri', $sDefaultUri), '/'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the value of a config option |
59
|
|
|
* |
60
|
|
|
* @param string $sOptionName The option name |
61
|
|
|
* @param mixed $xDefault The default value, to be returned if the option is not defined |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function getOption(string $sOptionName, $xDefault = null) |
66
|
|
|
{ |
67
|
|
|
$sOptionName = "dialogs.{$this->sName}.$sOptionName"; |
68
|
|
|
return $this->xConfigManager->getAppOption($sOptionName, $xDefault); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Check the presence of a config option |
73
|
|
|
* |
74
|
|
|
* @param string $sOptionName The option name |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function hasOption(string $sOptionName): bool |
79
|
|
|
{ |
80
|
|
|
return $this->xConfigManager->hasAppOption("dialogs.{$this->sName}.$sOptionName"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the options of the js library |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function getJsOptions(): array |
89
|
|
|
{ |
90
|
|
|
$xOptions = $this->xConfigManager->getAppOption("dialogs.{$this->sName}.options", []); |
91
|
|
|
return is_array($xOptions) ? $xOptions : []; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $sOption The assets option name |
96
|
|
|
* @param string $sFile The javascript file name |
97
|
|
|
* |
98
|
|
|
* @return string|null |
99
|
|
|
*/ |
100
|
|
|
private function getAssetUri(string $sOption, string $sFile): ?string |
101
|
|
|
{ |
102
|
|
|
return !$this->hasOption($sOption) ? "{$this->sUri}/$sFile" : |
103
|
|
|
(trim($this->getOption($sOption)) ?: null); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $sOption The assets option name |
108
|
|
|
* @param array $aFiles The asset files |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
private function getUris(string $sOption, array $aFiles): array |
113
|
|
|
{ |
114
|
|
|
$aFiles = array_map(fn($sFile) => |
115
|
|
|
$this->getAssetUri($sOption, $sFile), $aFiles); |
116
|
|
|
return array_filter($aFiles, fn($sFile) => $sFile !== null); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $sUri |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
private function getJsTag(string $sUri): string |
125
|
|
|
{ |
126
|
|
|
return '<script type="text/javascript" src="' . $sUri . '"></script>'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the javascript HTML header code |
131
|
|
|
* |
132
|
|
|
* @param array $aFiles The js files |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getJs(array $aFiles): string |
137
|
|
|
{ |
138
|
|
|
$aFiles = $this->getUris('assets.js', $aFiles); |
139
|
|
|
if($this->xConfigManager->getOption('js.app.export', false)) |
140
|
|
|
{ |
141
|
|
|
// Add the library script file to the list. |
142
|
|
|
$sJsFileName = !$this->xConfigManager->getOption('js.app.minify', false) ? |
143
|
|
|
"{$this->sName}.js" : "{$this->sName}.min.js"; |
144
|
|
|
$aFiles[] = self::JS_LIB_URL . "/$sJsFileName"; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$aFiles = array_map(fn($sUri) => $this->getJsTag($sUri), $aFiles); |
148
|
|
|
return implode("\n", $aFiles); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the javascript HTML header code |
153
|
|
|
* |
154
|
|
|
* @param string $sFile The javascript file name |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getJsHtml(string $sFile): string |
159
|
|
|
{ |
160
|
|
|
// If this 'assets.js' option is defined and evaluates to false, then the asset is not displayed. |
161
|
|
|
$sUri = $this->getAssetUri('assets.js', $sFile); |
162
|
|
|
return !$sUri ? '' : $this->getJsTag($sUri); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $sUri |
167
|
|
|
* |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
private function getCssTag(string $sUri): string |
171
|
|
|
{ |
172
|
|
|
return '<link rel="stylesheet" href="' . $sUri . '" />'; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get the CSS HTML header code |
177
|
|
|
* |
178
|
|
|
* @param array $aFiles The CSS files |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function getCss(array $aFiles): string |
183
|
|
|
{ |
184
|
|
|
$aFiles = $this->getUris('assets.css', $aFiles); |
185
|
|
|
$aFiles = array_map(fn($sUri) => $this->getCssTag($sUri), $aFiles); |
186
|
|
|
return implode("\n", $aFiles); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get the CSS HTML header code |
191
|
|
|
* |
192
|
|
|
* @param string $sFile The CSS file name |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function getCssHtml(string $sFile): string |
197
|
|
|
{ |
198
|
|
|
// If this 'assets.css' option is defined and evaluates to false, then the asset is not displayed. |
199
|
|
|
$sUri = $this->getAssetUri('assets.css', $sFile); |
200
|
|
|
return !$sUri ? '' : $this->getCssTag($sUri); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the library script code |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
|
209
|
|
|
public function getScript(): string |
210
|
|
|
{ |
211
|
|
|
return !$this->xConfigManager->getOption('js.app.export', false) ? |
212
|
|
|
$this->xTemplateEngine->render("jaxon::dialogs::{$this->sName}.js") : ''; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|