00001 <?php
00002
00023 class TKSelect extends TKFormElement {
00024 protected $optionId;
00025 protected $options = array();
00026 protected $selAction;
00027
00035 public function __construct($name, $multiple = false, $size = 0) {
00036 parent::__construct($name, "");
00037 $this->addProperty('selected', self::PROP_ARRAY);
00038 $this->addProperty('multiple', self::PROP_BOOLEAN);
00039 $this->addProperty('size', self::PROP_NUMERIC);
00040 $this->set('multiple', $multiple);
00041 $this->set('size', $size);
00042 $this->optionId = 0;
00043
00044 $this->selAction = new TKSelectAction($this, array(), array());
00045 $this->setAction('onchange', $this->selAction);
00046
00047 $this->setPropChangeListener('selected', array($this, 'makeString'));
00048 }
00049
00059 public function makeString($prop, $newValue) {
00060 if(is_array($newValue)) {
00061 foreach($newValue as $k => $v)
00062 $newValue[$k] = "$v";
00063 return array(true, $newValue);
00064 }
00065 $newValue = "$newValue";
00066 return array(true, $newValue);
00067 }
00068
00078 public function addOption($value, $label) {
00079 $this->options[$value] = $label;
00080 $this->addEvent('onselect' . $this->optionId, self::EVENT_INDIRECT);
00081 $this->addEvent('ondeselect' . $this->optionId, self::EVENT_INDIRECT);
00082 $this->selAction->addActions(new TKEmitAction($this, "onselect" . $this->optionId),
00083 new TKEmitAction($this, "ondeselect" . $this->optionId));
00084 $this->optionId++;
00085 if(count($this->getSelected()) == 0) {
00086 $this->setSelected($value);
00087 }
00088 }
00089
00094 public function getOptions() {
00095 return $this->options;
00096 }
00097
00103 public function getSelectedIndex() {
00104 $value = $this->getSelected();
00105 if($value == "")
00106 return 0;
00107 $i = 0;
00108 foreach($this->options as $val => $label) {
00109 if(in_array((string)$val, $value)) {
00110 return $i;
00111 }
00112 else
00113 $i++;
00114 }
00115 return -1;
00116 }
00117
00119 protected function renderComponent($class, $style, $events, $id) {
00120 list($name, $disabled, $value) = $this->getStandardProperties();
00121 $multiple = $this->get('multiple');
00122 if($multiple != "")
00123 $multiple = " multiple=\"$multiple\"";
00124 $size = $this->get('size');
00125 if($size > 0)
00126 $size = " size=\"$size\"";
00127 else
00128 $size = '';
00129
00130 $collect = "<select $id$name$multiple$size$disabled$class[0]$style[0]$events>";
00131 $selected = $this->get('selected');
00132 foreach($this->options as $value => $label) {
00133
00134
00135 if(in_array((string)$value, $selected, true)) {
00136 $collect .= "<option value=\"$value\" selected=\"selected\">"
00137 . "$label</option>\n";
00138 }
00139 else
00140 $collect .= "<option value=\"$value\">$label</option>\n";
00141 }
00142 $collect .= "</select>\n";
00143 return $collect;
00144 }
00145
00147 public static function createFromXML($attrs, $contents, $node) {
00148 $name = $attrs['name'];
00149 $obj = new TKSelect($name);
00150 self::convertStdAttributes($obj, $attrs);
00151 self::convertStdEvents($obj, $node);
00152 foreach($node['children'] as $child) {
00153 if($child['tag'] == 'item') {
00154 $value = $child['attrs']['value'];
00155 $label = $child['value'];
00156 $obj->addOption($value, $label);
00157 if(isset($child['attrs']['selected']) && $child['attrs']['selected'] == 'true')
00158 $obj->set('selected', $value);
00159 }
00160 }
00161 return $obj;
00162 }
00163 }
00164
00165 ?>