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-core |
|
|
|
|
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\App\Dialog\Library\DialogLibraryManager; |
19
|
|
|
use Jaxon\Exception\SetupException; |
20
|
|
|
use Jaxon\Plugin\ResponsePlugin; |
21
|
|
|
use Jaxon\Response\ResponseInterface; |
22
|
|
|
|
23
|
|
|
use function array_reduce; |
24
|
|
|
use function trim; |
25
|
|
|
|
26
|
|
|
class DialogPlugin extends ResponsePlugin |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @const The plugin name |
30
|
|
|
*/ |
31
|
|
|
const NAME = 'dialog'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var DialogLibraryManager |
35
|
|
|
*/ |
36
|
|
|
protected $xLibraryManager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $aLibraries = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The constructor |
45
|
|
|
* |
46
|
|
|
* @param DialogLibraryManager $xLibraryManager |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function __construct(DialogLibraryManager $xLibraryManager) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$this->xLibraryManager = $xLibraryManager; |
51
|
|
|
} |
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
|
|
|
|
56
|
|
|
public function getName(): string |
57
|
|
|
{ |
58
|
|
|
return self::NAME; |
59
|
|
|
} |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
|
|
|
|
64
|
|
|
public function getHash(): string |
65
|
|
|
{ |
66
|
|
|
// The version number is used as hash |
67
|
|
|
return '4.0.0'; |
68
|
|
|
} |
|
|
|
|
69
|
|
|
|
70
|
|
|
public function getUri(): string |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
return ''; |
73
|
|
|
} |
|
|
|
|
74
|
|
|
|
75
|
|
|
public function getSubdir(): string |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
return ''; |
78
|
|
|
} |
|
|
|
|
79
|
|
|
|
80
|
|
|
public function getVersion(): string |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
return ''; |
83
|
|
|
} |
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
|
|
|
|
86
|
|
|
* @inheritDoc |
87
|
|
|
*/ |
|
|
|
|
88
|
|
|
public function setResponse(ResponseInterface $xResponse) |
89
|
|
|
{ |
90
|
|
|
parent::setResponse($xResponse); |
91
|
|
|
|
92
|
|
|
// Hack the setResponse() method, to set the default libraries on each access to this plugin. |
93
|
|
|
$this->xLibraryManager->setNextLibrary(''); |
94
|
|
|
} |
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the library to use for the next call. |
98
|
|
|
* |
99
|
|
|
* @param string $sLibrary The name of the library |
100
|
|
|
* |
101
|
|
|
* @return DialogPlugin |
102
|
|
|
*/ |
103
|
|
|
public function with(string $sLibrary): DialogPlugin |
104
|
|
|
{ |
105
|
|
|
$this->xLibraryManager->setNextLibrary($sLibrary); |
106
|
|
|
return $this; |
107
|
|
|
} |
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
private function getLibraries(): array |
113
|
|
|
{ |
114
|
|
|
if($this->aLibraries === null) |
115
|
|
|
{ |
116
|
|
|
$this->aLibraries = $this->xLibraryManager->getLibraries(); |
117
|
|
|
} |
118
|
|
|
return $this->aLibraries; |
119
|
|
|
} |
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritDoc |
123
|
|
|
*/ |
|
|
|
|
124
|
|
|
public function getJs(): string |
125
|
|
|
{ |
126
|
|
|
return array_reduce($this->getLibraries(), function($sCode, $xLibrary) { |
127
|
|
|
return $sCode . $xLibrary->getJs() . "\n\n"; |
128
|
|
|
}, ''); |
129
|
|
|
} |
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritDoc |
133
|
|
|
*/ |
|
|
|
|
134
|
|
|
public function getCss(): string |
135
|
|
|
{ |
136
|
|
|
return array_reduce($this->getLibraries(), function($sCode, $xLibrary) { |
137
|
|
|
return $sCode . trim($xLibrary->getCss()) . "\n\n"; |
138
|
|
|
}, ''); |
139
|
|
|
} |
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritDoc |
143
|
|
|
* @throws SetupException |
144
|
|
|
*/ |
|
|
|
|
145
|
|
|
public function getScript(): string |
146
|
|
|
{ |
147
|
|
|
return array_reduce($this->getLibraries(), function($sCode, $xLibrary) { |
148
|
|
|
return $sCode . trim($xLibrary->getScript()) . "\n\n"; |
149
|
|
|
}, "jaxon.dialogs = {};\n"); |
150
|
|
|
} |
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @inheritDoc |
154
|
|
|
*/ |
|
|
|
|
155
|
|
|
public function getReadyScript(): string |
156
|
|
|
{ |
157
|
|
|
return array_reduce($this->getLibraries(), function($sCode, $xLibrary) { |
158
|
|
|
return $sCode . trim($xLibrary->getReadyScript()) . "\n\n"; |
159
|
|
|
}, ''); |
160
|
|
|
} |
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Show a modal dialog. |
164
|
|
|
* |
165
|
|
|
* @param string $sTitle The title of the dialog |
|
|
|
|
166
|
|
|
* @param string $sContent The content of the dialog |
167
|
|
|
* @param array $aButtons The buttons of the dialog |
|
|
|
|
168
|
|
|
* @param array $aOptions The options of the dialog |
|
|
|
|
169
|
|
|
* |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
|
|
public function show(string $sTitle, string $sContent, array $aButtons = [], array $aOptions = []) |
173
|
|
|
{ |
174
|
|
|
// Show the modal dialog |
175
|
|
|
$this->addCommand('dialog.modal.show', |
176
|
|
|
$this->xLibraryManager->show($sTitle, $sContent, $aButtons, $aOptions)); |
177
|
|
|
} |
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Hide the modal dialog. |
181
|
|
|
* |
182
|
|
|
* @return void |
183
|
|
|
*/ |
184
|
|
|
public function hide() |
185
|
|
|
{ |
186
|
|
|
// Hide the modal dialog |
187
|
|
|
$this->addCommand('dialog.modal.hide', $this->xLibraryManager->hide()); |
188
|
|
|
} |
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set the title of the next message. |
192
|
|
|
* |
193
|
|
|
* @param string $sTitle The title of the message |
|
|
|
|
194
|
|
|
* |
195
|
|
|
* @return DialogPlugin |
196
|
|
|
*/ |
197
|
|
|
public function title(string $sTitle): DialogPlugin |
198
|
|
|
{ |
199
|
|
|
$this->xLibraryManager->title($sTitle); |
200
|
|
|
return $this; |
201
|
|
|
} |
|
|
|
|
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Show a success message. |
205
|
|
|
* |
206
|
|
|
* @param string $sMessage The text of the message |
|
|
|
|
207
|
|
|
* @param array $aArgs The message arguments |
|
|
|
|
208
|
|
|
* |
209
|
|
|
* @return void |
210
|
|
|
*/ |
211
|
|
|
public function success(string $sMessage, array $aArgs = []) |
212
|
|
|
{ |
213
|
|
|
$this->addCommand('dialog.message', $this->xLibraryManager->success($sMessage, $aArgs)); |
214
|
|
|
} |
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Show an information message. |
218
|
|
|
* |
219
|
|
|
* @param string $sMessage The text of the message |
|
|
|
|
220
|
|
|
* @param array $aArgs The message arguments |
|
|
|
|
221
|
|
|
* |
222
|
|
|
* @return void |
223
|
|
|
*/ |
224
|
|
|
public function info(string $sMessage, array $aArgs = []) |
225
|
|
|
{ |
226
|
|
|
$this->addCommand('dialog.message', $this->xLibraryManager->info($sMessage, $aArgs)); |
227
|
|
|
} |
|
|
|
|
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Show a warning message. |
231
|
|
|
* |
232
|
|
|
* @param string $sMessage The text of the message |
|
|
|
|
233
|
|
|
* @param array $aArgs The message arguments |
|
|
|
|
234
|
|
|
* |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
|
|
public function warning(string $sMessage, array $aArgs = []) |
238
|
|
|
{ |
239
|
|
|
$this->addCommand('dialog.message', $this->xLibraryManager->warning($sMessage, $aArgs)); |
240
|
|
|
} |
|
|
|
|
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Show an error message. |
244
|
|
|
* |
245
|
|
|
* @param string $sMessage The text of the message |
|
|
|
|
246
|
|
|
* @param array $aArgs The message arguments |
|
|
|
|
247
|
|
|
* |
248
|
|
|
* @return void |
249
|
|
|
*/ |
250
|
|
|
public function error(string $sMessage, array $aArgs = []) |
251
|
|
|
{ |
252
|
|
|
$this->addCommand('dialog.message', $this->xLibraryManager->error($sMessage, $aArgs)); |
253
|
|
|
} |
|
|
|
|
254
|
|
|
} |
255
|
|
|
|