Skip to content

Commit

Permalink
Merge pull request #1032 from romainguerrero/feature/input-filter-for…
Browse files Browse the repository at this point in the history
…mat-for-datetime-column

DateTime and Date columns : add input filter format option
  • Loading branch information
romainguerrero committed Apr 1, 2020
2 parents 167cbcf + a738da5 commit a0466ae
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Grid/Column/DateColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class DateColumn extends DateTimeColumn

protected $fallbackFormat = 'Y-m-d';

protected $fallbackInputFormat = 'Y-m-d';

public function getFilters($source)
{
$parentFilters = parent::getFilters($source);
Expand Down
21 changes: 19 additions & 2 deletions Grid/Column/DateTimeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ class DateTimeColumn extends Column

protected $fallbackFormat = 'Y-m-d H:i:s';

protected $inputFormat;

protected $fallbackInputFormat = 'Y-m-d H:i:s';

protected $timezone;

public function __initialize(array $params)
{
parent::__initialize($params);

$this->setFormat($this->getParam('format'));
$this->setInputFormat($this->getParam('inputFormat', $this->fallbackInputFormat));
$this->setOperators($this->getParam('operators', [
self::OPERATOR_EQ,
self::OPERATOR_NEQ,
Expand All @@ -56,7 +61,7 @@ public function isQueryValid($query)

protected function isDateTime($query)
{
return strtotime($query) !== false;
return false !== \DateTime::createFromFormat($this->inputFormat, $query);
}

public function getFilters($source)
Expand All @@ -65,7 +70,7 @@ public function getFilters($source)

$filters = [];
foreach ($parentFilters as $filter) {
$filters[] = ($filter->getValue() === null) ? $filter : $filter->setValue(new \DateTime($filter->getValue()));
$filters[] = ($filter->getValue() === null) ? $filter : $filter->setValue(\DateTime::createFromFormat($this->inputFormat, $filter->getValue()));
}

return $filters;
Expand Down Expand Up @@ -160,6 +165,18 @@ public function getFormat()
return $this->format;
}

public function setInputFormat($inputFormat)
{
$this->inputFormat = $inputFormat;

return $this;
}

public function getInputFormat()
{
return $this->inputFormat;
}

public function getTimezone()
{
return $this->timezone;
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/columns_configuration/types/date_column.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Date Column
===========

The Date Column extends the [DateTime Column](datetime_column.md) and the default fallback format is `Y-m-d`.
The Date Column extends the [DateTime Column](datetime_column.md) and the default fallback format and filter input format are `Y-m-d`.

With this column, the time part of a datetime value is ignored when you filter the column.
So, if you filter the column with the value `2012-04-26` or `2012-04-26 12:23:45` and with the operator `=`, the query will be `date >= '2012-04-26 0:00:00' AND date <= '2012-04-26 23:59:59'`.
Expand Down
5 changes: 4 additions & 1 deletion Resources/doc/columns_configuration/types/datetime_column.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ See [Column annotation for properties](../annotations/column_annotation_property
|:--:|:--|:--|:--|:--|
|format|string| | |Define this attribute if you want to force the format of the displayed value.<br />(e.g. "Y-m-d H:i:s")|
|timezone|string|System default timezone| |The timezone to use for rendering.<br />(e.g. "Europe/Paris")|
|inputFormat|string|"Y-m-d H:i:s"| |Define this attribute if you want to force the format of the filtered value.<br />(e.g. "Y-m-d H:i:s")|

**Note**: If you want to filter using date input (and not datetime input), you should use the [Date Column](date_column.md) type instead and configure the display format to render the time (e.g. "Y-m-d H:i:s").

## Filter
#### Valid values
Expand All @@ -41,4 +44,4 @@ Wrong values are ignored.
|btw|Between exclusive|
|btwe|Between inclusive|
|isNull|Is not defined|
|isNotNull|Is defined|
|isNotNull|Is defined|
58 changes: 55 additions & 3 deletions Tests/Grid/Column/DateTimeColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ public function testGetFormat()
$this->assertEquals($format, $column->getFormat());
}

public function testSetInputFormat()
{
$inputFormat = 'Y-m-d';

$column = new DateTimeColumn();
$column->setInputFormat($inputFormat);

$this->assertAttributeEquals($inputFormat, 'inputFormat', $column);
}

public function testGetInputFormat()
{
$inputFormat = 'Y-m-d';

$column = new DateTimeColumn();
$column->setInputFormat($inputFormat);

$this->assertEquals($inputFormat, $column->getInputFormat());
}

public function testSetTimezone()
{
$timezone = 'UTC';
Expand Down Expand Up @@ -98,11 +118,23 @@ public function testRenderCellWithCallback()
public function testFilterWithValue()
{
$column = new DateTimeColumn();
$column->setData(['operator' => Column::OPERATOR_BTW, 'from' => '2017-03-22', 'to' => '2017-03-23']);
$column->setData(['operator' => Column::OPERATOR_BTW, 'from' => '2017-03-22 01:30:00', 'to' => '2017-03-23 19:00:00']);

$this->assertEquals([
new Filter(Column::OPERATOR_GT, new \DateTime('2017-03-22')),
new Filter(Column::OPERATOR_LT, new \DateTime('2017-03-23')),
new Filter(Column::OPERATOR_GT, new \DateTime('2017-03-22 01:30:00')),
new Filter(Column::OPERATOR_LT, new \DateTime('2017-03-23 19:00:00')),
], $column->getFilters('asource'));
}

public function testFilterWithFormattedValue()
{
$column = new DateTimeColumn();
$column->setInputFormat('m/d/Y H-i-s');
$column->setData(['operator' => Column::OPERATOR_BTW, 'from' => '03/22/2017 01-30-00', 'to' => '03/23/2017 19-00-00']);

$this->assertEquals([
new Filter(Column::OPERATOR_GT, new \DateTime('2017-03-22 01:30:00')),
new Filter(Column::OPERATOR_LT, new \DateTime('2017-03-23 19:00:00')),
], $column->getFilters('asource'));
}

Expand All @@ -128,11 +160,28 @@ public function testQueryIsInvalid()
$this->assertFalse($column->isQueryValid('foo'));
}

public function testInputFormattedQueryIsValid()
{
$column = new DateTimeColumn();
$column->setInputFormat('m/d/Y H-i-s');

$this->assertTrue($column->isQueryValid('03/22/2017 23-00-00'));
}

public function testInputFormattedQueryIsInvalid()
{
$column = new DateTimeColumn();
$column->setInputFormat('m/d/Y H-i-s');

$this->assertFalse($column->isQueryValid('2017-03-22 23:00:00'));
}

public function testInitializeDefaultParams()
{
$column = new DateTimeColumn();

$this->assertAttributeEquals(null, 'format', $column);
$this->assertAttributeEquals('Y-m-d H:i:s', 'inputFormat', $column);
$this->assertAttributeEquals([
Column::OPERATOR_EQ,
Column::OPERATOR_NEQ,
Expand All @@ -152,10 +201,12 @@ public function testInitializeDefaultParams()
public function testInitialize()
{
$format = 'Y-m-d H:i:s';
$inputFormat = 'Y-m-d H:i:s';
$timezone = 'UTC';

$params = [
'format' => $format,
'inputFormat' => $inputFormat,
'operators' => [Column::OPERATOR_LT, Column::OPERATOR_LTE],
'defaultOperator' => Column::OPERATOR_LT,
'timezone' => $timezone,
Expand All @@ -164,6 +215,7 @@ public function testInitialize()
$column = new DateTimeColumn($params);

$this->assertAttributeEquals($format, 'format', $column);
$this->assertAttributeEquals($inputFormat, 'inputFormat', $column);
$this->assertAttributeEquals([
Column::OPERATOR_LT, Column::OPERATOR_LTE,
], 'operators', $column);
Expand Down

0 comments on commit a0466ae

Please sign in to comment.