1: <?php
2: // $Id: formelement.php,v 1.1 2007/05/15 02:34:42 minahito Exp $
3: // ------------------------------------------------------------------------ //
4: // XOOPS - PHP Content Management System //
5: // Copyright (c) 2000 XOOPS.org //
6: // <http://www.xoops.org/> //
7: // ------------------------------------------------------------------------ //
8: // This program is free software; you can redistribute it and/or modify //
9: // it under the terms of the GNU General Public License as published by //
10: // the Free Software Foundation; either version 2 of the License, or //
11: // (at your option) any later version. //
12: // //
13: // You may not change or alter any portion of this comment or credits //
14: // of supporting developers from this source code or any supporting //
15: // source code which is considered copyrighted (c) material of the //
16: // original comment or credit authors. //
17: // //
18: // This program is distributed in the hope that it will be useful, //
19: // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21: // GNU General Public License for more details. //
22: // //
23: // You should have received a copy of the GNU General Public License //
24: // along with this program; if not, write to the Free Software //
25: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
26: // ------------------------------------------------------------------------ //
27: // Author: Kazumi Ono (AKA onokazu) //
28: // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29: // Project: The XOOPS Project //
30: // ------------------------------------------------------------------------- //
31: /**
32: *
33: *
34: * @package kernel
35: * @subpackage form
36: *
37: * @author Kazumi Ono <onokazu@xoops.org>
38: * @copyright copyright (c) 2000-2003 XOOPS.org
39: */
40:
41:
42: /**
43: * Abstract base class for form elements
44: *
45: * @author Kazumi Ono <onokazu@xoops.org>
46: * @copyright copyright (c) 2000-2003 XOOPS.org
47: *
48: * @package kernel
49: * @subpackage form
50: */
51: class XoopsFormElement {
52:
53: /**#@+
54: * @access private
55: */
56: /**
57: * "name" attribute of the element
58: * @var string
59: */
60: var $_name;
61:
62: /**
63: * caption of the element
64: * @var string
65: */
66: var $_caption;
67:
68: /**
69: * Accesskey for this element
70: * @var string
71: */
72: var $_accesskey = '';
73:
74: /**
75: * HTML class for this element
76: * @var string
77: */
78: var $_class = '';
79:
80: /**
81: * hidden?
82: * @var bool
83: */
84: var $_hidden = false;
85:
86: /**
87: * extra attributes to go in the tag
88: * @var string
89: */
90: var $_extra = "";
91:
92: /**
93: * required field?
94: * @var bool
95: */
96: var $_required = false;
97:
98: /**
99: * description of the field
100: * @var string
101: */
102: var $_description = "";
103:
104: /**
105: * specified id for the form.
106: * @var string
107: */
108: var $_id = null;
109: /**#@-*/
110:
111:
112: /**
113: * constructor
114: *
115: */
116: function XoopsFormElement(){
117: exit("This class cannot be instantiated!");
118: }
119:
120: /**
121: * Is this element a container of other elements?
122: *
123: * @return bool false
124: */
125: function isContainer()
126: {
127: return false;
128: }
129:
130: /**
131: * set the "name" attribute for the element
132: *
133: * @param string $name "name" attribute for the element
134: */
135: function setName($name) {
136: $this->_name = trim($name);
137: }
138:
139: /**
140: * get the "name" attribute for the element
141: *
142: * @param bool encode?
143: * @return string "name" attribute
144: */
145: function getName($encode=true) {
146: if (false != $encode) {
147: return str_replace("&", "&", str_replace("'","'",htmlspecialchars($this->_name)));
148: }
149: return $this->_name;
150: }
151:
152: /**
153: * Set $id to member property.
154: * @param string $id
155: */
156: function setId($id) {
157: $this->_id = $id;
158: }
159:
160: /**
161: * get the "id" attribute for the element. If ID is empty, return getName().
162: */
163: function getId() {
164: return $this->_id != null ? $this->_id : $this->getName();
165: }
166:
167: /**
168: * set the "accesskey" attribute for the element
169: *
170: * @param string $key "accesskey" attribute for the element
171: */
172: function setAccessKey($key) {
173: $this->_accesskey = trim($key);
174: }
175: /**
176: * get the "accesskey" attribute for the element
177: *
178: * @return string "accesskey" attribute value
179: */
180: function getAccessKey() {
181: return $this->_accesskey;
182: }
183: /**
184: * If the accesskey is found in the specified string, underlines it
185: *
186: * @param string $str String where to search the accesskey occurence
187: * @return string Enhanced string with the 1st occurence of accesskey underlined
188: */
189: function getAccessString( $str ) {
190: $access = $this->getAccessKey();
191: if ( !empty($access) && ( false !== ($pos = strpos($str, $access)) ) ) {
192: return substr($str, 0, $pos) . '<span style="text-decoration:underline">' . substr($str, $pos, 1) . '</span>' . substr($str, $pos+1);
193: }
194: return $str;
195: }
196:
197: /**
198: * set the "class" attribute for the element
199: *
200: * @param string $key "class" attribute for the element
201: */
202: function setClass($class) {
203: $class = trim($class);
204: if ( empty($class) ) {
205: $this->_class = '';
206: } else {
207: $this->_class .= (empty($this->_class) ? '' : ' ') . $class;
208: }
209: }
210: /**
211: * get the "class" attribute for the element
212: *
213: * @return string "class" attribute value
214: */
215: function getClass() {
216: return $this->_class;
217: }
218:
219: /**
220: * set the caption for the element
221: *
222: * @param string $caption
223: */
224: function setCaption($caption) {
225: $this->_caption = trim($caption);
226: }
227:
228: /**
229: * get the caption for the element
230: *
231: * @return string
232: */
233: function getCaption() {
234: return $this->_caption;
235: }
236:
237: /**
238: * set the element's description
239: *
240: * @param string $description
241: */
242: function setDescription($description) {
243: $this->_description = trim($description);
244: }
245:
246: /**
247: * get the element's description
248: *
249: * @return string
250: */
251: function getDescription() {
252: return $this->_description;
253: }
254:
255: /**
256: * flag the element as "hidden"
257: *
258: */
259: function setHidden() {
260: $this->_hidden = true;
261: }
262:
263: /**
264: * Find out if an element is "hidden".
265: *
266: * @return bool
267: */
268: function isHidden() {
269: return $this->_hidden;
270: }
271:
272: function isBreak() {
273: return false;
274: }
275:
276: /**
277: * Add extra attributes to the element.
278: *
279: * This string will be inserted verbatim and unvalidated in the
280: * element's tag. Know what you are doing!
281: *
282: * @param string $extra
283: * @param string $replace If true, passed string will replace current content otherwise it will be appended to it
284: * @return string New content of the extra string
285: */
286: function setExtra($extra, $replace = false){
287: if ( $replace) {
288: $this->_extra = " " .trim($extra);
289: } else {
290: $this->_extra .= " " . trim($extra);
291: }
292: return $this->_extra;
293: }
294:
295: /**
296: * Get the extra attributes for the element
297: *
298: * @return string
299: */
300: function getExtra(){
301: if (isset($this->_extra)) {
302: return $this->_extra;
303: }
304: }
305:
306: /**
307: * Generates output for the element.
308: *
309: * This method is abstract and must be overwritten by the child classes.
310: * @abstract
311: */
312: function render(){
313: }
314:
315: /**
316: * Gets a message for javascript in the specific member function of XoopsForm class.
317: */
318: function getMessageForJS()
319: {
320: $eltcaption = trim( $this->getCaption() );
321: $eltmsg = empty($eltcaption) ? sprintf( _FORM_ENTER, $eltname ) : sprintf( _FORM_ENTER, $eltcaption );
322: $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
323:
324: return $eltmsg;
325: }
326: }
327: ?>