Skip to content

Commit

Permalink
Fixed unselected year/month/day not working in html_select_date
Browse files Browse the repository at this point in the history
Fixes #395
  • Loading branch information
wisskid committed Sep 14, 2022
1 parent 55ea25d commit 813c83f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
69 changes: 38 additions & 31 deletions libs/plugins/function.html_select_date.php
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
Expand Up @@ -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);
Expand Down Expand Up @@ -239,6 +239,7 @@ public function setUp(): void

$this->years['default'] = "<option value=\"{$year}\" selected=\"selected\">{$year}</option>";
$this->years['none'] = "<option value=\"{$year}\">{$year}</option>";

}

protected function reverse($string)
Expand Down Expand Up @@ -395,6 +396,33 @@ public function testEmptyUnset()
$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";
Expand Down

0 comments on commit 813c83f

Please sign in to comment.