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
 * Smarty {html_select_date} plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     function
Chris PeBenito 696b41
 * Name:     html_select_date
Chris PeBenito 696b41
 * Purpose:  Prints the dropdowns for date selection.
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * ChangeLog:
Chris PeBenito 696b41
 *           - 1.0 initial release
Chris PeBenito 696b41
 *           - 1.1 added support for +/- N syntax for begin
Chris PeBenito 696b41
 *                and end year values. (Monte)
Chris PeBenito 696b41
 *           - 1.2 added support for yyyy-mm-dd syntax for
Chris PeBenito 696b41
 *                time value. (Jan Rosier)
Chris PeBenito 696b41
 *           - 1.3 added support for choosing format for
Chris PeBenito 696b41
 *                month values (Gary Loescher)
Chris PeBenito 696b41
 *           - 1.3.1 added support for choosing format for
Chris PeBenito 696b41
 *                day values (Marcus Bointon)
Chris PeBenito 696b41
 *           - 1.3.2 suppport negative timestamps, force year
Chris PeBenito 696b41
 *             dropdown to include given date unless explicitly set (Monte)
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
Chris PeBenito 696b41
 *      (Smarty online manual)
Chris PeBenito 696b41
 * @version 1.3.2
Chris PeBenito 696b41
 * @author   Andrei Zmievski
Chris PeBenito 696b41
 * @param array
Chris PeBenito 696b41
 * @param Smarty
