Completed
Push — develop ( 0d3233...1aa55d )
by John
05:06
created

Button::render()   D

Complexity

Conditions 10
Paths 24

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 42
rs 4.8196
cc 10
eloc 27
nc 24
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Alpha\View\Widget;
4
5
use Alpha\Util\Config\ConfigProvider;
6
use Alpha\Util\Security\SecurityUtils;
7
8
/**
9
 * Button HTML custom widget.
10
 *
11
 * @since 1.0
12
 *
13
 * @author John Collins <[email protected]>
14
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
15
 * @copyright Copyright (c) 2017, John Collins (founder of Alpha Framework).
16
 * All rights reserved.
17
 *
18
 * <pre>
19
 * Redistribution and use in source and binary forms, with or
20
 * without modification, are permitted provided that the
21
 * following conditions are met:
22
 *
23
 * * Redistributions of source code must retain the above
24
 *   copyright notice, this list of conditions and the
25
 *   following disclaimer.
26
 * * Redistributions in binary form must reproduce the above
27
 *   copyright notice, this list of conditions and the
28
 *   following disclaimer in the documentation and/or other
29
 *   materials provided with the distribution.
30
 * * Neither the name of the Alpha Framework nor the names
31
 *   of its contributors may be used to endorse or promote
32
 *   products derived from this software without specific
33
 *   prior written permission.
34
 *
35
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
36
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
37
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
38
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
40
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
46
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48
 * </pre>
49
 */
50
class Button
51
{
52
    /**
53
     * The Javascript action to carry out when the button is pressed.
54
     *
55
     * @var string
56
     *
57
     * @since 1.0
58
     */
59
    private $action;
60
61
    /**
62
     * The title to display on the button.
63
     *
64
     * @var string
65
     *
66
     * @since 1.0
67
     */
68
    private $title;
69
70
    /**
71
     * The HTML id attribute for the button.
72
     *
73
     * @var string
74
     *
75
     * @since 1.0
76
     */
77
    private $id;
78
79
    /**
80
     * If provided, the button will be a clickable image using this image.
81
     *
82
     * @var string
83
     *
84
     * @since 1.0
85
     */
86
    private $imgURL;
87
    
88
    /**
89
     * If provided, the Bootsrap glyphIcon to use for this button.
90
     *
91
     * @var string
92
     *
93
     * @since 3.0
94
     */
95
    private $glyphIcon;
96
97
    /**
98
     * If provided, the Bootsrap tooltip to use for this button.
99
     *
100
     * @var string
101
     *
102
     * @since 3.0
103
     */
104
    private $tooltip;
105
106
    /**
107
     * If provided, the Bootsrap tooltip position (left|right|top|bottom) to use for this button.
108
     *
109
     * @var string
110
     *
111
     * @since 3.0
112
     */
113
    private $tooltipPosition;
114
115
    /**
116
     * The constructor.
117
     *
118
     * @param string $action           The javascript action to be carried out (or set to "submit" to make a submit button, "file" for file uploads).
119
     * @param string $title            The title to appear on the button.
120
     * @param string $id               The HTML id attribute for the button.
121
     * @param string $imgURL           If provided, the button will be a clickable image using this image.
122
     * @param string $glyphIcon        If provided, the Bootsrap glyphIcon to use for this button.
123
     * @param string $tooltip          If provided, the Bootsrap tooltip to use for this button.
124
     * @param string $tooltipPosition  If provided, the Bootsrap tooltip position (left|right|top|bottom) to use for this button.
125
     *
126
     * @since 1.0
127
     */
128
    public function __construct($action, $title, $id, $imgURL = '', $glyphIcon = '', $tooltip = '', $tooltipPosition = '')
129
    {
130
        $config = ConfigProvider::getInstance();
131
132
        $this->action = $action;
133
        $this->title = $title;
134
        $this->id = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt($id)) : $id);
135
        $this->imgURL = $imgURL;
136
        $this->glyphIcon = $glyphIcon;
137
        $this->tooltip = $tooltip;
138
        $this->tooltipPosition = $tooltipPosition;
139
    }
140
141
    /**
142
     * Renders the HTML and javascript for the button.
143
     *
144
     * @param int $width The width in pixels of the button (will also accept percentage values), defaults to 0 meaning auto-width to fit text.
145
     *
146
     * @since 1.0
147
     *
148
     * @return string
149
     */
150
    public function render($width = 0)
151
    {
152
        $html = '';
153
        $tooltip = '';
154
155
        if (!empty($this->tooltip)) {
156
            $position = 'bottom';
157
            if (!empty($this->tooltipPosition)) {
158
                $position = $this->tooltipPosition;
159
            }
160
161
            $tooltip = ' data-toggle="tooltip" data-placement="'.$position.'" title="'.$this->tooltip.'"';
162
        }
163
164
        if (!empty($this->glyphIcon)) {
165
            $html .= '<button type="button" id="'.$this->id.'" name="'.$this->id.'" class="btn btn-default btn-xs"'.$tooltip.'><span class="glyphicon '.$this->glyphIcon.'"></span> '.$this->title.'</button>';
166
            $html .= '<script>document.getElementById(\''.$this->id.'\').onclick = function() { '.$this->action.'; };</script>';
167
168
            return $html;
169
        }
170
171
        if (!empty($this->imgURL)) {
172
            $html .= '<img src="'.$this->imgURL.'" alt="'.$this->title.'" onClick="'.$this->action.'" style="cursor:pointer; vertical-align:bottom;" id="'.$this->id.'"'.$tooltip.'/>';
173
174
            return $html;
175
        }
176
177
        switch ($this->action) {
178
            case 'submit':
179
                $html .= '<input type="submit" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'" class="btn btn-primary"'.($width == 0 ? '' : ' style="width:'.$width.';"').$tooltip.'/>';
180
            break;
181
            case 'file':
182
                $html .= '<input type="file" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'" class="btn btn-primary"'.($width == 0 ? '' : ' style="width:'.$width.';"').$tooltip.'/>';
183
            break;
184
            default:
185
                $html .= '<input type="button" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'" class="btn btn-primary"'.($width == 0 ? '' : ' style="width:'.$width.';"').$tooltip.'/>';
186
                $html .= '<script>document.getElementById(\''.$this->id.'\').onclick = function() { '.$this->action.'; };</script>';
187
            break;
188
        }
189
190
        return $html;
191
    }
192
}
193