JQueryConfirmLibrary::alert()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * JQueryConfirmLibrary.php
5
 *
6
 * Adapter for the JQuery-Confirm 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\JQueryConfirm;
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 JQueryConfirmLibrary implements ModalInterface, MessageInterface, QuestionInterface
23
{
24
    use DialogLibraryTrait;
25
26
    /**
27
     * @const The library name
28
     */
29
    const NAME = 'jconfirm';
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 'jquery-confirm';
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function getVersion(): string
51
    {
52
        return '3.3.0';
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function getJs(): string
59
    {
60
        return $this->helper()->getJsCode('jquery-confirm.min.js');
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function getCss(): string
67
    {
68
        return $this->helper()->getCssCode('jquery-confirm.min.css') . '
69
<style>
70
    .jconfirm .jconfirm-box div.jconfirm-content-pane {
71
        margin-top: 15px;
72
    }
73
</style>
74
';
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function getScript(): string
81
    {
82
        return $this->helper()->render('jqueryconfirm/alert.js');
83
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88
    public function getReadyScript(): string
89
    {
90
        return $this->helper()->render('jqueryconfirm/ready.js.php');
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    public function show(string $sTitle, string $sMessage, array $aButtons, array $aOptions = [])
97
    {
98
        $aOptions['title'] = $sTitle;
99
        $aOptions['content'] = $sMessage;
100
        $aOptions['buttons'] = [];
101
        if(!array_key_exists('boxWidth', $aOptions))
102
        {
103
            $aOptions['useBootstrap'] = false;
104
            $aOptions['boxWidth'] = '600';
105
        }
106
        $ind = 0;
107
        foreach($aButtons as $button)
108
        {
109
            $_button = [
110
                'text' => $button['title'],
111
                'btnClass' => $button['class'],
112
                'action' => $button['click'],
113
            ];
114
            // Optional attributes
115
            foreach($button as $attr => $value)
116
            {
117
                if(!in_array($attr, ['title', 'class', 'click']))
118
                {
119
                    $_button[$attr] = $value;
120
                }
121
            }
122
            $aOptions['buttons']['btn' . $ind++] = $_button;
123
        }
124
        // Show dialog
125
        $this->addCommand(array('cmd' => 'jconfirm.show'), $aOptions);
126
    }
127
128
    /**
129
     * @inheritDoc
130
     */
131
    public function hide()
132
    {
133
        // Hide dialog
134
        $this->addCommand(array('cmd' => 'jconfirm.hide'), array());
135
    }
136
137
    /**
138
     * Print an alert message.
139
     *
140
     * @param string $sMessage The text of the message
141
     * @param string $sTitle The title of the message
142
     * @param string $sType The type of the message
143
     * @param string $sIcon The icon of the message
144
     *
145
     * @return string
146
     */
147
    protected function alert(string $sMessage, string $sTitle, string $sType, string $sIcon): string
148
    {
149
        if($this->returnCode())
150
        {
151
            return "$.alert({content:" . $sMessage . ", title:'" . $sTitle .
152
                "', type:'" . $sType . "', icon:'" . $sIcon . "'})";
153
        }
154
        $this->addCommand(array('cmd' => 'jconfirm.alert'),
155
            ['content' => $sMessage, 'title' => $sTitle, 'type' => $sType, 'icon' => $sIcon]);
156
        return '';
157
    }
158
159
    /**
160
     * @inheritDoc
161
     */
162
    public function success(string $sMessage, string $sTitle = ''): string
163
    {
164
        return $this->alert($sMessage, $sTitle, 'green', 'fa fa-success');
165
    }
166
167
    /**
168
     * @inheritDoc
169
     */
170
    public function info(string $sMessage, string $sTitle = ''): string
171
    {
172
        return $this->alert($sMessage, $sTitle, 'blue', 'fa fa-info');
173
    }
174
175
    /**
176
     * @inheritDoc
177
     */
178
    public function warning(string $sMessage, string $sTitle = ''): string
179
    {
180
        return $this->alert($sMessage, $sTitle, 'orange', 'fa fa-warning');
181
    }
182
183
    /**
184
     * @inheritDoc
185
     */
186
    public function error(string $sMessage, string $sTitle = ''): string
187
    {
188
        return $this->alert($sMessage, $sTitle, 'red', 'fa fa-error');
189
    }
190
191
    /**
192
     * @inheritDoc
193
     */
194
    public function confirm(string $sQuestion, string $sYesScript, string $sNoScript): string
195
    {
196
        $sTitle = $this->helper()->getQuestionTitle();
197
        if(!$sNoScript)
198
        {
199
            return "jaxon.dialogs.jconfirm.confirm(" . $sQuestion . ",'" .
200
                $sTitle . "',function(){" . $sYesScript . ";})";
201
        }
202
        return "jaxon.dialogs.jconfirm.confirm(" . $sQuestion . ",'" . $sTitle .
203
            "',function(){" . $sYesScript . ";},function(){" . $sNoScript . ";})";
204
    }
205
}
206