Lubos Dz - 2010-04-18

Hi again,

here are methods for [form.php] to enhance manipulation with OPTION lists...
I use it to emulate AJAX response - I think it would be useful to include it (plus more..) into the SimpleForm object.

Regards
Lubos

#########################################

/**
* Return form element widget
*/
function getField($selector, $position=false){
$_position = 0;
for ($i = 0, $count = count($this->_widgets); $i < $count; ++$i) {
if ($selector->isMatch($this->_widgets[$i])) {
++$_position;
if ($position === false or $_position === (int)$position) {
return $this->_widgets[$i];
}
}
}
return false;
}

/**
* Set list of options
*/
function setOptions($selector, $options = array()){
$result = false;
$widget = &$this->getField($selector);
if( is_object($widget) && $widget->_name == 'select' ){
if(!is_array($options) && get_class($option) === 'SimpleOptionTag'){
$options = array($options);
}
$value = $widget->_content;
foreach($options as $option){
if(get_class($option) !== 'SimpleOptionTag'){
break;
}
if(isset($option->_attributes['selected']) && strtolower($option->_attributes['selected']) == 'selected'){
$value = $option->_attributes['value'];
}
}
$widget->_options = $options;
$widget->_content = $value;
$result = true;
}
return $result;
}

/**
*
* @param mixed $selector
* @param mixed $option
*/
function addOption($selector, $option){
$result = false;
$widget = &$this->getField($selector);
if( is_object($widget) && $widget->_name == 'select' ){
if(get_class($option) !== 'SimpleOptionTag'){
break;
}
if(isset($option->_attributes['selected']) && strtolower($option->_attributes['selected']) == 'selected'){
$widget->_content = $option->_attributes['value'];
}
$widget->_options[] = $option;
$result = true;
}
return $result;
}

/**
*
* @param mixed $selector
*/
function clearOptions($selector){
$widget = &$this->getField($selector);
if( is_object($widget) && $widget->_name == 'select' ){
$widget->_options[] = array();
return true;
}
return false;
}

#########################################