Commit 52e7402d authored by Nigel McNie's avatar Nigel McNie Committed by Nigel McNie
Browse files

Added a select form input element.

parent fc0ddc0b
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage form
* @author Nigel McNie <nigel@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
function form_render_select($element, $form) {
if (!empty($element['multiple'])) {
$element['name'] .= '[]';
}
$result = '<select'
. Form::element_attributes($element)
. (!empty($element['multiple']) ? ' multiple="multiple"' : '')
. ">\n";
if (!is_array($element['options']) || count($element['options']) < 1) {
$result .= "\t<option></option>\n";
log_warn('Select elements should have at least one option');
}
if (empty($element['multiple'])) {
$values = array($form->get_value($element));
}
else {
if (isset($element['value'])) {
$values = (array) $element['value'];
}
else if (isset($_POST[$element['name']])) {
$values = (array) $_POST[$element['name']];
}
else if (isset($element['defaultvalue'])) {
$values = (array) $element['defaultvalue'];
}
else {
$values = array();
}
}
foreach ($element['options'] as $key => $value) {
if (in_array($key, $values)) {
$selected = ' selected="selected"';
}
else {
$selected = '';
}
$result .= "\t<option value=\"" . hsc($key) . "\"$selected>" . hsc($value) . "</option>\n";
}
$result .= "</select>\n";
return $result;
}
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment