Passed
Push — master ( 0c5836...7e4fb9 )
by Thierry
02:39
created

SweetAlertLibrary::confirm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * DialogLibraryInterface.php - Adapter for the SweetAlert 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\Library\SweetAlert;
14
15
use Jaxon\Ui\Dialog\Library\AbstractDialogLibrary;
0 ignored issues
show
Bug introduced by
The type Jaxon\Ui\Dialog\Library\AbstractDialogLibrary was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Jaxon\Ui\Dialog\MessageInterface;
0 ignored issues
show
Bug introduced by
The type Jaxon\Ui\Dialog\MessageInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Jaxon\Ui\Dialog\QuestionInterface;
0 ignored issues
show
Bug introduced by
The type Jaxon\Ui\Dialog\QuestionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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