Skip to content

Commit

Permalink
Filters\Date: Fixed for DATETIME columns
Browse files Browse the repository at this point in the history
  • Loading branch information
o5 committed Jan 11, 2014
1 parent eaab5fc commit ba44903
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
9 changes: 2 additions & 7 deletions Grido/Components/Filters/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
*/
class Date extends Text
{
const DEFAULT_CONDITION = '= ?';

/** @var string */
protected $condition = self::DEFAULT_CONDITION;

/** @var string */
protected $formatValue;

Expand All @@ -39,7 +34,7 @@ class Date extends Text
/**
* @var string
*/
protected $dateFormatOutput = 'Y-m-d';
protected $dateFormatOutput = 'Y-m-d%';

/**
* Sets date-input format.
Expand Down Expand Up @@ -101,7 +96,7 @@ protected function getFormControl()
*/
public function __getCondition($value)
{
if ($this->where === NULL && $this->condition == self::DEFAULT_CONDITION) {
if ($this->where === NULL && is_string($this->condition)) {
return ($date = \DateTime::createFromFormat($this->dateFormatInput, $value))
? Condition::setupFromArray(array($this->getColumn(), $this->condition, $date->format($this->dateFormatOutput)))
: Condition::setupEmpty();
Expand Down
12 changes: 6 additions & 6 deletions Grido/Components/Filters/DateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
*/
class DateRange extends Date
{
const DEFAULT_CONDITION = 'BETWEEN ? AND ?';

/** @var string */
protected $condition = self::DEFAULT_CONDITION;
protected $condition = 'BETWEEN ? AND ?';

/** @var string */
protected $formatValue;
/**
* @var string
*/
protected $dateFormatOutput = 'Y-m-d';

/**
* @var string
Expand Down Expand Up @@ -76,7 +76,7 @@ protected function getFormControl()
*/
public function __getCondition($value)
{
if ($this->where === NULL && $this->condition == self::DEFAULT_CONDITION) {
if ($this->where === NULL && is_string($this->condition)) {
list (, $from, $to) = \Nette\Utils\Strings::match($value, $this->mask);
$from = \DateTime::createFromFormat($this->dateFormatInput, trim($from));
$to = \DateTime::createFromFormat($this->dateFormatInput, trim($to));
Expand Down
4 changes: 2 additions & 2 deletions tests/Grido/Components/Filter.Date.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class FilterDateTest extends \Tester\TestCase
$grid = new Grid;
$filter = $grid->addFilterDate('date', 'Date');

Assert::same(array('date = ?', '2012-12-21'), $filter->__getCondition('21.12.2012')->__toArray());
Assert::same(array('date LIKE ?', '2012-12-21%'), $filter->__getCondition('21.12.2012')->__toArray());
Assert::same(array('0 = 1'), $filter->__getCondition('TEST BAD INPUT')->__toArray());

$filter
->setDateFormatInput('d/m/Y')
->setDateFormatOutput('d.m.Y');

Assert::same(array('date = ?', '21.12.2012'), $filter->__getCondition('21/12/2012')->__toArray());
Assert::same(array('date LIKE ?', '21.12.2012'), $filter->__getCondition('21/12/2012')->__toArray());
Assert::same(array('0 = 1'), $filter->__getCondition('21.12.2012')->__toArray()); //test bad input
}
}
Expand Down

0 comments on commit ba44903

Please sign in to comment.