mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-02 17:34:26 +02:00
Fixed unselected year/month/day not working in html_select_date
Fixes #395
This commit is contained in:
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
- Fixed use of `rand()` without a parameter in math function [#794](https://github.com/smarty-php/smarty/issues/794)
|
||||
- Fixed unselected year/month/day not working in html_select_date [#395](https://github.com/smarty-php/smarty/issues/395)
|
||||
|
||||
## [3.1.46] - 2022-08-01
|
||||
|
||||
|
@@ -101,6 +101,7 @@ function smarty_function_html_select_date($params, Smarty_Internal_Template $tem
|
||||
$field_separator = "\n";
|
||||
$option_separator = "\n";
|
||||
$time = null;
|
||||
|
||||
// $all_empty = null;
|
||||
// $day_empty = null;
|
||||
// $month_empty = null;
|
||||
@@ -113,17 +114,7 @@ function smarty_function_html_select_date($params, Smarty_Internal_Template $tem
|
||||
foreach ($params as $_key => $_value) {
|
||||
switch ($_key) {
|
||||
case 'time':
|
||||
if (!is_array($_value) && $_value !== null) {
|
||||
$template->_checkPlugins(
|
||||
array(
|
||||
array(
|
||||
'function' => 'smarty_make_timestamp',
|
||||
'file' => SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php'
|
||||
)
|
||||
)
|
||||
);
|
||||
$time = smarty_make_timestamp($_value);
|
||||
}
|
||||
$$_key = $_value; // we'll handle conversion below
|
||||
break;
|
||||
case 'month_names':
|
||||
if (is_array($_value) && count($_value) === 12) {
|
||||
@@ -178,43 +169,59 @@ function smarty_function_html_select_date($params, Smarty_Internal_Template $tem
|
||||
}
|
||||
// Note: date() is faster than strftime()
|
||||
// Note: explode(date()) is faster than date() date() date()
|
||||
if (isset($params[ 'time' ]) && is_array($params[ 'time' ])) {
|
||||
if (isset($params[ 'time' ][ $prefix . 'Year' ])) {
|
||||
|
||||
if (isset($time) && is_array($time)) {
|
||||
if (isset($time[$prefix . 'Year'])) {
|
||||
// $_REQUEST[$field_array] given
|
||||
foreach (array(
|
||||
'Y' => 'Year',
|
||||
'm' => 'Month',
|
||||
'd' => 'Day'
|
||||
) as $_elementKey => $_elementName) {
|
||||
'Y' => 'Year',
|
||||
'm' => 'Month',
|
||||
'd' => 'Day'
|
||||
) as $_elementKey => $_elementName) {
|
||||
$_variableName = '_' . strtolower($_elementName);
|
||||
$$_variableName =
|
||||
isset($params[ 'time' ][ $prefix . $_elementName ]) ? $params[ 'time' ][ $prefix . $_elementName ] :
|
||||
isset($time[$prefix . $_elementName]) ? $time[$prefix . $_elementName] :
|
||||
date($_elementKey);
|
||||
}
|
||||
} elseif (isset($params[ 'time' ][ $field_array ][ $prefix . 'Year' ])) {
|
||||
} elseif (isset($time[$field_array][$prefix . 'Year'])) {
|
||||
// $_REQUEST given
|
||||
foreach (array(
|
||||
'Y' => 'Year',
|
||||
'm' => 'Month',
|
||||
'd' => 'Day'
|
||||
) as $_elementKey => $_elementName) {
|
||||
'Y' => 'Year',
|
||||
'm' => 'Month',
|
||||
'd' => 'Day'
|
||||
) as $_elementKey => $_elementName) {
|
||||
$_variableName = '_' . strtolower($_elementName);
|
||||
$$_variableName = isset($params[ 'time' ][ $field_array ][ $prefix . $_elementName ]) ?
|
||||
$params[ 'time' ][ $field_array ][ $prefix . $_elementName ] : date($_elementKey);
|
||||
$$_variableName = isset($time[$field_array][$prefix . $_elementName]) ?
|
||||
$time[$field_array][$prefix . $_elementName] : date($_elementKey);
|
||||
}
|
||||
} else {
|
||||
// no date found, use NOW
|
||||
list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
|
||||
[$_year, $_month, $_day] = explode('-', date('Y-m-d'));
|
||||
}
|
||||
} elseif (isset($time) && preg_match("/(\d*)-(\d*)-(\d*)/", $time, $matches)) {
|
||||
$_year = $_month = $_day = null;
|
||||
if ($matches[1] > '') $_year = (int) $matches[1];
|
||||
if ($matches[2] > '') $_month = (int) $matches[2];
|
||||
if ($matches[3] > '') $_day = (int) $matches[3];
|
||||
} elseif ($time === null) {
|
||||
if (array_key_exists('time', $params)) {
|
||||
$_year = $_month = $_day = $time = null;
|
||||
$_year = $_month = $_day = null;
|
||||
} else {
|
||||
list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
|
||||
[$_year, $_month, $_day] = explode('-', date('Y-m-d'));
|
||||
}
|
||||
} else {
|
||||
list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d', $time));
|
||||
$template->_checkPlugins(
|
||||
array(
|
||||
array(
|
||||
'function' => 'smarty_make_timestamp',
|
||||
'file' => SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php'
|
||||
)
|
||||
)
|
||||
);
|
||||
$time = smarty_make_timestamp($time);
|
||||
[$_year, $_month, $_day] = explode('-', date('Y-m-d', $time));
|
||||
}
|
||||
|
||||
// make syntax "+N" or "-N" work with $start_year and $end_year
|
||||
// Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr
|
||||
foreach (array(
|
||||
|
@@ -238,6 +238,7 @@ class PluginFunctionHtmlSelectDateTest extends PHPUnit_Smarty
|
||||
|
||||
$this->years['default'] = "<option value=\"{$year}\" selected=\"selected\">{$year}</option>";
|
||||
$this->years['none'] = "<option value=\"{$year}\">{$year}</option>";
|
||||
|
||||
}
|
||||
|
||||
protected function reverse($string)
|
||||
@@ -394,6 +395,33 @@ class PluginFunctionHtmlSelectDateTest extends PHPUnit_Smarty
|
||||
$this->assertEquals($result, $tpl->fetch());
|
||||
}
|
||||
|
||||
public function testEmptyDayWithDateString() {
|
||||
$n = "\n";
|
||||
$result = '<select name="Date_Month">' . $n . $this->months['default'] . $n . '</select>'
|
||||
. $n . '<select name="Date_Day">' . $n . '<option value="">day</option>' . $n . $this->days['none'] . $n . '</select>'
|
||||
. $n . '<select name="Date_Year">' . $n . $this->years['start_2005'] . $n . '</select>';
|
||||
$tpl = $this->smarty->createTemplate('eval:{html_select_date time="2022-02-" day_empty="day" start_year=2005}');
|
||||
$this->assertEquals($result, $tpl->fetch());
|
||||
}
|
||||
|
||||
public function testEmptyMonthWithDateStrings() {
|
||||
$n = "\n";
|
||||
$result = '<select name="Date_Month">' . $n . '<option value="">month</option>' . $n . $this->months['none'] . $n . '</select>'
|
||||
. $n . '<select name="Date_Day">' . $n . $this->days['default'] . $n . '</select>'
|
||||
. $n . '<select name="Date_Year">' . $n . $this->years['start_2005'] . $n . '</select>';
|
||||
$tpl = $this->smarty->createTemplate('eval:{html_select_date time="2022--20" month_empty="month" start_year=2005}');
|
||||
$this->assertEquals($result, $tpl->fetch());
|
||||
}
|
||||
|
||||
public function testEmptyYearWithDateStrings() {
|
||||
$n = "\n";
|
||||
$result = '<select name="Date_Month">' . $n . $this->months['default'] . $n . '</select>'
|
||||
. $n . '<select name="Date_Day">' . $n . $this->days['default'] . $n . '</select>'
|
||||
. $n . '<select name="Date_Year">' . $n . '<option value="">year</option>' . $n . $this->years['none'] . $n . '</select>';
|
||||
$tpl = $this->smarty->createTemplate('eval:{html_select_date time="-02-20" year_empty="year"}');
|
||||
$this->assertEquals($result, $tpl->fetch());
|
||||
}
|
||||
|
||||
public function testId()
|
||||
{
|
||||
$n = "\n";
|
||||
|
Reference in New Issue
Block a user