|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jaxon\Ui\Dialog\Library; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Config\ConfigManager; |
|
6
|
|
|
use Jaxon\Utils\Template\TemplateEngine; |
|
7
|
|
|
|
|
8
|
|
|
use function array_merge; |
|
9
|
|
|
use function is_string; |
|
10
|
|
|
use function is_bool; |
|
11
|
|
|
use function is_numeric; |
|
12
|
|
|
use function json_encode; |
|
13
|
|
|
use function rtrim; |
|
14
|
|
|
use function str_repeat; |
|
15
|
|
|
use function trim; |
|
16
|
|
|
|
|
17
|
|
|
class DialogLibraryHelper |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ConfigManager |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $xConfigManager; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The Jaxon template engine |
|
26
|
|
|
* |
|
27
|
|
|
* @var TemplateEngine |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $xTemplateEngine; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The name of the library |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $sName = ''; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The URI where to get the library files from |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $sUri = ''; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The subdir of the JS and CSS files in the CDN |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $sSubDir = ''; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The default version of the plugin library |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $sVersion = ''; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The constructor |
|
61
|
|
|
* |
|
62
|
|
|
* @param AbstractDialogLibrary $xDialogLibrary |
|
|
|
|
|
|
63
|
|
|
* @param ConfigManager $xConfigManager |
|
|
|
|
|
|
64
|
|
|
* @param TemplateEngine $xTemplateEngine |
|
|
|
|
|
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct(AbstractDialogLibrary $xDialogLibrary, |
|
|
|
|
|
|
67
|
|
|
ConfigManager $xConfigManager, TemplateEngine $xTemplateEngine) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->xConfigManager = $xConfigManager; |
|
70
|
|
|
$this->xTemplateEngine = $xTemplateEngine; |
|
71
|
|
|
|
|
72
|
|
|
// Set the library name |
|
73
|
|
|
$this->sName = $xDialogLibrary->getName(); |
|
74
|
|
|
// Set the default URI. |
|
75
|
|
|
$sDefaultUri = $xConfigManager->getOption('dialogs.lib.uri', $xDialogLibrary->getUri()); |
|
76
|
|
|
// Set the library URI. |
|
77
|
|
|
$this->sUri = rtrim($this->getOption('uri', $sDefaultUri), '/'); |
|
78
|
|
|
// Set the subdir |
|
79
|
|
|
$this->sSubDir = trim($this->getOption('subdir', $xDialogLibrary->getSubDir()), '/'); |
|
80
|
|
|
// Set the version number |
|
81
|
|
|
$this->sVersion = trim($this->getOption('version', $xDialogLibrary->getVersion()), '/'); |
|
82
|
|
|
} |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the value of a config option |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $sOptionName The option name |
|
88
|
|
|
* @param mixed $xDefault The default value, to be returned if the option is not defined |
|
|
|
|
|
|
89
|
|
|
* |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getOption(string $sOptionName, $xDefault = null) |
|
93
|
|
|
{ |
|
94
|
|
|
$sOptionName = 'dialogs.' . $this->sName . '.' . $sOptionName; |
|
95
|
|
|
return $this->xConfigManager->getOption($sOptionName, $xDefault); |
|
96
|
|
|
} |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Check the presence of a config option |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $sOptionName The option name |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
|
|
public function hasOption(string $sOptionName): bool |
|
106
|
|
|
{ |
|
107
|
|
|
$sOptionName = 'dialogs.' . $this->sName . '.' . $sOptionName; |
|
108
|
|
|
return $this->xConfigManager->hasOption($sOptionName); |
|
109
|
|
|
} |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get the names of the options matching a given prefix |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $sPrefix The prefix to match |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getOptionNames(string $sPrefix): array |
|
119
|
|
|
{ |
|
120
|
|
|
// The options names are relative to the plugin in Dialogs configuration |
|
121
|
|
|
return $this->xConfigManager->getOptionNames('dialogs.' . $this->sName . '.' . $sPrefix); |
|
122
|
|
|
} |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get the names of the options matching a given prefix |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $sVarPrefix |
|
|
|
|
|
|
128
|
|
|
* @param string $sKeyPrefix |
|
|
|
|
|
|
129
|
|
|
* @param int $nSpaces |
|
|
|
|
|
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getOptionScript(string $sVarPrefix, string $sKeyPrefix, int $nSpaces = 4): string |
|
134
|
|
|
{ |
|
135
|
|
|
$aOptions = $this->getOptionNames($sKeyPrefix); |
|
136
|
|
|
$sSpaces = str_repeat(' ', $nSpaces); |
|
137
|
|
|
$sScript = ''; |
|
138
|
|
|
foreach($aOptions as $sShortName => $sFullName) |
|
139
|
|
|
{ |
|
140
|
|
|
$value = $this->xConfigManager->getOption($sFullName); |
|
141
|
|
|
if(is_string($value)) |
|
142
|
|
|
{ |
|
143
|
|
|
$value = "'$value'"; |
|
144
|
|
|
} |
|
145
|
|
|
elseif(is_bool($value)) |
|
146
|
|
|
{ |
|
147
|
|
|
$value = ($value ? 'true' : 'false'); |
|
148
|
|
|
} |
|
149
|
|
|
elseif(!is_numeric($value)) |
|
150
|
|
|
{ |
|
151
|
|
|
$value = json_encode($value); |
|
152
|
|
|
} |
|
153
|
|
|
$sScript .= "\n" . $sSpaces . $sVarPrefix . $sShortName . ' = ' . $value . ';'; |
|
154
|
|
|
} |
|
155
|
|
|
return $sScript; |
|
156
|
|
|
} |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get the text of the "Yes" button for confirm dialog |
|
160
|
|
|
* |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getQuestionTitle(): string |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->xConfigManager->getOption('dialogs.question.title', ''); |
|
166
|
|
|
} |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Get the javascript HTML header code |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $sFile The javascript file name |
|
172
|
|
|
* |
|
173
|
|
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getJsCode(string $sFile): string |
|
176
|
|
|
{ |
|
177
|
|
|
return '<script type="text/javascript" src="' . $this->sUri . '/' . |
|
178
|
|
|
$this->sSubDir . '/' . $this->sVersion . '/' . $sFile . '"></script>'; |
|
179
|
|
|
} |
|
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get the CSS HTML header code |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $sFile The CSS file name |
|
185
|
|
|
* |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getCssCode(string $sFile): string |
|
189
|
|
|
{ |
|
190
|
|
|
return '<link rel="stylesheet" href="' . $this->sUri . '/' . |
|
191
|
|
|
$this->sSubDir . '/' . $this->sVersion . '/' . $sFile . '" />'; |
|
192
|
|
|
} |
|
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Render a template |
|
196
|
|
|
* |
|
197
|
|
|
* @param string $sTemplate The name of template to be rendered |
|
198
|
|
|
* @param array $aVars The template vars |
|
|
|
|
|
|
199
|
|
|
* |
|
200
|
|
|
* @return string |
|
201
|
|
|
*/ |
|
202
|
|
|
public function render(string $sTemplate, array $aVars = []): string |
|
203
|
|
|
{ |
|
204
|
|
|
// Is the library the default for alert messages? |
|
205
|
|
|
$isDefaultForMessage = ($this->sName == $this->xConfigManager->getOption('dialogs.default.message')); |
|
206
|
|
|
// Is the library the default for confirm questions? |
|
207
|
|
|
$isDefaultForQuestion = ($this->sName == $this->xConfigManager->getOption('dialogs.default.question')); |
|
208
|
|
|
$aLocalVars = [ |
|
|
|
|
|
|
209
|
|
|
'yes' => $this->xConfigManager->getOption('dialogs.question.yes', 'Yes'), |
|
210
|
|
|
'no' => $this->xConfigManager->getOption('dialogs.question.no', 'No'), |
|
|
|
|
|
|
211
|
|
|
'defaultForMessage' => $isDefaultForMessage, |
|
212
|
|
|
'defaultForQuestion' => $isDefaultForQuestion |
|
213
|
|
|
]; |
|
214
|
|
|
return $this->xTemplateEngine->render('jaxon::dialogs::' . $sTemplate, array_merge($aLocalVars, $aVars)); |
|
215
|
|
|
} |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
|