Chris PeBenito 696b41
 * @return string
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_function_html_select_date($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          = "Date_";
Chris PeBenito 696b41
    $start_year      = strftime("%Y");
Chris PeBenito 696b41
    $end_year        = $start_year;
Chris PeBenito 696b41
    $display_days    = true;
Chris PeBenito 696b41
    $display_months  = true;
Chris PeBenito 696b41
    $display_years   = true;
Chris PeBenito 696b41
    $month_format    = "%B";
Chris PeBenito 696b41
    /* Write months as numbers by default  GL */
Chris PeBenito 696b41
    $month_value_format = "%m";
Chris PeBenito 696b41
    $day_format      = "%02d";
Chris PeBenito 696b41
    /* Write day values using this format MB */
Chris PeBenito 696b41
    $day_value_format = "%d";
Chris PeBenito 696b41
    $year_as_text    = false;
Chris PeBenito 696b41
    /* Display years in reverse order? Ie. 2000,1999,.... */
Chris PeBenito 696b41
    $reverse_years   = false;
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[Day]",
Chris PeBenito 696b41
       "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
Chris PeBenito 696b41
    $field_array     = null;
Chris PeBenito 696b41
    /* <select size>'s of the different <select> tags.
Chris PeBenito 696b41
       If not set, uses default dropdown. */
Chris PeBenito 696b41
    $day_size        = null;
Chris PeBenito 696b41
    $month_size      = null;
Chris PeBenito 696b41
    $year_size       = null;
Chris PeBenito 696b41
    /* Unparsed attributes common to *ALL* the <select>/<input> tags.
Chris PeBenito 696b41
       An example might be in the template: all_extra ='class ="foo"'. */
Chris PeBenito 696b41
    $all_extra       = null;
Chris PeBenito 696b41
    /* Separate attributes for the tags. */
Chris PeBenito 696b41
    $day_extra       = null;
Chris PeBenito 696b41
    $month_extra     = null;
Chris PeBenito 696b41
    $year_extra      = null;
Chris PeBenito 696b41
    /* Order in which to display the fields.
Chris PeBenito 696b41
       "D" -> day, "M" -> month, "Y" -> year. */
Chris PeBenito 696b41
    $field_order     = 'MDY';
Chris PeBenito 696b41
    /* String printed between the different fields. */
Chris PeBenito 696b41
    $field_separator = "\n";
Chris PeBenito 696b41
    $time = time();
Chris PeBenito 696b41
    $all_empty       = null;
Chris PeBenito 696b41
    $day_empty       = null;
Chris PeBenito 696b41
    $month_empty     = null;
Chris PeBenito 696b41
    $year_empty      = 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 'start_year':
Chris PeBenito 696b41
            case 'end_year':
Chris PeBenito 696b41
            case 'month_format':
Chris PeBenito 696b41
            case 'day_format':
Chris PeBenito 696b41
            case 'day_value_format':
Chris PeBenito 696b41
            case 'field_array':
Chris PeBenito 696b41
            case 'day_size':
Chris PeBenito 696b41
            case 'month_size':
Chris PeBenito 696b41
            case 'year_size':
Chris PeBenito 696b41
            case 'all_extra':
Chris PeBenito 696b41
            case 'day_extra':
Chris PeBenito 696b41
            case 'month_extra':
Chris PeBenito 696b41
            case 'year_extra':
Chris PeBenito 696b41
            case 'field_order':
Chris PeBenito 696b41
            case 'field_separator':
Chris PeBenito 696b41
            case 'month_value_format':
Chris PeBenito 696b41
            case 'month_empty':
Chris PeBenito 696b41
            case 'day_empty':
Chris PeBenito 696b41
            case 'year_empty':
Chris PeBenito 696b41
                $$_key = (string)$_value;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'all_empty':
Chris PeBenito 696b41
                $$_key = (string)$_value;
Chris PeBenito 696b41
                $day_empty = $month_empty = $year_empty = $all_empty;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'display_days':
Chris PeBenito 696b41
            case 'display_months':
Chris PeBenito 696b41
            case 'display_years':
Chris PeBenito 696b41
            case 'year_as_text':
Chris PeBenito 696b41
            case 'reverse_years':
Chris PeBenito 696b41
                $$_key = (bool)$_value;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            default:
Chris PeBenito 696b41
                $smarty->trigger_error("[html_select_date] unknown parameter $_key", E_USER_WARNING);
Chris PeBenito 696b41
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if(preg_match('!^-\d+$!',$time)) {
Chris PeBenito 696b41
        // negative timestamp, use date()
Chris PeBenito 696b41
        $time = date('Y-m-d',$time);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    // If $time is not in format yyyy-mm-dd
Chris PeBenito 696b41
    if (!preg_match('/^\d{0,4}-\d{0,2}-\d{0,2}$/', $time)) {
Chris PeBenito 696b41
        // use smarty_make_timestamp to get an unix timestamp and
Chris PeBenito 696b41
        // strftime to make yyyy-mm-dd
Chris PeBenito 696b41
        $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    // Now split this in pieces, which later can be used to set the select
Chris PeBenito 696b41
    $time = explode("-", $time);
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    // make syntax "+N" or "-N" work with start_year and end_year
Chris PeBenito 696b41
    if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
Chris PeBenito 696b41
        if ($match[1] == '+') {
Chris PeBenito 696b41
            $end_year = strftime('%Y') + $match[2];
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $end_year = strftime('%Y') - $match[2];
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
Chris PeBenito 696b41
        if ($match[1] == '+') {
Chris PeBenito 696b41
            $start_year = strftime('%Y') + $match[2];
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $start_year = strftime('%Y') - $match[2];
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    if (strlen($time[0]) > 0) { 
Chris PeBenito 696b41
        if ($start_year > $time[0] && !isset($params['start_year'])) {
Chris PeBenito 696b41
            // force start year to include given date if not explicitly set
Chris PeBenito 696b41
            $start_year = $time[0];
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if($end_year < $time[0] && !isset($params['end_year'])) {
Chris PeBenito 696b41
            // force end year to include given date if not explicitly set
Chris PeBenito 696b41
            $end_year = $time[0];
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    $field_order = strtoupper($field_order);
Chris PeBenito 696b41
Chris PeBenito 696b41
    $html_result = $month_result = $day_result = $year_result = "";
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($display_months) {
Chris PeBenito 696b41
        $month_names = array();
Chris PeBenito 696b41
        $month_values = array();
Chris PeBenito 696b41
        if(isset($month_empty)) {
Chris PeBenito 696b41
            $month_names[''] = $month_empty;
Chris PeBenito 696b41
            $month_values[''] = '';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        for ($i = 1; $i <= 12; $i++) {
Chris PeBenito 696b41
            $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
Chris PeBenito 696b41
            $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        $month_result .= '
Chris PeBenito 696b41
        if (null !== $field_array){
Chris PeBenito 696b41
            $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $month_result .= '"' . $prefix . 'Month"';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if (null !== $month_size){
Chris PeBenito 696b41
            $month_result .= ' size="' . $month_size . '"';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if (null !== $month_extra){
Chris PeBenito 696b41
            $month_result .= ' ' . $month_extra;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if (null !== $all_extra){
Chris PeBenito 696b41
            $month_result .= ' ' . $all_extra;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        $month_result .= '>'."\n";
Chris PeBenito 696b41
Chris PeBenito 696b41
        $month_result .= smarty_function_html_options(array('output'     => $month_names,
Chris PeBenito 696b41
                                                            'values'     => $month_values,
Chris PeBenito 696b41
                                                            'selected'   => $a=$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '',
Chris PeBenito 696b41
                                                            'print_result' => false),
Chris PeBenito 696b41
                                                      $smarty);
Chris PeBenito 696b41
        $month_result .= '</select>';
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($display_days) {
Chris PeBenito 696b41
        $days = array();
Chris PeBenito 696b41
        if (isset($day_empty)) {
Chris PeBenito 696b41
            $days[''] = $day_empty;
Chris PeBenito 696b41
            $day_values[''] = '';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        for ($i = 1; $i <= 31; $i++) {
Chris PeBenito 696b41
            $days[] = sprintf($day_format, $i);
Chris PeBenito 696b41
            $day_values[] = sprintf($day_value_format, $i);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        $day_result .= '
Chris PeBenito 696b41
        if (null !== $field_array){
Chris PeBenito 696b41
            $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $day_result .= '"' . $prefix . 'Day"';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if (null !== $day_size){
Chris PeBenito 696b41
            $day_result .= ' size="' . $day_size . '"';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if (null !== $all_extra){
Chris PeBenito 696b41
            $day_result .= ' ' . $all_extra;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if (null !== $day_extra){
Chris PeBenito 696b41
            $day_result .= ' ' . $day_extra;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        $day_result .= '>'."\n";
Chris PeBenito 696b41
        $day_result .= smarty_function_html_options(array('output'     => $days,
Chris PeBenito 696b41
                                                          'values'     => $day_values,
Chris PeBenito 696b41
                                                          'selected'   => $time[2],
Chris PeBenito 696b41
                                                          'print_result' => false),
Chris PeBenito 696b41
                                                    $smarty);
Chris PeBenito 696b41
        $day_result .= '</select>';
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($display_years) {
Chris PeBenito 696b41
        if (null !== $field_array){
Chris PeBenito 696b41
            $year_name = $field_array . '[' . $prefix . 'Year]';
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $year_name = $prefix . 'Year';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if ($year_as_text) {
Chris PeBenito 696b41
            $year_result .= '
Chris PeBenito 696b41
            if (null !== $all_extra){
Chris PeBenito 696b41
                $year_result .= ' ' . $all_extra;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            if (null !== $year_extra){
Chris PeBenito 696b41
                $year_result .= ' ' . $year_extra;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            $year_result .= '>';
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $years = range((int)$start_year, (int)$end_year);
Chris PeBenito 696b41
            if ($reverse_years) {
Chris PeBenito 696b41
                rsort($years, SORT_NUMERIC);
Chris PeBenito 696b41
            } else {
Chris PeBenito 696b41
                sort($years, SORT_NUMERIC);
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            $yearvals = $years;
Chris PeBenito 696b41
            if(isset($year_empty)) {
Chris PeBenito 696b41
                array_unshift($years, $year_empty);
Chris PeBenito 696b41
                array_unshift($yearvals, '');
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            $year_result .= '
Chris PeBenito 696b41
            if (null !== $year_size){
Chris PeBenito 696b41
                $year_result .= ' size="' . $year_size . '"';
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            if (null !== $all_extra){
Chris PeBenito 696b41
                $year_result .= ' ' . $all_extra;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            if (null !== $year_extra){
Chris PeBenito 696b41
                $year_result .= ' ' . $year_extra;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            $year_result .= '>'."\n";
Chris PeBenito 696b41
            $year_result .= smarty_function_html_options(array('output' => $years,
Chris PeBenito 696b41
                                                               'values' => $yearvals,
Chris PeBenito 696b41
                                                               'selected'   => $time[0],
Chris PeBenito 696b41
                                                               'print_result' => false),
Chris PeBenito 696b41
                                                         $smarty);
Chris PeBenito 696b41
            $year_result .= '</select>';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    // Loop thru the field_order field
Chris PeBenito 696b41
    for ($i = 0; $i <= 2; $i++){
Chris PeBenito 696b41
        $c = substr($field_order, $i, 1);
Chris PeBenito 696b41
        switch ($c){
Chris PeBenito 696b41
            case 'D':
Chris PeBenito 696b41
                $html_result .= $day_result;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'M':
Chris PeBenito 696b41
                $html_result .= $month_result;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'Y':
Chris PeBenito 696b41
                $html_result .= $year_result;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        // Add the field seperator
Chris PeBenito 696b41
        if($i != 2) {
Chris PeBenito 696b41
            $html_result .= $field_separator;
Chris PeBenito 696b41
        }
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
?>