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 <dev@alphaframework.org>
14: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
15: * @copyright Copyright (c) 2015, 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: * The constructor.
90: *
91: * @param string $action The javascript action to be carried out (or set to "submit" to make a submit button, "file" for file uploads).
92: * @param string $title The title to appear on the button.
93: * @param string $id The HTML id attribute for the button.
94: * @param string $imgURL If provided, the button will be a clickable image using this image.
95: * @param string $glyphIcon If provided, the Bootsrap glyphIcon to use for this button.
96: *
97: * @since 1.0
98: */
99: public function __construct($action, $title, $id, $imgURL = '', $glyphIcon = '')
100: {
101: $config = ConfigProvider::getInstance();
102:
103: $this->action = $action;
104: $this->title = $title;
105: $this->id = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt($id)) : $id);
106: $this->imgURL = $imgURL;
107: $this->glyphIcon = $glyphIcon;
108: $this->title = $title;
109: }
110:
111: /**
112: * Renders the HTML and javascript for the button.
113: *
114: * @param int $width The width in pixels of the button (will also accept percentage values), defaults to 0 meaning auto-width to fit text.
115: *
116: * @since 1.0
117: *
118: * @return string
119: */
120: public function render($width = 0)
121: {
122: $html = '';
123:
124: if (!empty($this->glyphIcon)) {
125: $html .= '<button type="button" id="'.$this->id.'" name="'.$this->id.'" class="btn btn-default btn-xs"><span class="glyphicon '.$this->glyphIcon.'"></span> '.$this->title.'</button>';
126: $html .= '<script>document.getElementById(\''.$this->id.'\').onclick = function() { '.$this->action.'; };</script>';
127:
128: return $html;
129: }
130:
131: if (!empty($this->imgURL)) {
132: $html .= '<img src="'.$this->imgURL.'" alt="'.$this->title.'" onClick="'.$this->action.'" style="cursor:pointer; vertical-align:bottom;"/>';
133:
134: return $html;
135: }
136:
137: switch ($this->action) {
138: case 'submit':
139: $html .= '<input type="submit" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'" class="btn btn-primary"'.($width == 0 ? '' : ' style="width:'.$width.';"').'/>';
140: break;
141: case 'file':
142: $html .= '<input type="file" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'" class="btn btn-primary"'.($width == 0 ? '' : ' style="width:'.$width.';"').'/>';
143: break;
144: default:
145: $html .= '<input type="button" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'" class="btn btn-primary"'.($width == 0 ? '' : ' style="width:'.$width.';"').'/>';
146: $html .= '<script>document.getElementById(\''.$this->id.'\').onclick = function() { '.$this->action.'; };</script>';
147: break;
148: }
149:
150: return $html;
151: }
152: }
153: