3 * Class to generate XML <select>.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Module of static functions for generating XML <select> elements
27 protected $options = array();
28 protected $default = false;
29 protected $attributes = array();
31 public function __construct( $name = false, $id = false, $default = false ) {
33 $this->setAttribute( 'name', $name );
37 $this->setAttribute( 'id', $id );
40 if ( $default !== false ) {
41 $this->default = $default;
46 * @param string $default
48 public function setDefault( $default ) {
49 $this->default = $default;
56 public function setAttribute( $name, $value ) {
57 $this->attributes
[$name] = $value;
64 public function getAttribute( $name ) {
65 if ( isset( $this->attributes
[$name] ) ) {
66 return $this->attributes
[$name];
76 public function addOption( $name, $value = false ) {
78 $value = $value !== false ?
$value : $name;
80 $this->options
[] = array( $name => $value );
84 * This accepts an array of form
86 * label => ( label => value, label => value )
88 * @param array $options
90 public function addOptions( $options ) {
91 $this->options
[] = $options;
95 * This accepts an array of form
97 * label => ( label => value, label => value )
99 * @param array $options
100 * @param bool $default
103 static function formatOptions( $options, $default = false ) {
106 foreach ( $options as $label => $value ) {
107 if ( is_array( $value ) ) {
108 $contents = self
::formatOptions( $value, $default );
109 $data .= Html
::rawElement( 'optgroup', array( 'label' => $label ), $contents ) . "\n";
111 $data .= Xml
::option( $label, $value, $value === $default ) . "\n";
121 public function getHTML() {
124 foreach ( $this->options
as $options ) {
125 $contents .= self
::formatOptions( $options, $this->default );
128 return Html
::rawElement( 'select', $this->attributes
, rtrim( $contents ) );