{select} {up} {down} {add} {edit} {del} '; var $_actions = array(); function HTML_QuickForm_selectEdit($elementName=null, $elementLabel=null, $options=null, $attributes=array()) { parent::HTML_QuickForm_select($elementName, $elementLabel, $options, $attributes); $this->setMultiple(true); if (is_null($this->getAttribute('size'))) { $this->updateAttributes(array('size' => 10)); } if (is_null($this->getAttribute('style'))) { $this->updateAttributes(array('style' => 'width:100%;')); } $this->setAction('up', new HTML_QuickForm_button('up', 'Move Up', array('class' => 'btn', 'onClick'=>'selectEdit_move_up(\'{name}\');' )), " function selectEdit_move_up(where) { var moved_value = false; var moved_text = false; var sel = document.getElementsByName('_'+where)[0]; var hide = document.getElementsByName(where)[0]; for(var i = 0; i < sel.options.length; i++){ if (!sel.options[i].selected){ moved_value = sel.options[i].value; moved_text = sel.options[i].text; }else if(!(moved_value == false && 'boolean' == typeof(moved_value)) ) { sel.options[i - 1].value = sel.options[i].value; sel.options[i - 1].text = sel.options[i].text; sel.options[i - 1].selected = true; sel.options[i].value = moved_value; sel.options[i].text = moved_text; sel.options[i].selected = false; } } for(var i = 0; i < sel.options.length; i++){ hide.options[i].value = sel.options[i].value; hide.options[i].text = sel.options[i].text; } selectEdit_select_hiden(where); return true; }" ); $this->setAction('down', new HTML_QuickForm_button('down', 'Move Down', array('class' => 'btn', 'onClick'=>'selectEdit_move_down(\'{name}\');' )), " function selectEdit_move_down(where) { var moved_value = false; var moved_text = false; var sel = document.getElementsByName('_'+where)[0]; var hide = document.getElementsByName(where)[0]; for(var i = sel.options.length - 1; i >= 0; i--){ if (!sel.options[i].selected ){ moved_value = sel.options[i].value; moved_text = sel.options[i].text; }else if(!(moved_value == false && 'boolean' == typeof(moved_value)) ) { sel.options[i + 1].value = sel.options[i].value; sel.options[i + 1].text = sel.options[i].text; sel.options[i + 1].selected = true; sel.options[i].value = moved_value; sel.options[i].text = moved_text; sel.options[i].selected = false; } } for(i = 0; i < sel.options.length; i++){ hide.options[i].value = sel.options[i].value; hide.options[i].text = sel.options[i].text; } selectEdit_select_hiden(where); return true; }" ); $this->setAction('del', new HTML_QuickForm_button('down', 'Delete', array('class' => 'btn', 'onClick'=>'selectEdit_delOption(\'{name}\');' )), "function selectEdit_delOption(where) { var sel = document.getElementsByName('_'+where)[0]; var hide = document.getElementsByName(where)[0]; var i = 0; var confirmed = false; while (isetAction('edit', new HTML_QuickForm_button('edit', 'Edit', array('class' => 'btn', 'onClick'=>'selectEdit_editOption(\'{name}\');' )), " function selectEdit_editOption(where) { var text; var sel = document.getElementsByName('_'+where)[0]; for(var i = 0; i < sel.options.length; i++){ if (sel.options[i].selected){ if (text = prompt('Enter text line', sel.options[i].text)) { selectEdit_edit(where, i, text) } return true; } } return false; } " ); $this->setAction('add', new HTML_QuickForm_button('add', 'Add', array('class' => 'btn', 'onClick'=>'selectEdit_addOption(\'{name}\');' )), " function selectEdit_addOption(where) { var text; if (text = prompt('Enter text line', '') ){ selectEdit_add(where, text); } return true; } " ); } function setAction($name, $button = null, $function = null) { // $this->_actions[$name]['button'] = $button; if (!is_null($button)){ $this->_actions[$name]['button'] = $button; } if (!is_null($function)){ $this->_actions[$name]['function'] = $function; } return true; } function getAction($name) { return $this->_actions[$name]; } function getActions() { return $this->_actions; } function delAction($name) { $this->_actions[$name] = null; return true; } function setTemplate($template) { $this->_template = $template; } function toHtml() { $options = $this->_options; $optionsOut = array(); foreach ($options as $otp){ $optionsOut[$otp['attr']['value']] = $otp['text']; } $hide = new HTML_QuickForm_select($this->getName(), null, $optionsOut); $hide->setMultiple(true); $hide->setValue(array_keys($optionsOut)); $hide->updateAttributes(array('style' => 'visibility:hidden;width:1px; height:1px;')); $name = $this->getName(); $this->setName('_'.$name); $showHtml = parent::toHtml(); $this->setName($name); $html = $hide->toHtml(); $html .= str_replace('{select}', $showHtml, $this->_template); $actionsJsFunctions = ''; foreach ($this->getActions() as $name => $action) { if (method_exists($action['button'], 'toHtml')){ $buttonHtml = str_replace('{name}', $this->getName().'[]', $action['button']->toHtml()); }else { $buttonHtml = ''; } $html = str_replace('{'.$name.'}', $buttonHtml, $html); $actionsJsFunctions .= $action['function']; } $JS = ' '; $html .= str_replace('{actions_js_functions}', $actionsJsFunctions, $JS); return $html; } } ?>