/add-ons/smarty/plugins/function.html_dualselect.php
PHP | 166 lines | 113 code | 20 blank | 33 comment | 17 complexity | 50e825f1ab60989817744619aa5d3990 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause
- <?php
- /**
- * Smarty plugin
- * @package Smarty
- * @subpackage actindo_plugins
- */
- /**
- * Smarty {html_dualselect} function plugin
- *
- * File: function.html_dualselect.php<br>
- * Type: function<br>
- * Name: html_dualselect<br>
- * Date: 29.Nov.2005<br>
- * Purpose: Print out two selectboxes with buttons<br>
- * Input:<br>
- * - options (required) - associative array
- * - selected (optional) - array default not set
- * - name (optional) - string default "dualselect"
- * - size (optional) - int default 20
- * - width (optional) - width of whole control, default 400
- * - js_check_fcn (optional) - javascript function to call on add or remove from left list [ function xyz( action, listname, option ) ]
- * Examples:
- * <pre>sc
- * {html_dualselect options=$ids output=$names}
- * {html_dualselect options=$ids name='box'}
- * </pre>
- * @todo: DHTML Version
- * @author Patrick Prasse <pprasse@actindo.de>
- * @version $Revision: 1.4 $
- * @param array
- * @param Smarty
- * @return string
- * @uses smarty_function_escape_special_chars()
- */
- function smarty_function_html_dualselect($params, &$smarty)
- {
- require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
- require_once $smarty->_get_plugin_filepath('shared','get_var');
- require_once $smarty->_get_plugin_filepath('function','png_image');
- require_once $smarty->_get_plugin_filepath('function','html_select'); // for smarty_function_html_select_option
- $name = 'dualselect';
- $options = null;
- $selected = null;
- $extra = '';
- $size = 20;
- $width = 400;
- $js_check_fcn = null;
- foreach($params as $_key => $_val)
- {
- switch($_key)
- {
- case 'extra':
- case 'name':
- case 'js_check_fcn':
- $$_key = $_val;
- break;
- case 'size':
- case 'width':
- $$_key = (int)$_val;
- break;
- case 'options':
- $$_key = (array)$_val;
- break;
- case 'selected':
- $selected = array_values((array)$_val);
- break;
- default:
- if(!is_array($_val))
- {
- $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
- }
- else
- {
- $smarty->trigger_error("html_select: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
- }
- break;
- }
- }
- if( !isset($id) )
- $id = strtr( $name, array(']['=>'_', ']_'=>'_', '_['=>'_', '['=>'_', ']'=>'_') );
- if( !isset($js_check_fcn) )
- $js_check_fcn = 'check_'.$id;
- if( !isset($selected) )
- $selected = smarty_function_get_var( $name, $smarty );
- if (!isset($options))
- return ''; /* raise error here? */
- settype($selected, 'array');
- $_html_result = '<table border="0" width="'.$width.'"><tr><td width="45%">';
- $width = ($width-40)/2;
- $_html_result .= '<select name="'.smarty_function_escape_special_chars($name.'_sel').'" '.$extra.' multiple="multiple" size="'.$size.'" style="width: '.$width.'px;">';
- foreach( $options as $_key => $_val )
- {
- if( !in_array($_key,$selected) )
- continue;
- $_html_result .= smarty_function_html_select_option($name, $_key, $_val, array(), $extra);
- }
- $_html_result .= '</select></td>';
- $_html_result .= '<td>';
- $_html_result .= '<button class="btn" style="width: 40px;" type="button" name="'.$name.'_add" onClick="dualselect(\'add\',\''.$name.'\')">'.smarty_function_png_image( array('src'=>'images/toolbar/nav_left.png'), $smarty ).'</button>';
- $_html_result .= '<br><br>';
- $_html_result .= '<button class="btn" style="width: 40px;" type="button" name="'.$name.'_del" onClick="dualselect(\'del\',\''.$name.'\')">'.smarty_function_png_image( array('src'=>'images/toolbar/nav_right.png'), $smarty ).'</button>';
- $_html_result .= '</td><td width="45%">';
- $_html_result .= '<select name="'.smarty_function_escape_special_chars($name.'_opts').'" '.$extra.' multiple="multiple" size="'.$size.'" style="width: '.$width.'px;">';
- foreach( $options as $_key => $_val )
- {
- if( in_array($_key,$selected) )
- continue;
- $_html_result .= smarty_function_html_select_option($name, $_key, $_val, array(), $extra);
- }
- $_html_result .= '</select>';
- $_html_result .= "</td></tr></table>\n";
- $_html_result .= <<<JSCODE
- \n<script language="JavaScript" type="text/javascript">
- function dualselect( action, listname )
- {
- var sel = document.getElementsByName( listname+'_sel' )[0];
- var opts = document.getElementsByName( listname+'_opts' )[0];
- var i;
- if( action == 'del' )
- {
- for( i=sel.options.length-1; i>=0; i-- )
- {
- if( sel.options[i].selected && (!window.{$js_check_fcn} || window.{$js_check_fcn}(action,listname,sel.options[i])) )
- {
- opts.options[opts.options.length] = new Option( sel.options[i].text, sel.options[i].value );
- sel.options[i] = null;
- }
- }
- }
- else
- {
- for( i=opts.options.length-1; i>=0; i-- )
- {
- if( opts.options[i].selected && (!window.{$js_check_fcn} || window.{$js_check_fcn}(action,listname,opts.options[i])) )
- {
- sel.options[sel.options.length] = new Option( opts.options[i].text, opts.options[i].value );
- opts.options[i] = null;
- }
- }
- }
- }
- </script>\n
- JSCODE;
- return $_html_result;
- }
- ?>