|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* DialogLibraryManager.php - Shows alert and confirm dialogs |
|
5
|
|
|
* |
|
6
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
7
|
|
|
* @copyright 2019 Thierry Feuzeu <[email protected]> |
|
8
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
9
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Jaxon\Ui\Dialog\Library; |
|
13
|
|
|
|
|
14
|
|
|
use Jaxon\Di\Container; |
|
15
|
|
|
use Jaxon\Exception\SetupException; |
|
16
|
|
|
use Jaxon\Ui\Dialog\MessageInterface; |
|
17
|
|
|
use Jaxon\Ui\Dialog\ModalInterface; |
|
18
|
|
|
use Jaxon\Ui\Dialog\QuestionInterface; |
|
19
|
|
|
use Jaxon\Utils\Translation\Translator; |
|
20
|
|
|
|
|
21
|
|
|
use function array_map; |
|
22
|
|
|
use function class_implements; |
|
23
|
|
|
use function in_array; |
|
24
|
|
|
|
|
25
|
|
|
class DialogLibraryManager |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var Container |
|
29
|
|
|
*/ |
|
30
|
|
|
private $di; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $aLibraries = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $aQuestionLibraries = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $aMessageLibraries = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $aModalLibraries = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The QuestionInterface class name |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
private $sQuestionLibrary = ''; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The MessageInterface class name |
|
61
|
|
|
* |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
private $sMessageLibrary = ''; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* The ModalInterface class name |
|
68
|
|
|
* |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
private $sModalLibrary = ''; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* The name of the library to use for the next call. |
|
75
|
|
|
* This is used to override the default library. |
|
76
|
|
|
* |
|
77
|
|
|
* @var string |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $sNextLibrary = ''; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Default javascript alert library |
|
83
|
|
|
* |
|
84
|
|
|
* @var AlertLibrary |
|
85
|
|
|
*/ |
|
86
|
|
|
private $xAlertLibrary; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var Translator |
|
90
|
|
|
*/ |
|
91
|
|
|
private $xTranslator; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* The constructor |
|
95
|
|
|
* |
|
96
|
|
|
* @param Container $di |
|
|
|
|
|
|
97
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
98
|
|
|
*/ |
|
99
|
|
|
public function __construct(Container $di, Translator $xTranslator) |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
$this->di = $di; |
|
|
|
|
|
|
102
|
|
|
$this->xTranslator = $xTranslator; |
|
103
|
|
|
// Library for javascript confirm and alert functions. |
|
104
|
|
|
$this->xAlertLibrary = new AlertLibrary(); |
|
105
|
|
|
} |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Register a javascript dialog library adapter. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $sClassName |
|
|
|
|
|
|
111
|
|
|
* @param string $sName |
|
|
|
|
|
|
112
|
|
|
* |
|
113
|
|
|
* @return void |
|
114
|
|
|
* @throws SetupException |
|
115
|
|
|
*/ |
|
116
|
|
|
public function registerLibrary(string $sClassName, string $sName) |
|
117
|
|
|
{ |
|
118
|
|
|
$bIsUsed = false; |
|
|
|
|
|
|
119
|
|
|
$aInterfaces = class_implements($sClassName); |
|
120
|
|
|
if(in_array(QuestionInterface::class, $aInterfaces)) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->aQuestionLibraries[$sName] = $sClassName; |
|
123
|
|
|
$bIsUsed = true; |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
if(in_array(MessageInterface::class, $aInterfaces)) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->aMessageLibraries[$sName] = $sClassName; |
|
128
|
|
|
$bIsUsed = true; |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
if(in_array(ModalInterface::class, $aInterfaces)) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->aModalLibraries[$sName] = $sClassName; |
|
133
|
|
|
$bIsUsed = true; |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if(!$bIsUsed) |
|
137
|
|
|
{ |
|
138
|
|
|
// The class is invalid. |
|
139
|
|
|
$sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
|
140
|
|
|
throw new SetupException($sMessage); |
|
141
|
|
|
} |
|
142
|
|
|
$this->aLibraries[] = $sClassName; |
|
143
|
|
|
$this->di->registerDialogLibrary($sClassName); |
|
144
|
|
|
} |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get the library class instances |
|
148
|
|
|
* |
|
149
|
|
|
* @return array |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getLibraries(): array |
|
152
|
|
|
{ |
|
153
|
|
|
return array_map(function($sClassName) { |
|
154
|
|
|
return $this->di->g($sClassName); |
|
155
|
|
|
}, $this->aLibraries); |
|
156
|
|
|
} |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Set the QuestionInterface library |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $sLibraryName The QuestionInterface library name |
|
162
|
|
|
* |
|
163
|
|
|
* @return void |
|
164
|
|
|
* @throws SetupException |
|
165
|
|
|
*/ |
|
166
|
|
|
public function setQuestionLibrary(string $sLibraryName) |
|
167
|
|
|
{ |
|
168
|
|
|
if(!isset($this->aQuestionLibraries[$sLibraryName])) |
|
169
|
|
|
{ |
|
170
|
|
|
$sMessage = $this->xTranslator->trans('errors.dialog.library', |
|
171
|
|
|
['type' => 'question', 'name' => $sLibraryName]); |
|
172
|
|
|
throw new SetupException($sMessage); |
|
173
|
|
|
} |
|
174
|
|
|
$this->sQuestionLibrary = $sLibraryName; |
|
175
|
|
|
} |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Get the QuestionInterface library |
|
179
|
|
|
* |
|
180
|
|
|
* @return QuestionInterface |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getQuestionLibrary(): QuestionInterface |
|
183
|
|
|
{ |
|
184
|
|
|
$sLibraryName = $this->sNextLibrary ?: $this->sQuestionLibrary; |
|
185
|
|
|
return ($sLibraryName) ? $this->di->g($this->aQuestionLibraries[$sLibraryName]) : $this->xAlertLibrary; |
|
186
|
|
|
} |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Set MessageInterface library |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $sLibraryName The MessageInterface library name |
|
192
|
|
|
* |
|
193
|
|
|
* @return void |
|
194
|
|
|
* @throws SetupException |
|
195
|
|
|
*/ |
|
196
|
|
|
public function setMessageLibrary(string $sLibraryName) |
|
197
|
|
|
{ |
|
198
|
|
|
if(!isset($this->aMessageLibraries[$sLibraryName])) |
|
199
|
|
|
{ |
|
200
|
|
|
$sMessage = $this->xTranslator->trans('errors.dialog.library', |
|
201
|
|
|
['type' => 'message', 'name' => $sLibraryName]); |
|
202
|
|
|
throw new SetupException($sMessage); |
|
203
|
|
|
} |
|
204
|
|
|
$this->sMessageLibrary = $sLibraryName; |
|
205
|
|
|
} |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Get the MessageInterface library |
|
209
|
|
|
* |
|
210
|
|
|
* @return MessageInterface |
|
211
|
|
|
*/ |
|
212
|
|
|
public function getMessageLibrary(): MessageInterface |
|
213
|
|
|
{ |
|
214
|
|
|
$sLibraryName = $this->sNextLibrary ?: $this->sMessageLibrary; |
|
215
|
|
|
return ($sLibraryName) ? $this->di->g($this->aMessageLibraries[$sLibraryName]) : $this->xAlertLibrary; |
|
216
|
|
|
} |
|
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Set the ModalInterface library |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $sLibraryName The ModalInterface library name |
|
222
|
|
|
* |
|
223
|
|
|
* @return void |
|
224
|
|
|
* @throws SetupException |
|
225
|
|
|
*/ |
|
226
|
|
|
public function setModalLibrary(string $sLibraryName) |
|
227
|
|
|
{ |
|
228
|
|
|
if(!isset($this->aModalLibraries[$sLibraryName])) |
|
229
|
|
|
{ |
|
230
|
|
|
$sMessage = $this->xTranslator->trans('errors.dialog.library', |
|
231
|
|
|
['type' => 'modal', 'name' => $sLibraryName]); |
|
232
|
|
|
throw new SetupException($sMessage); |
|
233
|
|
|
} |
|
234
|
|
|
$this->sModalLibrary = $sLibraryName; |
|
235
|
|
|
} |
|
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Get the ModalInterface library |
|
239
|
|
|
* |
|
240
|
|
|
* @return ModalInterface |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getModalLibrary(): ?ModalInterface |
|
243
|
|
|
{ |
|
244
|
|
|
$sLibraryName = $this->sNextLibrary ?: $this->sModalLibrary; |
|
245
|
|
|
return ($sLibraryName) ? $this->di->g($this->aModalLibraries[$sLibraryName]) : null; |
|
246
|
|
|
} |
|
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Set the name of the library to use for the next call |
|
250
|
|
|
* |
|
251
|
|
|
* @param string $sNextLibrary |
|
|
|
|
|
|
252
|
|
|
* |
|
253
|
|
|
* @return void |
|
254
|
|
|
*/ |
|
255
|
|
|
public function setNextLibrary(string $sNextLibrary): void |
|
256
|
|
|
{ |
|
257
|
|
|
$this->sNextLibrary = $sNextLibrary; |
|
258
|
|
|
} |
|
|
|
|
|
|
259
|
|
|
} |
|
260
|
|
|
|