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

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