Passed
Push — master ( 2cb546...9b3a22 )
by Thierry
08:13
created

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