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_select_time} function plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     function
Chris PeBenito 696b41
 * Name:     html_select_time
Chris PeBenito 696b41
 * Purpose:  Prints the dropdowns for time selection
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.html.select.time.php {html_select_time}
Chris PeBenito 696b41
 *          (Smarty online manual)
Chris PeBenito 696b41
 * @param array
Chris PeBenito 696b41
 * @param Smarty
Chris PeBenito 696b41
 * @return string
Chris PeBenito 696b41
 * @uses smarty_make_timestamp()
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_function_html_select_time($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
Chris PeBenito 696b41
    require_once $smarty->_get_plugin_filepath('function','html_options');
Chris PeBenito 696b41
    /* Default values. */
Chris PeBenito 696b41
    $prefix             = "Time_";
Chris PeBenito 696b41
    $time               = time();
Chris PeBenito 696b41
    $display_hours      = true;
Chris PeBenito 696b41
    $display_minutes    = true;
Chris PeBenito 696b41
    $display_seconds    = true;
Chris PeBenito 696b41
    $display_meridian   = true;
Chris PeBenito 696b41
    $use_24_hours       = true;
Chris PeBenito 696b41
    $minute_interval    = 1;
Chris PeBenito 696b41
    $second_interval    = 1;
Chris PeBenito 696b41
    /* Should the select boxes be part of an array when returned from PHP?
Chris PeBenito 696b41
       e.g. setting it to "birthday", would create "birthday[Hour]",
Chris PeBenito 696b41
       "birthday[Minute]", "birthday[Seconds]" & "birthday[Meridian]".
Chris PeBenito 696b41
       Can be combined with prefix. */
Chris PeBenito 696b41
    $field_array        = null;
Chris PeBenito 696b41
    $all_extra          = null;
Chris PeBenito 696b41
    $hour_extra         = null;
Chris PeBenito 696b41
    $minute_extra       = null;
Chris PeBenito 696b41
    $second_extra       = null;
Chris PeBenito 696b41
    $meridian_extra     = null;
Chris PeBenito 696b41
Chris PeBenito 696b41
    foreach ($params as $_key=>$_value) {
Chris PeBenito 696b41
        switch ($_key) {
Chris PeBenito 696b41
            case 'prefix':
Chris PeBenito 696b41
            case 'time':
Chris PeBenito 696b41
            case 'field_array':
Chris PeBenito 696b41
            case 'all_extra':
Chris PeBenito 696b41
            case 'hour_extra':
Chris PeBenito 696b41
            case 'minute_extra':
Chris PeBenito 696b41
            case 'second_extra':
Chris PeBenito 696b41
            case 'meridian_extra':
Chris PeBenito 696b41
                $$_key = (string)$_value;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'display_hours':
Chris PeBenito 696b41
            case 'display_minutes':
Chris PeBenito 696b41
            case 'display_seconds':
Chris PeBenito 696b41
            case 'display_meridian':
Chris PeBenito 696b41
            case 'use_24_hours':
Chris PeBenito 696b41
                $$_key = (bool)$_value;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'minute_interval':
Chris PeBenito 696b41
            case 'second_interval':
Chris PeBenito 696b41
                $$_key = (int)$_value;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            default:
Chris PeBenito 696b41
                $smarty->trigger_error("[html_select_time] unknown parameter $_key", E_USER_WARNING);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    $time = smarty_make_timestamp($time);
Chris PeBenito 696b41
Chris PeBenito 696b41
    $html_result = '';
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($display_hours) {
Chris PeBenito 696b41
        $hours       = $use_24_hours ? range(0, 23) : range(1, 12);
Chris PeBenito 696b41
        $hour_fmt = $use_24_hours ? '%H' : '%I';
Chris PeBenito 696b41
        for ($i = 0, $for_max = count($hours); $i < $for_max; $i++)
Chris PeBenito 696b41
            $hours[$i] = sprintf('%02d', $hours[$i]);
Chris PeBenito 696b41
        $html_result .= '
Chris PeBenito 696b41
        if (null !== $field_array) {
Chris PeBenito 696b41
            $html_result .= '"' . $field_array . '[' . $prefix . 'Hour]"';
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $html_result .= '"' . $prefix . 'Hour"';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if (null !== $hour_extra){
Chris PeBenito 696b41
            $html_result .= ' ' . $hour_extra;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if (null !== $all_extra){
Chris PeBenito 696b41
            $html_result .= ' ' . $all_extra;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        $html_result .= '>'."\n";
Chris PeBenito 696b41
        $html_result .= smarty_function_html_options(array('output'          => $hours,
Chris PeBenito 696b41
                                                           'values'          => $hours,
Chris PeBenito 696b41
                                                           'selected'      => strftime($hour_fmt, $time),
Chris PeBenito 696b41
                                                           'print_result' => false),
Chris PeBenito 696b41
                                                     $smarty);
Chris PeBenito 696b41
        $html_result .= "</select>\n";
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($display_minutes) {
Chris PeBenito 696b41
        $all_minutes = range(0, 59);
Chris PeBenito 696b41
        for ($i = 0, $for_max = count($all_minutes); $i < $for_max; $i+= $minute_interval)
Chris PeBenito 696b41
            $minutes[] = sprintf('%02d', $all_minutes[$i]);
Chris PeBenito 696b41
        $selected = intval(floor(strftime('%M', $time) / $minute_interval) * $minute_interval);
Chris PeBenito 696b41
        $html_result .= '
Chris PeBenito 696b41
        if (null !== $field_array) {
Chris PeBenito 696b41
            $html_result .= '"' . $field_array . '[' . $prefix . 'Minute]"';
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $html_result .= '"' . $prefix . 'Minute"';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if (null !== $minute_extra){
Chris PeBenito 696b41
            $html_result .= ' ' . $minute_extra;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if (null !== $all_extra){
Chris PeBenito 696b41
            $html_result .= ' ' . $all_extra;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        $html_result .= '>'."\n";
Chris PeBenito 696b41
        
Chris PeBenito 696b41
        $html_result .= smarty_function_html_options(array('output'          => $minutes,
Chris PeBenito 696b41
                                                           'values'          => $minutes,
Chris PeBenito 696b41
                                                           'selected'      => $selected,
Chris PeBenito 696b41
                                                           'print_result' => false),
Chris PeBenito 696b41
                                                     $smarty);
Chris PeBenito 696b41
        $html_result .= "</select>\n";
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($display_seconds) {
Chris PeBenito 696b41
        $all_seconds = range(0, 59);
Chris PeBenito 696b41
        for ($i = 0, $for_max = count($all_seconds); $i < $for_max; $i+= $second_interval)
Chris PeBenito 696b41
            $seconds[] = sprintf('%02d', $all_seconds[$i]);
Chris PeBenito 696b41
        $selected = intval(floor(strftime('%S', $time) / $second_interval) * $second_interval);
Chris PeBenito 696b41
        $html_result .= '
Chris PeBenito 696b41
        if (null !== $field_array) {
Chris PeBenito 696b41
            $html_result .= '"' . $field_array . '[' . $prefix . 'Second]"';
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $html_result .= '"' . $prefix . 'Second"';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        
Chris PeBenito 696b41
        if (null !== $second_extra){
Chris PeBenito 696b41
            $html_result .= ' ' . $second_extra;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if (null !== $all_extra){
Chris PeBenito 696b41
            $html_result .= ' ' . $all_extra;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        $html_result .= '>'."\n";
Chris PeBenito 696b41
        
Chris PeBenito 696b41
        $html_result .= smarty_function_html_options(array('output'          => $seconds,
Chris PeBenito 696b41
                                                           'values'          => $seconds,
Chris PeBenito 696b41
                                                           'selected'      => $selected,
Chris PeBenito 696b41
                                                           'print_result' => false),
Chris PeBenito 696b41
                                                     $smarty);
Chris PeBenito 696b41
        $html_result .= "</select>\n";
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($display_meridian && !$use_24_hours) {
Chris PeBenito 696b41
        $html_result .= '
Chris PeBenito 696b41
        if (null !== $field_array) {
Chris PeBenito 696b41
            $html_result .= '"' . $field_array . '[' . $prefix . 'Meridian]"';
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $html_result .= '"' . $prefix . 'Meridian"';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        
Chris PeBenito 696b41
        if (null !== $meridian_extra){
Chris PeBenito 696b41
            $html_result .= ' ' . $meridian_extra;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if (null !== $all_extra){
Chris PeBenito 696b41
            $html_result .= ' ' . $all_extra;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        $html_result .= '>'."\n";
Chris PeBenito 696b41
        
Chris PeBenito 696b41
        $html_result .= smarty_function_html_options(array('output'          => array('AM', 'PM'),
Chris PeBenito 696b41
                                                           'values'          => array('am', 'pm'),
Chris PeBenito 696b41
                                                           'selected'      => strtolower(strftime('%p', $time)),
Chris PeBenito 696b41
                                                           'print_result' => false),
Chris PeBenito 696b41
                                                     $smarty);
Chris PeBenito 696b41
        $html_result .= "</select>\n";
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    return $html_result;
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>