Passed
Push — master ( c9d535...a1a723 )
by Thierry
10:07 queued 02:14
created

XDialogLibrary::alert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * XDialogLibrary.php
5
 *
6
 * Adapter for the XDialog library.
7
 *
8
 * @package jaxon-dialogs
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 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\XDialog;
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 XDialogLibrary implements ModalInterface, MessageInterface, QuestionInterface
23
{
24
    use DialogLibraryTrait;
25
26
    /**
27
     * @const The library name
28
     */
29
    const NAME = 'xdialog';
30
31
    /**
32
     * @inheritDoc
33
     */
34
    public function getName(): string
35
    {
36
        return self::NAME;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function getUri(): string
43
    {
44
        return 'https://cdn.jsdelivr.net/gh/xxjapp/xdialog@3';
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function getCss(): string
51
    {
52
        return $this->helper()->getCssCode('xdialog.min.css');
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function getJs(): string
59
    {
60
        return $this->helper()->getJsCode('xdialog.min.js');
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function getScript(): string
67
    {
68
        return $this->helper()->render('xdialog/alert.js');
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function getReadyScript(): string
75
    {
76
        return $this->helper()->render('xdialog/ready.js.php');
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function show(string $sTitle, string $sContent, array $aButtons, array $aOptions = [])
83
    {
84
        $aOptions['title'] = $sTitle;
85
        $aOptions['body'] = $sContent;
86
        $aOptions['buttons'] = [];
87
        foreach($aButtons as $aButton)
88
        {
89
            if($aButton['click'] === 'close')
90
            {
91
                $aOptions['buttons']['cancel'] = $aButton['title'];
92
                $aOptions['oncancel'] = 'jaxon.dialogs.xdialog.hide()';
93
            }
94
            else
95
            {
96
                $aOptions['buttons']['ok'] = $aButton['title'];
97
                $aOptions['onok'] = $aButton['click'];
98
            }
99
        }
100
101
        // Assign dialog content
102
        $this->addCommand(['cmd' => 'xdialog.show'], $aOptions);
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108
    public function hide()
109
    {
110
        $this->addCommand(['cmd' => 'xdialog.hide'], []);
111
    }
112
113
    /**
114
     * Print an alert message.
115
     *
116
     * @param string $sContent The text of the message
117
     * @param string $sTitle The title of the message
118
     * @param string $sType The type of the message
119
     *
120
     * @return string
121
     */
122
    protected function alert(string $sContent, string $sTitle, string $sType): string
123
    {
124
        if($this->returnCode())
125
        {
126
            return "jaxon.dialogs.xdialog.$sType(" . $sContent . "'" . $sTitle . "')";
127
        }
128
129
        $this->addCommand(['cmd' => "xdialog.$sType"], ['body' => $sContent, 'title' => $sTitle]);
130
        return '';
131
    }
132
133
    /**
134
     * @inheritDoc
135
     */
136
    public function success(string $sMessage, string $sTitle = ''): string
137
    {
138
        return $this->alert($sMessage, $sTitle, 'success');
139
    }
140
141
    /**
142
     * @inheritDoc
143
     */
144
    public function info(string $sMessage, string $sTitle = ''): string
145
    {
146
        return $this->alert($sMessage, $sTitle, 'info');
147
    }
148
149
    /**
150
     * @inheritDoc
151
     */
152
    public function warning(string $sMessage, string $sTitle = ''): string
153
    {
154
        return $this->alert($sMessage, $sTitle, 'warning');
155
    }
156
157
    /**
158
     * @inheritDoc
159
     */
160
    public function error(string $sMessage, string $sTitle = ''): string
161
    {
162
        return $this->alert($sMessage, $sTitle, 'error');
163
    }
164
165
    /**
166
     * @inheritDoc
167
     */
168
    public function confirm(string $sQuestion, string $sYesScript, string $sNoScript): string
169
    {
170
        $sTitle = $this->helper()->getQuestionTitle();
171
172
        return empty($sNoScript) ?
173
            "jaxon.dialogs.xdialog.confirm(" . $sQuestion . ",'" . $sTitle . "',function(){" . $sYesScript . ";})" :
174
            "jaxon.dialogs.xdialog.confirm(" . $sQuestion . ",'" . $sTitle . "',function(){" . $sYesScript .
175
                ";},function(){" . $sNoScript . ";})";
176
    }
177
}
178