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_checkboxes} function plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * File:       function.html_checkboxes.php
Chris PeBenito 696b41
 * Type:       function
Chris PeBenito 696b41
 * Name:       html_checkboxes
Chris PeBenito 696b41
 * Date:       24.Feb.2003
Chris PeBenito 696b41
 * Purpose:    Prints out a list of checkbox input types
Chris PeBenito 696b41
 * Input:
Chris PeBenito 696b41
 *           - name       (optional) - string default "checkbox"
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 checkbox
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_checkboxes values=$ids output=$names}
Chris PeBenito 696b41
 * {html_checkboxes values=$ids name='box' separator='
' output=$names}
Chris PeBenito 696b41
 * {html_checkboxes values=$ids checked=$checked separator='
' output=$names}
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
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_checkboxes($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 = 'checkbox';
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
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 = $_val;
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 'checked':
Chris PeBenito 696b41
            case 'selected':
Chris PeBenito 696b41
                $selected = array_map('strval', array_values((array)$_val));
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'checkboxes':
Chris PeBenito 696b41
                $smarty->trigger_error('html_checkboxes: the use of the "checkboxes" 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_checkboxes: 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
    settype($selected, 'array');
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_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
Chris PeBenito 696b41
Chris PeBenito 696b41
Chris PeBenito 696b41
    } else {
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_checkboxes_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_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels) {
Chris PeBenito 696b41
    $_output = '';
Chris PeBenito 696b41
    if ($labels) $_output .= '<label>';
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 (in_array((string)$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
?>