diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2ff747ab7..784d5b10a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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 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 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
diff --git a/libs/plugins/function.html_select_date.php b/libs/plugins/function.html_select_date.php
index 763fc60f9..a396046b2 100644
--- a/libs/plugins/function.html_select_date.php
+++ b/libs/plugins/function.html_select_date.php
@@ -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) {
+ foreach ([
+ '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) {
+ foreach ([
+ '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(
diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionHtmlSelectDateTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionHtmlSelectDateTest.php
index 26ea6faa8..0cf4f9ba7 100644
--- a/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionHtmlSelectDateTest.php
+++ b/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionHtmlSelectDateTest.php
@@ -210,7 +210,7 @@ class PluginFunctionHtmlSelectDateTest extends PHPUnit_Smarty
public function setUp(): void
{
$this->setUpSmarty(dirname(__FILE__));
- $this->smarty->setErrorReporting(E_ALL & ~E_DEPRECATED);
+ $this->smarty->setErrorReporting(E_ALL & ~E_DEPRECATED);
$year = date('Y');
$this->now = mktime(15, 0, 0, 2, 20, $year);
@@ -239,6 +239,7 @@ public function setUp(): void
$this->years['default'] = "";
$this->years['none'] = "";
+
}
protected function reverse($string)
@@ -395,6 +396,33 @@ public function testEmptyUnset()
$this->assertEquals($result, $tpl->fetch());
}
+ public function testEmptyDayWithDateString() {
+ $n = "\n";
+ $result = ''
+ . $n . ''
+ . $n . '';
+ $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 = ''
+ . $n . ''
+ . $n . '';
+ $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 = ''
+ . $n . ''
+ . $n . '';
+ $tpl = $this->smarty->createTemplate('eval:{html_select_date time="-02-20" year_empty="year"}');
+ $this->assertEquals($result, $tpl->fetch());
+ }
+
public function testId()
{
$n = "\n";