1: <?php
2:
3: /**
4: * Button HTML custom widget
5: *
6: * @package alpha::view::widgets
7: * @since 1.0
8: * @author John Collins <dev@alphaframework.org>
9: * @version $Id: Button.inc 1745 2014-03-29 15:19:05Z alphadevx $
10: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
11: * @copyright Copyright (c) 2014, John Collins (founder of Alpha Framework).
12: * All rights reserved.
13: *
14: * <pre>
15: * Redistribution and use in source and binary forms, with or
16: * without modification, are permitted provided that the
17: * following conditions are met:
18: *
19: * * Redistributions of source code must retain the above
20: * copyright notice, this list of conditions and the
21: * following disclaimer.
22: * * Redistributions in binary form must reproduce the above
23: * copyright notice, this list of conditions and the
24: * following disclaimer in the documentation and/or other
25: * materials provided with the distribution.
26: * * Neither the name of the Alpha Framework nor the names
27: * of its contributors may be used to endorse or promote
28: * products derived from this software without specific
29: * prior written permission.
30: *
31: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
36: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
42: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44: * </pre>
45: *
46: */
47: class Button {
48: /**
49: * The Javascript action to carry out when the button is pressed.
50: *
51: * @var string
52: * @since 1.0
53: */
54: private $action;
55:
56: /**
57: * The title to display on the button.
58: *
59: * @var string
60: * @since 1.0
61: */
62: private $title;
63:
64: /**
65: * The HTML id attribute for the button.
66: *
67: * @var string
68: * @since 1.0
69: */
70: private $id;
71:
72: /**
73: * If provided, the button will be a clickable image using this image.
74: *
75: * @var string
76: * @since 1.0
77: */
78: private $imgURL;
79:
80: /**
81: * The constructor
82: *
83: * @param string $action The javascript action to be carried out (or set to "submit" to make a submit button, "file" for file uploads).
84: * @param string $title The title to appear on the button.
85: * @param string $id The HTML id attribute for the button.
86: * @param string $imgURL If provided, the button will be a clickable image using this image.
87: * @param string $glyphIcon If provided, the Bootsrap glyphIcon to use for this button.
88: * @since 1.0
89: */
90: public function __construct($action, $title, $id, $imgURL='', $glyphIcon='') {
91: global $config;
92:
93: $this->action = $action;
94: $this->title = $title;
95: $this->id = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt($id)) : $id);
96: $this->imgURL = $imgURL;
97: $this->glyphIcon = $glyphIcon;
98: $this->title = $title;
99: }
100:
101: /**
102: * Renders the HTML and javascript for the button.
103: *
104: * @param integer $width The width in pixels of the button (will also accept percentage values), defaults to 0 meaning auto-width to fit text.
105: * @since 1.0
106: * @return string
107: */
108: public function render($width=0) {
109: $html = '';
110:
111: if(!empty($this->glyphIcon)) {
112: $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>';
113: $html .= '<script>document.getElementById(\''.$this->id.'\').onclick = function() { '.$this->action.'; };</script>';
114: return $html;
115: }
116:
117: if(!empty($this->imgURL)) {
118: $html .= '<img src="'.$this->imgURL.'" alt="'.$this->title.'" onClick="'.$this->action.'" style="cursor:pointer; vertical-align:bottom;"/>';
119: return $html;
120: }
121:
122: switch ($this->action) {
123: case 'submit':
124: $html .= '<input type="submit" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'" class="btn btn-primary"'.($width == 0? '':' style="width:'.$width.';"').'/>';
125: break;
126: case 'file':
127: $html .= '<input type="file" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'" class="btn btn-primary"'.($width == 0? '':' style="width:'.$width.';"').'/>';
128: break;
129: default:
130: $html .= '<input type="button" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'" class="btn btn-primary"'.($width == 0? '':' style="width:'.$width.';"').'/>';
131: $html .= '<script>document.getElementById(\''.$this->id.'\').onclick = function() { '.$this->action.'; };</script>';
132: break;
133: }
134:
135: return $html;
136: }
137: }
138:
139: ?>