BootstrapLibrary::getReadyScript()   A
last analyzed

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