Passed
Push — master ( 7e4fb9...90ec13 )
by Thierry
02:59 queued 30s
created

BootstrapLibrary::getSubdir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * DialogLibraryInterface.php - Adapter for the Bootstrap 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\Bootstrap;
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 BootstrapLibrary extends AbstractDialogLibrary implements ModalInterface, MessageInterface, QuestionInterface
21
{
22
    /**
23
     * @const The library name
24
     */
25
    const NAME = 'bootstrap';
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 'bootstrap-dialog';
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function getVersion(): string
47
    {
48
        return '1.35.3';
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function getJs(): string
55
    {
56
        return $this->xHelper->getJsCode('bootstrap-dialog.min.js');
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function getCss(): string
63
    {
64
        return $this->xHelper->getCssCode('bootstrap-dialog.min.css');
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70
    public function getScript(): string
71
    {
72
        return $this->xHelper->render('bootstrap/alert.js');
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function getReadyScript(): string
79
    {
80
        return $this->xHelper->render('bootstrap/ready.js.php');
81
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86
    public function show(string $sTitle, string $sContent, array $aButtons, array $aOptions = [])
87
    {
88
        // Fill the options array with the parameters
89
        $aOptions['title'] = $sTitle;
90
        $aOptions['message'] = $sContent;
91
        $aOptions['buttons'] = [];
92
        foreach($aButtons as $button)
93
        {
94
            $_button = [
95
                'label' => $button['title'],
96
                'cssClass' => $button['class'],
97
                'action' => $button['click'],
98
            ];
99
            // Optional attributes
100
            foreach($button as $attr => $value)
101
            {
102
                if(!in_array($attr, ['title', 'class', 'click']))
103
                {
104
                    $_button[$attr] = $value;
105
                }
106
            }
107
            $aOptions['buttons'][] = $_button;
108
        }
109
        // Turn the value of the nl2br option to false, because it alters form rendering.
110
        if(!array_key_exists('nl2br', $aOptions))
111
        {
112
            $aOptions['nl2br'] = false;
113
        }
114
        // Show the modal dialog
115
        $this->addCommand(array('cmd' => 'bootstrap.show'), $aOptions);
116
    }
117
118
    /**
119
     * @inheritDoc
120
     */
121
    public function hide()
122
    {
123
        // Hide the modal dialog
124
        $this->addCommand(array('cmd' => 'bootstrap.hide'), array());
125
    }
126
127
    /**
128
     * Print an alert message.
129
     *
130
     * @param string $sMessage The text of the message
131
     * @param string $sTitle The title of the message
132
     * @param string $sType The type of the message
133
     *
134
     * @return string
135
     */
136
    protected function alert(string $sMessage, string $sTitle, string $sType): string
137
    {
138
        if($this->returnCode())
139
        {
140
            $aDataTypes = [
141
                'success' => 'BootstrapDialog.TYPE_SUCCESS',
142
                'info' => 'BootstrapDialog.TYPE_INFO',
143
                'warning' => 'BootstrapDialog.TYPE_WARNING',
144
                'danger' => 'BootstrapDialog.TYPE_DANGER',
145
            ];
146
            $sType = $aDataTypes[$sType];
147
            if(($sTitle))
148
            {
149
                return "BootstrapDialog.alert({message:" . $sMessage . ", title:'" . $sTitle . "', type:" . $sType . "})";
150
            }
151
            else
152
            {
153
                return "BootstrapDialog.alert({message:" . $sMessage . ", type:" . $sType . "})";
154
            }
155
        }
156
        $aOptions = array('message' => $sMessage, 'type' => $sType);
157
        if(($sTitle))
158
        {
159
            $aOptions['title'] = $sTitle;
160
        }
161
        // Show the alert
162
        $this->addCommand(array('cmd' => 'bootstrap.alert'), $aOptions);
163
        return '';
164
    }
165
166
    /**
167
     * @inheritDoc
168
     */
169
    public function success(string $sMessage, string $sTitle = ''): string
170
    {
171
        return $this->alert($sMessage, $sTitle, 'success');
172
    }
173
174
    /**
175
     * @inheritDoc
176
     */
177
    public function info(string $sMessage, string $sTitle = ''): string
178
    {
179
        return $this->alert($sMessage, $sTitle, 'info');
180
    }
181
182
    /**
183
     * @inheritDoc
184
     */
185
    public function warning(string $sMessage, string $sTitle = ''): string
186
    {
187
        return $this->alert($sMessage, $sTitle, 'warning');
188
    }
189
190
    /**
191
     * @inheritDoc
192
     */
193
    public function error(string $sMessage, string $sTitle = ''): string
194
    {
195
        return $this->alert($sMessage, $sTitle, 'danger');
196
    }
197
198
    /**
199
     * @inheritDoc
200
     */
201
    public function confirm(string $sQuestion, string $sYesScript, string $sNoScript): string
202
    {
203
        $sTitle = $this->xHelper->getQuestionTitle();
204
        if(!$sNoScript)
205
        {
206
            return "jaxon.dialogs.bootstrap.confirm(" . $sQuestion . ",'" .
207
                $sTitle . "',function(){" . $sYesScript . ";})";
208
        }
209
        return "jaxon.dialogs.bootstrap.confirm(" . $sQuestion . ",'" . $sTitle .
210
            "',function(){" . $sYesScript . ";},function(){" . $sNoScript . ";})";
211
    }
212
}
213