Skip to content

Commit

Permalink
Handle the invalid date values in the date and date range filters (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
petaak authored and paveljanda committed Apr 23, 2019
1 parent 325f565 commit cfbeda4
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 74 deletions.
29 changes: 21 additions & 8 deletions src/DataSource/DibiFluentDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Dibi;
use DibiFluent;
use Ublaboo\DataGrid\AggregationFunction\IAggregatable;
use Ublaboo\DataGrid\Exception\DataGridDateTimeHelperException;
use Ublaboo\DataGrid\Filter;
use Ublaboo\DataGrid\Utils\DateTimeHelper;
use Ublaboo\DataGrid\Utils\Sorting;
Expand Down Expand Up @@ -92,9 +93,13 @@ public function applyFilterDate(Filter\FilterDate $filter)
{
$conditions = $filter->getCondition();

$date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);
try {
$date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);

$this->data_source->where('DATE(%n) = ?', $filter->getColumn(), $date->format('Y-m-d'));
$this->data_source->where('DATE(%n) = ?', $filter->getColumn(), $date->format('Y-m-d'));
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}


Expand All @@ -111,17 +116,25 @@ public function applyFilterDateRange(Filter\FilterDateRange $filter)
$value_to = $conditions[$filter->getColumn()]['to'];

if ($value_from) {
$date_from = DateTimeHelper::tryConvertToDateTime($value_from, [$filter->getPhpFormat()]);
$date_from->setTime(0, 0, 0);
try {
$date_from = DateTimeHelper::tryConvertToDateTime($value_from, [$filter->getPhpFormat()]);
$date_from->setTime(0, 0, 0);

$this->data_source->where('DATE(%n) >= ?', $filter->getColumn(), $date_from);
$this->data_source->where('DATE(%n) >= ?', $filter->getColumn(), $date_from);
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}

if ($value_to) {
$date_to = DateTimeHelper::tryConvertToDateTime($value_to, [$filter->getPhpFormat()]);
$date_to->setTime(23, 59, 59);
try {
$date_to = DateTimeHelper::tryConvertToDateTime($value_to, [$filter->getPhpFormat()]);
$date_to->setTime(23, 59, 59);

$this->data_source->where('DATE(%n) <= ?', $filter->getColumn(), $date_to);
$this->data_source->where('DATE(%n) <= ?', $filter->getColumn(), $date_to);
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/DataSource/DibiFluentMssqlDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Dibi;
use DibiFluent;
use Ublaboo\DataGrid\Exception\DataGridDateTimeHelperException;
use Ublaboo\DataGrid\Filter;
use Ublaboo\DataGrid\Utils\DateTimeHelper;

Expand Down Expand Up @@ -83,9 +84,13 @@ public function applyFilterDate(Filter\FilterDate $filter)
{
$conditions = $filter->getCondition();

$date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);
try {
$date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);

$this->data_source->where('CONVERT(varchar(10), %n, 112) = ?', $filter->getColumn(), $date->format('Ymd'));
$this->data_source->where('CONVERT(varchar(10), %n, 112) = ?', $filter->getColumn(), $date->format('Ymd'));
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}


Expand Down
41 changes: 27 additions & 14 deletions src/DataSource/DoctrineCollectionDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Ublaboo\DataGrid\AggregationFunction\IAggregatable;
use Ublaboo\DataGrid\Exception\DataGridDateTimeHelperException;
use Ublaboo\DataGrid\Filter;
use Ublaboo\DataGrid\Utils\DateTimeHelper;
use Ublaboo\DataGrid\Utils\Sorting;
Expand Down Expand Up @@ -104,12 +105,16 @@ public function filterOne(array $condition)
public function applyFilterDate(Filter\FilterDate $filter)
{
foreach ($filter->getCondition() as $column => $value) {
$date = DateTimeHelper::tryConvertToDateTime($value, [$filter->getPhpFormat()]);
try {
$date = DateTimeHelper::tryConvertToDateTime($value, [$filter->getPhpFormat()]);

$from = Criteria::expr()->gte($filter->getColumn(), $date->format('Y-m-d 00:00:00'));
$to = Criteria::expr()->lte($filter->getColumn(), $date->format('Y-m-d 23:59:59'));
$from = Criteria::expr()->gte($filter->getColumn(), $date->format('Y-m-d 00:00:00'));
$to = Criteria::expr()->lte($filter->getColumn(), $date->format('Y-m-d 23:59:59'));

$this->criteria->andWhere($from)->andWhere($to);
$this->criteria->andWhere($from)->andWhere($to);
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}
}

Expand All @@ -125,19 +130,27 @@ public function applyFilterDateRange(Filter\FilterDateRange $filter)
$values = $conditions[$filter->getColumn()];

if ($value_from = $values['from']) {
$date_from = DateTimeHelper::tryConvertToDateTime($value_from, [$filter->getPhpFormat()]);
$date_from->setTime(0, 0, 0);

$expr = Criteria::expr()->gte($filter->getColumn(), $date_from->format('Y-m-d H:i:s'));
$this->criteria->andWhere($expr);
try {
$date_from = DateTimeHelper::tryConvertToDateTime($value_from, [$filter->getPhpFormat()]);
$date_from->setTime(0, 0, 0);

$expr = Criteria::expr()->gte($filter->getColumn(), $date_from->format('Y-m-d H:i:s'));
$this->criteria->andWhere($expr);
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}

if ($value_to = $values['to']) {
$date_to = DateTimeHelper::tryConvertToDateTime($value_to, [$filter->getPhpFormat()]);
$date_to->setTime(23, 59, 59);

$expr = Criteria::expr()->lte($filter->getColumn(), $date_to->format('Y-m-d H:i:s'));
$this->criteria->andWhere($expr);
try {
$date_to = DateTimeHelper::tryConvertToDateTime($value_to, [$filter->getPhpFormat()]);
$date_to->setTime(23, 59, 59);

$expr = Criteria::expr()->lte($filter->getColumn(), $date_to->format('Y-m-d H:i:s'));
$this->criteria->andWhere($expr);
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}
}

Expand Down
41 changes: 27 additions & 14 deletions src/DataSource/DoctrineDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\ORM\Tools\Pagination\Paginator;
use Nette\Utils\Strings;
use Ublaboo\DataGrid\AggregationFunction\IAggregatable;
use Ublaboo\DataGrid\Exception\DataGridDateTimeHelperException;
use Ublaboo\DataGrid\Filter;
use Ublaboo\DataGrid\Utils\DateTimeHelper;
use Ublaboo\DataGrid\Utils\Sorting;
Expand Down Expand Up @@ -170,12 +171,16 @@ public function applyFilterDate(Filter\FilterDate $filter)
$p2 = $this->getPlaceholder();

foreach ($filter->getCondition() as $column => $value) {
$date = DateTimeHelper::tryConvertToDateTime($value, [$filter->getPhpFormat()]);
$c = $this->checkAliases($column);

$this->data_source->andWhere("$c >= :$p1 AND $c <= :$p2")
->setParameter($p1, $date->format('Y-m-d 00:00:00'))
->setParameter($p2, $date->format('Y-m-d 23:59:59'));
try {
$date = DateTimeHelper::tryConvertToDateTime($value, [$filter->getPhpFormat()]);
$c = $this->checkAliases($column);

$this->data_source->andWhere("$c >= :$p1 AND $c <= :$p2")
->setParameter($p1, $date->format('Y-m-d 00:00:00'))
->setParameter($p2, $date->format('Y-m-d 23:59:59'));
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}
}

Expand All @@ -193,21 +198,29 @@ public function applyFilterDateRange(Filter\FilterDateRange $filter)
$value_to = $conditions[$filter->getColumn()]['to'];

if ($value_from) {
$date_from = DateTimeHelper::tryConvertToDate($value_from, [$filter->getPhpFormat()]);
$date_from->setTime(0, 0, 0);
try {
$date_from = DateTimeHelper::tryConvertToDate($value_from, [$filter->getPhpFormat()]);
$date_from->setTime(0, 0, 0);

$p = $this->getPlaceholder();
$p = $this->getPlaceholder();

$this->data_source->andWhere("$c >= :$p")->setParameter($p, $date_from->format('Y-m-d H:i:s'));
$this->data_source->andWhere("$c >= :$p")->setParameter($p, $date_from->format('Y-m-d H:i:s'));
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}

if ($value_to) {
$date_to = DateTimeHelper::tryConvertToDate($value_to, [$filter->getPhpFormat()]);
$date_to->setTime(23, 59, 59);
try {
$date_to = DateTimeHelper::tryConvertToDate($value_to, [$filter->getPhpFormat()]);
$date_to->setTime(23, 59, 59);

$p = $this->getPlaceholder();
$p = $this->getPlaceholder();

$this->data_source->andWhere("$c <= :$p")->setParameter($p, $date_to->format('Y-m-d H:i:s'));
$this->data_source->andWhere("$c <= :$p")->setParameter($p, $date_to->format('Y-m-d H:i:s'));
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}
}

Expand Down
29 changes: 21 additions & 8 deletions src/DataSource/NetteDatabaseTableDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Nette\Database\Table\Selection;
use Nette\Utils\Strings;
use Ublaboo\DataGrid\Exception\DataGridDateTimeHelperException;
use Ublaboo\DataGrid\Filter;
use Ublaboo\DataGrid\Utils\DateTimeHelper;
use Ublaboo\DataGrid\Utils\Sorting;
Expand Down Expand Up @@ -114,9 +115,13 @@ public function applyFilterDate(Filter\FilterDate $filter)
{
$conditions = $filter->getCondition();

$date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);
try {
$date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);

$this->data_source->where("DATE({$filter->getColumn()}) = ?", $date->format('Y-m-d'));
$this->data_source->where("DATE({$filter->getColumn()}) = ?", $date->format('Y-m-d'));
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}


Expand All @@ -133,17 +138,25 @@ public function applyFilterDateRange(Filter\FilterDateRange $filter)
$value_to = $conditions[$filter->getColumn()]['to'];

if ($value_from) {
$date_from = DateTimeHelper::tryConvertToDateTime($value_from, [$filter->getPhpFormat()]);
$date_from->setTime(0, 0, 0);
try {
$date_from = DateTimeHelper::tryConvertToDateTime($value_from, [$filter->getPhpFormat()]);
$date_from->setTime(0, 0, 0);

$this->data_source->where("DATE({$filter->getColumn()}) >= ?", $date_from->format('Y-m-d'));
$this->data_source->where("DATE({$filter->getColumn()}) >= ?", $date_from->format('Y-m-d'));
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}

if ($value_to) {
$date_to = DateTimeHelper::tryConvertToDateTime($value_to, [$filter->getPhpFormat()]);
$date_to->setTime(23, 59, 59);
try {
$date_to = DateTimeHelper::tryConvertToDateTime($value_to, [$filter->getPhpFormat()]);
$date_to->setTime(23, 59, 59);

$this->data_source->where("DATE({$filter->getColumn()}) <= ?", $date_to->format('Y-m-d'));
$this->data_source->where("DATE({$filter->getColumn()}) <= ?", $date_to->format('Y-m-d'));
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}
}

Expand Down
47 changes: 30 additions & 17 deletions src/DataSource/NetteDatabaseTableMssqlDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Ublaboo\DataGrid\DataSource;

use Ublaboo\DataGrid\Exception\DataGridDateTimeHelperException;
use Ublaboo\DataGrid\Filter;
use Ublaboo\DataGrid\Utils\DateTimeHelper;

Expand All @@ -23,12 +24,16 @@ public function applyFilterDate(Filter\FilterDate $filter)
{
$conditions = $filter->getCondition();

$date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);
try {
$date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);

$this->data_source->where(
"CONVERT(varchar(10), {$filter->getColumn()}, 112) = ?",
$date->format('Ymd')
);
$this->data_source->where(
"CONVERT(varchar(10), {$filter->getColumn()}, 112) = ?",
$date->format('Ymd')
);
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}


Expand All @@ -45,23 +50,31 @@ public function applyFilterDateRange(Filter\FilterDateRange $filter)
$value_to = $conditions[$filter->getColumn()]['to'];

if ($value_from) {
$date_from = DateTimeHelper::tryConvertToDateTime($value_from, [$filter->getPhpFormat()]);
$date_from->setTime(0, 0, 0);
try {
$date_from = DateTimeHelper::tryConvertToDateTime($value_from, [$filter->getPhpFormat()]);
$date_from->setTime(0, 0, 0);

$this->data_source->where(
"CONVERT(varchar(10), {$filter->getColumn()}, 112) >= ?",
$date_from->format('Ymd')
);
$this->data_source->where(
"CONVERT(varchar(10), {$filter->getColumn()}, 112) >= ?",
$date_from->format('Ymd')
);
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}

if ($value_to) {
$date_to = DateTimeHelper::tryConvertToDateTime($value_to, [$filter->getPhpFormat()]);
$date_to->setTime(23, 59, 59);
try {
$date_to = DateTimeHelper::tryConvertToDateTime($value_to, [$filter->getPhpFormat()]);
$date_to->setTime(23, 59, 59);

$this->data_source->where(
"CONVERT(varchar(10), {$filter->getColumn()}, 112) <= ?",
$date_to->format('Ymd')
);
$this->data_source->where(
"CONVERT(varchar(10), {$filter->getColumn()}, 112) <= ?",
$date_to->format('Ymd')
);
} catch (DataGridDateTimeHelperException $ex) {
// ignore the invalid filter value
}
}
}
}
Loading

0 comments on commit cfbeda4

Please sign in to comment.