Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty {html_radios} function plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * File:       function.html_radios.php
Chris PeBenito 696b41
 * Type:       function
Chris PeBenito 696b41
 * Name:       html_radios
Chris PeBenito 696b41
 * Date:       24.Feb.2003
Chris PeBenito 696b41
 * Purpose:    Prints out a list of radio input types
Chris PeBenito 696b41
 * Input:
Chris PeBenito 696b41
 *           - name       (optional) - string default "radio"
Chris PeBenito 696b41
 *           - values     (required) - array
Chris PeBenito 696b41
 *           - options    (optional) - associative array
Chris PeBenito 696b41
 *           - checked    (optional) - array default not set
Chris PeBenito 696b41
 *           - separator  (optional) - ie 
or  
Chris PeBenito 696b41
 *           - output     (optional) - the output next to each radio button
Chris PeBenito 696b41
 *           - assign     (optional) - assign the output as an array to this variable
Chris PeBenito 696b41
 * Examples:
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * {html_radios values=$ids output=$names}
Chris PeBenito 696b41
 * {html_radios values=$ids name='box' separator='
' output=$names}
Chris PeBenito 696b41
 * {html_radios values=$ids checked=$checked separator='
' output=$names}
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios}
Chris PeBenito 696b41
 *      (Smarty online manual)
Chris PeBenito 696b41
 * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
Chris PeBenito 696b41
 * @author credits to Monte Ohrt <monte at ohrt dot com>
Chris PeBenito 696b41
 * @version    1.0
Chris PeBenito 696b41
 * @param array
Chris PeBenito 696b41
 * @param Smarty
Chris PeBenito 696b41
 * @return string
Chris PeBenito 696b41
 * @uses smarty_function_escape_special_chars()
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_function_html_radios($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
Chris PeBenito 696b41
   
Chris PeBenito 696b41
    $name = 'radio';
Chris PeBenito 696b41
    $values = null;
Chris PeBenito 696b41
    $options = null;
Chris PeBenito 696b41
    $selected = null;
Chris PeBenito 696b41
    $separator = '';
Chris PeBenito 696b41
    $labels = true;
Chris PeBenito 696b41
    $output = null;
Chris PeBenito 696b41
    $extra = '';
Chris PeBenito 696b41
Chris PeBenito 696b41
    foreach($params as $_key => $_val) {
Chris PeBenito 696b41
        switch($_key) {
Chris PeBenito 696b41
            case 'name':
Chris PeBenito 696b41
            case 'separator':
Chris PeBenito 696b41
                $$_key = (string)$_val;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'checked':
Chris PeBenito 696b41
            case 'selected':
Chris PeBenito 696b41
                if(is_array($_val)) {
Chris PeBenito 696b41
                    $smarty->trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
Chris PeBenito 696b41
                } else {
Chris PeBenito 696b41
                    $selected = (string)$_val;
Chris PeBenito 696b41
                }
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'labels':
Chris PeBenito 696b41
                $$_key = (bool)$_val;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'options':
Chris PeBenito 696b41
                $$_key = (array)$_val;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'values':
Chris PeBenito 696b41
            case 'output':
Chris PeBenito 696b41
                $$_key = array_values((array)$_val);
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'radios':
Chris PeBenito 696b41
                $smarty->trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
Chris PeBenito 696b41
                $options = (array)$_val;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'assign':
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            default:
Chris PeBenito 696b41
                if(!is_array($_val)) {
Chris PeBenito 696b41
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
Chris PeBenito 696b41
                } else {
Chris PeBenito 696b41
                    $smarty->trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
Chris PeBenito 696b41
                }
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (!isset($options) && !isset($values))
Chris PeBenito 696b41
        return ''; /* raise error here? */
Chris PeBenito 696b41
Chris PeBenito 696b41
    $_html_result = array();
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (isset($options)) {
Chris PeBenito 696b41
Chris PeBenito 696b41
        foreach ($options as $_key=>$_val)
Chris PeBenito 696b41
            $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
Chris PeBenito 696b41
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
Chris PeBenito 696b41
        foreach ($values as $_i=>$_key) {
Chris PeBenito 696b41
            $_val = isset($output[$_i]) ? $output[$_i] : '';
Chris PeBenito 696b41
            $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if(!empty($params['assign'])) {
Chris PeBenito 696b41
        $smarty->assign($params['assign'], $_html_result);
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        return implode("\n",$_html_result);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels) {
Chris PeBenito 696b41
    $_output = '';
Chris PeBenito 696b41
    if ($labels) {
Chris PeBenito 696b41
      $_id = smarty_function_escape_special_chars($name . '_' . $value);
Chris PeBenito 696b41
      $_output .= '<label for="' . $_id . '">';
Chris PeBenito 696b41
   }
Chris PeBenito 696b41
   $_output .= '
Chris PeBenito 696b41
        . smarty_function_escape_special_chars($name) . '" value="'
Chris PeBenito 696b41
        . smarty_function_escape_special_chars($value) . '"';
Chris PeBenito 696b41
Chris PeBenito 696b41
   if ($labels) $_output .= ' id="' . $_id . '"';
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($value==$selected) {
Chris PeBenito 696b41
        $_output .= ' checked="checked"';
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    $_output .= $extra . ' />' . $output;
Chris PeBenito 696b41
    if ($labels) $_output .= '</label>';
Chris PeBenito 696b41
    $_output .=  $separator;
Chris PeBenito 696b41
Chris PeBenito 696b41
    return $_output;
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
?>