PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/add-ons/smarty/plugins/function.html_dualselect.php

https://github.com/jcplat/console-seolan
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
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage actindo_plugins
  6. */
  7. /**
  8. * Smarty {html_dualselect} function plugin
  9. *
  10. * File: function.html_dualselect.php<br>
  11. * Type: function<br>
  12. * Name: html_dualselect<br>
  13. * Date: 29.Nov.2005<br>
  14. * Purpose: Print out two selectboxes with buttons<br>
  15. * Input:<br>
  16. * - options (required) - associative array
  17. * - selected (optional) - array default not set
  18. * - name (optional) - string default "dualselect"
  19. * - size (optional) - int default 20
  20. * - width (optional) - width of whole control, default 400
  21. * - js_check_fcn (optional) - javascript function to call on add or remove from left list [ function xyz( action, listname, option ) ]
  22. * Examples:
  23. * <pre>sc
  24. * {html_dualselect options=$ids output=$names}
  25. * {html_dualselect options=$ids name='box'}
  26. * </pre>
  27. * @todo: DHTML Version
  28. * @author Patrick Prasse <pprasse@actindo.de>
  29. * @version $Revision: 1.4 $
  30. * @param array
  31. * @param Smarty
  32. * @return string
  33. * @uses smarty_function_escape_special_chars()
  34. */
  35. function smarty_function_html_dualselect($params, &$smarty)
  36. {
  37. require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
  38. require_once $smarty->_get_plugin_filepath('shared','get_var');
  39. require_once $smarty->_get_plugin_filepath('function','png_image');
  40. require_once $smarty->_get_plugin_filepath('function','html_select'); // for smarty_function_html_select_option
  41. $name = 'dualselect';
  42. $options = null;
  43. $selected = null;
  44. $extra = '';
  45. $size = 20;
  46. $width = 400;
  47. $js_check_fcn = null;
  48. foreach($params as $_key => $_val)
  49. {
  50. switch($_key)
  51. {
  52. case 'extra':
  53. case 'name':
  54. case 'js_check_fcn':
  55. $$_key = $_val;
  56. break;
  57. case 'size':
  58. case 'width':
  59. $$_key = (int)$_val;
  60. break;
  61. case 'options':
  62. $$_key = (array)$_val;
  63. break;
  64. case 'selected':
  65. $selected = array_values((array)$_val);
  66. break;
  67. default:
  68. if(!is_array($_val))
  69. {
  70. $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
  71. }
  72. else
  73. {
  74. $smarty->trigger_error("html_select: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  75. }
  76. break;
  77. }
  78. }
  79. if( !isset($id) )
  80. $id = strtr( $name, array(']['=>'_', ']_'=>'_', '_['=>'_', '['=>'_', ']'=>'_') );
  81. if( !isset($js_check_fcn) )
  82. $js_check_fcn = 'check_'.$id;
  83. if( !isset($selected) )
  84. $selected = smarty_function_get_var( $name, $smarty );
  85. if (!isset($options))
  86. return ''; /* raise error here? */
  87. settype($selected, 'array');
  88. $_html_result = '<table border="0" width="'.$width.'"><tr><td width="45%">';
  89. $width = ($width-40)/2;
  90. $_html_result .= '<select name="'.smarty_function_escape_special_chars($name.'_sel').'" '.$extra.' multiple="multiple" size="'.$size.'" style="width: '.$width.'px;">';
  91. foreach( $options as $_key => $_val )
  92. {
  93. if( !in_array($_key,$selected) )
  94. continue;
  95. $_html_result .= smarty_function_html_select_option($name, $_key, $_val, array(), $extra);
  96. }
  97. $_html_result .= '</select></td>';
  98. $_html_result .= '<td>';
  99. $_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>';
  100. $_html_result .= '<br><br>';
  101. $_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>';
  102. $_html_result .= '</td><td width="45%">';
  103. $_html_result .= '<select name="'.smarty_function_escape_special_chars($name.'_opts').'" '.$extra.' multiple="multiple" size="'.$size.'" style="width: '.$width.'px;">';
  104. foreach( $options as $_key => $_val )
  105. {
  106. if( in_array($_key,$selected) )
  107. continue;
  108. $_html_result .= smarty_function_html_select_option($name, $_key, $_val, array(), $extra);
  109. }
  110. $_html_result .= '</select>';
  111. $_html_result .= "</td></tr></table>\n";
  112. $_html_result .= <<<JSCODE
  113. \n<script language="JavaScript" type="text/javascript">
  114. function dualselect( action, listname )
  115. {
  116. var sel = document.getElementsByName( listname+'_sel' )[0];
  117. var opts = document.getElementsByName( listname+'_opts' )[0];
  118. var i;
  119. if( action == 'del' )
  120. {
  121. for( i=sel.options.length-1; i>=0; i-- )
  122. {
  123. if( sel.options[i].selected && (!window.{$js_check_fcn} || window.{$js_check_fcn}(action,listname,sel.options[i])) )
  124. {
  125. opts.options[opts.options.length] = new Option( sel.options[i].text, sel.options[i].value );
  126. sel.options[i] = null;
  127. }
  128. }
  129. }
  130. else
  131. {
  132. for( i=opts.options.length-1; i>=0; i-- )
  133. {
  134. if( opts.options[i].selected && (!window.{$js_check_fcn} || window.{$js_check_fcn}(action,listname,opts.options[i])) )
  135. {
  136. sel.options[sel.options.length] = new Option( opts.options[i].text, opts.options[i].value );
  137. opts.options[i] = null;
  138. }
  139. }
  140. }
  141. }
  142. </script>\n
  143. JSCODE;
  144. return $_html_result;
  145. }
  146. ?>