source: temp/test-xoops.ec-cube.net/html/class/xoopsform/formelement.php @ 405

Revision 405, 7.0 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: formelement.php,v 1.3 2005/09/04 20:46:08 onokazu 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  <[email protected]>
38 * @copyright   copyright (c) 2000-2003 XOOPS.org
39 */
40
41
42/**
43 * Abstract base class for form elements
44 *
45 * @author  Kazumi Ono  <[email protected]>
46 * @copyright   copyright (c) 2000-2003 XOOPS.org
47 *
48 * @package     kernel
49 * @subpackage  form
50 */
51class 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
106    /**
107     * constructor
108     *
109     */
110    function XoopsFormElement(){
111        exit("This class cannot be instantiated!");
112    }
113
114    /**
115     * Is this element a container of other elements?
116     *
117     * @return  bool false
118     */
119    function isContainer()
120    {
121        return false;
122    }
123
124    /**
125     * set the "name" attribute for the element
126     *
127     * @param   string  $name   "name" attribute for the element
128     */
129    function setName($name) {
130        $this->_name = trim($name);
131    }
132
133    /**
134     * get the "name" attribute for the element
135     *
136     * @param   bool    encode?
137     * @return  string  "name" attribute
138     */
139    function getName($encode=true) {
140        if (false != $encode) {
141            return str_replace("&amp;", "&", str_replace("'","&#039;",htmlspecialchars($this->_name)));
142        }
143        return $this->_name;
144    }
145
146    /**
147     * set the "accesskey" attribute for the element
148     *
149     * @param   string  $key   "accesskey" attribute for the element
150     */
151    function setAccessKey($key) {
152        $this->_accesskey = trim($key);
153    }
154    /**
155     * get the "accesskey" attribute for the element
156     *
157     * @return  string  "accesskey" attribute value
158     */
159    function getAccessKey() {
160        return $this->_accesskey;
161    }
162    /**
163     * If the accesskey is found in the specified string, underlines it
164     *
165     * @param   string  $str   String where to search the accesskey occurence
166     * @return  string  Enhanced string with the 1st occurence of accesskey underlined
167     */
168    function getAccessString( $str ) {
169        $access = $this->getAccessKey();
170        if ( !empty($access) && ( false !== ($pos = strpos($str, $access)) ) ) {
171            return substr($str, 0, $pos) . '<span style="text-decoration:underline">' . substr($str, $pos, 1) . '</span>' . substr($str, $pos+1);
172        }
173        return $str;
174    }
175
176    /**
177     * set the "class" attribute for the element
178     *
179     * @param   string  $key   "class" attribute for the element
180     */
181    function setClass($class) {
182        $class = trim($class);
183        if ( empty($class) ) {
184            $this->_class = '';
185        } else {
186            $this->_class .= (empty($this->_class) ? '' : ' ') . $class;
187        }
188    }
189    /**
190     * get the "class" attribute for the element
191     *
192     * @return  string  "class" attribute value
193     */
194    function getClass() {
195        return $this->_class;
196    }
197
198    /**
199     * set the caption for the element
200     *
201     * @param   string  $caption
202     */
203    function setCaption($caption) {
204        $this->_caption = trim($caption);
205    }
206
207    /**
208     * get the caption for the element
209     *
210     * @return  string
211     */
212    function getCaption() {
213        return $this->_caption;
214    }
215
216    /**
217     * set the element's description
218     *
219     * @param   string  $description
220     */
221    function setDescription($description) {
222        $this->_description = trim($description);
223    }
224
225    /**
226     * get the element's description
227     *
228     * @return  string
229     */
230    function getDescription() {
231        return $this->_description;
232    }
233
234    /**
235     * flag the element as "hidden"
236     *
237     */
238    function setHidden() {
239        $this->_hidden = true;
240    }
241
242    /**
243     * Find out if an element is "hidden".
244     *
245     * @return  bool
246     */
247    function isHidden() {
248        return $this->_hidden;
249    }
250
251    /**
252     * Add extra attributes to the element.
253     *
254     * This string will be inserted verbatim and unvalidated in the
255     * element's tag. Know what you are doing!
256     *
257     * @param   string  $extra
258     * @param   string  $replace If true, passed string will replace current content otherwise it will be appended to it
259     * @return  string New content of the extra string
260     */
261    function setExtra($extra, $replace = false){
262        if ( $replace) {
263            $this->_extra = " " .trim($extra);
264        } else {
265            $this->_extra .= " " . trim($extra);
266        }
267        return $this->_extra;
268    }
269
270    /**
271     * Get the extra attributes for the element
272     *
273     * @return  string
274     */
275    function getExtra(){
276        if (isset($this->_extra)) {
277            return $this->_extra;
278        }
279    }
280
281    /**
282     * Generates output for the element.
283     *
284     * This method is abstract and must be overwritten by the child classes.
285     * @abstract
286     */
287    function render(){
288    }
289}
290?>
Note: See TracBrowser for help on using the repository browser.