1: <?php
2: // $Id: formcheckbox.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: if (!defined('XOOPS_ROOT_PATH')) exit();
33:
34: /**
35: * @package kernel
36: * @subpackage form
37: *
38: * @author Kazumi Ono <onokazu@xoops.org>
39: * @copyright copyright (c) 2000-2003 XOOPS.org
40: */
41: /**
42: * One or more Checkbox(es)
43: *
44: * @package kernel
45: * @subpackage form
46: *
47: * @author Kazumi Ono <onokazu@xoops.org>
48: * @copyright copyright (c) 2000-2003 XOOPS.org
49: */
50: class XoopsFormCheckBox extends XoopsFormElement {
51:
52: /**
53: * Availlable options
54: * @var array
55: * @access private
56: */
57: var $_options = array();
58:
59: /**
60: * pre-selected values in array
61: * @var array
62: * @access private
63: */
64: var $_value = array();
65:
66: /**
67: * Constructor
68: *
69: * @param string $caption
70: * @param string $name
71: * @param mixed $value Either one value as a string or an array of them.
72: */
73: function XoopsFormCheckBox($caption, $name, $value = null){
74: $this->setCaption($caption);
75: $this->setName($name);
76: if (isset($value)) {
77: $this->setValue($value);
78: }
79: }
80:
81: /**
82: * Get the "value"
83: *
84: * @return array
85: */
86: function getValue(){
87: return $this->_value;
88: }
89:
90: /**
91: * Set the "value"
92: *
93: * @param array
94: */
95: function setValue($value){
96: $this->_value = array();
97: if (is_array($value)) {
98: foreach ($value as $v) {
99: $this->_value[] = $v;
100: }
101: } else {
102: $this->_value[] = $value;
103: }
104: }
105:
106: /**
107: * Add an option
108: *
109: * @param string $value
110: * @param string $name
111: */
112: function addOption($value, $name=""){
113: if ($name != "") {
114: $this->_options[$value] = $name;
115: } else {
116: $this->_options[$value] = $value;
117: }
118: }
119:
120: /**
121: * Add multiple Options at once
122: *
123: * @param array $options Associative array of value->name pairs
124: */
125: function addOptionArray($options){
126: if ( is_array($options) ) {
127: foreach ( $options as $k=>$v ) {
128: $this->addOption($k, $v);
129: }
130: }
131: }
132:
133: /**
134: * Get an array with all the options
135: *
136: * @return array Associative array of value->name pairs
137: */
138: function getOptions(){
139: return $this->_options;
140: }
141:
142: /**
143: * prepare HTML for output
144: *
145: * @return string
146: */
147: function render()
148: {
149: $root =& XCube_Root::getSingleton();
150: $renderSystem =& $root->getRenderSystem(XOOPSFORM_DEPENDENCE_RENDER_SYSTEM);
151:
152: $renderTarget =& $renderSystem->createRenderTarget('main');
153:
154: if ( count($this->getOptions()) > 1 && substr($this->getName(), -2, 2) != "[]" ) {
155: $newname = $this->getName()."[]";
156: $this->setName($newname);
157: }
158:
159: $renderTarget->setAttribute('legacy_module', 'legacy');
160: $renderTarget->setTemplateName("legacy_xoopsform_checkbox.html");
161: $renderTarget->setAttribute("element", $this);
162:
163: $renderSystem->render($renderTarget);
164:
165: return $renderTarget->getResult();
166: }
167: }
168: ?>