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