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\Dialogs; |
17
|
|
|
|
18
|
|
|
use Jaxon\Config\ConfigManager; |
|
|
|
|
19
|
|
|
use Jaxon\Di\Container; |
|
|
|
|
20
|
|
|
use Jaxon\Dialogs\Libraries\Library; |
21
|
|
|
use Jaxon\Plugin\ResponsePlugin; |
|
|
|
|
22
|
|
|
use Jaxon\Ui\Dialogs\DialogFacade; |
|
|
|
|
23
|
|
|
use Jaxon\Ui\Dialogs\MessageInterface; |
|
|
|
|
24
|
|
|
use Jaxon\Ui\Dialogs\ModalInterface; |
|
|
|
|
25
|
|
|
use Jaxon\Ui\Dialogs\QuestionInterface; |
|
|
|
|
26
|
|
|
use Jaxon\Utils\Template\TemplateEngine; |
|
|
|
|
27
|
|
|
|
28
|
|
|
use function array_merge; |
29
|
|
|
use function array_reduce; |
30
|
|
|
use function dirname; |
31
|
|
|
|
32
|
|
|
class DialogPlugin extends ResponsePlugin |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @const The plugin name |
36
|
|
|
*/ |
37
|
|
|
const NAME = 'dialog'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Dependency Injection manager |
41
|
|
|
* |
42
|
|
|
* @var Container |
43
|
|
|
*/ |
44
|
|
|
protected $di; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var DialogFacade |
48
|
|
|
*/ |
49
|
|
|
protected $xDialogFacade; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ConfigManager |
53
|
|
|
*/ |
54
|
|
|
protected $xConfigManager; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The Jaxon template engine |
58
|
|
|
* |
59
|
|
|
* @var TemplateEngine |
60
|
|
|
*/ |
61
|
|
|
protected $xTemplateEngine; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Javascript dialog library adapters |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $aLibraries = array( |
69
|
|
|
// Bootbox |
70
|
|
|
'bootbox' => Libraries\Bootbox\Plugin::class, |
71
|
|
|
// Bootstrap |
72
|
|
|
'bootstrap' => Libraries\Bootstrap\Plugin::class, |
73
|
|
|
// PgwJS |
74
|
|
|
'pgwjs' => Libraries\PgwJS\Plugin::class, |
75
|
|
|
// Toastr |
76
|
|
|
'toastr' => Libraries\Toastr\Plugin::class, |
77
|
|
|
// JAlert |
78
|
|
|
'jalert' => Libraries\JAlert\Plugin::class, |
79
|
|
|
// Tingle |
80
|
|
|
'tingle' => Libraries\Tingle\Plugin::class, |
81
|
|
|
// SimplyToast |
82
|
|
|
'simply' => Libraries\SimplyToast\Plugin::class, |
83
|
|
|
// Noty |
84
|
|
|
'noty' => Libraries\Noty\Plugin::class, |
85
|
|
|
// Notify |
86
|
|
|
'notify' => Libraries\Notify\Plugin::class, |
87
|
|
|
// Lobibox |
88
|
|
|
'lobibox' => Libraries\Lobibox\Plugin::class, |
89
|
|
|
// Overhang |
90
|
|
|
'overhang' => Libraries\Overhang\Plugin::class, |
91
|
|
|
// PNotify |
92
|
|
|
'pnotify' => Libraries\PNotify\Plugin::class, |
93
|
|
|
// SweetAlert |
94
|
|
|
'sweetalert' => Libraries\SweetAlert\Plugin::class, |
95
|
|
|
// JQuery Confirm |
96
|
|
|
'jconfirm' => Libraries\JQueryConfirm\Plugin::class, |
97
|
|
|
// YmzBox |
98
|
|
|
'ymzbox' => Libraries\YmzBox\Plugin::class, |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* The name of the library to use for the next call |
103
|
|
|
* |
104
|
|
|
* @var string |
105
|
|
|
*/ |
106
|
|
|
protected $sNextLibrary = ''; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @vr array |
110
|
|
|
*/ |
111
|
|
|
protected $aLibrariesInUse = []; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* The constructor |
115
|
|
|
* |
116
|
|
|
* @param Container $di |
117
|
|
|
* @param ConfigManager $xConfigManager |
118
|
|
|
* @param TemplateEngine $xTemplateEngine The template engine |
119
|
|
|
* @param DialogFacade $xDialogFacade |
120
|
|
|
*/ |
121
|
|
|
public function __construct(Container $di, ConfigManager $xConfigManager, |
122
|
|
|
TemplateEngine $xTemplateEngine, DialogFacade $xDialogFacade) |
123
|
|
|
{ |
124
|
|
|
$this->xDialogFacade = $xDialogFacade; |
125
|
|
|
$this->di = $di; |
126
|
|
|
$this->xConfigManager = $xConfigManager; |
127
|
|
|
$this->xTemplateEngine = $xTemplateEngine; |
128
|
|
|
|
129
|
|
|
// Register the template dir into the template renderer |
130
|
|
|
$xTemplateEngine->addNamespace('jaxon::dialogs', dirname(__DIR__) . '/templates'); |
131
|
|
|
|
132
|
|
|
$this->registerLibraries(); |
133
|
|
|
$this->registerClasses(); |
134
|
|
|
// Get the default modal library |
135
|
|
|
if(($sName = $this->xConfigManager->getOption('dialogs.default.modal', ''))) |
136
|
|
|
{ |
137
|
|
|
$this->aLibrariesInUse[] = $sName; |
138
|
|
|
$xDialogFacade->setModalLibrary($sName); |
139
|
|
|
} |
140
|
|
|
// Get the configured message library |
141
|
|
|
if(($sName = $this->xConfigManager->getOption('dialogs.default.message', ''))) |
142
|
|
|
{ |
143
|
|
|
$this->aLibrariesInUse[] = $sName; |
144
|
|
|
$xDialogFacade->setMessageLibrary($sName); |
145
|
|
|
} |
146
|
|
|
// Get the configured question library |
147
|
|
|
if(($sName = $this->xConfigManager->getOption('dialogs.default.question', ''))) |
148
|
|
|
{ |
149
|
|
|
$this->aLibrariesInUse[] = $sName; |
150
|
|
|
$xDialogFacade->setQuestionLibrary($sName); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritDoc |
156
|
|
|
*/ |
157
|
|
|
public function getName(): string |
158
|
|
|
{ |
159
|
|
|
return self::NAME; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the value of a config option |
164
|
|
|
* |
165
|
|
|
* @param string $sName The option name |
166
|
|
|
* @param mixed $xDefault The default value, to be returned if the option is not defined |
167
|
|
|
* |
168
|
|
|
* @return mixed |
169
|
|
|
*/ |
170
|
|
|
public function getOption(string $sName, $xDefault = null) |
171
|
|
|
{ |
172
|
|
|
return $this->xConfigManager->getOption($sName, $xDefault); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Check the presence of a config option |
177
|
|
|
* |
178
|
|
|
* @param string $sName The option name |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function hasOption(string $sName): bool |
183
|
|
|
{ |
184
|
|
|
return $this->xConfigManager->hasOption($sName); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the names of the options matching a given prefix |
189
|
|
|
* |
190
|
|
|
* @param string $sPrefix The prefix to match |
191
|
|
|
* |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
|
|
public function getOptionNames(string $sPrefix): array |
195
|
|
|
{ |
196
|
|
|
return $this->xConfigManager->getOptionNames($sPrefix); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Render a template |
201
|
|
|
* |
202
|
|
|
* @param string $sTemplate The name of template to be rendered |
203
|
|
|
* @param array $aVars The template vars |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function render(string $sTemplate, array $aVars = []): string |
208
|
|
|
{ |
209
|
|
|
return $this->xTemplateEngine->render($sTemplate, $aVars); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @inheritDoc |
214
|
|
|
*/ |
215
|
|
|
public function getHash(): string |
216
|
|
|
{ |
217
|
|
|
// The version number is used as hash |
218
|
|
|
return '3.1.0'; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Register the javascript libraries adapters in the DI container. |
223
|
|
|
* |
224
|
|
|
* @param string $sName |
225
|
|
|
* @param string $sClass |
226
|
|
|
* |
227
|
|
|
* @return void |
228
|
|
|
*/ |
229
|
|
|
protected function registerLibrary(string $sName, string $sClass) |
230
|
|
|
{ |
231
|
|
|
// Register the library in the DI container |
232
|
|
|
$this->di->set($sName, function() use($sName, $sClass) { |
233
|
|
|
$xLibrary = new $sClass; |
234
|
|
|
$xLibrary->init($sName, $this); |
235
|
|
|
return $xLibrary; |
236
|
|
|
}); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Register the javascript libraries adapters in the DI container. |
241
|
|
|
* |
242
|
|
|
* @return void |
243
|
|
|
*/ |
244
|
|
|
protected function registerLibraries() |
245
|
|
|
{ |
246
|
|
|
foreach($this->aLibraries as $sName => $sClass) |
247
|
|
|
{ |
248
|
|
|
$this->registerLibrary($sName, $sClass); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Register the javascript libraries adapters in the DI container. |
254
|
|
|
* |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
|
|
protected function registerClasses() |
258
|
|
|
{ |
259
|
|
|
$aLibraries = $this->xConfigManager->getOptionNames('dialogs.classes'); |
260
|
|
|
foreach($aLibraries as $sShortName => $sFullName) |
261
|
|
|
{ |
262
|
|
|
$this->registerLibrary($sShortName, $this->xConfigManager->getOption($sFullName)); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get a library adapter by its name. |
268
|
|
|
* |
269
|
|
|
* @param string $sName The name of the library adapter |
270
|
|
|
* |
271
|
|
|
* @return Library|null |
272
|
|
|
*/ |
273
|
|
|
public function getLibrary(string $sName): ?Library |
274
|
|
|
{ |
275
|
|
|
return $this->di->h($sName) ? $this->di->g($sName) : null; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Set the library to use for the next call. |
280
|
|
|
* |
281
|
|
|
* @param string $sLibrary The name of the library |
282
|
|
|
* |
283
|
|
|
* @return DialogPlugin |
284
|
|
|
*/ |
285
|
|
|
public function with(string $sLibrary): DialogPlugin |
286
|
|
|
{ |
287
|
|
|
$this->sNextLibrary = $sLibrary; |
288
|
|
|
return $this; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get the library adapter to use for modals. |
293
|
|
|
* |
294
|
|
|
* @return ModalInterface|null |
295
|
|
|
*/ |
296
|
|
|
protected function getModalLibrary(): ?ModalInterface |
297
|
|
|
{ |
298
|
|
|
$xLibrary = $this->xDialogFacade->getModalLibrary($this->xResponse, $this->sNextLibrary); |
299
|
|
|
$this->sNextLibrary = ''; |
300
|
|
|
return $xLibrary; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Get the library adapter to use for messages. |
305
|
|
|
* |
306
|
|
|
* @return MessageInterface|null |
307
|
|
|
*/ |
308
|
|
|
protected function getMessageLibrary(): ?MessageInterface |
309
|
|
|
{ |
310
|
|
|
$xLibrary = $this->xDialogFacade->getMessageLibrary(false, $this->xResponse, $this->sNextLibrary); |
311
|
|
|
$this->sNextLibrary = ''; |
312
|
|
|
return $xLibrary; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Get the library adapter to use for question. |
317
|
|
|
* |
318
|
|
|
* @return QuestionInterface|null |
319
|
|
|
*/ |
320
|
|
|
protected function getQuestionLibrary(): ?QuestionInterface |
321
|
|
|
{ |
322
|
|
|
$xLibrary = $this->xDialogFacade->getQuestionLibrary($this->xResponse, $this->sNextLibrary); |
323
|
|
|
$this->sNextLibrary = ''; |
324
|
|
|
return $xLibrary; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Get the list of library adapters that are present in the configuration. |
329
|
|
|
* |
330
|
|
|
* @return array |
331
|
|
|
*/ |
332
|
|
|
protected function getLibrariesInUse(): array |
333
|
|
|
{ |
334
|
|
|
$aNames = array_merge($this->aLibrariesInUse, |
335
|
|
|
$this->xConfigManager->getOption('dialogs.libraries', [])); |
336
|
|
|
$aLibraries = []; |
337
|
|
|
foreach($aNames as $sName) |
338
|
|
|
{ |
339
|
|
|
if(($xLibrary = $this->getLibrary($sName))) |
340
|
|
|
{ |
341
|
|
|
$aLibraries[$xLibrary->getName()] = $xLibrary; |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
return $aLibraries; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @inheritDoc |
349
|
|
|
*/ |
350
|
|
|
public function getJs(): string |
351
|
|
|
{ |
352
|
|
|
return array_reduce($this->getLibrariesInUse(), function($sCode, $xLibrary) { |
353
|
|
|
return $sCode . $xLibrary->getJs() . "\n\n"; |
354
|
|
|
}, ''); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @inheritDoc |
359
|
|
|
*/ |
360
|
|
|
public function getCss(): string |
361
|
|
|
{ |
362
|
|
|
return array_reduce($this->getLibrariesInUse(), function($sCode, $xLibrary) { |
363
|
|
|
return $sCode . $xLibrary->getCss() . "\n\n"; |
364
|
|
|
}, ''); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @inheritDoc |
369
|
|
|
*/ |
370
|
|
|
public function getScript(): string |
371
|
|
|
{ |
372
|
|
|
return array_reduce($this->getLibrariesInUse(), function($sCode, $xLibrary) { |
373
|
|
|
return $sCode . $xLibrary->getScript() . "\n\n"; |
374
|
|
|
}, "jaxon.dialogs = {};\n"); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @inheritDoc |
379
|
|
|
*/ |
380
|
|
|
public function getReadyScript(): string |
381
|
|
|
{ |
382
|
|
|
return array_reduce($this->getLibrariesInUse(), function($sCode, $xLibrary) { |
383
|
|
|
return $sCode . $xLibrary->getReadyScript() . "\n\n"; |
384
|
|
|
}, ''); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Show a modal dialog. |
389
|
|
|
* |
390
|
|
|
* It is a function of the Jaxon\Dialogs\Contracts\ModalInterface interface. |
391
|
|
|
* |
392
|
|
|
* @param string $sTitle The title of the dialog |
393
|
|
|
* @param string $sContent The content of the dialog |
394
|
|
|
* @param array $aButtons The buttons of the dialog |
395
|
|
|
* @param array $aOptions The options of the dialog |
396
|
|
|
* |
397
|
|
|
* Each button is an array containin the following entries: |
398
|
|
|
* - title: the text to be printed in the button |
399
|
|
|
* - class: the CSS class of the button |
400
|
|
|
* - click: the javascript function to be called when the button is clicked |
401
|
|
|
* If the click value is set to "close", then the buttons closes the dialog. |
402
|
|
|
* |
403
|
|
|
* The content of the $aOptions depends on the javascript library in use. |
404
|
|
|
* Check their specific documentation for more information. |
405
|
|
|
* |
406
|
|
|
* @return void |
407
|
|
|
*/ |
408
|
|
|
public function show(string $sTitle, string $sContent, array $aButtons = [], array $aOptions = []) |
409
|
|
|
{ |
410
|
|
|
$this->getModalLibrary()->show($sTitle, $sContent, $aButtons, $aOptions); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Show a modal dialog. |
415
|
|
|
* |
416
|
|
|
* It is another name for the show() function. |
417
|
|
|
* |
418
|
|
|
* @param string $sTitle The title of the dialog |
419
|
|
|
* @param string $sContent The content of the dialog |
420
|
|
|
* @param array $aButtons The buttons of the dialog |
421
|
|
|
* @param array $aOptions The options of the dialog |
422
|
|
|
* |
423
|
|
|
* @return void |
424
|
|
|
*/ |
425
|
|
|
public function modal(string $sTitle, string $sContent, array $aButtons = [], array $aOptions = []) |
426
|
|
|
{ |
427
|
|
|
$this->show($sTitle, $sContent, $aButtons, $aOptions); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Hide the modal dialog. |
432
|
|
|
* |
433
|
|
|
* It is a function of the Jaxon\Dialogs\Contracts\ModalInterface interface. |
434
|
|
|
* |
435
|
|
|
* @return void |
436
|
|
|
*/ |
437
|
|
|
public function hide() |
438
|
|
|
{ |
439
|
|
|
$this->getModalLibrary()->hide(); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* @inheritDoc |
444
|
|
|
*/ |
445
|
|
|
public function success(string $sMessage, string $sTitle = ''): string |
446
|
|
|
{ |
447
|
|
|
return $this->getMessageLibrary()->success($sMessage, $sTitle); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* @inheritDoc |
452
|
|
|
*/ |
453
|
|
|
public function info(string $sMessage, string $sTitle = ''): string |
454
|
|
|
{ |
455
|
|
|
return $this->getMessageLibrary()->info($sMessage, $sTitle); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* @inheritDoc |
460
|
|
|
*/ |
461
|
|
|
public function warning(string $sMessage, string $sTitle = ''): string |
462
|
|
|
{ |
463
|
|
|
return $this->getMessageLibrary()->warning($sMessage, $sTitle); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* @inheritDoc |
468
|
|
|
*/ |
469
|
|
|
public function error(string $sMessage, string $sTitle = ''): string |
470
|
|
|
{ |
471
|
|
|
return $this->getMessageLibrary()->error($sMessage, $sTitle); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* @inheritDoc |
476
|
|
|
*/ |
477
|
|
|
public function confirm(string $sQuestion, string $sYesScript, string $sNoScript): string |
478
|
|
|
{ |
479
|
|
|
return $this->getQuestionLibrary()->confirm($sQuestion, $sYesScript, $sNoScript); |
480
|
|
|
} |
481
|
|
|
} |
482
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths