|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* DialogPlugin.php - ModalInterface, message and question dialogs for Jaxon. |
|
5
|
|
|
* |
|
6
|
|
|
* Show modal, message and question dialogs with various javascript libraries |
|
7
|
|
|
* based on user settings. |
|
8
|
|
|
* |
|
9
|
|
|
* @package jaxon-dialogs |
|
|
|
|
|
|
10
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
11
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
13
|
|
|
* @link https://github.com/jaxon-php/jaxon-dialogs |
|
14
|
|
|
*/ |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
namespace Jaxon\Plugin\Response\Dialog; |
|
17
|
|
|
|
|
18
|
|
|
use Jaxon\Config\ConfigManager; |
|
19
|
|
|
use Jaxon\Di\Container; |
|
20
|
|
|
use Jaxon\Plugin\ResponsePlugin; |
|
21
|
|
|
use Jaxon\Response\Response; |
|
22
|
|
|
use Jaxon\Ui\Dialog\Library\DialogLibraryManager; |
|
23
|
|
|
use Jaxon\Ui\Dialog\MessageInterface; |
|
24
|
|
|
use Jaxon\Ui\Dialog\ModalInterface; |
|
25
|
|
|
|
|
26
|
|
|
use function array_reduce; |
|
27
|
|
|
use function trim; |
|
28
|
|
|
|
|
29
|
|
|
class DialogPlugin extends ResponsePlugin implements ModalInterface, MessageInterface |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @const The plugin name |
|
33
|
|
|
*/ |
|
34
|
|
|
const NAME = 'dialog'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Dependency Injection manager |
|
38
|
|
|
* |
|
39
|
|
|
* @var Container |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $di; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var DialogLibraryManager |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $xLibraryManager; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var ConfigManager |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $xConfigManager; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $aLibraries = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The constructor |
|
60
|
|
|
* |
|
61
|
|
|
* @param Container $di |
|
|
|
|
|
|
62
|
|
|
* @param ConfigManager $xConfigManager |
|
|
|
|
|
|
63
|
|
|
* @param DialogLibraryManager $xLibraryManager |
|
|
|
|
|
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct(Container $di, ConfigManager $xConfigManager, DialogLibraryManager $xLibraryManager) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$this->di = $di; |
|
|
|
|
|
|
68
|
|
|
$this->xConfigManager = $xConfigManager; |
|
69
|
|
|
$this->xLibraryManager = $xLibraryManager; |
|
70
|
|
|
|
|
71
|
|
|
$aLibraries = $this->xConfigManager->getOption('dialogs.libraries', []); |
|
72
|
|
|
foreach($aLibraries as $sClassName => $sName) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->registerLibrary($sClassName, $sName); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @inheritDoc |
|
80
|
|
|
*/ |
|
|
|
|
|
|
81
|
|
|
public function getName(): string |
|
82
|
|
|
{ |
|
83
|
|
|
return self::NAME; |
|
84
|
|
|
} |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritDoc |
|
88
|
|
|
*/ |
|
|
|
|
|
|
89
|
|
|
public function getHash(): string |
|
90
|
|
|
{ |
|
91
|
|
|
return '4.0.0'; // The version number is used as hash |
|
92
|
|
|
} |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Register a javascript dialog library adapter. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $sClassName |
|
|
|
|
|
|
98
|
|
|
* @param string $sName |
|
|
|
|
|
|
99
|
|
|
* |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
|
|
public function registerLibrary(string $sClassName, string $sName) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->aLibraries[] = $sName; |
|
105
|
|
|
$this->di->registerDialogLibrary($sClassName, $sName); |
|
106
|
|
|
} |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function setDefaultLibraries() |
|
112
|
|
|
{ |
|
113
|
|
|
// Set the default modal library |
|
114
|
|
|
if(($sName = $this->xConfigManager->getOption('dialogs.default.modal', ''))) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$this->xLibraryManager->setModalLibrary($sName); |
|
117
|
|
|
} |
|
118
|
|
|
// Set the default message library |
|
119
|
|
|
if(($sName = $this->xConfigManager->getOption('dialogs.default.message', ''))) |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
$this->xLibraryManager->setMessageLibrary($sName); |
|
122
|
|
|
} |
|
123
|
|
|
// Set the default question library |
|
124
|
|
|
if(($sName = $this->xConfigManager->getOption('dialogs.default.question', ''))) |
|
|
|
|
|
|
125
|
|
|
{ |
|
126
|
|
|
$this->xLibraryManager->setQuestionLibrary($sName); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
/** |
|
|
|
|
|
|
131
|
|
|
* @inheritDoc |
|
132
|
|
|
*/ |
|
|
|
|
|
|
133
|
|
|
public function setResponse(Response $xResponse) |
|
134
|
|
|
{ |
|
135
|
|
|
parent::setResponse($xResponse); |
|
136
|
|
|
|
|
137
|
|
|
// Hack the setResponse() method, to set the default libraries on each access to this plugin. |
|
138
|
|
|
$this->setDefaultLibraries(); |
|
139
|
|
|
$this->xLibraryManager->setNextLibrary(''); |
|
140
|
|
|
} |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Set the library to use for the next call. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $sLibrary The name of the library |
|
146
|
|
|
* |
|
147
|
|
|
* @return DialogPlugin |
|
148
|
|
|
*/ |
|
149
|
|
|
public function with(string $sLibrary): DialogPlugin |
|
150
|
|
|
{ |
|
151
|
|
|
$this->xLibraryManager->setNextLibrary($sLibrary); |
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get the library adapter to use for modals. |
|
157
|
|
|
* |
|
158
|
|
|
* @return ModalInterface|null |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function getModalLibrary(): ?ModalInterface |
|
161
|
|
|
{ |
|
162
|
|
|
$xLibrary = $this->xLibraryManager->getModalLibrary(); |
|
163
|
|
|
$xLibrary->setResponse($this->xResponse); |
|
|
|
|
|
|
164
|
|
|
return $xLibrary; |
|
165
|
|
|
} |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get the library adapter to use for messages. |
|
169
|
|
|
* |
|
170
|
|
|
* @return MessageInterface|null |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function getMessageLibrary(): ?MessageInterface |
|
173
|
|
|
{ |
|
174
|
|
|
$xLibrary = $this->xLibraryManager->getMessageLibrary(); |
|
175
|
|
|
$xLibrary->setResponse($this->xResponse); |
|
176
|
|
|
// By default, always add commands to the response |
|
177
|
|
|
$xLibrary->setReturnCode(false); |
|
178
|
|
|
return $xLibrary; |
|
179
|
|
|
} |
|
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @inheritDoc |
|
183
|
|
|
*/ |
|
|
|
|
|
|
184
|
|
|
public function getJs(): string |
|
185
|
|
|
{ |
|
186
|
|
|
return array_reduce($this->aLibraries, function($sCode, $sName) { |
|
187
|
|
|
$xLibrary = $this->di->g($sName); |
|
188
|
|
|
return $sCode . $xLibrary->getJs() . "\n\n"; |
|
189
|
|
|
}, ''); |
|
190
|
|
|
} |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @inheritDoc |
|
194
|
|
|
*/ |
|
|
|
|
|
|
195
|
|
|
public function getCss(): string |
|
196
|
|
|
{ |
|
197
|
|
|
return array_reduce($this->aLibraries, function($sCode, $sName) { |
|
198
|
|
|
$xLibrary = $this->di->g($sName); |
|
199
|
|
|
return $sCode . trim($xLibrary->getCss()) . "\n\n"; |
|
200
|
|
|
}, ''); |
|
201
|
|
|
} |
|
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @inheritDoc |
|
205
|
|
|
*/ |
|
|
|
|
|
|
206
|
|
|
public function getScript(): string |
|
207
|
|
|
{ |
|
208
|
|
|
// The default scripts need to be set in the js code. |
|
209
|
|
|
$this->setDefaultLibraries(); |
|
210
|
|
|
return array_reduce($this->aLibraries, function($sCode, $sName) { |
|
211
|
|
|
$xLibrary = $this->di->g($sName); |
|
212
|
|
|
return $sCode . trim($xLibrary->getScript()) . "\n\n"; |
|
213
|
|
|
}, "jaxon.dialogs = {};\n"); |
|
214
|
|
|
} |
|
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @inheritDoc |
|
218
|
|
|
*/ |
|
|
|
|
|
|
219
|
|
|
public function getReadyScript(): string |
|
220
|
|
|
{ |
|
221
|
|
|
return array_reduce($this->aLibraries, function($sCode, $sName) { |
|
222
|
|
|
$xLibrary = $this->di->g($sName); |
|
223
|
|
|
return $sCode . trim($xLibrary->getReadyScript()) . "\n\n"; |
|
224
|
|
|
}, ''); |
|
225
|
|
|
} |
|
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
/** |
|
|
|
|
|
|
228
|
|
|
* @inheritDoc |
|
229
|
|
|
*/ |
|
|
|
|
|
|
230
|
|
|
public function show(string $sTitle, string $sContent, array $aButtons = [], array $aOptions = []) |
|
231
|
|
|
{ |
|
232
|
|
|
$this->getModalLibrary()->show($sTitle, $sContent, $aButtons, $aOptions); |
|
233
|
|
|
} |
|
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @inheritDoc |
|
237
|
|
|
*/ |
|
|
|
|
|
|
238
|
|
|
public function hide() |
|
239
|
|
|
{ |
|
240
|
|
|
$this->getModalLibrary()->hide(); |
|
241
|
|
|
} |
|
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
/** |
|
|
|
|
|
|
244
|
|
|
* @inheritDoc |
|
245
|
|
|
*/ |
|
|
|
|
|
|
246
|
|
|
public function success(string $sMessage, string $sTitle = ''): string |
|
247
|
|
|
{ |
|
248
|
|
|
return $this->getMessageLibrary()->success($sMessage, $sTitle); |
|
249
|
|
|
} |
|
|
|
|
|
|
250
|
|
|
|
|
251
|
|
|
/** |
|
|
|
|
|
|
252
|
|
|
* @inheritDoc |
|
253
|
|
|
*/ |
|
|
|
|
|
|
254
|
|
|
public function info(string $sMessage, string $sTitle = ''): string |
|
255
|
|
|
{ |
|
256
|
|
|
return $this->getMessageLibrary()->info($sMessage, $sTitle); |
|
257
|
|
|
} |
|
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
/** |
|
|
|
|
|
|
260
|
|
|
* @inheritDoc |
|
261
|
|
|
*/ |
|
|
|
|
|
|
262
|
|
|
public function warning(string $sMessage, string $sTitle = ''): string |
|
263
|
|
|
{ |
|
264
|
|
|
return $this->getMessageLibrary()->warning($sMessage, $sTitle); |
|
265
|
|
|
} |
|
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
/** |
|
|
|
|
|
|
268
|
|
|
* @inheritDoc |
|
269
|
|
|
*/ |
|
|
|
|
|
|
270
|
|
|
public function error(string $sMessage, string $sTitle = ''): string |
|
271
|
|
|
{ |
|
272
|
|
|
return $this->getMessageLibrary()->error($sMessage, $sTitle); |
|
273
|
|
|
} |
|
|
|
|
|
|
274
|
|
|
} |
|
275
|
|
|
|