|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Library.php - Base class for javascript library adapters. |
|
5
|
|
|
* |
|
6
|
|
|
* @package jaxon-dialogs |
|
7
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
8
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
9
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
10
|
|
|
* @link https://github.com/jaxon-php/jaxon-dialogs |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Jaxon\Dialogs\Libraries; |
|
14
|
|
|
|
|
15
|
|
|
use Jaxon\Dialogs\DialogPlugin; |
|
16
|
|
|
use Jaxon\Dialogs\PluginInterface; |
|
17
|
|
|
use Jaxon\Response\Response; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
use function trim; |
|
20
|
|
|
use function str_repeat; |
|
21
|
|
|
use function is_string; |
|
22
|
|
|
use function is_bool; |
|
23
|
|
|
use function is_numeric; |
|
24
|
|
|
use function json_encode; |
|
25
|
|
|
|
|
26
|
|
|
class Library implements PluginInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* The plugin instance |
|
30
|
|
|
* |
|
31
|
|
|
* @var DialogPlugin |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $xDialogPlugin = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The name of the plugin |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $sName = ''; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The subdir of the JS and CSS files in the CDN |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $sSubDir = ''; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The default version of the plugin library |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $sVersion = ''; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The default URI where to get the library files from |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $sUri = 'https://cdn.jaxon-php.org/libs'; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The constructor |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $sSubDir The subdir of the JS and CSS files in the CDN |
|
67
|
|
|
* @param string $sVersion The default version of the plugin library |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function __construct(string $sSubDir, string $sVersion) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->sSubDir = $sSubDir; |
|
72
|
|
|
$this->sVersion = $sVersion; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Add a client side plugin command to the response object |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $aAttributes The attributes of the command |
|
79
|
|
|
* @param mixed $xData The data to be added to the command |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
final public function addCommand(array $aAttributes, $xData) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->xResponse->addPluginCommand($this->xDialogPlugin, $aAttributes, $xData); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Initialize the library class instance |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $sName The plugin name |
|
92
|
|
|
* @param DialogPlugin $xDialogPlugin The DialogPlugin plugin instance |
|
93
|
|
|
* |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
final public function init(string $sName, DialogPlugin $xDialogPlugin) |
|
97
|
|
|
{ |
|
98
|
|
|
// Set the library name |
|
99
|
|
|
$this->sName = $sName; |
|
100
|
|
|
// Set the dialog plugin |
|
101
|
|
|
$this->xDialogPlugin = $xDialogPlugin; |
|
102
|
|
|
// Set the default URI. |
|
103
|
|
|
$this->sUri = $xDialogPlugin->getOption('dialogs.lib.uri', $this->sUri); |
|
104
|
|
|
// Set the library URI. |
|
105
|
|
|
$this->sUri = rtrim($this->getOption('uri', $this->sUri), '/'); |
|
106
|
|
|
// Set the subdir |
|
107
|
|
|
$this->sSubDir = trim($this->getOption('subdir', $this->sSubDir), '/'); |
|
108
|
|
|
// Set the version number |
|
109
|
|
|
$this->sVersion = trim($this->getOption('version', $this->sVersion), '/'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get the value of a config option |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $sName The option name |
|
116
|
|
|
* @param mixed $xDefault The default value, to be returned if the option is not defined |
|
117
|
|
|
* |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
*/ |
|
120
|
|
|
final public function getOption(string $sName, $xDefault = null) |
|
121
|
|
|
{ |
|
122
|
|
|
$sName = 'dialogs.' . $this->getName() . '.' . $sName; |
|
123
|
|
|
return $this->xDialogPlugin->getOption($sName, $xDefault); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Check the presence of a config option |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $sName The option name |
|
130
|
|
|
* |
|
131
|
|
|
* @return bool |
|
132
|
|
|
*/ |
|
133
|
|
|
final public function hasOption(string $sName): bool |
|
134
|
|
|
{ |
|
135
|
|
|
$sName = 'dialogs.' . $this->getName() . '.' . $sName; |
|
136
|
|
|
return $this->xDialogPlugin->hasOption($sName); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get the names of the options matching a given prefix |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $sPrefix The prefix to match |
|
143
|
|
|
* |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
final public function getOptionNames(string $sPrefix): array |
|
147
|
|
|
{ |
|
148
|
|
|
// The options names are relative to the plugin in Dialogs configuration |
|
149
|
|
|
return $this->xDialogPlugin->getOptionNames('dialogs.' . $this->getName() . '.' . $sPrefix); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get the names of the options matching a given prefix |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $sVarPrefix |
|
156
|
|
|
* @param string $sKeyPrefix |
|
157
|
|
|
* @param int $nSpaces |
|
158
|
|
|
* |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
|
|
final public function getOptionScript(string $sVarPrefix, string $sKeyPrefix, int $nSpaces = 4): string |
|
162
|
|
|
{ |
|
163
|
|
|
$aOptions = $this->getOptionNames($sKeyPrefix); |
|
164
|
|
|
$sSpaces = str_repeat(' ', $nSpaces); |
|
165
|
|
|
$sScript = ''; |
|
166
|
|
|
foreach($aOptions as $sShortName => $sFullName) |
|
167
|
|
|
{ |
|
168
|
|
|
$value = $this->xDialogPlugin->getOption($sFullName); |
|
169
|
|
|
if(is_string($value)) |
|
170
|
|
|
{ |
|
171
|
|
|
$value = "'$value'"; |
|
172
|
|
|
} |
|
173
|
|
|
elseif(is_bool($value)) |
|
174
|
|
|
{ |
|
175
|
|
|
$value = ($value ? 'true' : 'false'); |
|
176
|
|
|
} |
|
177
|
|
|
elseif(!is_numeric($value)) |
|
178
|
|
|
{ |
|
179
|
|
|
$value = json_encode($value); |
|
180
|
|
|
} |
|
181
|
|
|
$sScript .= "\n" . $sSpaces . $sVarPrefix . $sShortName . ' = ' . $value . ';'; |
|
182
|
|
|
} |
|
183
|
|
|
return $sScript; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @inheritDoc |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getName(): string |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->sName; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @inheritDoc |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getJs(): string |
|
198
|
|
|
{ |
|
199
|
|
|
return ''; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @inheritDoc |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getCss(): string |
|
206
|
|
|
{ |
|
207
|
|
|
return ''; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @inheritDoc |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getScript(): string |
|
214
|
|
|
{ |
|
215
|
|
|
return ''; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @inheritDoc |
|
220
|
|
|
*/ |
|
221
|
|
|
public function getReadyScript(): string |
|
222
|
|
|
{ |
|
223
|
|
|
return ''; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Get the text of the "Yes" button for confirm dialog |
|
228
|
|
|
* |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
|
|
public function getQuestionTitle(): string |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->xDialogPlugin->getOption('dialogs.question.title', ''); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Get the text of the "Yes" button for confirm dialog |
|
238
|
|
|
* |
|
239
|
|
|
* @return string |
|
240
|
|
|
*/ |
|
241
|
|
|
public function getYesButtonText(): string |
|
242
|
|
|
{ |
|
243
|
|
|
return $this->xDialogPlugin->getOption('dialogs.question.yes', 'Yes'); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Get the text of the "No" button for confirm dialog |
|
248
|
|
|
* |
|
249
|
|
|
* @return string |
|
250
|
|
|
*/ |
|
251
|
|
|
public function getNoButtonText(): string |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->xDialogPlugin->getOption('dialogs.question.no', 'No'); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Get the javascript HTML header code |
|
258
|
|
|
* |
|
259
|
|
|
* @param string $sFile The javascript file name |
|
260
|
|
|
* |
|
261
|
|
|
* @return string |
|
262
|
|
|
*/ |
|
263
|
|
|
public function getJsCode(string $sFile): string |
|
264
|
|
|
{ |
|
265
|
|
|
return '<script type="text/javascript" src="' . $this->sUri . '/' . |
|
266
|
|
|
$this->sSubDir . '/' . $this->sVersion . '/' . $sFile . '"></script>'; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Get the CSS HTML header code |
|
271
|
|
|
* |
|
272
|
|
|
* @param string $sFile The CSS file name |
|
273
|
|
|
* |
|
274
|
|
|
* @return string |
|
275
|
|
|
*/ |
|
276
|
|
|
public function getCssCode(string $sFile): string |
|
277
|
|
|
{ |
|
278
|
|
|
return '<link rel="stylesheet" href="' . $this->sUri . '/' . |
|
279
|
|
|
$this->sSubDir . '/' . $this->sVersion . '/' . $sFile . '" />'; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Render a template |
|
284
|
|
|
* |
|
285
|
|
|
* @param string $sTemplate The name of template to be rendered |
|
286
|
|
|
* @param array $aVars The template vars |
|
287
|
|
|
* |
|
288
|
|
|
* @return string |
|
289
|
|
|
*/ |
|
290
|
|
|
protected function render(string $sTemplate, array $aVars = []): string |
|
291
|
|
|
{ |
|
292
|
|
|
// Is the library the default for alert messages? |
|
293
|
|
|
$isDefaultForMessage = ($this->getName() == $this->xDialogPlugin->getOption('dialogs.default.message')); |
|
294
|
|
|
// Is the library the default for confirm questions? |
|
295
|
|
|
$isDefaultForQuestion = ($this->getName() == $this->xDialogPlugin->getOption('dialogs.default.question')); |
|
296
|
|
|
$aLocalVars = [ |
|
297
|
|
|
'yes' => $this->getYesButtonText(), |
|
298
|
|
|
'no' => $this->getNoButtonText(), |
|
299
|
|
|
'defaultForMessage' => $isDefaultForMessage, |
|
300
|
|
|
'defaultForQuestion' => $isDefaultForQuestion |
|
301
|
|
|
]; |
|
302
|
|
|
return $this->xDialogPlugin->render('jaxon::dialogs::' . $sTemplate, array_merge($aLocalVars, $aVars)); |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths