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 1644 2013-02-12 22:34:51Z alphadevx $
10: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
11: * @copyright Copyright (c) 2012, 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: * @since 1.0
88: */
89: public function __construct($action, $title, $id, $imgURL='') {
90: global $config;
91:
92: $this->action = $action;
93: $this->title = $title;
94: $this->id = $id;
95: $this->imgURL = $imgURL;
96: $this->title = $title;
97: }
98:
99: /**
100: * Renders the HTML and javascript for the button.
101: *
102: * @param integer $width The width in pixels of the button (will also accept percentage values), defaults to 0 meaning auto-width to fit text.
103: * @since 1.0
104: * @return string
105: */
106: public function render($width=0) {
107: $html = '';
108:
109: if(empty($this->imgURL)) {
110: switch ($this->action) {
111: case 'submit':
112: $html .= '<script>
113: $(function() {
114: $("[Id=\''.$this->id.'\']").button();
115: });
116: </script>
117: ';
118: $html .= '<input type="submit" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'"'.($width == 0? '':' style="width:'.$width.';"').'/>';
119: break;
120: case 'file':
121: $html .= '<script>
122: $(function() {
123: $("[Id=\''.$this->id.'\']").button();
124: });
125: </script>
126: ';
127: $html .= '<input type="file" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'"'.($width == 0? '':' style="width:'.$width.';"').'/>';
128: break;
129: default:
130: $html .= '<script>
131: $(function() {
132: $("[Id=\''.$this->id.'\']").button();
133: $("[Id=\''.$this->id.'\']").click(function() { '.$this->action.'; });
134: });
135: </script>
136: ';
137: $html .= '<input type="button" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'"'.($width == 0? '':' style="width:'.$width.';"').'/>';
138: break;
139: }
140: }else{
141: // in the special case where a clickable image is being used
142: $html .= '<img src="'.$this->imgURL.'" alt="'.$this->title.'" onClick="'.$this->action.'" style="cursor:pointer; vertical-align:bottom;"/>';
143: }
144:
145: return $html;
146: }
147: }
148:
149: ?>