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