Passed
Push — master ( 0c5836...7e4fb9 )
by Thierry
02:39
created

BootboxLibrary::confirm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * DialogLibraryInterface.php - Adapter for the Bootbox library.
5
 *
6
 * @package jaxon-dialogs
7
 * @author Thierry Feuzeu <[email protected]>
8
 * @copyright 2016 Thierry Feuzeu <[email protected]>
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
10
 * @link https://github.com/jaxon-php/jaxon-dialogs
11
 */
12
13
namespace Jaxon\Dialogs\Library\Bootbox;
14
15
use Jaxon\Ui\Dialog\Library\AbstractDialogLibrary;
0 ignored issues
show
Bug introduced by
The type Jaxon\Ui\Dialog\Library\AbstractDialogLibrary was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Jaxon\Ui\Dialog\ModalInterface;
0 ignored issues
show
Bug introduced by
The type Jaxon\Ui\Dialog\ModalInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Jaxon\Ui\Dialog\MessageInterface;
0 ignored issues
show
Bug introduced by
The type Jaxon\Ui\Dialog\MessageInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Jaxon\Ui\Dialog\QuestionInterface;
0 ignored issues
show
Bug introduced by
The type Jaxon\Ui\Dialog\QuestionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
class BootboxLibrary extends AbstractDialogLibrary implements ModalInterface, MessageInterface, QuestionInterface
21
{
22
    /**
23
     * @const The library name
24
     */
25
    const NAME = 'bootbox';
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function getName(): string
31
    {
32
        return self::NAME;
33
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38
    public function getSubdir(): string
39
    {
40
        return 'bootbox';
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function getVersion(): string
47
    {
48
        return '4.3.0';
49
    }
50
51
    /**
52
     * The id of the HTML container block
53
     *
54
     * @return string
55
     */
56
    protected function getContainer(): string
57
    {
58
        $sContainer = 'bootbox-container';
59
        if($this->xHelper->hasOption('dom.container'))
60
        {
61
            $sContainer = $this->xHelper->getOption('dom.container');
62
        }
63
        return $sContainer;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function getJs(): string
70
    {
71
        return $this->xHelper->getJsCode('bootbox.min.js');
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function getScript(): string
78
    {
79
        return $this->xHelper->render('bootbox/alert.js');
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function getReadyScript(): string
86
    {
87
        return $this->xHelper->render('bootbox/ready.js.php', ['container' => $this->getContainer()]);
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function show(string $sTitle, string $sContent, array $aButtons, array $aOptions = [])
94
    {
95
        // ModalInterface container
96
        $sContainer = $this->getContainer();
97
98
        // Set the value of the max width, if there is no value defined
99
        $width = $aOptions['width'] ?? 600;
100
        $html = $this->xHelper->render('bootbox/dialog.html',
101
            ['title' => $sTitle, 'content' => $sContent, 'buttons' => $aButtons]);
102
103
        // Assign dialog content
104
        $this->response()->assign($sContainer, 'innerHTML', $html);
105
        $this->response()->script("$('.modal-dialog').css('width', '{$width}px')");
106
        $this->response()->script("$('#styledModal').modal('show')");
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function hide()
113
    {
114
        $this->response()->script("$('#styledModal').modal('hide')");
115
    }
116
117
    /**
118
     * Print an alert message.
119
     *
120
     * @param string $sContent The text of the message
121
     * @param string $sTitle The title of the message
122
     * @param string $sType The type of the message
123
     *
124
     * @return string
125
     */
126
    protected function alert(string $sContent, string $sTitle, string $sType): string
127
    {
128
        if($this->returnCode())
129
        {
130
            return "jaxon.dialogs.bootbox.alert('" . $sType . "'," . $sContent . ",'" . $sTitle . "')";
131
        }
132
        $this->addCommand(['cmd' => 'bootbox'], ['type' => $sType, 'content' => $sContent, 'title' => $sTitle]);
133
        return '';
134
    }
135
136
    /**
137
     * @inheritDoc
138
     */
139
    public function success(string $sMessage, string $sTitle = ''): string
140
    {
141
        return $this->alert($sMessage, $sTitle, 'success');
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147
    public function info(string $sMessage, string $sTitle = ''): string
148
    {
149
        return $this->alert($sMessage, $sTitle, 'info');
150
    }
151
152
    /**
153
     * @inheritDoc
154
     */
155
    public function warning(string $sMessage, string $sTitle = ''): string
156
    {
157
        return $this->alert($sMessage, $sTitle, 'warning');
158
    }
159
160
    /**
161
     * @inheritDoc
162
     */
163
    public function error(string $sMessage, string $sTitle = ''): string
164
    {
165
        return $this->alert($sMessage, $sTitle, 'danger');
166
    }
167
168
    /**
169
     * @inheritDoc
170
     */
171
    public function confirm(string $sQuestion, string $sYesScript, string $sNoScript): string
172
    {
173
        $sTitle = $this->xHelper->getQuestionTitle();
174
        return empty($sNoScript) ?
175
            "jaxon.dialogs.bootbox.confirm(" . $sQuestion . ",'" . $sTitle . "',function(){" . $sYesScript . ";})" :
176
            "jaxon.dialogs.bootbox.confirm(" . $sQuestion . ",'" . $sTitle . "',function(){" . $sYesScript .
177
                ";},function(){" . $sNoScript . ";})";
178
    }
179
}
180