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

Revision 405, 4.7 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: formcheckbox.php,v 1.2 2005/03/18 12:51:55 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 * @package     kernel
33 * @subpackage  form
34 *
35 * @author      Kazumi Ono  <[email protected]>
36 * @copyright   copyright (c) 2000-2003 XOOPS.org
37 */
38/**
39 * One or more Checkbox(es)
40 *
41 * @package     kernel
42 * @subpackage  form
43 *
44 * @author  Kazumi Ono  <[email protected]>
45 * @copyright   copyright (c) 2000-2003 XOOPS.org
46 */
47class XoopsFormCheckBox extends XoopsFormElement {
48
49    /**
50     * Availlable options
51     * @var array   
52     * @access  private
53     */
54    var $_options = array();
55
56    /**
57     * pre-selected values in array
58     * @var array   
59     * @access  private
60     */
61    var $_value = array();
62
63    /**
64     * Constructor
65     *
66     * @param   string  $caption
67     * @param   string  $name
68     * @param   mixed   $value  Either one value as a string or an array of them.   
69     */
70    function XoopsFormCheckBox($caption, $name, $value = null){
71        $this->setCaption($caption);
72        $this->setName($name);
73        if (isset($value)) {
74            $this->setValue($value);
75        }
76    }
77
78    /**
79     * Get the "value"
80     *
81     * @return  array
82     */
83    function getValue(){
84        return $this->_value;
85    }
86
87    /**
88     * Set the "value"
89     *
90     * @param   array
91     */
92    function setValue($value){
93        $this->_value = array();
94        if (is_array($value)) {
95            foreach ($value as $v) {
96                $this->_value[] = $v;
97            }
98        } else {
99            $this->_value[] = $value;
100        }
101    }
102
103    /**
104     * Add an option
105     *
106     * @param   string  $value 
107     * @param   string  $name   
108     */
109    function addOption($value, $name=""){
110        if ($name != "") {
111            $this->_options[$value] = $name;
112        } else {
113            $this->_options[$value] = $value;
114        }
115    }
116
117    /**
118     * Add multiple Options at once
119     *
120     * @param   array   $options    Associative array of value->name pairs
121     */
122    function addOptionArray($options){
123        if ( is_array($options) ) {
124            foreach ( $options as $k=>$v ) {
125                $this->addOption($k, $v);
126            }
127        }
128    }
129
130    /**
131     * Get an array with all the options
132     *
133     * @return  array   Associative array of value->name pairs
134     */
135    function getOptions(){
136        return $this->_options;
137    }
138
139    /**
140     * prepare HTML for output
141     *
142     * @return  string
143     */
144    function render(){
145        $ret = "";
146        if ( count($this->getOptions()) > 1 && substr($this->getName(), -2, 2) != "[]" ) {
147            $newname = $this->getName()."[]";
148            $this->setName($newname);
149        }
150        foreach ( $this->getOptions() as $value => $name ) {
151            $ret .= "<input type='checkbox' name='".$this->getName()."' value='".$value."'";
152            if (count($this->getValue()) > 0 && in_array($value, $this->getValue())) {
153                $ret .= " checked='checked'";
154            }
155            $ret .= $this->getExtra()." />".$name."\n";
156        }
157        return $ret;
158    }
159}
160?>
Note: See TracBrowser for help on using the repository browser.