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

CuteAlertLibrary::alert()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 13
rs 10
1
<?php
2
3
/**
4
 * CuteAlertLibrary.php
5
 *
6
 * Adapter for the CuteAlert 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\CuteAlert;
16
17
use Jaxon\App\Dialog\Library\DialogLibraryTrait;
18
use Jaxon\App\Dialog\MessageInterface;
19
use Jaxon\App\Dialog\QuestionInterface;
20
21
class CuteAlertLibrary implements MessageInterface, QuestionInterface
22
{
23
    use DialogLibraryTrait;
24
25
    /**
26
     * @const The library name
27
     */
28
    const NAME = 'cute';
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function getName(): string
34
    {
35
        return self::NAME;
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function getUri(): string
42
    {
43
        return 'https://cdn.jsdelivr.net/gh/gustavosmanc/cute-alert';
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function getJs(): string
50
    {
51
        return $this->helper()->getJsCode('cute-alert.min.js');
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function getCss(): string
58
    {
59
        return $this->helper()->getCssCode('style.min.css');
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function getScript(): string
66
    {
67
        return $this->helper()->render('cutealert/alert.js');
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function getReadyScript(): string
74
    {
75
        return $this->helper()->render('cutealert/ready.js.php');
76
    }
77
78
    /**
79
     * Print an alert message.
80
     *
81
     * @param string $sContent The text of the message
82
     * @param string $sTitle The title of the message
83
     * @param string $sType The type of the message
84
     *
85
     * @return string
86
     */
87
    protected function alert(string $sContent, string $sTitle, string $sType): string
88
    {
89
        if(!$sTitle)
90
        {
91
            $sTitle = '&nbsp;';
92
        }
93
        if($this->returnCode())
94
        {
95
            return "cuteAlert({message:" . $sContent . ", title:'" . $sTitle . "', type:'" . $sType . "'})";
96
        }
97
98
        $this->addCommand(['cmd' => 'cutealert.alert'], ['message' => $sContent, 'title' => $sTitle, 'type' => $sType]);
99
        return '';
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function success(string $sMessage, string $sTitle = ''): string
106
    {
107
        return $this->alert($sMessage, $sTitle, 'success');
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function info(string $sMessage, string $sTitle = ''): string
114
    {
115
        return $this->alert($sMessage, $sTitle, 'info');
116
    }
117
118
    /**
119
     * @inheritDoc
120
     */
121
    public function warning(string $sMessage, string $sTitle = ''): string
122
    {
123
        return $this->alert($sMessage, $sTitle, 'warning');
124
    }
125
126
    /**
127
     * @inheritDoc
128
     */
129
    public function error(string $sMessage, string $sTitle = ''): string
130
    {
131
        return $this->alert($sMessage, $sTitle, 'error');
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137
    public function confirm(string $sQuestion, string $sYesScript, string $sNoScript): string
138
    {
139
        $sTitle = $this->helper()->getQuestionTitle();
140
141
        return "jaxon.dialogs.cutealert.confirm(" . $sQuestion . ",'" . $sTitle . "',function(){" . $sYesScript .
142
            ($sNoScript ? ";},function(){" . $sNoScript . ";})" : ";})");
143
    }
144
}
145