SweetAlertLibrary::getVersion()   A
last analyzed

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
 * SweetAlertLibrary.php
5
 *
6
 * Adapter for the SweetAlert 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\SweetAlert;
16
17
use Jaxon\App\Dialog\Library\DialogLibraryTrait;
18
use Jaxon\App\Dialog\MessageInterface;
19
use Jaxon\App\Dialog\QuestionInterface;
20
21
class SweetAlertLibrary implements MessageInterface, QuestionInterface
22
{
23
    use DialogLibraryTrait;
24
25
    /**
26
     * @const The library name
27
     */
28
    const NAME = 'sweetalert';
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 'sweetalert';
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function getVersion(): string
50
    {
51
        return '1.1.1';
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function getJs(): string
58
    {
59
        return $this->helper()->getJsCode('sweetalert.min.js');
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function getCss(): string
66
    {
67
        return $this->helper()->getCssCode('sweetalert.css');
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function getScript(): string
74
    {
75
        return $this->helper()->render('sweetalert/alert.js');
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function getReadyScript(): string
82
    {
83
        return $this->helper()->render('sweetalert/ready.js.php', [
84
            'options' =>  $this->helper()->getOptionScript('jaxon.dialogs.swal.options.', 'options.')
85
        ]);
86
    }
87
88
    /**
89
     * Print an alert message.
90
     *
91
     * @param string $sMessage The text of the message
92
     * @param string $sTitle The title of the message
93
     * @param string $sType The type of the message
94
     *
95
     * @return string
96
     */
97
    protected function alert(string $sMessage, string $sTitle, string $sType): string
98
    {
99
        if($this->returnCode())
100
        {
101
            return "swal({text:" . $sMessage . ", title:'" . $sTitle . "', type:'" . $sType . "'})";
102
        }
103
        $aOptions = ['text' => $sMessage, 'title' => '', 'type' => $sType];
104
        if(($sTitle))
105
        {
106
            $aOptions['title'] = $sTitle;
107
        }
108
        // Show the alert
109
        $this->addCommand(['cmd' => 'sweetalert.alert'], $aOptions);
110
        return '';
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function success(string $sMessage, string $sTitle = ''): string
117
    {
118
        return $this->alert($sMessage, $sTitle, 'success');
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function info(string $sMessage, string $sTitle = ''): string
125
    {
126
        return $this->alert($sMessage, $sTitle, 'info');
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function warning(string $sMessage, string $sTitle = ''): string
133
    {
134
        return $this->alert($sMessage, $sTitle, 'warning');
135
    }
136
137
    /**
138
     * @inheritDoc
139
     */
140
    public function error(string $sMessage, string $sTitle = ''): string
141
    {
142
        return $this->alert($sMessage, $sTitle, 'error');
143
    }
144
145
    /**
146
     * @inheritDoc
147
     */
148
    public function confirm(string $sQuestion, string $sYesScript, string $sNoScript): string
149
    {
150
        $sTitle = $this->helper()->getQuestionTitle();
151
        if(!$sNoScript)
152
        {
153
            return "jaxon.dialogs.swal.confirm(" . $sQuestion . ",'" . $sTitle . "',function(){" . $sYesScript . ";})";
154
        }
155
        else
156
        {
157
            return "jaxon.dialogs.swal.confirm(" . $sQuestion . ",'" . $sTitle . "',function(){" . $sYesScript . ";},function(){" . $sNoScript . ";})";
158
        }
159
    }
160
}
161