X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=smarty%2Fplugins%2Ffunction.html_select_date.php;h=6153c59f26ea23905461ab354cf7fe696a4f87cd;hb=94d8eb634c53fea341b849bf13f9224e70db2d16;hp=dd90e921e385256fa60cc1465f27ed381dafd66e;hpb=49b2955320c19e4e23097c66e438e51fdc70fc58;p=contagged diff --git a/smarty/plugins/function.html_select_date.php b/smarty/plugins/function.html_select_date.php index dd90e92..6153c59 100644 --- a/smarty/plugins/function.html_select_date.php +++ b/smarty/plugins/function.html_select_date.php @@ -18,20 +18,24 @@ * and end year values. (Monte) * - 1.2 added support for yyyy-mm-dd syntax for * time value. (Jan Rosier) - * - 1.3 added support for choosing format for + * - 1.3 added support for choosing format for * month values (Gary Loescher) * - 1.3.1 added support for choosing format for * day values (Marcus Bointon) + * - 1.3.2 support negative timestamps, force year + * dropdown to include given date unless explicitly set (Monte) * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date} * (Smarty online manual) - * @version 1.3 - * @author Andrei Zmievski + * @version 1.3.2 + * @author Andrei Zmievski + * @author Monte Ohrt * @param array * @param Smarty * @return string */ function smarty_function_html_select_date($params, &$smarty) { + require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); require_once $smarty->_get_plugin_filepath('shared','make_timestamp'); require_once $smarty->_get_plugin_filepath('function','html_options'); /* Default values. */ @@ -68,40 +72,104 @@ function smarty_function_html_select_date($params, &$smarty) $year_extra = null; /* Order in which to display the fields. "D" -> day, "M" -> month, "Y" -> year. */ - $field_order = 'MDY'; + $field_order = 'MDY'; /* String printed between the different fields. */ $field_separator = "\n"; - $time = time(); - - - extract($params); - - // If $time is not in format yyyy-mm-dd - if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $time)) { - // then $time is empty or unix timestamp or mysql timestamp - // using smarty_make_timestamp to get an unix timestamp and - // strftime to make yyyy-mm-dd - $time = strftime('%Y-%m-%d', smarty_make_timestamp($time)); - } - // Now split this in pieces, which later can be used to set the select - $time = explode("-", $time); - - // make syntax "+N" or "-N" work with start_year and end_year - if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) { - if ($match[1] == '+') { - $end_year = strftime('%Y') + $match[2]; - } else { - $end_year = strftime('%Y') - $match[2]; - } - } - if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) { - if ($match[1] == '+') { - $start_year = strftime('%Y') + $match[2]; - } else { - $start_year = strftime('%Y') - $match[2]; - } - } - + $time = time(); + $all_empty = null; + $day_empty = null; + $month_empty = null; + $year_empty = null; + $extra_attrs = ''; + + foreach ($params as $_key=>$_value) { + switch ($_key) { + case 'prefix': + case 'time': + case 'start_year': + case 'end_year': + case 'month_format': + case 'day_format': + case 'day_value_format': + case 'field_array': + case 'day_size': + case 'month_size': + case 'year_size': + case 'all_extra': + case 'day_extra': + case 'month_extra': + case 'year_extra': + case 'field_order': + case 'field_separator': + case 'month_value_format': + case 'month_empty': + case 'day_empty': + case 'year_empty': + $$_key = (string)$_value; + break; + + case 'all_empty': + $$_key = (string)$_value; + $day_empty = $month_empty = $year_empty = $all_empty; + break; + + case 'display_days': + case 'display_months': + case 'display_years': + case 'year_as_text': + case 'reverse_years': + $$_key = (bool)$_value; + break; + + default: + if(!is_array($_value)) { + $extra_attrs .= ' '.$_key.'="'.smarty_function_escape_special_chars($_value).'"'; + } else { + $smarty->trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE); + } + break; + } + } + + if(preg_match('!^-\d+$!',$time)) { + // negative timestamp, use date() + $time = date('Y-m-d',$time); + } + // If $time is not in format yyyy-mm-dd + if (!preg_match('/^\d{0,4}-\d{0,2}-\d{0,2}$/', $time)) { + // use smarty_make_timestamp to get an unix timestamp and + // strftime to make yyyy-mm-dd + $time = strftime('%Y-%m-%d', smarty_make_timestamp($time)); + } + // Now split this in pieces, which later can be used to set the select + $time = explode("-", $time); + + // make syntax "+N" or "-N" work with start_year and end_year + if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) { + if ($match[1] == '+') { + $end_year = strftime('%Y') + $match[2]; + } else { + $end_year = strftime('%Y') - $match[2]; + } + } + if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) { + if ($match[1] == '+') { + $start_year = strftime('%Y') + $match[2]; + } else { + $start_year = strftime('%Y') - $match[2]; + } + } + if (strlen($time[0]) > 0) { + if ($start_year > $time[0] && !isset($params['start_year'])) { + // force start year to include given date if not explicitly set + $start_year = $time[0]; + } + if($end_year < $time[0] && !isset($params['end_year'])) { + // force end year to include given date if not explicitly set + $end_year = $time[0]; + } + } + $field_order = strtoupper($field_order); $html_result = $month_result = $day_result = $year_result = ""; @@ -109,10 +177,13 @@ function smarty_function_html_select_date($params, &$smarty) if ($display_months) { $month_names = array(); $month_values = array(); - + if(isset($month_empty)) { + $month_names[''] = $month_empty; + $month_values[''] = ''; + } for ($i = 1; $i <= 12; $i++) { - $month_names[] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000)); - $month_values[] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000)); + $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000)); + $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000)); } $month_result .= ''; } if ($display_days) { $days = array(); + if (isset($day_empty)) { + $days[''] = $day_empty; + $day_values[''] = ''; + } for ($i = 1; $i <= 31; $i++) { $days[] = sprintf($day_format, $i); $day_values[] = sprintf($day_value_format, $i); @@ -163,7 +237,7 @@ function smarty_function_html_select_date($params, &$smarty) if (null !== $day_extra){ $day_result .= ' ' . $day_extra; } - $day_result .= '>'."\n"; + $day_result .= $extra_attrs . '>'."\n"; $day_result .= smarty_function_html_options(array('output' => $days, 'values' => $day_values, 'selected' => $time[2], @@ -186,13 +260,19 @@ function smarty_function_html_select_date($params, &$smarty) if (null !== $year_extra){ $year_result .= ' ' . $year_extra; } - $year_result .= '>'; + $year_result .= ' />'; } else { $years = range((int)$start_year, (int)$end_year); if ($reverse_years) { rsort($years, SORT_NUMERIC); + } else { + sort($years, SORT_NUMERIC); + } + $yearvals = $years; + if(isset($year_empty)) { + array_unshift($years, $year_empty); + array_unshift($yearvals, ''); } - $year_result .= '