Fixed unselected year/month/day not working in html_select_date

Fixes #395
This commit is contained in:
Simon Wisselink
2022-09-14 12:44:37 +02:00
parent 55ea25d1f5
commit 813c83f7a3
3 changed files with 68 additions and 32 deletions

View File

@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed PHP8.1 deprecation errors in modifiers (upper, explode, number_format and replace) [#755](https://github.com/smarty-php/smarty/pull/755) and [#788](https://github.com/smarty-php/smarty/pull/788) - Fixed PHP8.1 deprecation errors in modifiers (upper, explode, number_format and replace) [#755](https://github.com/smarty-php/smarty/pull/755) and [#788](https://github.com/smarty-php/smarty/pull/788)
- Fixed PHP8.1 deprecation errors in capitalize modifier [#789](https://github.com/smarty-php/smarty/issues/789) - Fixed PHP8.1 deprecation errors in capitalize modifier [#789](https://github.com/smarty-php/smarty/issues/789)
- Fixed use of `rand()` without a parameter in math function [#794](https://github.com/smarty-php/smarty/issues/794) - 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)
## [4.2.0] - 2022-08-01 ## [4.2.0] - 2022-08-01

View File

@@ -101,6 +101,7 @@ function smarty_function_html_select_date($params, Smarty_Internal_Template $tem
$field_separator = "\n"; $field_separator = "\n";
$option_separator = "\n"; $option_separator = "\n";
$time = null; $time = null;
// $all_empty = null; // $all_empty = null;
// $day_empty = null; // $day_empty = null;
// $month_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) { foreach ($params as $_key => $_value) {
switch ($_key) { switch ($_key) {
case 'time': case 'time':
if (!is_array($_value) && $_value !== null) { $$_key = $_value; // we'll handle conversion below
$template->_checkPlugins(
array(
array(
'function' => 'smarty_make_timestamp',
'file' => SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php'
)
)
);
$time = smarty_make_timestamp($_value);
}
break; break;
case 'month_names': case 'month_names':
if (is_array($_value) && count($_value) === 12) { 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: date() is faster than strftime()
// Note: explode(date()) is faster than date() date() date() // 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 // $_REQUEST[$field_array] given
foreach (array( foreach ([
'Y' => 'Year', 'Y' => 'Year',
'm' => 'Month', 'm' => 'Month',
'd' => 'Day' 'd' => 'Day'
) as $_elementKey => $_elementName) { ] as $_elementKey => $_elementName) {
$_variableName = '_' . strtolower($_elementName); $_variableName = '_' . strtolower($_elementName);
$$_variableName = $$_variableName =
isset($params[ 'time' ][ $prefix . $_elementName ]) ? $params[ 'time' ][ $prefix . $_elementName ] : isset($time[$prefix . $_elementName]) ? $time[$prefix . $_elementName] :
date($_elementKey); date($_elementKey);
} }
} elseif (isset($params[ 'time' ][ $field_array ][ $prefix . 'Year' ])) { } elseif (isset($time[$field_array][$prefix . 'Year'])) {
// $_REQUEST given // $_REQUEST given
foreach (array( foreach ([
'Y' => 'Year', 'Y' => 'Year',
'm' => 'Month', 'm' => 'Month',
'd' => 'Day' 'd' => 'Day'
) as $_elementKey => $_elementName) { ] as $_elementKey => $_elementName) {
$_variableName = '_' . strtolower($_elementName); $_variableName = '_' . strtolower($_elementName);
$$_variableName = isset($params[ 'time' ][ $field_array ][ $prefix . $_elementName ]) ? $$_variableName = isset($time[$field_array][$prefix . $_elementName]) ?
$params[ 'time' ][ $field_array ][ $prefix . $_elementName ] : date($_elementKey); $time[$field_array][$prefix . $_elementName] : date($_elementKey);
} }
} else { } else {
// no date found, use NOW // 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) { } elseif ($time === null) {
if (array_key_exists('time', $params)) { if (array_key_exists('time', $params)) {
$_year = $_month = $_day = $time = null; $_year = $_month = $_day = null;
} else { } else {
list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d')); [$_year, $_month, $_day] = explode('-', date('Y-m-d'));
} }
} else { } 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 // 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 // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr
foreach (array( foreach (array(

View File

@@ -210,7 +210,7 @@ class PluginFunctionHtmlSelectDateTest extends PHPUnit_Smarty
public function setUp(): void public function setUp(): void
{ {
$this->setUpSmarty(dirname(__FILE__)); $this->setUpSmarty(dirname(__FILE__));
$this->smarty->setErrorReporting(E_ALL & ~E_DEPRECATED); $this->smarty->setErrorReporting(E_ALL & ~E_DEPRECATED);
$year = date('Y'); $year = date('Y');
$this->now = mktime(15, 0, 0, 2, 20, $year); $this->now = mktime(15, 0, 0, 2, 20, $year);
@@ -239,6 +239,7 @@ class PluginFunctionHtmlSelectDateTest extends PHPUnit_Smarty
$this->years['default'] = "<option value=\"{$year}\" selected=\"selected\">{$year}</option>"; $this->years['default'] = "<option value=\"{$year}\" selected=\"selected\">{$year}</option>";
$this->years['none'] = "<option value=\"{$year}\">{$year}</option>"; $this->years['none'] = "<option value=\"{$year}\">{$year}</option>";
} }
protected function reverse($string) protected function reverse($string)
@@ -395,6 +396,33 @@ class PluginFunctionHtmlSelectDateTest extends PHPUnit_Smarty
$this->assertEquals($result, $tpl->fetch()); $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() public function testId()
{ {
$n = "\n"; $n = "\n";