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

BootstrapLibrary::error()   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 2
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
     * The constructor
24
     */
25
    public function __construct()
26
    {
27
        parent::__construct('bootstrap-dialog', '1.35.3');
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function getJs(): string
34
    {
35
        return $this->xHelper->getJsCode('bootstrap-dialog.min.js');
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function getCss(): string
42
    {
43
        return $this->xHelper->getCssCode('bootstrap-dialog.min.css');
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function getScript(): string
50
    {
51
        return $this->xHelper->render('bootstrap/alert.js');
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function getReadyScript(): string
58
    {
59
        return $this->xHelper->render('bootstrap/ready.js.php');
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function show(string $sTitle, string $sContent, array $aButtons, array $aOptions = [])
66
    {
67
        // Fill the options array with the parameters
68
        $aOptions['title'] = $sTitle;
69
        $aOptions['message'] = $sContent;
70
        $aOptions['buttons'] = [];
71
        foreach($aButtons as $button)
72
        {
73
            $_button = [
74
                'label' => $button['title'],
75
                'cssClass' => $button['class'],
76
                'action' => $button['click'],
77
            ];
78
            // Optional attributes
79
            foreach($button as $attr => $value)
80
            {
81
                if(!in_array($attr, ['title', 'class', 'click']))
82
                {
83
                    $_button[$attr] = $value;
84
                }
85
            }
86
            $aOptions['buttons'][] = $_button;
87
        }
88
        // Turn the value of the nl2br option to false, because it alters form rendering.
89
        if(!array_key_exists('nl2br', $aOptions))
90
        {
91
            $aOptions['nl2br'] = false;
92
        }
93
        // Show the modal dialog
94
        $this->addCommand(array('cmd' => 'bootstrap.show'), $aOptions);
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    public function hide()
101
    {
102
        // Hide the modal dialog
103
        $this->addCommand(array('cmd' => 'bootstrap.hide'), array());
104
    }
105
106
    /**
107
     * Print an alert message.
108
     *
109
     * @param string $sMessage The text of the message
110
     * @param string $sTitle The title of the message
111
     * @param string $sType The type of the message
112
     *
113
     * @return string
114
     */
115
    protected function alert(string $sMessage, string $sTitle, string $sType): string
116
    {
117
        if($this->returnCode())
118
        {
119
            $aDataTypes = [
120
                'success' => 'BootstrapDialog.TYPE_SUCCESS',
121
                'info' => 'BootstrapDialog.TYPE_INFO',
122
                'warning' => 'BootstrapDialog.TYPE_WARNING',
123
                'danger' => 'BootstrapDialog.TYPE_DANGER',
124
            ];
125
            $sType = $aDataTypes[$sType];
126
            if(($sTitle))
127
            {
128
                return "BootstrapDialog.alert({message:" . $sMessage . ", title:'" . $sTitle . "', type:" . $sType . "})";
129
            }
130
            else
131
            {
132
                return "BootstrapDialog.alert({message:" . $sMessage . ", type:" . $sType . "})";
133
            }
134
        }
135
        $aOptions = array('message' => $sMessage, 'type' => $sType);
136
        if(($sTitle))
137
        {
138
            $aOptions['title'] = $sTitle;
139
        }
140
        // Show the alert
141
        $this->addCommand(array('cmd' => 'bootstrap.alert'), $aOptions);
142
        return '';
143
    }
144
145
    /**
146
     * @inheritDoc
147
     */
148
    public function success(string $sMessage, string $sTitle = ''): string
149
    {
150
        return $this->alert($sMessage, $sTitle, 'success');
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156
    public function info(string $sMessage, string $sTitle = ''): string
157
    {
158
        return $this->alert($sMessage, $sTitle, 'info');
159
    }
160
161
    /**
162
     * @inheritDoc
163
     */
164
    public function warning(string $sMessage, string $sTitle = ''): string
165
    {
166
        return $this->alert($sMessage, $sTitle, 'warning');
167
    }
168
169
    /**
170
     * @inheritDoc
171
     */
172
    public function error(string $sMessage, string $sTitle = ''): string
173
    {
174
        return $this->alert($sMessage, $sTitle, 'danger');
175
    }
176
177
    /**
178
     * @inheritDoc
179
     */
180
    public function confirm(string $sQuestion, string $sYesScript, string $sNoScript): string
181
    {
182
        $sTitle = $this->xHelper->getQuestionTitle();
183
        if(!$sNoScript)
184
        {
185
            return "jaxon.dialogs.bootstrap.confirm(" . $sQuestion . ",'" .
186
                $sTitle . "',function(){" . $sYesScript . ";})";
187
        }
188
        return "jaxon.dialogs.bootstrap.confirm(" . $sQuestion . ",'" . $sTitle .
189
            "',function(){" . $sYesScript . ";},function(){" . $sNoScript . ";})";
190
    }
191
}
192