From de4e7eaeb6d7db7fa015f73b0e47a7e05aa66626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20de=20Marqu=C3=A9=20Fromentin?= Date: Wed, 28 Jun 2017 15:35:26 +0200 Subject: [PATCH 1/4] Locale aware for datefilter --- Datatable/Filter/DateRangeFilter.php | 50 +++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/Datatable/Filter/DateRangeFilter.php b/Datatable/Filter/DateRangeFilter.php index b48b3ac5..2c016048 100644 --- a/Datatable/Filter/DateRangeFilter.php +++ b/Datatable/Filter/DateRangeFilter.php @@ -11,9 +11,10 @@ namespace Sg\DatatablesBundle\Datatable\Filter; -use Doctrine\ORM\QueryBuilder; -use Doctrine\ORM\Query\Expr\Andx; use DateTime; +use Doctrine\ORM\Query\Expr\Andx; +use Doctrine\ORM\QueryBuilder; +use \IntlDateFormatter; use Symfony\Component\OptionsResolver\OptionsResolver; /** @@ -23,6 +24,8 @@ */ class DateRangeFilter extends AbstractFilter { + protected $locale; + //------------------------------------------------- // FilterInterface //------------------------------------------------- @@ -41,8 +44,8 @@ public function getTemplate() public function addAndExpression(Andx $andExpr, QueryBuilder $qb, $searchField, $searchValue, &$parameterCounter) { list($_dateStart, $_dateEnd) = explode(' - ', $searchValue); - $dateStart = new DateTime($_dateStart); - $dateEnd = new DateTime($_dateEnd); + $dateStart = $this->getDateTime($_dateStart); + $dateEnd = $this->getDateTime($_dateEnd); $dateEnd->setTime(23, 59, 59); $andExpr = $this->getBetweenAndExpression($andExpr, $qb, $searchField, $dateStart->format('Y-m-d H:i:s'), $dateEnd->format('Y-m-d H:i:s'), $parameterCounter); @@ -51,6 +54,41 @@ public function addAndExpression(Andx $andExpr, QueryBuilder $qb, $searchField, return $andExpr; } + /** + * @param string $date + * @return DateTime + */ + protected function getDateTime($date) + { + $dateFormatter = IntlDateFormatter::create( + $this->locale, + IntlDateFormatter::SHORT, + IntlDateFormatter::SHORT + ); + $timestamp = $dateFormatter->parse($date); + + return new \DateTime($timestamp); + } + + /** + * @return string + */ + public function getLocale() + { + return $this->locale; + } + + /** + * @param string $locale + * @return $this + */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } + //------------------------------------------------- // Options //------------------------------------------------- @@ -65,7 +103,9 @@ public function addAndExpression(Andx $andExpr, QueryBuilder $qb, $searchField, public function configureOptions(OptionsResolver $resolver) { parent::configureOptions($resolver); - + + $resolver->setDefault('locale', 'en'); + $resolver->setAllowedTypes('locale', 'string'); $resolver->remove('search_type'); return $this; From cf9880da6d8c42683e3951a936a1ecbfddcfe7b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20de=20Marqu=C3=A9=20Fromentin?= Date: Wed, 9 Aug 2017 17:08:06 +0200 Subject: [PATCH 2/4] Update DateRangeFilter.php --- Datatable/Filter/DateRangeFilter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Datatable/Filter/DateRangeFilter.php b/Datatable/Filter/DateRangeFilter.php index 2c016048..a82d1dba 100644 --- a/Datatable/Filter/DateRangeFilter.php +++ b/Datatable/Filter/DateRangeFilter.php @@ -63,7 +63,7 @@ protected function getDateTime($date) $dateFormatter = IntlDateFormatter::create( $this->locale, IntlDateFormatter::SHORT, - IntlDateFormatter::SHORT + IntlDateFormatter::NONE ); $timestamp = $dateFormatter->parse($date); From 1e2c50e951e6f36e79b96931027a5c9029cc7745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20de=20Marqu=C3=A9=20Fromentin?= Date: Wed, 9 Aug 2017 17:13:43 +0200 Subject: [PATCH 3/4] Update DateRangeFilter.php --- Datatable/Filter/DateRangeFilter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Datatable/Filter/DateRangeFilter.php b/Datatable/Filter/DateRangeFilter.php index a82d1dba..afba8467 100644 --- a/Datatable/Filter/DateRangeFilter.php +++ b/Datatable/Filter/DateRangeFilter.php @@ -67,7 +67,7 @@ protected function getDateTime($date) ); $timestamp = $dateFormatter->parse($date); - return new \DateTime($timestamp); + return new \DateTime('@' . $timestamp); } /** From a2dc7c1ef39922d62315a4dbf551774c056e6c47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20de=20Marqu=C3=A9=20Fromentin?= Date: Wed, 12 Jun 2019 11:32:24 +0200 Subject: [PATCH 4/4] Update Datatable/Filter/DateRangeFilter.php Co-Authored-By: Stephan Vierkant --- Datatable/Filter/DateRangeFilter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Datatable/Filter/DateRangeFilter.php b/Datatable/Filter/DateRangeFilter.php index afba8467..ae9a5269 100644 --- a/Datatable/Filter/DateRangeFilter.php +++ b/Datatable/Filter/DateRangeFilter.php @@ -58,7 +58,7 @@ public function addAndExpression(Andx $andExpr, QueryBuilder $qb, $searchField, * @param string $date * @return DateTime */ - protected function getDateTime($date) + protected function getDateTime($date) : \DateTime { $dateFormatter = IntlDateFormatter::create( $this->locale,