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

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